Comments (10)
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.
Same question here.
from pytest-factoryboy.
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.
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.
@syradium I think what the OP means is that you want to be able to override the default.
from pytest-factoryboy.
@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.
@syradium OP = Original Poster. The one who opened the issue.
from pytest-factoryboy.
@thedrow Oh, did not get that. Yeah, it's a separate issue maybe.
from pytest-factoryboy.
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.
@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)
- fixture not found using SubFactory HOT 1
- @register decorator should accept parameters HOT 2
- Allow to register factories in the class namespace.
- pytest-factoryboy seems to break factory.django.mute_signals HOT 4
- Fix tests/test_postgen_dependencies::test_postgen_related
- AttributeError using `LazyFixture` in `register()` HOT 5
- Parent factory model name disregards `_name` override. HOT 1
- Ship tests with source distribution HOT 1
- function uses no argument HOT 1
- UserWarning: UserWarning: Using a <class 'list'> as model type for <ListFactory for <class 'list'>> is discouraged HOT 5
- Error `fixture not found` if define `register(_name=)` for connected SubFactory which is connected with another SubFactory HOT 1
- DeprecationWarning with pytest 7.2.0 HOT 4
- PytestAssertRewriteWarning
- Feature request: override name of factory fixture? HOT 3
- Alternative for `register`
- register with type hints HOT 3
- Improve handling of `factory.Maybe`
- Request other fixtures in fixtures generated by register HOT 3
- How can I pass function-scope fixtures (e.g. db sessions) to each Factory in combination with pytest-asyncio?
- Question regarding of usage of LazyFixture in @pytest.mark.parametrize on alias instead of chained fixture. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest-factoryboy.