Code Monkey home page Code Monkey logo

Comments (10)

Glueon avatar Glueon commented on May 31, 2024

Accidentally closed this issue. I use that in django project:

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = settings.AUTH_USER_MODEL

    username = factory.Faker('user_name')

    @factory.post_generation
    def post(obj, create, extracted, **kwargs):
        obj.set_password('password')

If I use user fixture then the model object I get has pk but seems to be not in DB unless I explicitly call user.save().

So this does not work:

def test_foo(user, client):
     client.login(username=user.username, password='password')

client is an instance of TestClient from Django.

Unless I do:

def test_foo(user, client):
     user.save()
     client.login(username=user.username, password='password')

Here is output from IPDB:

ipdb> user.pk
1857
ipdb> client.login(username='vrath', password='password')
False
ipdb> user.save()
ipdb> client.login(username='vrath', password='password')
True

from pytest-factoryboy.

thedrow avatar thedrow commented on May 31, 2024

Same question here.

from pytest-factoryboy.

thedrow avatar thedrow commented on May 31, 2024

Seems possible but it's not implemented yet. See https://github.com/pytest-dev/pytest-factoryboy/blob/master/pytest_factoryboy/fixture.py#L174
How do you imagine this working?

from pytest-factoryboy.

sysradium avatar sysradium commented on May 31, 2024

It's interesting that the line you gave should actually call build or create based on strategy: https://github.com/rbarrois/factory_boy/blob/master/factory/base.py#L78

So this should work, but does not ...

from pytest-factoryboy.

thedrow avatar thedrow commented on May 31, 2024

@syradium I think what the OP means is that you want to be able to override the default.

from pytest-factoryboy.

sysradium avatar sysradium commented on May 31, 2024

@thedrow I am sorry, OP?

According to the line I gave Factory() call should take into account default strategy. I think it would sufficient if this fixture would take into account default strategy as well. But it looks like it does not. I tried to set BUILD_STRATEGY explicitly but still object isn't created.

In fact fixture object has a pk set but object does not exist in DB which is weird. So maybe Django/pytest-django does something unexpected.

from pytest-factoryboy.

thedrow avatar thedrow commented on May 31, 2024

@syradium OP = Original Poster. The one who opened the issue.

from pytest-factoryboy.

sysradium avatar sysradium commented on May 31, 2024

@thedrow Oh, did not get that. Yeah, it's a separate issue maybe.

from pytest-factoryboy.

olegpidsadnyi avatar olegpidsadnyi commented on May 31, 2024

I'm not using Django ORM so I didn't realize that save is being called on the postgen hook. I have to think how to invoke it since all post-gens are deferred

from pytest-factoryboy.

olegpidsadnyi avatar olegpidsadnyi commented on May 31, 2024

@Glueon I've added support of the _after_postgeneration hook. Could you try if it works for you now in the branch of the PR #22?

from pytest-factoryboy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.