author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Fri, 01 Jul 2011 20:14:20 +0200 | |
branch | stable |
changeset 7613 | 63bac28113a5 |
parent 6585 | 9af22f2c0c4c |
child 7791 | 31bb51ea5485 |
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/>. |
6585
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6340
diff
changeset
|
19 |
"""unit tests for database value generator""" |
0 | 20 |
|
21 |
import os.path as osp |
|
22 |
import re |
|
4651
f9cd35dece09
[devtools/fill] fix date generation and its test
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4471
diff
changeset
|
23 |
import datetime |
0 | 24 |
|
25 |
from logilab.common.testlib import TestCase, unittest_main |
|
26 |
||
27 |
from cubicweb.schema import Schema, EntitySchema |
|
28 |
from cubicweb.devtools.fill import ValueGenerator, make_tel |
|
29 |
from cubicweb.devtools import ApptestConfiguration |
|
30 |
||
31 |
DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data') |
|
32 |
ISODATE_SRE = re.compile('(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$') |
|
33 |
||
34 |
||
35 |
class MyValueGenerator(ValueGenerator): |
|
36 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
37 |
def generate_Bug_severity(self, entity, index): |
0 | 38 |
return u'dangerous' |
39 |
||
4471
71fbc449e814
db fill api has changed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
40 |
def generate_Any_description(self, entity, index, format=None): |
0 | 41 |
return u'yo' |
42 |
||
43 |
||
44 |
class ValueGeneratorTC(TestCase): |
|
45 |
"""test case for ValueGenerator""" |
|
46 |
||
47 |
def _choice_func(self, etype, attrname): |
|
48 |
try: |
|
49 |
return getattr(self, '_available_%s_%s' % (etype, attrname))(etype, attrname) |
|
50 |
except AttributeError: |
|
51 |
return None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
52 |
|
0 | 53 |
def _available_Person_firstname(self, etype, attrname): |
54 |
return [f.strip() for f in file(osp.join(DATADIR, 'firstnames.txt'))] |
|
55 |
||
56 |
def setUp(self): |
|
6585
9af22f2c0c4c
[test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6340
diff
changeset
|
57 |
config = ApptestConfiguration('data', apphome=DATADIR) |
0 | 58 |
config.bootstrap_cubes() |
59 |
schema = config.load_schema() |
|
3755
9b973e8eabb3
deprecated api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
60 |
e_schema = schema.eschema('Person') |
0 | 61 |
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
|
62 |
e_schema = schema.eschema('Bug') |
0 | 63 |
self.bug_valgen = MyValueGenerator(e_schema) |
64 |
self.config = config |
|
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) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
69 |
self.assertEqual(surname, u'�&surname12') |
0 | 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 |
|
4651
f9cd35dece09
[devtools/fill] fix date generation and its test
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4471
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
|
0 | 99 |
def test_phone(self): |
100 |
"""tests make_tel utility""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
101 |
self.assertEqual(make_tel(22030405), '22 03 04 05') |
0 | 102 |
|
103 |
def test_customized_generation(self): |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
104 |
self.assertEqual(self.bug_valgen.generate_attribute_value({}, 'severity', 12), |
0 | 105 |
u'dangerous') |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
106 |
self.assertEqual(self.bug_valgen.generate_attribute_value({}, 'description', 12), |
0 | 107 |
u'yo') |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
108 |
self.assertEqual(self.person_valgen.generate_attribute_value({}, 'description', 12), |
0 | 109 |
u'yo') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
110 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
111 |
|
0 | 112 |
class ConstraintInsertionTC(TestCase): |
113 |
||
114 |
def test_writeme(self): |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
115 |
self.skipTest('Test automatic insertion / Schema Constraints') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
116 |
|
0 | 117 |
|
118 |
if __name__ == '__main__': |
|
119 |
unittest_main() |