test/unittest_apycot.py
author David Douard <david.douard@logilab.fr>
Fri, 29 May 2015 11:27:00 +0200
changeset 1819 21c020c077a5
parent 1566 ffcd83fb3766
child 1580 2f465d1682da
child 1813 82c30b47a087
permissions -rw-r--r--
merge upcoming 3.3 and 3.2.2

#!/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

import os.path
import narvalbot

##override default ini file
test_dir = os.path.dirname(__file__)
narvalbot._CW_SOURCES_FILE = os.path.join(test_dir, 'data', 'narval-cw-sources.ini')

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()
        with self.admin_access.client_cnx() as cnx:
            lgc = cnx.entity_from_eid(self.lgc)
            lgce = cnx.entity_from_eid(self.lgce)
            te = lgc.start(lgce).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()