test/unittest_apycot.py
author David Douard <david.douard@logilab.fr>
Sun, 26 Oct 2014 18:28:22 +0100
changeset 1766 42e60f760703
parent 1757 224cd20b7284
permissions -rw-r--r--
[checkers] rationalize the way project path is configured in checkers Generalize the use of the 'project_path' (mandatry) option in checkers

#!/usr/bin/python
import cubicweb.devtools

from cubes.apycot.testutils import ApycotBaseTC

from apycotlib import FAILURE, SUCCESS
from apycotlib.writer import TestDataWriter
from narvalbot import HTTPConnectionHandler

from cubes.narval.logformat import log_to_html

CW_NAMESPACE_DIV = '<div xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">%s</div>'

class MockChecker(object):
    def __init__(self, id, options):
        self.id = id
        self.options = options
        self.options_def = dict( (k, {}) for k in options )

class ApycotTC(ApycotBaseTC):
    _repo_path = (u'project1',)

    def setUp(self):
        super(ApycotTC, self).setUp()
        with self.admin_access.client_cnx() as cnx:
            pe = cnx.find('ProjectEnvironment').one()
            tc = cnx.find('TestConfig', name='tc_project1').one()
            te = tc.start(pe).eid
            cnx.commit()

        cnxh = HTTPConnectionHandler('narval')
        writer = TestDataWriter(cnxh, te)
        writer.start()
        cwriter = writer.make_check_writer()
        cwriter.start(MockChecker(u'pylint', {}))
        cwriter.raw('pylint_version', '0.18.1', type=u'version')
        cwriter.debug('hip', path='/tmp/something', line=12)
        cwriter.info('hop', path='/tmp/something')
        cwriter.warning('''momo\n\n<br/>''')
        cwriter.end(SUCCESS)
        cwriter = writer.make_check_writer()
        cwriter.start(MockChecker(u'lintian', {'option': 'value'}))
        cwriter.raw('lintian_version', '1.0')
        cwriter.error('bouh')
        cwriter.fatal('di&d')
        cwriter.end(FAILURE)
        writer.end(FAILURE)

    def test_writer_log_content(self):
        with self.admin_access.client_cnx() as cnx:
            checks = cnx.execute('Any X, N ORDERBY N WHERE X is CheckResult, '
                                 'X name N')
            self.assertEqual(len(checks), 2)
            self.assertEqual(checks.get_entity(0, 0).log_file[0].data.getvalue(),
                             '20\t\t\toption=value<br/>40\t\t\tbouh<br/>50\t\t\tdi&amp;d<br/>')
            self.assertEqual(checks.get_entity(1, 0).log_file[0].data.getvalue(),
                             '10\t/tmp/something\t12\thip<br/>20\t/tmp/something\t\thop<br/>30\t\t\tmomo\n\n&lt;br/&gt;<br/>')

    def test_log_formatting_first_check(self):
        stream = []
        with self.admin_access.web_request() as req:
            checks = req.execute('Any X, N ORDERBY N WHERE X is CheckResult, '
                                 'X name N')
            log_to_html(req, '',
                        checks.get_entity(0, 0).log_file[0].data.read(),
                        stream.append)
        log_html = '\n'.join(stream)
        self.assertWellFormed(self.get_validator(content_type='application/xml'),
                              CW_NAMESPACE_DIV % log_html)
        for pattern, count in (
                ('<table class="listing" id="">', 1),
                ('<tr class="logError"', 1),
                ('<tr class="logFatal"', 1),
                ('<tr class="logInfo"', 1),
                ('<td class="logSeverity"', 3),
                ('<td class="logPath"',  3),
                ('<td class="logMsg"',   3),
                ('<div class="rawtext"', 3),
                ('bouh', 1),
                ('di&amp;d',1),
                ('option=value', 1),
            ):
            self.assertIn(pattern, log_html)
            self.assertEqual(log_html.count(pattern), count)

    def test_log_formatting_second_check(self):
        stream = []
        with self.admin_access.web_request() as req:
            checks = req.execute('Any X, N ORDERBY N WHERE X is CheckResult, X name N')
            log_to_html(req, '',
                        checks.get_entity(1, 0).log_file[0].data.read(),
                        stream.append)
        log_html = '\n'.join(stream)
        self.assertWellFormed(self.get_validator(content_type='application/xml'),
                              CW_NAMESPACE_DIV % log_html)
        for pattern, count in (
                ('<table class="listing" id="">', 1),
                ('<tr class="logDebug"', 1),
                ('<tr class="logInfo"', 1),
                ('<tr class="logWarning"', 1),
                ('<td class="logSeverity"', 3),
                ('<td class="logPath"',  3),
                ('<td class="logMsg"',   3),
                ('<div class="rawtext"', 3),
                ('hip', 1),
                ('hop', 1),
                ('momo', 1),
            ):
            self.assertIn(pattern, log_html)
            self.assertEqual(log_html.count(pattern), count)

if __name__ == '__main__':
    from logilab.common.testlib import unittest_main
    unittest_main()