easyfactory package

Submodules

easyfactory.auto_factory module

class easyfactory.auto_factory.AutoFactory

Bases: object

class easyfactory.auto_factory.AutoFactoryMeta(name, bases, attributes: dict[str, type[T] | Any])

Bases: type

easyfactory.auto_factory.make_factory_for(model: PT | ST, **attributes: Any) Factory

easyfactory.pydantic module

class easyfactory.pydantic.PydanticFactoryGenerator

Bases: object

A class responsible for generating Pydantic factories dynamically based on provided attributes.

generated_factories: dict[tuple[int, str], Factory | None] = {}
classmethod make_factory_for(model: T, **attributes: Any) Factory

Generates a factory for the given Pydantic model with the specified attributes if not already existing otherwise return the existing one.

Parameters:
  • model (Type[T]) – The Pydantic model class for which the factory is generated.

  • attributes (Any) – Additional attributes to be included in the factory.

Returns:

A factory class for the given Pydantic model.

Return type:

factory.Factory

classmethod make_pydantic_factory(model: T, attributes: dict[str, Any])

Generates a Pydantic factory for the given model with the specified attributes.

Parameters:
  • model (Type[T]) – The Pydantic model class for which the factory is generated.

  • attributes (dict[str, Any]) – Dictionary containing additional attributes to be included in the factory.

Returns:

A dynamically generated Pydantic factory class.

Return type:

factory.Factory

easyfactory.sqlalchemy module

class easyfactory.sqlalchemy.SQLAlchemyFactoryGenerator

Bases: object

A class responsible for generating SQLAlchemy factories dynamically based on provided attributes.

generated_factories: dict[tuple[int, str], Factory | None] = {}
classmethod make_factory_for(model: T, **attributes: Any) Factory

Generates a factory for the given model with the specified attributes if not already existing otherwise return the existing one.

Parameters:
  • model (Type[T]) – The SQLAlchemy model class for which the factory is generated.

  • attributes (Any) – Additional attributes to be included in the factory.

Returns:

A factory class for the given model.

Return type:

factory.Factory

classmethod make_sqlalchemy_factory(model: T, attributes: dict[str, Any])

Generates a SQLAlchemy factory for the given model with the specified attributes.

Parameters:
  • model (Type[T]) – The SQLAlchemy model class for which the factory is generated.

  • attributes (dict[str, Any]) – Dictionary containing additional attributes to be included in the factory.

Returns:

A dynamically generated SQLAlchemy factory class.

Return type:

factory.Factory

easyfactory.sqlalchemy.load_type(mappers: frozenset[Mapper], type_name: str) type[T]

Load a class from sqlalchemy registry (useful for Mapped[“className”]) :param mappers: the list of mappers to search for :param type_name: the name of the type to load :return: the python type woth given name

easyfactory.sqlalchemy.make_post_gen(field_name: str, sub_factory_model: type[Factory], remote_prop_name: str, local_use_list: bool, remote_use_list: bool | None)
generate a postgen function that will generate the relationship model and set the relation properties such

that X.y == Y and Y.x == X or X.y==[Y] and Y.x == X or X.y == X and Y.x == [Y] depending on declared relation on model

Parameters:
  • field_name – the local field name to populate

  • sub_factory_model – the sub factory model to instantiate

  • remote_prop_name – the property name on the sub object to use for linking with parent

  • local_use_list – should we set the local property to Parent.field_name = [Child] or Child

  • remote_use_list – should we set the remote property to Child.remote_prop_name =`[Parent]` or Parent

Returns:

a post_gen function

easyfactory.utils module

easyfactory.utils.is_optional(field)

Checks if a field is optional.

Parameters:

field (Any) – The field to be checked.

Returns:

True if the field is optional, False otherwise.

Return type:

bool

easyfactory.utils.is_valid(word: str) bool

Checks if a word is valid based on a set of banned prefixes and words.

Parameters:

word (str) – The word to be checked.

Returns:

True if the word is valid, False otherwise.

Return type:

bool

easyfactory.utils.make_factory_class(model_: type, attributes: dict[str, Any]) Factory

Generates a factory class based on the provided model and attributes.

Parameters:
  • model (type) – The model for which the factory class is generated.

  • attributes (dict[str, Any]) – Additional attributes to be included in the factory.

Returns:

A factory class.

Return type:

factory.Factory

easyfactory.utils.select_faker_based_on_name(field_name: str) Faker

Selects a Faker provider based on the field name.

Parameters:

field_name (str) – The name of the field.

Returns:

A Faker object with the selected provider.

Return type:

factory.Faker

easyfactory.utils.select_generator_for_type(field_name: str, type_: type[T], child_model_callback: Callable[[type[T]], BaseDeclaration]) BaseDeclaration | None

Selects a suitable generator for a field based on its name, type, and a callback for child models.

Parameters:
  • field_name (str) – The name of the field.

  • type (type[T]) – The type of the field.

  • child_model_callback (Callable[[type[T]], BaseDeclaration]) – A callback function to handle child models.

Returns:

A suitable generator for the field.

Return type:

BaseDeclaration | None

Module contents

class easyfactory.AutoFactory

Bases: object

easyfactory.make_factory_for(model: PT | ST, **attributes: Any) Factory