Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

generator required for new syntax

  • Loading branch information...
commit f195f06f2f605fcbc03155d5fef8762bd53dd36e 1 parent 12f973a
Mikko Hellsing authored
View
32 autofixture/base.py
@@ -233,25 +233,18 @@ def get_generator(self, field):
specified field will be ignored (e.g. if no matching generator was
found).
'''
+
+ if field.name in self.field_generators:
+ return self.field_generators[field.name]
+
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_generators:
- value = self.field_generators[field.name]
- if isinstance(value, generators.Generator):
- return value
- elif isinstance(value, AutoFixture):
- return generators.InstanceGenerator(autofixture=value)
- elif callable(value):
- return generators.CallableGenerator(value=value)
- return generators.StaticGenerator(value=value)
if field.choices:
- return generators.ChoicesGenerator(choices=field.choices, **kwargs)
+ return generators.ChoicesGenerator(choices=field.choices)
if isinstance(field, related.ForeignKey):
# if generate_fk is set, follow_fk is ignored.
if field.name in self.generate_fk:
@@ -286,7 +279,7 @@ def get_generator(self, field):
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(
@@ -294,7 +287,7 @@ def get_generator(self, field):
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(
@@ -309,7 +302,7 @@ def get_generator(self, field):
if isinstance(field, fields.FilePathField):
return generators.FilePathGenerator(
path=field.path, match=field.match, recursive=field.recursive,
- max_length=field.max_length, **kwargs)
+ max_length=field.max_length)
if isinstance(field, fields.CharField):
if isinstance(field, fields.SlugField):
generator = generators.SlugGenerator
@@ -335,10 +328,10 @@ def get_generator(self, field):
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 generator()
return None
def get_value(self, field):
@@ -384,7 +377,6 @@ def process_m2m(self, instance, field):
# * 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
@@ -404,13 +396,13 @@ def process_m2m(self, instance, field):
get_autofixture(
through,
field_generators={
- self_fk.name: instance,
+ self_fk.name: generators.StaticGenerator(instance),
related_fk.name: generators.InstanceGenerator(
get_autofixture(field.rel.to))
}),
min_count=min_count,
max_count=max_count,
- **kwargs).generate()
+ ).generate()
def check_constrains(self, instance):
'''
View
20 autofixture/contrib/auth.py
@@ -22,16 +22,16 @@ class UserFixture(AutoFixture):
* ``date_joined`` and ``last_login`` are always in the past and it is
assured that ``date_joined`` will be lower than ``last_login``.
'''
- username = generators.UUIDGenerator()
- first_name = generators.LoremWordGenerator(1)
- last_name = generators.LoremWordGenerator(1)
- password = generators.StaticGenerator(UNUSABLE_PASSWORD)
- is_active = generators.StaticGenerator(True)
- # don't generate admin users
- is_staff = generators.StaticGenerator(False)
- is_superuser = generators.StaticGenerator(False)
- date_joined = generators.DateTimeGenerator(max_date=datetime.now())
- last_login = generators.DateTimeGenerator(max_date=datetime.now())
+ username = generators.UUIDGenerator(max_length=30)
+ first_name = generators.LoremWordGenerator(1)
+ last_name = generators.LoremWordGenerator(1)
+ password = generators.StaticGenerator(UNUSABLE_PASSWORD)
+ is_active = generators.StaticGenerator(True)
+ # don't generate admin users
+ is_staff = generators.StaticGenerator(False)
+ is_superuser = generators.StaticGenerator(False)
+ date_joined = generators.DateTimeGenerator(max_date=datetime.now())
+ last_login = generators.DateTimeGenerator(max_date=datetime.now())
# don't follow permissions and groups
follow_m2m = False
View
2  autofixture/generators.py
@@ -85,7 +85,7 @@ def generate(self):
value = unicode(uuid.uuid1())
if self.max_length is not None:
value = value[:self.max_length]
- return max_length
+ return value
class StringGenerator(Generator):
View
8 autofixture_tests/autofixture_test/tests.py
@@ -62,9 +62,9 @@ def test_field_generators(self):
filler = AutoFixture(
BasicModel,
field_generators={
- 'intfield': 1,
+ 'intfield': generators.StaticGenerator(1),
'chars': generators.ChoicesGenerator(values=char_values),
- 'shortchars': lambda: u'ab',
+ 'shortchars': generators.CallableGenerator(lambda: u'ab'),
})
for obj in filler.create(100):
self.assertEqual(obj.intfield, int_value)
@@ -356,10 +356,10 @@ def test_create(self):
def test_overwrite_attributes(self):
autofixture.register(SimpleModel, SimpleAutoFixture)
for obj in autofixture.create(
- SimpleModel, 10, field_generators={'name': 'bar'}):
+ SimpleModel, 10, field_generators={'name': generators.StaticGenerator('bar')}):
self.assertEqual(obj.name, 'bar')
obj = autofixture.create_one(
- SimpleModel, field_generators={'name': 'bar'})
+ SimpleModel, field_generators={'name': generators.StaticGenerator('bar')})
self.assertEqual(obj.name, 'bar')
Please sign in to comment.
Something went wrong with that request. Please try again.