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

ManyToMany PostGeneration class #59

Open
stevelacey opened this issue Feb 4, 2018 · 0 comments
Open

ManyToMany PostGeneration class #59

stevelacey opened this issue Feb 4, 2018 · 0 comments

Comments

@stevelacey
Copy link

stevelacey commented Feb 4, 2018

Most m2m relation handling for factories in Django is pretty simple, is this of interest?

class ManyToManyPostGeneration(factory.PostGeneration):
    """
    Simplified factory post_generation for many-to-many relationships - sets values passed to the collection
    https://factoryboy.readthedocs.io/en/latest/recipes.html?highlight=many#simple-many-to-many-relationship

    Arguments:
        name: the name of the many-to-many relation

    Usage:
        You can pass the factory a list of model instances

        - article_factory.create(tags=LazyFixture(lambda tag: [tag])))

        Or you can use the parametrize decorator

        - @pytest.mark.parametrize('article__tags', [LazyFixture(lambda tag: [tag])])
    """
    def __init__(self, name):
        def func(self, create, extracted, **kwargs):
            if create and extracted:
                getattr(self, name).set(extracted)
        func.__name__ = name
        return super().__init__(func)

Makes basic m2m declarations as simple as

class ArticleFactory(factory.django.DjangoModelFactory):
    tags = ManyToManyPostGeneration('tags')

You can also define and use the inverse side too

class TagFactory(factory.django.DjangoModelFactory):
    articles = ManyToManyPostGeneration('articles')

I'd drop the string arg but I couldn't figure out how to know the relation name otherwise

Another thought was whether it should add or set – the examples provided use add but I figure given the whole list is passed in an explicit set is probably what is expected (i.e. clobber)

Any interest in something like this in pytest-factoryboy?

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