1 # -*- coding: iso-8859-1 -*- |
|
2 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
4 # |
|
5 # This file is part of CubicWeb. |
|
6 # |
|
7 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
8 # terms of the GNU Lesser General Public License as published by the Free |
|
9 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
10 # any later version. |
|
11 # |
|
12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
15 # details. |
|
16 # |
|
17 # You should have received a copy of the GNU Lesser General Public License along |
|
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
19 """unit tests for database value generator""" |
|
20 |
|
21 import os.path as osp |
|
22 import re |
|
23 import datetime |
|
24 import io |
|
25 |
|
26 from six.moves import range |
|
27 |
|
28 from logilab.common.testlib import TestCase, unittest_main |
|
29 |
|
30 from cubicweb.devtools.fill import ValueGenerator, make_tel |
|
31 from cubicweb.devtools import ApptestConfiguration |
|
32 |
|
33 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data') |
|
34 ISODATE_SRE = re.compile('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$') |
|
35 |
|
36 |
|
37 class MyValueGenerator(ValueGenerator): |
|
38 |
|
39 def generate_Bug_severity(self, entity, index): |
|
40 return u'dangerous' |
|
41 |
|
42 def generate_Any_description(self, entity, index, format=None): |
|
43 return u'yo' |
|
44 |
|
45 |
|
46 class ValueGeneratorTC(TestCase): |
|
47 """test case for ValueGenerator""" |
|
48 |
|
49 def _choice_func(self, etype, attrname): |
|
50 try: |
|
51 return getattr(self, '_available_%s_%s' % (etype, attrname))(etype, attrname) |
|
52 except AttributeError: |
|
53 return None |
|
54 |
|
55 def _available_Person_firstname(self, etype, attrname): |
|
56 return [f.strip() for f in io.open(osp.join(DATADIR, 'firstnames.txt'), encoding='latin1')] |
|
57 |
|
58 def setUp(self): |
|
59 config = ApptestConfiguration('data', apphome=DATADIR) |
|
60 config.bootstrap_cubes() |
|
61 schema = config.load_schema() |
|
62 e_schema = schema.eschema('Person') |
|
63 self.person_valgen = ValueGenerator(e_schema, self._choice_func) |
|
64 e_schema = schema.eschema('Bug') |
|
65 self.bug_valgen = MyValueGenerator(e_schema) |
|
66 self.config = config |
|
67 |
|
68 def test_string(self): |
|
69 """test string generation""" |
|
70 surname = self.person_valgen.generate_attribute_value({}, 'surname', 12) |
|
71 self.assertEqual(surname, u'é&surname12') |
|
72 |
|
73 def test_domain_value(self): |
|
74 """test value generation from a given domain value""" |
|
75 firstname = self.person_valgen.generate_attribute_value({}, 'firstname', 12) |
|
76 possible_choices = self._choice_func('Person', 'firstname') |
|
77 self.assertTrue(firstname in possible_choices, |
|
78 '%s not in %s' % (firstname, possible_choices)) |
|
79 |
|
80 def test_choice(self): |
|
81 """test choice generation""" |
|
82 # Test for random index |
|
83 for index in range(5): |
|
84 sx_value = self.person_valgen.generate_attribute_value({}, 'civility', index) |
|
85 self.assertTrue(sx_value in ('Mr', 'Mrs', 'Ms')) |
|
86 |
|
87 def test_integer(self): |
|
88 """test integer generation""" |
|
89 # Test for random index |
|
90 for index in range(5): |
|
91 cost_value = self.bug_valgen.generate_attribute_value({}, 'cost', index) |
|
92 self.assertIn(cost_value, list(range(index+1))) |
|
93 |
|
94 def test_date(self): |
|
95 """test date generation""" |
|
96 # Test for random index |
|
97 for index in range(10): |
|
98 date_value = self.person_valgen.generate_attribute_value({}, 'birthday', index) |
|
99 self.assertTrue(isinstance(date_value, datetime.date)) |
|
100 |
|
101 def test_phone(self): |
|
102 """tests make_tel utility""" |
|
103 self.assertEqual(make_tel(22030405), '22 03 04 05') |
|
104 |
|
105 def test_customized_generation(self): |
|
106 self.assertEqual(self.bug_valgen.generate_attribute_value({}, 'severity', 12), |
|
107 u'dangerous') |
|
108 self.assertEqual(self.bug_valgen.generate_attribute_value({}, 'description', 12), |
|
109 u'yo') |
|
110 self.assertEqual(self.person_valgen.generate_attribute_value({}, 'description', 12), |
|
111 u'yo') |
|
112 |
|
113 |
|
114 class ConstraintInsertionTC(TestCase): |
|
115 |
|
116 def test_writeme(self): |
|
117 self.skipTest('Test automatic insertion / Schema Constraints') |
|
118 |
|
119 |
|
120 if __name__ == '__main__': |
|
121 unittest_main() |
|