Referring to django-autofixture documentation, I wrote the following code (edited for terseness):
def random_state_generator(self, states=None):
import random
if not states:
states = "waiting|email|post|einv".split('|')
while 1:
yield random.choice(states)
…
rsg = self.random_state_generator()
self.create_test_foo(10, values={'state': rsg})
…
field_values = dict(field_values.items() + values.items())
foo_fixture = AutoFixture(FooClass, overwrite_defaults=True,
generate_fk=generate_fk_values,
field_values=field_values)
bunch_of_stuff = foo_fixture.create(foo_count)
Okay,
now when I have the DB all set up, and I filter myself a list of stuff,
and go
for foo in bunch_of_stuff: print (foo.state)
instead of getting the expected random state string, I get the following output:
generator object random_state_generator at 0x242c640>
which is not what I’d expect based on the documentation, specifically
field_values: A dictionary with field names of model as keys. Values may be static values that are assigned to the field, a Generator instance that generates a value on the fly or a callable which takes no arguments and returns the wanted value.
What am I doing wrong?
Okay, the field_values takes a class instance from autofixture.generators, so all I had to do was:
generate the list of states;
the misunderstanding rose from confusing autofixture’s generators with generic python generators.