test/unittest_entities.py
author David Douard <david.douard@logilab.fr>
Thu, 13 Nov 2014 14:32:39 +0100
changeset 1742 3cdc3ef300a4
parent 1740 f3137466457a
child 1597 437e6bc9d9f9
permissions -rw-r--r--
[atest] improve environment variable reporting

#!/usr/bin/python
from logilab.common.testlib import unittest_main

from cubicweb import ValidationError
from cubicweb import devtools # setup import machinery
from cubes.apycot.views.reports import all_check_results

from cubes.apycot.testutils import ApycotBaseTC

class MockWriter(object):
    """fake apycot.IWriter class, ignore every thing"""

    def skip(self, *args, **kwargs):
        pass
    def _debug(self, *args, **kwargs):
        print args, kwargs
    __init__= skip
    raw = log = execution_info = skip
    start_test = start_check = end_check = end_test = skip
    set_exec_status = skip


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

    def setup_database(self):
        ApycotBaseTC.setup_database(self)
        with self.admin_access.repo_cnx() as cnx:
            pyp = cnx.find('TestConfig', name='PYTHONPACKAGE').one()
            pe = cnx.find('ProjectEnvironment').one()
            self.add_test_config(cnx, u'tcd', check_config=None,
                                 env=pe, group=pyp)
            cnx.commit()

    def test_refinement_base(self):
        with self.admin_access.client_cnx() as cnx:
            pe = cnx.find('ProjectEnvironment').one()
            pyp = cnx.find('TestConfig', name='PYTHONPACKAGE').one()
            tcd = cnx.find('TestConfig', name='tcd').one()
            self.assertEqual(pe.apycot_process_environment(),
                             {'DISPLAY': ':2.0', 'SETUPTOOLS': '1'})
            self.assertEqual(pyp.apycot_process_environment(),
                             {'DISPLAY': ':1.0', 'NO_SETUPTOOLS': '1'})

            self.assertEqual(tcd.apycot_configuration(pe),
                              {'python_lint_treshold': '7',
                               'python_lint_ignore': 'thirdparty',
                               'python_test_coverage_treshold': '70',
                               'env-option': 'value'})

    def test_refinement_override(self):
        with self.admin_access.client_cnx() as cnx:
            pe = cnx.find('ProjectEnvironment').one()
            tc = cnx.find('TestConfig', name='tc_project1').one()
            self.assertEqual(tc.apycot_configuration(pe),
                              {'python_lint_treshold': '8',
                               'python_lint_ignore': 'thirdparty',
                               'python_test_coverage_treshold': '70',
                               'pouet': '5',
                               'env-option': 'value'})

    def test_all_check_results(self):
        with self.admin_access.client_cnx() as cnx:
            pe = cnx.find('ProjectEnvironment').one()
            tc = cnx.find('TestConfig', name='tc_project1').one()
            ex1eid = tc.start(pe, check_duplicate=False).eid
            ex2eid = tc.start(pe, check_duplicate=False).eid
            cnx.commit()

        with self.new_access('narval').client_cnx() as cnx:
            ex1 = cnx.entity_from_eid(ex1eid)
            ex2 = cnx.entity_from_eid(ex2eid)
            self.dumb_execution(cnx, ex1, [('unittest', 'success'),
                                           ('coverage', 'success')])
            cnx.commit()
            covcr = ex1.check_result_by_name('coverage').eid
            self.dumb_execution(cnx, ex2, [('unittest', 'failure')])
            cnx.commit()
            ucr = ex2.check_result_by_name('unittest').eid
            cnx.commit()

        with self.admin_access.client_cnx() as cnx:
            tc = cnx.find('TestConfig', name='tc_project1').one()
            self.assertEqual([cr.eid for cr in all_check_results(tc)],
                             [covcr, ucr])

    def test_duplicated_tc_same_env(self):
        tcncstrs = self.schema['TestConfig'].rdef('name').constraints
        self.assertEqual([cstr.type() for cstr in tcncstrs], ['RQLUniqueConstraint', 'SizeConstraint'])
        with self.admin_access.client_cnx() as cnx:
            pe = cnx.find('ProjectEnvironment').one()
            cnx.create_entity('TestConfig', name=u'lgd', use_environment=pe)
            self.assertRaises(ValidationError, cnx.commit)

    def test_status_change(self):
        with self.admin_access.client_cnx() as cnx:
            pe = cnx.find('ProjectEnvironment').one()
            tc = cnx.find('TestConfig', name='tc_project1').one()
            ex1eid = tc.start(pe, check_duplicate=False).eid
            ex2eid = tc.start(pe, check_duplicate=False).eid
            ex3eid = tc.start(pe, check_duplicate=False).eid
            cnx.commit()

        with self.new_access('narval').client_cnx() as cnx:
            ex1 = cnx.entity_from_eid(ex1eid)
            ex2 = cnx.entity_from_eid(ex2eid)
            ex3 = cnx.entity_from_eid(ex3eid)
            self.dumb_execution(cnx, ex1, [('unittest', 'success'), ('coverage', 'success')])
            self.dumb_execution(cnx, ex2, [('unittest', 'failure'), ('coverage', 'success')])
            self.dumb_execution(cnx, ex3, [('unittest', 'success'), ('coverage', 'error')])
            cnx.commit()

            status_changes = ex2.status_changes()
            for key, values in status_changes.iteritems():
                self.assertTrue(hasattr(values[0], 'eid'))
                self.assertTrue(hasattr(values[1], 'eid'))
                status_changes[key] = (values[0].eid, values[1].eid)
            self.assertDictEqual(status_changes,
                                 {'unittest': (ex1.check_result_by_name('unittest').eid,
                                               ex2.check_result_by_name('unittest').eid)})
            status_changes = ex3.status_changes()
            for key, values in status_changes.iteritems():
                self.assertTrue(hasattr(values[0], 'eid'))
                self.assertTrue(hasattr(values[1], 'eid'))
                status_changes[key] = (values[0].eid, values[1].eid)
            self.assertDictEqual(status_changes,
                                 {'unittest': (ex2.check_result_by_name('unittest').eid,
                                               ex3.check_result_by_name('unittest').eid),
                                  'coverage': (ex2.check_result_by_name('coverage').eid,
                                               ex3.check_result_by_name('coverage').eid)})


if __name__ == '__main__':
    unittest_main()