API Documentation

FixturesLoader

class py_yaml_fixtures.FixturesLoader(factory: py_yaml_fixtures.factories.factory_interface.FactoryInterface, fixture_dirs: List[str], env: Optional[jinja2.environment.Environment] = None)[source]

The factory “driver” class. Does most of the hard work of loading fixtures, leaving the responsibility of model instantiation up to the factory class passed in.

Parameters:
  • factory – An instance of the concrete factory to use for creating models
  • fixture_dirs – A list of directory paths to load fixtures templates from
  • env – An optional jinja environment (the default one will include faker as a template global, but if you want to customize its tags/filters/etc, then you need to create an env yourself - the correct loader will be set automatically for you)
env = None

The Jinja Environment used for rendering the yaml template files.

factory = None

The factory instance.

fixture_dirs = None

A list of directories where fixture files should be loaded from.

relationships = None

A dict keyed by model name where values are a list of related model names.

model_fixtures = None

A dict of models names to their semi-processed data from the yaml files.

create_all(progress_callback: Optional[callable] = None) → Dict[str, object][source]

Creates all the models discovered from fixture files in fixtures_dir.

Parameters:progress_callback

An optional function to track progress. It must take three parameters:

  • an Identifier
  • the model instance
  • and a boolean specifying whether the model was created
Returns:A dictionary keyed by identifier where the values are model instances.
convert_identifiers(identifiers: Union[py_yaml_fixtures.types.Identifier, List[py_yaml_fixtures.types.Identifier]])[source]

Convert an individual Identifier to a model instance, or a list of Identifiers to a list of model instances.

FactoryInterface

class py_yaml_fixtures.FactoryInterface[source]

Abstract base class for ORM factories. Extend this base class to add support for a database ORM.

create_or_update(identifier: py_yaml_fixtures.types.Identifier, data: Dict[str, Any]) → Tuple[object, bool][source]

Create or update a model.

Parameters:
  • identifier – An object with class_name and key attributes
  • data – A dictionary keyed by column name, with values being the converted values to set on the model instance
Returns:

A two-tuple of model instance and whether or not it was created.

get_relationships(class_name: str) → Set[str][source]

Return a list of model attribute names that could have relationships for the given model class name.

Parameters:class_name – The name of the class name to discover relationships for.
Returns:A set of model attribute names.
maybe_convert_values(identifier: py_yaml_fixtures.types.Identifier, data: Dict[str, Any]) → Dict[str, Any][source]

Takes a dictionary of raw values for a specific identifier, as parsed from the YAML file, and depending upon the type of db column the data is meant for, decides what to do with the value (eg leave it alone, convert a string to a date/time instance, or convert identifiers to model instances by calling self.loader.convert_identifiers())

Parameters:
  • identifier – An object with class_name and key attributes
  • data – A dictionary keyed by column name, with values being the raw values as parsed from the YAML
Returns:

A dictionary keyed by column name, with values being the converted values meant to be set on the model instance

commit()[source]

If your ORM implements the data mapper pattern instead of active record, then you can implement this method to commit the session after all the models have been added to it.

Concrete ORM Factories

DjangoModelFactory

class py_yaml_fixtures.factories.django.DjangoModelFactory(models: Union[List[type], Dict[str, type]], date_factory: Optional[function] = None, datetime_factory: Optional[function] = None)[source]

Concrete factory for the Django ORM.

SQLAlchemyModelFactory

class py_yaml_fixtures.factories.sqlalchemy.SQLAlchemyModelFactory(session: sqlalchemy.orm.session.Session, models: Union[List[type], Dict[str, type]], date_factory: Optional[function] = None, datetime_factory: Optional[function] = None)[source]

Concrete factory for the SQLAlchemy ORM.

Jinja Helper Functions

random_model

py_yaml_fixtures.utils.random_model(ctx, model_class_name)[source]

Get a random model identifier by class name. For example:

# db/fixtures/Category.yml
{% for i in range(0, 10) %}
category{{ i }}:
    name: {{ faker.name() }}
{% endfor %}

# db/fixtures/Post.yml
a_blog_post:
    category: {{ random_model('Category') }}

Will render to something like the following:

# db/fixtures/Post.yml (rendered)
a blog_post:
    category: "Category(category7)"
Parameters:
  • ctx – The context variables of the current template (passed automatically)
  • model_class_name – The class name of the model to get.

random_models

py_yaml_fixtures.utils.random_models(ctx, model_class_name, min_count=0, max_count=3)[source]

Get a random model identifier by class name. Example usage:

# db/fixtures/Tag.yml
{% for i in range(0, 10) %}
tag{{ i }}:
    name: {{ faker.name() }}
{% endfor %}

# db/fixtures/Post.yml
a_blog_post:
    tags: {{ random_models('Tag') }}

Will render to something like the following:

# db/fixtures/Post.yml (rendered)
a blog_post:
    tags: ["Tag(tag2, tag5)"]
Parameters:
  • ctx – The context variables of the current template (passed automatically)
  • model_class_name – The class name of the models to get.
  • min_count – The minimum number of models to return.
  • max_count – The maximum number of models to return.