test/unittest_entities.py
author David Douard <david.douard@logilab.fr>
Sun, 26 Oct 2014 18:30:36 +0100
changeset 1736 72ec6c7e956c
parent 1566 ffcd83fb3766
child 1580 2f465d1682da
permissions -rw-r--r--
[apycotlib] port to narval 4.2 The connection handler is now a CWProxy (cwclientlib), and there is no CreateSubEntity view any more since we can now create File entity using cwclientlib and rqlcontroller.

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

    def setup_database(self):
        ApycotBaseTC.setup_database(self)
        with self.admin_access.repo_cnx() as cnx:
            self.lgd = self.add_test_config(cnx, u'lgd', check_config=None,
                                            env=self.lgce, group=self.pyp).eid
            cnx.commit()

    def test_refinement_base(self):
        with self.admin_access.client_cnx() as cnx:
            lgce = cnx.entity_from_eid(self.lgce)
            lgc = cnx.entity_from_eid(self.lgc)
            lgd = cnx.entity_from_eid(self.lgd)
            self.assertEqual(lgce.apycot_process_environment(),
                             {'DISPLAY': ':2.0', 'SETUPTOOLS': '1'})
            self.assertEqual(lgc.apycot_process_environment(),
                             {'DISPLAY': ':1.0', 'NO_SETUPTOOLS': '1'})

            self.assertEqual(lgd.apycot_configuration(lgce),
                              {'install': 'python_setup',
                               '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:
            lgce = cnx.entity_from_eid(self.lgce)
            lgc = cnx.entity_from_eid(self.lgc)
            self.assertEqual(lgc.apycot_configuration(lgce),
                              {'install': 'python_setup',
                               '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:
            lgce = cnx.entity_from_eid(self.lgce)
            lgc = cnx.entity_from_eid(self.lgc)
            ex1eid = lgc.start(lgce, check_duplicate=False).eid
            ex2eid = lgc.start(lgce, 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:
            lgc = cnx.entity_from_eid(self.lgc)
            self.assertEqual([cr.eid for cr in all_check_results(lgc)],
                             [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:
            cnx.create_entity('TestConfig', name=u'lgd', use_environment=self.lgce)
            self.assertRaises(ValidationError, cnx.commit)

    def test_status_change(self):
        with self.admin_access.client_cnx() as cnx:
            lgce = cnx.entity_from_eid(self.lgce)
            lgc = cnx.entity_from_eid(self.lgc)
            ex1eid = lgc.start(lgce, check_duplicate=False).eid
            ex2eid = lgc.start(lgce, check_duplicate=False).eid
            ex3eid = lgc.start(lgce, 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)})
    # XXX  to backport
    # def test_branch_for_pe(self):
    #     #check that branch defined in ProjectEnvironement are propertly retrieved

    #     data = {
    #         'cc': self.lgce.check_config + '\nbranch=toto',
    #         'e': self.lgce.eid,

    #     }
    #     self.execute('SET PE check_config %(cc)s WHERE PE eid %(e)s', data)

    #     entity = self.execute('Any PE WHERE PE eid %(e)s', data).get_entity(0,0)

    #     repo_def = entity.apycot_repository_def
    #     self.assertIn('branch', repo_def)
    #     self.assertEqual(repo_def['branch'], 'toto')


if __name__ == '__main__':
    unittest_main()