author | Julien Cristau <julien.cristau@logilab.fr> |
Thu, 01 Oct 2015 12:15:14 +0200 | |
changeset 10755 | 4c48d3bab38c |
parent 10614 | 57dfde80df11 |
child 10773 | 5b051dd4a4f5 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: iso-8859-1 -*- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
2 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
3 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
4 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
5 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
6 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
7 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
8 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
9 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
10 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
11 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
12 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
14 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
15 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
16 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
17 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4651
diff
changeset
|
18 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6585
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6340
diff
changeset
|
19 |
"""unit tests for database value generator""" |
0 | 20 |
|
21 |
import os.path as osp |
|
22 |
import re |
|
4651
f9cd35dece09
[devtools/fill] fix date generation and its test
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4471
diff
changeset
|
23 |
import datetime |
0 | 24 |
|
10609
e2d8e81bfe68
[py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9674
diff
changeset
|
25 |
from six.moves import range |
e2d8e81bfe68
[py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9674
diff
changeset
|
26 |
|
0 | 27 |
from logilab.common.testlib import TestCase, unittest_main |
28 |
||
29 |
from cubicweb.devtools.fill import ValueGenerator, make_tel |
|
30 |
from cubicweb.devtools import ApptestConfiguration |
|
31 |
||
32 |
DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data') |
|
33 |
ISODATE_SRE = re.compile('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$') |
|
34 |
||
35 |
||
36 |
class MyValueGenerator(ValueGenerator): |
|
37 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
38 |
def generate_Bug_severity(self, entity, index): |
0 | 39 |
return u'dangerous' |
40 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
41 |
def generate_Any_description(self, entity, index, format=None): |
0 | 42 |
return u'yo' |
43 |
||
44 |
||
45 |
class ValueGeneratorTC(TestCase): |
|
46 |
"""test case for ValueGenerator""" |
|
47 |
||
48 |
def _choice_func(self, etype, attrname): |
|
49 |
try: |
|
50 |
return getattr(self, '_available_%s_%s' % (etype, attrname))(etype, attrname) |
|
51 |
except AttributeError: |
|
52 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
53 |
|
0 | 54 |
def _available_Person_firstname(self, etype, attrname): |
10614
57dfde80df11
[py3k] file → open
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10609
diff
changeset
|
55 |
return [f.strip() for f in open(osp.join(DATADIR, 'firstnames.txt'))] |
0 | 56 |
|
57 |
def setUp(self): |
|
6585
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6340
diff
changeset
|
58 |
config = ApptestConfiguration('data', apphome=DATADIR) |
0 | 59 |
config.bootstrap_cubes() |
60 |
schema = config.load_schema() |
|
3755
9b973e8eabb3
deprecated api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
61 |
e_schema = schema.eschema('Person') |
0 | 62 |
self.person_valgen = ValueGenerator(e_schema, self._choice_func) |
3755
9b973e8eabb3
deprecated api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
63 |
e_schema = schema.eschema('Bug') |
0 | 64 |
self.bug_valgen = MyValueGenerator(e_schema) |
65 |
self.config = config |
|
66 |
||
67 |
def test_string(self): |
|
68 |
"""test string generation""" |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
69 |
surname = self.person_valgen.generate_attribute_value({}, 'surname', 12) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
70 |
self.assertEqual(surname, u'�&surname12') |
0 | 71 |
|
72 |
def test_domain_value(self): |
|
73 |
"""test value generation from a given domain value""" |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
74 |
firstname = self.person_valgen.generate_attribute_value({}, 'firstname', 12) |
0 | 75 |
possible_choices = self._choice_func('Person', 'firstname') |
7791
31bb51ea5485
[deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6585
diff
changeset
|
76 |
self.assertTrue(firstname in possible_choices, |
0 | 77 |
'%s not in %s' % (firstname, possible_choices)) |
78 |
||
79 |
def test_choice(self): |
|
80 |
"""test choice generation""" |
|
81 |
# Test for random index |
|
82 |
for index in range(5): |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
83 |
sx_value = self.person_valgen.generate_attribute_value({}, 'civility', index) |
7791
31bb51ea5485
[deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6585
diff
changeset
|
84 |
self.assertTrue(sx_value in ('Mr', 'Mrs', 'Ms')) |
0 | 85 |
|
86 |
def test_integer(self): |
|
87 |
"""test integer generation""" |
|
88 |
# Test for random index |
|
89 |
for index in range(5): |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
90 |
cost_value = self.bug_valgen.generate_attribute_value({}, 'cost', index) |
10609
e2d8e81bfe68
[py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9674
diff
changeset
|
91 |
self.assertIn(cost_value, list(range(index+1))) |
0 | 92 |
|
93 |
def test_date(self): |
|
94 |
"""test date generation""" |
|
95 |
# Test for random index |
|
4651
f9cd35dece09
[devtools/fill] fix date generation and its test
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4471
diff
changeset
|
96 |
for index in range(10): |
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
97 |
date_value = self.person_valgen.generate_attribute_value({}, 'birthday', index) |
7791
31bb51ea5485
[deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6585
diff
changeset
|
98 |
self.assertTrue(isinstance(date_value, datetime.date)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
99 |
|
0 | 100 |
def test_phone(self): |
101 |
"""tests make_tel utility""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
102 |
self.assertEqual(make_tel(22030405), '22 03 04 05') |
0 | 103 |
|
104 |
def test_customized_generation(self): |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
105 |
self.assertEqual(self.bug_valgen.generate_attribute_value({}, 'severity', 12), |
0 | 106 |
u'dangerous') |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
107 |
self.assertEqual(self.bug_valgen.generate_attribute_value({}, 'description', 12), |
0 | 108 |
u'yo') |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
109 |
self.assertEqual(self.person_valgen.generate_attribute_value({}, 'description', 12), |
0 | 110 |
u'yo') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
111 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
112 |
|
0 | 113 |
class ConstraintInsertionTC(TestCase): |
114 |
||
115 |
def test_writeme(self): |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
116 |
self.skipTest('Test automatic insertion / Schema Constraints') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
117 |
|
0 | 118 |
|
119 |
if __name__ == '__main__': |
|
120 |
unittest_main() |