author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 16 Feb 2010 09:25:18 +0100 | |
changeset 4593 | d0b5ef72a492 |
parent 4471 | 71fbc449e814 |
child 4651 | f9cd35dece09 |
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 |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3755
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1977
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 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
25 |
def generate_Bug_severity(self, entity, index): |
0 | 26 |
return u'dangerous' |
27 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
28 |
def generate_Any_description(self, entity, index, format=None): |
0 | 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""" |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
68 |
surname = self.person_valgen.generate_attribute_value({}, 'surname', 12) |
0 | 69 |
self.assertEquals(surname, u'�&surname12') |
70 |
||
71 |
def test_domain_value(self): |
|
72 |
"""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
|
73 |
firstname = self.person_valgen.generate_attribute_value({}, 'firstname', 12) |
0 | 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): |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
82 |
sx_value = self.person_valgen.generate_attribute_value({}, 'civility', index) |
0 | 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): |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
89 |
cost_value = self.bug_valgen.generate_attribute_value({}, 'cost', index) |
0 | 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): |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
96 |
date_value = self.person_valgen.generate_attribute_value({}, 'birthday', index) |
0 | 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): |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
105 |
self.assertEquals(self.bug_valgen.generate_attribute_value({}, 'severity', 12), |
0 | 106 |
u'dangerous') |
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
107 |
self.assertEquals(self.bug_valgen.generate_attribute_value({}, 'description', 12), |
0 | 108 |
u'yo') |
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
109 |
self.assertEquals(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 |
|
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() |