15 # details. |
15 # details. |
16 # |
16 # |
17 # You should have received a copy of the GNU Lesser General Public License along |
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/>. |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
19 """This modules defines func / methods for creating test repositories""" |
19 """This modules defines func / methods for creating test repositories""" |
20 from __future__ import print_function |
|
21 |
|
22 |
|
23 |
20 |
24 import logging |
21 import logging |
25 from random import randint, choice |
22 from random import randint, choice |
26 from copy import deepcopy |
23 from copy import deepcopy |
27 from datetime import datetime, date, time, timedelta |
24 from datetime import datetime, date, time, timedelta |
28 from decimal import Decimal |
25 from decimal import Decimal |
29 import inspect |
26 import inspect |
30 |
|
31 from six import text_type, add_metaclass |
|
32 from six.moves import range |
|
33 |
27 |
34 from logilab.common import attrdict |
28 from logilab.common import attrdict |
35 from logilab.mtconverter import xml_escape |
29 from logilab.mtconverter import xml_escape |
36 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
30 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
37 IntervalBoundConstraint, BoundaryConstraint, |
31 IntervalBoundConstraint, BoundaryConstraint, |
232 """generates a consistent value for 'attrname' if it has some static |
226 """generates a consistent value for 'attrname' if it has some static |
233 vocabulary set, else return None. |
227 vocabulary set, else return None. |
234 """ |
228 """ |
235 for cst in self.eschema.rdef(attrname).constraints: |
229 for cst in self.eschema.rdef(attrname).constraints: |
236 if isinstance(cst, StaticVocabularyConstraint): |
230 if isinstance(cst, StaticVocabularyConstraint): |
237 return text_type(choice(cst.vocabulary())) |
231 return choice(cst.vocabulary()) |
238 return None |
232 return None |
239 |
233 |
240 # XXX nothing to do here |
234 # XXX nothing to do here |
241 def generate_Any_data_format(self, entity, index, **kwargs): |
235 def generate_Any_data_format(self, entity, index, **kwargs): |
242 # data_format attribute of File has no vocabulary constraint, we |
236 # data_format attribute of File has no vocabulary constraint, we |
268 raise TypeError('generate_xxx must accept at least 1 argument') |
262 raise TypeError('generate_xxx must accept at least 1 argument') |
269 setattr(_ValueGenerator, attrname, attrvalue) |
263 setattr(_ValueGenerator, attrname, attrvalue) |
270 return type.__new__(mcs, name, bases, classdict) |
264 return type.__new__(mcs, name, bases, classdict) |
271 |
265 |
272 |
266 |
273 @add_metaclass(autoextend) |
267 class ValueGenerator(_ValueGenerator, metaclass=autoextend): |
274 class ValueGenerator(_ValueGenerator): |
|
275 pass |
268 pass |
276 |
269 |
277 |
270 |
278 def _default_choice_func(etype, attrname): |
271 def _default_choice_func(etype, attrname): |
279 """default choice_func for insert_entity_queries""" |
272 """default choice_func for insert_entity_queries""" |
357 value = value.strftime(vreg.property_value('ui.time-format')) |
350 value = value.strftime(vreg.property_value('ui.time-format')) |
358 elif attrschema.type == 'Float': |
351 elif attrschema.type == 'Float': |
359 fmt = vreg.property_value('ui.float-format') |
352 fmt = vreg.property_value('ui.float-format') |
360 value = fmt % value |
353 value = fmt % value |
361 else: |
354 else: |
362 value = text_type(value) |
355 value = value |
363 return entity |
356 return entity |
364 |
357 |
365 |
358 |
366 |
359 |
367 def select(constraints, cnx, selectvar='O', objtype=None): |
360 def select(constraints, cnx, selectvar='O', objtype=None): |