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

Duplicate Subfactory not working, reusing existing object #90

Closed
wikti opened this issue Apr 1, 2020 · 1 comment
Closed

Duplicate Subfactory not working, reusing existing object #90

wikti opened this issue Apr 1, 2020 · 1 comment

Comments

@wikti
Copy link

wikti commented Apr 1, 2020

I'm using Django and have a Model with multiple ForeginKey pointing to the same Model. I have factories created like:

 class FooFactory(DjangoModelFactory):
     class Meta:
         model = Foo
     value = factory.fuzzy.FuzzyInteger(100)
     active = False
 
 class BarFactory(DjangoModelFactory):
     class Meta:
         model = Bar
     a = factory.SubFactory(FooFactory, active=True)
     b = factory.SubFactory(FooFactory, active=False)
     c = factory.SubFactory(FooFactory)

register(FooFactory, "foo")
register(BarFactory, "bar")

When I call BarFactory in test it creates object having different objects having different values. But when i get it by fixture it returns all fields having the same model attached ignoring additional flags.

So far I have made a workaround assigning a, b and c in BarFactory _create() method, but in that way i loose all benefits of this plugin.

@lsnk
Copy link

lsnk commented Apr 23, 2020

I guess I have kinda similar problem.
I have a factory with Subfactory:

class SupplierStatusFactory(factory.DjangoModelFactory):
    name = factory.Faker('word')
    code = factory.Sequence(lambda n: f'status_{n}')

    class Meta:
        model = SupplierStatus

class SupplierFactory(factory.DjangoModelFactory):

    class Meta:
        model = Supplier
    ...
    status = factory.SubFactory(SupplierStatusFactory)

When I create objects calling SupplierFactory directly it creates 2 different SupplierStatus objects.
But when I use fixtures like this:

register(factories.SupplierFactory, 'supplier')
register(factories.SupplierFactory, 'other_supplier')

they point to the same object

Is this an expected behavior?

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

2 participants