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

SubFactory uses wrong fixture name when factory class registered with different model_name #91

Open
ryancausey opened this issue May 12, 2020 · 1 comment

Comments

@ryancausey
Copy link

It appears that when processing SubFactory attributes on the parent factory and generating deps for the SubFactory fixture, the introspected names do not take into account whether a given factory was registered earlier with a different model_name.

This gist demonstrates a failing test where the generated SubFactory fixture is expecting to find a b_model__name fixture when the one available is actually named b_model_with_some_other_name__name.
https://gist.github.com/ryancausey/904ad5b77349769d32c72d413dd79a59

I think the root cause of all the issues I'm experiencing can be boiled down to this. This generally shows up when a second factory class is created from a parent subclass. Using the documentation's AuthorFactory as an example:

class BobAuthorFactory(AuthorFactory):
    name = "Bob"

register(BobAuthorFactory, "author_bob")

If we create a second BookFactory subclass that uses BobAuthorFactory as the SubFactory:

class BobBookFactory(BookFactory):
    author = factory.SubFactory(BobAuthorFactory)

register(BobBookFactory, "book_bob")

Then any use of book_bob will actually end up using the original author fixture and end up with an author whose name is "Charles Dickens" based on the docs at the time of this writing.

It doesn't appear that there's an easy fix for register to be able to look up what model_name a given factory class was registered with so I don't have a good answer on how to fix this right now.

@Yelinz
Copy link

Yelinz commented Jun 15, 2023

I had a similar issue with SubFactory registering automatically.
I used a new Factory with a conflicting name DocumentFactory only as a SubFactory and later the old DocumentFactory is registered. This caused the old one to not be registered and the new one to take precedent, even though I never explicitly registered it.
Which caused all references to the old factory to break, as they were now using the new one.

if factory_name not in caller_locals.value:

Maybe throwing a warning here would be nice, if there is a name conflict if they arent the same class.

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