[test] Use stdlib unittest's main

authorDenis Laxalde <denis.laxalde@logilab.fr>
changeset31967b695715
branchdefault
phasedraft
hiddenyes
parent revision#c491af72dace Add .tox to .hgignore
child revision#649f423bd947 Move to package layout, #b18e13dfe4fb Move to package layout
files modified by this revision
test/test_celery.py
# HG changeset patch
# User Denis Laxalde <denis.laxalde@logilab.fr>
# Date 1475651379 -7200
# Wed Oct 05 09:09:39 2016 +0200
# Node ID 31967b6957159ab6ad79d6cd3b29a6b06a29554f
# Parent c491af72dace198b1fa6351fc360cd73b54a053e
[test] Use stdlib unittest's main

diff --git a/test/test_celery.py b/test/test_celery.py
@@ -0,0 +1,87 @@
1 +# copyright 2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2 +# contact http://www.logilab.fr -- mailto:contact@logilab.fr
3 +#
4 +# This program is free software: you can redistribute it and/or modify it under
5 +# the terms of the GNU Lesser General Public License as published by the Free
6 +# Software Foundation, either version 2.1 of the License, or (at your option)
7 +# any later version.
8 +#
9 +# This program is distributed in the hope that it will be useful, but WITHOUT
10 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 +# details.
13 +#
14 +# You should have received a copy of the GNU Lesser General Public License
15 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
16 +
17 +
18 +from cubicweb.devtools import testlib
19 +from cubicweb_celery import app, init_repo, CWTask
20 +
21 +
22 +class DefaultTC(testlib.CubicWebTC):
23 +    def setUp(self):
24 +        super(DefaultTC, self).setUp()
25 +        app.cwrepo = init_repo(app.cwconfig)
26 +
27 +    def test_cwtask(self):
28 +        eid = app.tasks['newgroup'](u'test')
29 +        with self.admin_access.cnx() as cnx:
30 +            self.assertEqual(cnx.entity_from_eid(eid).name, u'test')
31 +
32 +    def test_cw_user_cnx(self):
33 +        with self.admin_access.cnx() as cnx:
34 +            user_eid = self.create_user(cnx, u'testuser',
35 +                                        groups=('managers',)).eid
36 +            admin_eid = cnx.find('CWUser', login=u'admin').one().eid
37 +            cnx.commit()
38 +
39 +        groups = {app.tasks['newgroup'](name, eid): name for name, eid in (
40 +            (u'test_group_user', user_eid),
41 +            (u'test_group_admin', admin_eid),
42 +            (u'test_group_internal', -1),
43 +        )}
44 +
45 +        with self.admin_access.cnx() as cnx:
46 +            for group_eid, expected_name in groups.items():
47 +                self.assertEqual(cnx.entity_from_eid(group_eid).name,
48 +                                 expected_name)
49 +
50 +    def test_missing_need_cnx(self):
51 +
52 +        class MissingNeedCnxTask(CWTask):
53 +            name = 'missing-need-cnx'
54 +
55 +            def run(me):
56 +                self.assertFalse(hasattr(me, 'cw_cnx'))
57 +                with self.assertRaises(ValueError) as exc:
58 +                    with me.cw_user_cnx(None):
59 +                        pass
60 +                self.assertEqual(str(exc.exception),
61 +                                 'use of cw_user_cnx() requires need_cnx')
62 +                return 42
63 +
64 +        self.assertEqual(MissingNeedCnxTask()(), 42)
65 +
66 +    def test_default_config(self):
67 +        self.assertTrue(app.conf.CELERY_ENABLE_UTC)
68 +        self.assertEqual('Indian/Maldives',
69 +                         app.conf.CELERY_TIMEZONE)
70 +
71 +    def test_propagate_exception_in_test_mode(self):
72 +        # by default propagate exception
73 +        with self.assertRaises(ValueError) as excinfo:
74 +            app.tasks['newgroup'].delay(u'magic')
75 +        self.assertEqual(str(excinfo.exception), 'Cannot add a magic group')
76 +
77 +        app.conf.CELERY_EAGER_PROPAGATES_EXCEPTIONS = False
78 +        try:
79 +            # should not raise
80 +            app.tasks['newgroup'].delay(u'magic')
81 +        finally:
82 +            app.conf.CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
83 +
84 +
85 +if __name__ == '__main__':
86 +    import unittest
87 +    unittest.main()