author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 24 Feb 2010 11:21:51 +0100 | |
branch | stable |
changeset 4687 | 082e66184f71 |
parent 4651 | f9cd35dece09 |
child 5421 | 8167de96c523 |
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 |
|
4651
f9cd35dece09
[devtools/fill] fix date generation and its test
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4471
diff
changeset
|
12 |
import datetime |
0 | 13 |
|
14 |
from logilab.common.testlib import TestCase, unittest_main |
|
15 |
||
16 |
from cubicweb.schema import Schema, EntitySchema |
|
17 |
from cubicweb.devtools.fill import ValueGenerator, make_tel |
|
18 |
from cubicweb.devtools import ApptestConfiguration |
|
19 |
||
20 |
DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data') |
|
21 |
ISODATE_SRE = re.compile('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$') |
|
22 |
||
23 |
||
24 |
class MyValueGenerator(ValueGenerator): |
|
25 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
26 |
def generate_Bug_severity(self, entity, index): |
0 | 27 |
return u'dangerous' |
28 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
29 |
def generate_Any_description(self, entity, index, format=None): |
0 | 30 |
return u'yo' |
31 |
||
32 |
||
33 |
class ValueGeneratorTC(TestCase): |
|
34 |
"""test case for ValueGenerator""" |
|
35 |
||
36 |
def _choice_func(self, etype, attrname): |
|
37 |
try: |
|
38 |
return getattr(self, '_available_%s_%s' % (etype, attrname))(etype, attrname) |
|
39 |
except AttributeError: |
|
40 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
41 |
|
0 | 42 |
def _available_Person_firstname(self, etype, attrname): |
43 |
return [f.strip() for f in file(osp.join(DATADIR, 'firstnames.txt'))] |
|
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 test_string(self): |
|
56 |
"""test string generation""" |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
57 |
surname = self.person_valgen.generate_attribute_value({}, 'surname', 12) |
0 | 58 |
self.assertEquals(surname, u'�&surname12') |
59 |
||
60 |
def test_domain_value(self): |
|
61 |
"""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
|
62 |
firstname = self.person_valgen.generate_attribute_value({}, 'firstname', 12) |
0 | 63 |
possible_choices = self._choice_func('Person', 'firstname') |
64 |
self.failUnless(firstname in possible_choices, |
|
65 |
'%s not in %s' % (firstname, possible_choices)) |
|
66 |
||
67 |
def test_choice(self): |
|
68 |
"""test choice generation""" |
|
69 |
# Test for random index |
|
70 |
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
|
71 |
sx_value = self.person_valgen.generate_attribute_value({}, 'civility', index) |
0 | 72 |
self.failUnless(sx_value in ('Mr', 'Mrs', 'Ms')) |
73 |
||
74 |
def test_integer(self): |
|
75 |
"""test integer generation""" |
|
76 |
# Test for random index |
|
77 |
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
|
78 |
cost_value = self.bug_valgen.generate_attribute_value({}, 'cost', index) |
0 | 79 |
self.failUnless(cost_value in range(index+1)) |
80 |
||
81 |
def test_date(self): |
|
82 |
"""test date generation""" |
|
83 |
# Test for random index |
|
4651
f9cd35dece09
[devtools/fill] fix date generation and its test
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4471
diff
changeset
|
84 |
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
|
85 |
date_value = self.person_valgen.generate_attribute_value({}, 'birthday', index) |
4651
f9cd35dece09
[devtools/fill] fix date generation and its test
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4471
diff
changeset
|
86 |
self.failUnless(isinstance(date_value, datetime.date)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
87 |
|
0 | 88 |
def test_phone(self): |
89 |
"""tests make_tel utility""" |
|
90 |
self.assertEquals(make_tel(22030405), '22 03 04 05') |
|
91 |
||
92 |
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
|
93 |
self.assertEquals(self.bug_valgen.generate_attribute_value({}, 'severity', 12), |
0 | 94 |
u'dangerous') |
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
95 |
self.assertEquals(self.bug_valgen.generate_attribute_value({}, 'description', 12), |
0 | 96 |
u'yo') |
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
97 |
self.assertEquals(self.person_valgen.generate_attribute_value({}, 'description', 12), |
0 | 98 |
u'yo') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
99 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
100 |
|
0 | 101 |
class ConstraintInsertionTC(TestCase): |
102 |
||
103 |
def test_writeme(self): |
|
104 |
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
|
105 |
|
0 | 106 |
|
107 |
if __name__ == '__main__': |
|
108 |
unittest_main() |