Code Monkey home page Code Monkey logo

Comments (11)

ekohl avatar ekohl commented on May 31, 2024

Note that this is because factory_class.declarations() is used, which only lists explicitly declared parameters.

from pytest-factoryboy.

olegpidsadnyi avatar olegpidsadnyi commented on May 31, 2024

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.

ekohl avatar ekohl commented on May 31, 2024

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.

olegpidsadnyi avatar olegpidsadnyi commented on May 31, 2024

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.

ekohl avatar ekohl commented on May 31, 2024

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.

ekohl avatar ekohl commented on May 31, 2024

I created an issue to extend FactoryBoy.

from pytest-factoryboy.

olegpidsadnyi avatar olegpidsadnyi commented on May 31, 2024

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.

ekohl avatar ekohl commented on May 31, 2024

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.

ekohl avatar ekohl commented on May 31, 2024

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.

blueyed avatar blueyed commented on May 31, 2024

FactoryBoy/factory_boy@6f20207 and the commits around it might be relevant?

from pytest-factoryboy.

ekohl avatar ekohl commented on May 31, 2024

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)

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.