Open rbarrois opened 3 years ago
Oh, this feature would be awesome!
If I can add my two cents regarding foreign keys: my team and I are used to manually creating generators for our models (which we want to stop doing XD). What has worked well for us is to only generate the required fields (that applies to FKs too). That means that every optional/nullable field will be set to None
by default, while required foreign objects will be created (until there's no more foreign models). And all that is possible to override in the call site. So, for example, if a foreign object is part of a test of mine and I want to use that in a factory, I can simply pass it in as an argument.
About the integration with the ORM factories, I would make it integrated, possibly with a Meta
attribute to disable that.
Edit: the library below does something like this. It could be looked at as inspiration, an example, or previous experience in implementing this.
Hi, I would like to share a very alpha and simple PoC to generate a factory for dataclasses.
https://gist.github.com/mgaitan/dcbe08bf44a5af696f2af752624ac11b
it respects defaults, support builtin types, basic relationships, list/tuples/set, enums and email as a particular case based on the attribute name.
I'm brand new to Factory Boy, and I just want to share that as a Django user with dozens of models and hundreds of fields, I'm quite surprised that Factory Boy doesn't have a way of inspecting a model/dataclass/etc and generate reasonable fakers for each field.
I've been reading docs for an hour or two, and it's only after I began working with the code I'm realizing I'm going to have to define Faker
attributes for dozens of fields. I am pretty sad. Definitely not the turnkey kind of thing I was hoping for. Ah well.
Onwards with my explorations, but kinda bummed.
Don't have too much experience with it myself yet, but at first glance the Pydantic ecosystem seems to be good for this kind of stuff. Might need a little up front investment. Check out https://github.com/Goldziher/pydantic-factories
This issue will be used as the discussion basis for the automated factory generation feature.
The problem
Many libraries (ORMs, API schema languages,
dataclasses.dataclass
) provide a way to describe the fields of a class and their types. In those cases, it is cumbersome to have to add all the declarations manually; it would be great if factory_boy could provide a set of default declarations from an introspection of the class:The typical example would be:
Existing work
Design constraints
Developer experience
make_factory
;factory.Faker
to use the field name as a hint (e.g callingfactory.Faker("user_name")
for a field calledusername
).Library integration
Factory
subclass, or through a customFactoryOptions
;DjangoModelFactory
; project B should be able to leverage it intoAutoDjangoModelFactory
)Open questions
DjangoModelFactory
/SQLAlchemyModelFactory
, or provide as extra classes?