forked from sorl/django-mockups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
478 lines (425 loc) · 18.4 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# -*- coding: utf-8 -*-
from django.db import models
from django.db.models import fields
from django.db.models.fields import related
from django.utils.datastructures import SortedDict
from autofixture import constraints, generators, signals
class CreateInstanceError(Exception):
pass
class Link(object):
'''
Handles logic of following or generating foreignkeys and m2m relations.
'''
def __init__(self, fields=None, default=None):
self.fields = {}
self.subfields = {}
self.default = default
fields = fields or {}
if fields is True:
fields = {'ALL': None}
if not isinstance(fields, dict):
fields = dict([(v, None) for v in fields])
for field, value in fields.items():
try:
fieldname, subfield = field.split('__', 1)
self.subfields.setdefault(fieldname, {})[subfield] = value
except ValueError:
self.fields[field] = value
def __getitem__(self, key):
return self.fields.get(key,
self.fields.get('ALL', self.default))
def __iter__(self):
for field in self.fields:
yield field
for key, value in self.subfields.items():
yield '%s__%s' % (key, value)
def __contains__(self, value):
if 'ALL' in self.fields:
return True
if value in self.fields:
return True
return False
def get_deep_links(self, field):
if 'ALL' in self.fields:
fields = {'ALL': self.fields['ALL']}
else:
fields = self.subfields.get(field, {})
if 'ALL' in fields:
fields = {'ALL': fields['ALL']}
return Link(fields, default=self.default)
class AutoFixture(object):
'''
.. We don't support the following fields yet:
* ``XMLField``
* ``FileField``
* ``ImageField``
Patches are welcome.
'''
class IGNORE_FIELD(object):
pass
overwrite_defaults = False
follow_fk = True
generate_fk = False
follow_m2m = {'ALL': (1,5)}
generate_m2m = False
none_chance = 0.2
tries = 1000
field_to_generator = SortedDict((
(fields.BooleanField, generators.BooleanGenerator),
(fields.NullBooleanField, generators.NullBooleanGenerator),
(fields.DateTimeField, generators.DateTimeGenerator),
(fields.DateField, generators.DateGenerator),
(fields.PositiveSmallIntegerField, generators.PositiveSmallIntegerGenerator),
(fields.PositiveIntegerField, generators.PositiveIntegerGenerator),
(fields.SmallIntegerField, generators.SmallIntegerGenerator),
(fields.IntegerField, generators.IntegerGenerator),
(fields.IPAddressField, generators.IPAddressGenerator),
(fields.TextField, generators.LoremGenerator),
(fields.TimeField, generators.TimeGenerator),
))
field_values = {}
default_constraints = [
constraints.unique_constraint,
constraints.unique_together_constraint]
def __init__(self, model,
field_values=None, none_chance=None, overwrite_defaults=None,
constraints=None, follow_fk=None, generate_fk=None,
follow_m2m=None, generate_m2m=None):
'''
Parameters:
``model``: A model class which is used to create the test data.
``field_values``: A dictionary with field names of ``model`` as
keys. Values may be static values that are assigned to the field
or a ``Generator`` instance that generates a value on the fly.
``none_chance``: The chance (between 0 and 1, 1 equals 100%) to
assign ``None`` to nullable fields.
``overwrite_defaults``: All default values of fields are preserved
by default. If set to ``True``, default values will be treated
like any other field.
``constraints``: A list of callables. The constraints are used to
verify if the created model instance may be used. The callable
gets the actual model as first and the instance as second
parameter. The instance is not populated yet at this moment. The
callable may raise an :exc:`InvalidConstraint` exception to
indicate which fields violate the constraint.
``follow_fk``: A boolean value indicating if foreign keys should be
set to random, already existing, instances of the related model.
``generate_fk``: A boolean which indicates if related models should
also be created with random values. The *follow_fk* parameter will
be ignored if *generate_fk* is set to ``True``.
``follow_m2m``: A tuple containing minium and maximum of model
instances that are assigned to ``ManyToManyField``. No new
instances will be created. Default is (1, 5). You can ignore
``ManyToManyField`` fields by setting this parameter to ``False``.
``generate_m2m``: A tuple containing minimum and maximum number of
model instance that are newly created and assigned to the
``ManyToManyField``. Default is ``False`` which disables the
generation of new related instances. The value of ``follow_m2m``
will be ignored if this parameter is set.
'''
self.model = model
self.field_values = self.__class__.field_values.copy()
self.field_values.update(field_values or {})
self.constraints = constraints or []
if none_chance is not None:
self.none_chance = none_chance
if overwrite_defaults is not None:
self.overwrite_defaults = overwrite_defaults
if follow_fk is not None:
self.follow_fk = follow_fk
if not isinstance(self.follow_fk, Link):
self.follow_fk = Link(self.follow_fk)
if generate_fk is not None:
self.generate_fk = generate_fk
if not isinstance(self.generate_fk, Link):
self.generate_fk = Link(self.generate_fk)
if follow_m2m is not None:
if not isinstance(follow_m2m, dict):
if follow_m2m:
follow_m2m = Link({'ALL': follow_m2m})
else:
follow_m2m = Link(False)
self.follow_m2m = follow_m2m
if not isinstance(self.follow_m2m, Link):
self.follow_m2m = Link(self.follow_m2m)
if generate_m2m is not None:
if not isinstance(generate_m2m, dict):
if generate_m2m:
generate_m2m = Link({'ALL': generate_m2m})
else:
generate_m2m = Link(False)
self.generate_m2m = generate_m2m
if not isinstance(self.generate_m2m, Link):
self.generate_m2m = Link(self.generate_m2m)
for constraint in self.default_constraints:
self.add_constraint(constraint)
self._field_generators = {}
self.prepare_class()
def prepare_class(self):
'''
This method is called after the :meth:`__init__` method. It has no
semantic by default.
'''
pass
def add_field_value(self, name, value):
'''
Pass a static *value* that should be assigned to the field called
*name*. *value* may be a :ref:`Generator <Generator>` instance.
'''
self.field_values[name] = value
def add_constraint(self, constraint):
'''
Add a *constraint* to the autofixture.
'''
self.constraints.append(constraint)
def get_generator(self, field):
'''
Return a value generator based on the field instance that is passed to
this method. This function may return ``None`` which means that the
specified field will be ignored (e.g. if no matching generator was
found).
'''
if isinstance(field, fields.AutoField):
return None
if field.default is not fields.NOT_PROVIDED and \
not self.overwrite_defaults:
return None
kwargs = {}
if field.name in self.field_values:
value = self.field_values[field.name]
if isinstance(value, generators.Generator):
return value
elif isinstance(value, AutoFixture):
return generators.InstanceGenerator(autofixture=value)
return generators.StaticGenerator(value=value)
if field.null:
kwargs['none_chance'] = self.none_chance
if field.choices:
return generators.ChoicesGenerator(choices=field.choices, **kwargs)
if isinstance(field, related.ForeignKey):
# if generate_fk is set, follow_fk is ignored.
if field.name in self.generate_fk:
return generators.InstanceGenerator(
AutoFixture(
field.rel.to,
follow_fk=self.follow_fk.get_deep_links(field.name),
generate_fk=self.generate_fk.get_deep_links(field.name)),
limit_choices_to=field.rel.limit_choices_to)
if field.name in self.follow_fk:
return generators.InstanceSelector(
field.rel.to,
limit_choices_to=field.rel.limit_choices_to)
if field.blank or field.null:
return generators.NoneGenerator()
raise CreateInstanceError(
u'Cannot resolve ForeignKey "%s" to "%s". Provide either '
u'"follow_fk" or "generate_fk" parameters.' % (
field.name,
'%s.%s' % (
field.rel.to._meta.app_label,
field.rel.to._meta.object_name,
)
))
if isinstance(field, related.ManyToManyField):
if field.name in self.generate_m2m:
min_count, max_count = self.generate_m2m[field.name]
return generators.MultipleInstanceGenerator(
AutoFixture(
field.rel.to
),
limit_choices_to=field.rel.limit_choices_to,
min_count=min_count,
max_count=max_count,
**kwargs)
if field.name in self.follow_m2m:
min_count, max_count = self.follow_m2m[field.name]
return generators.InstanceSelector(
field.rel.to,
limit_choices_to=field.rel.limit_choices_to,
min_count=min_count,
max_count=max_count,
**kwargs)
if field.blank or field.null:
return generators.StaticGenerator([])
raise CreateInstanceError(
u'Cannot assign instances of "%s" to ManyToManyField "%s". '
u'Provide either "follow_m2m" or "generate_m2m" argument.' % (
'%s.%s' % (
field.rel.to._meta.app_label,
field.rel.to._meta.object_name,
),
field.name,
))
if isinstance(field, fields.FilePathField):
return generators.FilePathGenerator(
path=field.path, match=field.match, recursive=field.recursive,
max_length=field.max_length, **kwargs)
if isinstance(field, fields.CharField):
if isinstance(field, fields.SlugField):
generator = generators.SlugGenerator
elif isinstance(field, fields.EmailField):
return generators.EmailGenerator(
max_length=min(field.max_length, 30))
elif isinstance(field, fields.URLField):
return generators.URLGenerator(
max_length=min(field.max_length, 25))
elif field.max_length > 15:
return generators.LoremSentenceGenerator(
common=False,
max_length=field.max_length)
else:
generator = generators.StringGenerator
return generator(max_length=field.max_length)
if isinstance(field, fields.DecimalField):
return generators.DecimalGenerator(
decimal_places=field.decimal_places,
max_digits=field.max_digits)
if hasattr(fields, 'BigIntegerField'):
if isinstance(field, fields.BigIntegerField):
return generators.IntegerGenerator(
min_value=-field.MAX_BIGINT - 1,
max_value=field.MAX_BIGINT,
**kwargs)
for field_class, generator in self.field_to_generator.items():
if isinstance(field, field_class):
return generator(**kwargs)
return None
def get_value(self, field):
'''
Return a random value that can be assigned to the passed *field*
instance.
'''
if field not in self._field_generators:
self._field_generators[field] = self.get_generator(field)
generator = self._field_generators[field]
if generator is None:
return self.IGNORE_FIELD
value = generator.get_value()
return value
def process_field(self, instance, field):
value = self.get_value(field)
if value is self.IGNORE_FIELD:
return
setattr(instance, field.name, value)
def process_m2m(self, instance, field):
# check django's version number to determine how intermediary models
# are checked if they are auto created or not.
from django import VERSION
auto_created_through_model = False
through = field.rel.through
if VERSION < (1,2):
if through:
if isinstance(through, basestring):
bits = through.split('.')
if len(bits) < 2:
bits = [instance._meta.app_label] + bits
through = models.get_model(*bits)
else:
auto_created_through_model = True
else:
auto_created_through_model = through._meta.auto_created
if auto_created_through_model:
return self.process_field(instance, field)
# if m2m relation has intermediary model:
# * only generate relation if 'generate_m2m' is given
# * first generate intermediary model and assign a newly created
# related model to the foreignkey
kwargs = {}
if field.name in self.generate_m2m:
# get fk to related model on intermediary model
related_fks = [fk
for fk in through._meta.fields
if isinstance(fk, related.ForeignKey) and \
fk.rel.to is field.rel.to]
self_fks = [fk
for fk in through._meta.fields
if isinstance(fk, related.ForeignKey) and \
fk.rel.to is self.model]
assert len(related_fks) == 1
assert len(self_fks) == 1
related_fk = related_fks[0]
self_fk = self_fks[0]
min_count, max_count = self.generate_m2m[field.name]
intermediary_model = generators.MultipleInstanceGenerator(
AutoFixture(
through,
field_values={
self_fk.name: instance,
related_fk.name: generators.InstanceGenerator(
AutoFixture(field.rel.to))
}),
min_count=min_count,
max_count=max_count,
**kwargs).generate()
def check_constrains(self, instance):
'''
Return fieldnames which need recalculation.
'''
recalc_fields = []
for constraint in self.constraints:
try:
constraint(self.model, instance)
except constraints.InvalidConstraint, e:
recalc_fields.extend(e.fields)
return recalc_fields
def post_process_instance(self, instance):
'''
Overwrite this method to modify the created *instance* at the last
possible moment. It gets the generated *instance* and must return the
modified instance.
'''
return instance
def create_one(self, commit=True):
'''
Create and return one model instance. If *commit* is ``False`` the
instance will not be saved and many to many relations will not be
processed.
May raise :exc:`CreateInstanceError` if constraints are not satisfied.
'''
tries = self.tries
instance = self.model()
process = instance._meta.fields
while process and tries > 0:
for field in process:
self.process_field(instance, field)
process = self.check_constrains(instance)
tries -= 1
if tries == 0:
raise CreateInstanceError(
u'Cannot solve constraints for "%s", tried %d times. '
u'Please check value generators or model constraints. '
u'At least the following fields are involved: %s' % (
'%s.%s' % (
self.model._meta.app_label,
self.model._meta.object_name),
self.tries,
', '.join([field.name for field in process]),
))
if commit:
instance.save()
for field in instance._meta.many_to_many:
self.process_m2m(instance, field)
signals.instance_created.send(
sender=self,
model=self.model,
instance=instance,
committed=commit)
return self.post_process_instance(instance)
def create(self, count=1, commit=True):
'''
Create and return ``count`` model instances. If *commit* is ``False``
the instances will not be saved and many to many relations will not be
processed.
May raise ``CreateInstanceError`` if constraints are not satisfied.
The method internally calls :meth:`create_one` to generate instances.
'''
object_list = []
for i in xrange(count):
instance = self.create_one(commit=commit)
object_list.append(instance)
return object_list
def iter(self, count=1, commit=True):
for i in xrange(count):
yield self.create_one(commit=commit)
def __iter__(self):
yield self.create_one()