test/unittest_entities.py
author David Douard <david.douard@logilab.fr>
Wed, 30 Apr 2014 15:44:54 +0200
changeset 1501 a45f5a997ab2
parent 1456 c7e5156479d1
child 1563 000605df69cf
permissions -rw-r--r--
[test] ensure cubicweb.devtools is imported first so the cubes are in sys.path

#!/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)
        self.lgd = self.add_test_config(u'lgd', check_config=None,
                                        env=self.lgce, group=self.pyp)

    def test_refinement_base(self):
        self.assertEqual(self.lgce.apycot_process_environment(),
                         {'DISPLAY': ':2.0', 'SETUPTOOLS': '1'})
        self.assertEqual(self.lgc.apycot_process_environment(),
                         {'DISPLAY': ':1.0', 'NO_SETUPTOOLS': '1'})

        self.assertEqual(self.lgd.apycot_configuration(self.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):
        self.assertEqual(self.lgc.apycot_configuration(self.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):
        ex1 = self.lgc.start(self.lgce, check_duplicate=False)
        ex2 = self.lgc.start(self.lgce, check_duplicate=False)
        self.commit()
        self.login('narval', password='narval0')
        self.dumb_execution(ex1, [('unittest', 'success'), ('coverage', 'success')])
        self.commit()
        covcr = ex1.check_result_by_name('coverage').eid
        self.dumb_execution(ex2, [('unittest', 'failure')])
        self.commit()
        ucr = ex2.check_result_by_name('unittest').eid
        self.commit()
        self.restore_connection()
        self.assertEqual([cr.eid for cr in all_check_results(self.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'])
        self.request().create_entity('TestConfig', name=u'lgd', use_environment=self.lgce)
        self.assertRaises(ValidationError, self.commit)

    def test_status_change(self):
        ex1 = self.lgc.start(self.lgce, check_duplicate=False)
        ex2 = self.lgc.start(self.lgce, check_duplicate=False)
        ex3 = self.lgc.start(self.lgce, check_duplicate=False)
        self.commit()
        self.login('narval', password='narval0')
        self.dumb_execution(ex1, [('unittest', 'success'), ('coverage', 'success')])
        self.dumb_execution(ex2, [('unittest', 'failure'), ('coverage', 'success')])
        self.dumb_execution(ex3, [('unittest', 'success'), ('coverage', 'error')])
        self.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()