Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inconsistent behaviour between gen_from_choices and gen_string (and others) #49

Open
syphar opened this issue Dec 9, 2019 · 0 comments

Comments

@syphar
Copy link

syphar commented Dec 9, 2019

We're using model-bakery for our testing, and in relation with recipes we stumbled onto an inconsistency in the library. While gen_from_choices returns a callable that is then generates the random value when being called by baker.make_recipe, for example gen_string (and the others) directly return the random value.

Expected behavior

With the example Recipe as written here, I would expect one of the two behaviours:

everything returns a callable

MyModel = Recipe(
    'myapp.MyModel,
    first_name=gen_string(10),
    last_name=gen_string(10),
    user_type=gen_from_choices(USER_TYPES),
    is_staff=True, 
)

and each time I create a model instance with this recipe I get different values for first_name or last_name and also user_type.

everything returns a value

If I know everything returns a value, I would need to define the recipe in this way to achieve my goal of random values per creation:

MyModel = Recipe(
    'myapp.MyModel,
    first_name=lambda: gen_string(10),
    last_name=lambda: gen_string(10),
    user_type=lambda: gen_from_choices(USER_TYPES),
    is_staff=True, 
)

functools.partial also works.

Actual behavior

MyModel = Recipe(
    'myapp.MyModel,
    first_name=gen_string(10),
    last_name=gen_string(10),
    user_type=gen_from_choices(USER_TYPES),
    is_staff=True, 
)

With this recipe, what actually happens is:

  • first_name and last_name always have the same (random) value, since they are called on module-load
  • user_type has a random value generated per instance

Reproduction Steps

use the examples, add the imports, and use baker.make_recipe

Versions

Python: 3.6 / 3.7
Django: 2.2.8
Model Bakery: 1.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant