test/unittest_apycot.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Nov 2011 18:04:25 +0100
branchstable
changeset 791 5ac76ce656dd
parent 783 caf114cc9c86
child 844 83cb6c5ccaba
permissions -rw-r--r--
update to cw 3.14 api. Closes #2063529

from __future__ import with_statement

from utils import ApycotBaseTC

from apycotlib.writer import TestDataWriter
from narvalbot import ConnectionHandler

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):

    def setUp(self):
        super(ApycotBaseTC, self).setUp()
        te = self.lgc.start(self.lgce)
        self.commit()
        with self.login('narval', password='narval0') as cu:
            cnxh = ConnectionHandler(self.config.appid, cnxinfo={})
            cnxh.cnx = cu.connection
            cnxh._cu = cu
            cnxh.cw = cu.req
            writer = TestDataWriter(cnxh, te.eid)
            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(u'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(u'failure')
            writer.end(u'failure')
        self.checks = self.execute('Any X, N ORDERBY N WHERE X is CheckResult, X name N')

    def test_writer_log_content(self):
        checks = self.checks
        self.assertEqual(len(checks), 2)
        self.assertMultiLineEqual(checks.get_entity(0, 0).log, '''\
20\t\t\toption=value<br/>
40\t\t\tbouh<br/>
50\t\t\tdi&amp;d<br/>''')
        self.assertMultiLineEqual(checks.get_entity(1, 0).log, '''\
10\t/tmp/something\t12\thip<br/>
20\t/tmp/something\t\thop<br/>
30\t\t\tmomo

&lt;br/&gt;<br/>''')

    def test_log_formatting_first_check(self):
        stream = []
        log_to_html(self.request(), '', self.checks.get_entity(0, 0).log, 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),
                ('<pre 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 = []
        log_to_html(self.request(), '', self.checks.get_entity(1, 0).log, 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),
                ('<pre 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()