author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 20 Oct 2009 16:48:37 +0200 | |
branch | stable |
changeset 3755 | 9b973e8eabb3 |
parent 1977 | 606923dff11b |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: iso-8859-1 -*- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
"""unit tests for database value generator |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
8 |
""" |
0 | 9 |
|
10 |
import os.path as osp |
|
11 |
import re |
|
12 |
||
13 |
from logilab.common.testlib import TestCase, unittest_main |
|
14 |
||
15 |
from cubicweb.schema import Schema, EntitySchema |
|
16 |
from cubicweb.devtools.fill import ValueGenerator, make_tel |
|
17 |
from cubicweb.devtools import ApptestConfiguration |
|
18 |
||
19 |
DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data') |
|
20 |
ISODATE_SRE = re.compile('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$') |
|
21 |
||
22 |
||
23 |
class MyValueGenerator(ValueGenerator): |
|
24 |
||
25 |
def generate_Bug_severity(self, index): |
|
26 |
return u'dangerous' |
|
27 |
||
28 |
def generate_Any_description(self, index, format=None): |
|
29 |
return u'yo' |
|
30 |
||
31 |
||
32 |
class ValueGeneratorTC(TestCase): |
|
33 |
"""test case for ValueGenerator""" |
|
34 |
||
35 |
def _choice_func(self, etype, attrname): |
|
36 |
try: |
|
37 |
return getattr(self, '_available_%s_%s' % (etype, attrname))(etype, attrname) |
|
38 |
except AttributeError: |
|
39 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
40 |
|
0 | 41 |
def _available_Person_firstname(self, etype, attrname): |
42 |
return [f.strip() for f in file(osp.join(DATADIR, 'firstnames.txt'))] |
|
43 |
||
44 |
||
45 |
def setUp(self): |
|
46 |
config = ApptestConfiguration('data') |
|
47 |
config.bootstrap_cubes() |
|
48 |
schema = config.load_schema() |
|
3755
9b973e8eabb3
deprecated api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
49 |
e_schema = schema.eschema('Person') |
0 | 50 |
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
|
51 |
e_schema = schema.eschema('Bug') |
0 | 52 |
self.bug_valgen = MyValueGenerator(e_schema) |
53 |
self.config = config |
|
54 |
||
55 |
def _check_date(self, date): |
|
56 |
"""checks that 'date' is well-formed""" |
|
57 |
year = date.year |
|
58 |
month = date.month |
|
59 |
day = date.day |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
60 |
self.failUnless(day in range(1, 29), '%s not in [0;28]' % day) |
0 | 61 |
self.failUnless(month in range(1, 13), '%s not in [1;12]' % month) |
62 |
self.failUnless(year in range(2000, 2005), |
|
63 |
'%s not in [2000;2004]' % year) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
64 |
|
0 | 65 |
|
66 |
def test_string(self): |
|
67 |
"""test string generation""" |
|
68 |
surname = self.person_valgen._generate_value('surname', 12) |
|
69 |
self.assertEquals(surname, u'�&surname12') |
|
70 |
||
71 |
def test_domain_value(self): |
|
72 |
"""test value generation from a given domain value""" |
|
73 |
firstname = self.person_valgen._generate_value('firstname', 12) |
|
74 |
possible_choices = self._choice_func('Person', 'firstname') |
|
75 |
self.failUnless(firstname in possible_choices, |
|
76 |
'%s not in %s' % (firstname, possible_choices)) |
|
77 |
||
78 |
def test_choice(self): |
|
79 |
"""test choice generation""" |
|
80 |
# Test for random index |
|
81 |
for index in range(5): |
|
82 |
sx_value = self.person_valgen._generate_value('civility', index) |
|
83 |
self.failUnless(sx_value in ('Mr', 'Mrs', 'Ms')) |
|
84 |
||
85 |
def test_integer(self): |
|
86 |
"""test integer generation""" |
|
87 |
# Test for random index |
|
88 |
for index in range(5): |
|
89 |
cost_value = self.bug_valgen._generate_value('cost', index) |
|
90 |
self.failUnless(cost_value in range(index+1)) |
|
91 |
||
92 |
def test_date(self): |
|
93 |
"""test date generation""" |
|
94 |
# Test for random index |
|
95 |
for index in range(5): |
|
96 |
date_value = self.person_valgen._generate_value('birthday', index) |
|
97 |
self._check_date(date_value) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
98 |
|
0 | 99 |
def test_phone(self): |
100 |
"""tests make_tel utility""" |
|
101 |
self.assertEquals(make_tel(22030405), '22 03 04 05') |
|
102 |
||
103 |
||
104 |
def test_customized_generation(self): |
|
105 |
self.assertEquals(self.bug_valgen._generate_value('severity', 12), |
|
106 |
u'dangerous') |
|
107 |
self.assertEquals(self.bug_valgen._generate_value('description', 12), |
|
108 |
u'yo') |
|
109 |
self.assertEquals(self.person_valgen._generate_value('description', 12), |
|
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 |
|
114 |
class ConstraintInsertionTC(TestCase): |
|
115 |
||
116 |
def test_writeme(self): |
|
117 |
self.skip('Test automatic insertion / Schema Constraints') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
118 |
|
0 | 119 |
|
120 |
if __name__ == '__main__': |
|
121 |
unittest_main() |