author | Stephanie Marcu <stephanie.marcu@logilab.fr> |
Fri, 25 Jun 2010 11:29:48 +0200 | |
branch | stable |
changeset 5835 | 4125a8021956 |
parent 5424 | 8ecbcbff9777 |
child 6340 | 470d8e828fda |
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/>. |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
19 |
"""unit tests for database value generator |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
20 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
21 |
""" |
0 | 22 |
|
23 |
import os.path as osp |
|
24 |
import re |
|
4651
f9cd35dece09
[devtools/fill] fix date generation and its test
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4471
diff
changeset
|
25 |
import datetime |
0 | 26 |
|
27 |
from logilab.common.testlib import TestCase, unittest_main |
|
28 |
||
29 |
from cubicweb.schema import Schema, EntitySchema |
|
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 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
39 |
def generate_Bug_severity(self, entity, index): |
0 | 40 |
return u'dangerous' |
41 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
42 |
def generate_Any_description(self, entity, index, format=None): |
0 | 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 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
54 |
|
0 | 55 |
def _available_Person_firstname(self, etype, attrname): |
56 |
return [f.strip() for f in file(osp.join(DATADIR, 'firstnames.txt'))] |
|
57 |
||
58 |
def setUp(self): |
|
59 |
config = ApptestConfiguration('data') |
|
60 |
config.bootstrap_cubes() |
|
61 |
schema = config.load_schema() |
|
3755
9b973e8eabb3
deprecated api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
62 |
e_schema = schema.eschema('Person') |
0 | 63 |
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
|
64 |
e_schema = schema.eschema('Bug') |
0 | 65 |
self.bug_valgen = MyValueGenerator(e_schema) |
66 |
self.config = config |
|
67 |
||
68 |
def test_string(self): |
|
69 |
"""test string generation""" |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
70 |
surname = self.person_valgen.generate_attribute_value({}, 'surname', 12) |
0 | 71 |
self.assertEquals(surname, u'�&surname12') |
72 |
||
73 |
def test_domain_value(self): |
|
74 |
"""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
|
75 |
firstname = self.person_valgen.generate_attribute_value({}, 'firstname', 12) |
0 | 76 |
possible_choices = self._choice_func('Person', 'firstname') |
77 |
self.failUnless(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): |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
84 |
sx_value = self.person_valgen.generate_attribute_value({}, 'civility', index) |
0 | 85 |
self.failUnless(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): |
|
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
91 |
cost_value = self.bug_valgen.generate_attribute_value({}, 'cost', index) |
0 | 92 |
self.failUnless(cost_value in range(index+1)) |
93 |
||
94 |
def test_date(self): |
|
95 |
"""test date generation""" |
|
96 |
# Test for random index |
|
4651
f9cd35dece09
[devtools/fill] fix date generation and its test
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4471
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
|
0 | 101 |
def test_phone(self): |
102 |
"""tests make_tel utility""" |
|
103 |
self.assertEquals(make_tel(22030405), '22 03 04 05') |
|
104 |
||
105 |
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
|
106 |
self.assertEquals(self.bug_valgen.generate_attribute_value({}, 'severity', 12), |
0 | 107 |
u'dangerous') |
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
108 |
self.assertEquals(self.bug_valgen.generate_attribute_value({}, 'description', 12), |
0 | 109 |
u'yo') |
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
110 |
self.assertEquals(self.person_valgen.generate_attribute_value({}, 'description', 12), |
0 | 111 |
u'yo') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
112 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
113 |
|
0 | 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() |