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

integration with factory-boy or other similar solution #78

Closed
skarzi opened this issue May 9, 2020 · 7 comments
Closed

integration with factory-boy or other similar solution #78

skarzi opened this issue May 9, 2020 · 7 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@skarzi
Copy link
Collaborator

skarzi commented May 9, 2020

Many of existing Django projects use factory-boy or other similar solution to create models instances before actual tests, so it will be really handy to provide some integration with such tools to make migrations testing easier and less verbose than Model.objects.create() etc.

@skarzi skarzi added enhancement New feature or request help wanted Extra attention is needed labels May 9, 2020
@sobolevn
Copy link
Member

sobolevn commented May 10, 2020

Thanks a lot for the suggestion! However, I think that data creation is out of scope of this project.
Currently I don't have any problems creating it with a code like this:

@pytest.mark.django_db
def test_my_app_migration0031(migrator, fakery):
    """Ensures that the some fields are deleted correctly."""
    old_state = migrator.before(
        ('my_app', '0030_pre'),
    )
    my_model = old_state.apps.get_model(
        'my_app', 'MyModel',
    )

    kwargs_to_delete = [
        {'a': None},
        {'b': 'in_progress'},
        {'c': None},
        {'d': ''},
        {'e': ''},
        {'f': None},
    ]

    for kwargs in kwargs_to_delete:
        fakery.m(my_model)(**kwargs)

    assert my_model.objects.count() == len(kwargs_to_delete)

    new_state = migrator.after(('my_app', '0031_some'))
    my_model = new_state.apps.get_model(
        'my_app', 'MyModel',
    )

    assert not my_model.objects.count()

@skarzi
Copy link
Collaborator Author

skarzi commented May 10, 2020

What's exactly fakery fixtures?
Is it provided by some pytest plugin or it's implemented by you?

@sobolevn
Copy link
Member

Oh, sorry. It is a tool similar to factoryboy: https://github.com/fcurella/django-fakery
I use it in all of my projects. It is just awesome!

@skarzi
Copy link
Collaborator Author

skarzi commented May 10, 2020

Looks really good! I will give it a shot, even just for migrations tests.
I will also do some deeper research on possible ways to integrate factory-boy with our project.

@antonagestam
Copy link

It seems like using factories would be pretty hard without either duplicating factories for each tested state, or getting a lot of strange errors because factories are always a mirror of the last database state. To me neither of those options seem worth it.

@skarzi
Copy link
Collaborator Author

skarzi commented Sep 28, 2020

That's true, it's possible, but implementation and maintenance will be much more complicated than the benefits, so I am closing this issue

@skarzi skarzi closed this as completed Sep 28, 2020
@sobolevn
Copy link
Member

https://github.com/fcurella/django-fakery can be used, because it uses _meta to inspect models in runtime.
You don't have to pre-define anything. So, that's quite useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants