Comments (11)
Note that this is because factory_class.declarations()
is used, which only lists explicitly declared parameters.
from pytest-factoryboy.
That's right. I actually started working on a project called alchemyboy time ago. The idea is to generate factories based on the model.
pytest-factoryboy can't be responsible for introspection of the Django or SQLAlchemy models.
And even if declaration (or fixture) is generated it has to take a default from the ORM model. It is interesting problem, but i think it has to extend FactoryBoy rather than pytest.
from pytest-factoryboy.
I agree that FactoryBoy needs something similar to declarations()
to get all overrideable attributes, like accepted_arguments()
. Then it's trivial for pytest-factoryboy to use that and it should work.
from pytest-factoryboy.
You can try fixing it for your project for now.
https://github.com/olegpidsadnyi/alchemyboy/blob/master/alchemyboy/base.py#L27
So basically you need a metaclass and a base class for model factories that will generate base attributes with incorporating default column values into LazyAttribute.
In the subclasses you can always override it with specific values.
from pytest-factoryboy.
I don't know SQLAlchemy + FactoryBoy now since we use Django models, but FactoryBoy already accepts the model parameters even if they're not specified on the factory. It's just that pytest-factoryboy doesn't know that and doesn't convert them into fixture parameters.
from pytest-factoryboy.
I created an issue to extend FactoryBoy.
from pytest-factoryboy.
Do you mean if you have a shallow factory like one above and then if you pass any of the arguments during creation of an object like BookFactory(title="bla") it works?
I think it works just because it treats kwargs as declarations as well.
Of course declaration has to be in the class somehow before you imperatively call it in order to generate parameters. I don't see FactoryBoy introspecting Django models in their code.
But you can do that. You could iterate django columns and add declarations for the columns that have defaults.
The example that I sent you I made for SQLA, but shouldn't be much of a difference for Django. This could be very interesting project to contribute to FactoryBoy. Imagine you have rich column types in your ORM. For example if a field represents Email type you can use faker.email to generate realistic values.
from pytest-factoryboy.
Yes, BookFactory(title="bla")
in the above example currently works. That means if you use [field.name for field in model_class._meta.fields]
to inspect which fields are available (besides just declarations()
) then I suspect the current code in pytest-factoryboy would work. I believe that should be an API call in FactoryBoy.
from pytest-factoryboy.
This could be very interesting project to contribute to FactoryBoy. Imagine you have rich column types in your ORM. For example if a field represents Email type you can use faker.email to generate realistic values.
That certainly sounds interesting. An AutoFieldFuzzer
that inspects the model and selects the best fuzzer sounds very useful.
from pytest-factoryboy.
FactoryBoy/factory_boy@6f20207 and the commits around it might be relevant?
from pytest-factoryboy.
Due to these limitations I ended up not using this project and currently I don't work on any Django projects so my need for this no longer exists. Feel free to close this.
from pytest-factoryboy.
Related Issues (20)
- 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
- Broken support for Python 3.7 HOT 1
- 2.7.0: missing git version tag
- factory.SubFactory fixture collision when the model has the same name HOT 6
- `Trait`s not being considered by pytest_factoryboy
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.