API Reference

FixturesManager

class charlatan.FixturesManager(db_session=None)

Manage Fixture objects.

Parameters:db_session (Session) – sqlalchemy Session object

New in version 0.3.0: db_session argument was added.

clean_cache()

Clean the cache.

delete_instance(instance)

Delete a fixture instance.

If it’s a SQLAlchemy model, it will be deleted from the session and the session will be committed.

Otherwise, delete_instance() will be run first. If the instance does not have it, delete() will be run. If the instance does not have it, nothing will happen.

Before and after the process, the before_delete() and after_delete() hook are run.

get_fixture(fixture_key, include_relationships=True, attrs=None)

Return a fixture instance (but do not save it).

Parameters:
  • fixture_key (str) –
  • include_relationships (bool) – False if relationships should be removed.
  • attrs (dict) – override fields
Return type:

instantiated but unsaved fixture

get_fixtures(fixture_keys, include_relationships=True)

Return a list of fixtures instances.

Parameters:
  • fixture_keys (iterable) –
  • include_relationships (bool) – False if relationships should be removed.
Return type:

list of instantiated but unsaved fixtures

install_all_fixtures(do_not_save=False, include_relationships=True)

Install all fixtures.

Parameters:
  • do_not_save (bool) – True if fixture should not be saved.
  • include_relationships (bool) – False if relationships should be removed.
Return type:

list of fixture_instance

install_fixture(fixture_key, do_not_save=False, include_relationships=True, attrs=None)

Install a fixture.

Parameters:
  • fixture_key (str) –
  • do_not_save (bool) – True if fixture should not be saved.
  • include_relationships (bool) – False if relationships should be removed.
  • attrs (dict) – override fields
Return type:

fixture_instance

install_fixtures(fixture_keys, do_not_save=False, include_relationships=True)

Install a list of fixtures.

Parameters:
  • fixture_keys (str or list of strs) – fixtures to be installed
  • do_not_save (bool) – True if fixture should not be saved.
  • include_relationships (bool) – False if relationships should be removed.
Return type:

list of fixture_instance

load(filename, models_package='')

Pre-load the fixtures.

Parameters:
  • filename (str) – file that holds the fixture data
  • models_package (str) – package holding the models definition

Note that this does not effectively instantiate anything. It just does some pre-instantiation work, like prepending the root model package and doing some basic sanity check.

Changed in version 0.3.0: db_session argument was removed and put in the object’s constructor arguments.

save_instance(instance)

Save a fixture instance.

If it’s a SQLAlchemy model, it will be added to the session and the session will be committed.

Otherwise, a save() method will be run if the instance has one. If it does not have one, nothing will happen.

Before and after the process, the before_save() and after_save() hook are run.

set_hook(hookname, func)

Add a hook.

Parameters:
  • hookname (str) –
  • func (function) –
uninstall_all_fixtures(do_not_delete=False)

Uninstall all installed fixtures.

Parameters:do_not_delete (bool) – True if fixture should not be deleted.
Return type:list of fixture_instance
uninstall_fixture(fixture_key, do_not_delete=False)

Uninstall a fixture.

Parameters:
  • fixture_key (str) –
  • do_not_delete (bool) – True if fixture should not be deleted.
Return type:

fixture_instance or None if no instance was uninstalled

with the given key

uninstall_fixtures(fixture_keys, do_not_delete=False)

Uninstall a list of installed fixtures.

If a given fixture was not previously installed, nothing happens and its instance is not part of the returned list.

Parameters:
  • fixture_keys (str or list of strs) – fixtures to be uninstalled
  • do_not_delete (bool) – True if fixture should not be deleted.
Return type:

list of fixture_instance

FixturesManagerMixin

class charlatan.FixturesManagerMixin

Class from which test cases should inherit to use fixtures.

Changed in version 0.3.0: use_fixtures_manager method renamed init_fixtures.

Changed in version 0.3.0: Extensive change to the function signatures.

get_fixture(*args, **kwargs)

Return a fixture instance (but do not save it).

Parameters:
  • fixture_key (str) –
  • include_relationships (bool) – False if relationships should be removed.
  • attrs (dict) – override fields
Return type:

instantiated but unsaved fixture

get_fixtures(*args, **kwargs)

Return a list of fixtures instances.

Parameters:
  • fixture_keys (iterable) –
  • include_relationships (bool) – False if relationships should be removed.
Return type:

list of instantiated but unsaved fixtures

init_fixtures()

Initialize the fixtures.

This function must be called before doing anything else.

install_all_fixtures(*args, **kwargs)

Install all fixtures.

Parameters:
  • do_not_save (bool) – True if fixture should not be saved.
  • include_relationships (bool) – False if relationships should be removed.
Return type:

list of fixture_instance

install_fixture(*args, **kwargs)

Install a fixture.

Parameters:
  • fixture_key (str) –
  • do_not_save (bool) – True if fixture should not be saved.
  • include_relationships (bool) – False if relationships should be removed.
  • attrs (dict) – override fields
Return type:

fixture_instance

install_fixtures(*args, **kwargs)

Install a list of fixtures.

Parameters:
  • fixture_keys (str or list of strs) – fixtures to be installed
  • do_not_save (bool) – True if fixture should not be saved.
  • include_relationships (bool) – False if relationships should be removed.
Return type:

list of fixture_instance

uninstall_all_fixtures(*args, **kwargs)

Uninstall all installed fixtures.

Parameters:do_not_delete (bool) – True if fixture should not be deleted.
Return type:list of fixture_instance
uninstall_fixture(*args, **kwargs)

Uninstall a fixture.

Parameters:
  • fixture_key (str) –
  • do_not_delete (bool) – True if fixture should not be deleted.
Return type:

fixture_instance or None if no instance was uninstalled

with the given key

uninstall_fixtures(*args, **kwargs)

Uninstall a list of installed fixtures.

If a given fixture was not previously installed, nothing happens and its instance is not part of the returned list.

Parameters:
  • fixture_keys (str or list of strs) – fixtures to be uninstalled
  • do_not_delete (bool) – True if fixture should not be deleted.
Return type:

list of fixture_instance

Fixture

class charlatan.Fixture(key, fixture_manager, model=None, fields=None, inherit_from=None, post_creation=None, id_=None, depend_on=frozenset([]))

Represent a fixture that can be installed.

static extract_rel_name(name)

Helper function to extract the relationship and attr from an argument to !rel

extract_relationships()

Iterator of all relationships and dependencies for this fixture

get_class()

Return class object for this instance.

get_instance(include_relationships=True)

Instantiate the fixture using the model and return the instance.

Parameters:include_relationships (boolean) – false if relationships should be removed.
get_relationship(name)

Get a relationship and its attribute if necessary.

update_with_parent()

Update the object in place using its chain of inheritance.

Utils

charlatan.utils.apply_delta(dt, delta)

Apply datetime delta in string format to a datetime object.

Parameters:
  • dt (datetime) –
  • delta (str) –
Return type:

datetime.DateTime instance

>>> base = datetime.datetime(2012, 1, 1, 1, 1, 1)
>>> apply_delta(base, "+1h")
datetime.datetime(2012, 1, 1, 2, 1, 1)
>>> apply_delta(base, "+10h")
datetime.datetime(2012, 1, 1, 11, 1, 1)
>>> apply_delta(base, "-10d")
datetime.datetime(2011, 12, 22, 1, 1, 1)
>>> apply_delta(base, "+1m")
datetime.datetime(2012, 1, 31, 1, 1, 1)
>>> apply_delta(base, "-1y")
datetime.datetime(2011, 1, 1, 1, 1, 1)
>>> apply_delta(base, "+10d2h")
datetime.datetime(2012, 1, 11, 3, 1, 1)
>>> apply_delta(base, "-10d2h")
datetime.datetime(2011, 12, 21, 23, 1, 1)
>>> apply_delta(base, "-21y2m1d24h")
datetime.datetime(1990, 11, 5, 1, 1, 1)
>>> apply_delta(base, "+5M")
datetime.datetime(2012, 1, 1, 1, 6, 1)
>>> apply_delta(base, "+4M30s")
datetime.datetime(2012, 1, 1, 1, 5, 31)
charlatan.utils.copy_docstring_from(klass)

Copy docstring from another class, using the same function name.

charlatan.utils.datetime_to_epoch_timestamp(a_datetime)

Return the epoch timestamp for the given datetime

Parameters:a_datetime (datetime) – The datetime to translate
Return type:float
>>> a_datetime = datetime.datetime(2013, 11, 21, 1, 33, 11, 160611)
>>> datetime_to_epoch_timestamp(a_datetime)
1384997591.160611
charlatan.utils.extended_timedelta(**kwargs)

Return a timedelta object based on the arguments.

Parameters:
  • years (integer) –
  • months (integer) –
  • days (integer) –
Return type:

timedelta instance

Since timedelta‘s largest unit are days, timedelta objects cannot be created with a number of months or years as an argument. This function lets you create timedelta objects based on a number of days, months and years.

>>> extended_timedelta(months=1)
datetime.timedelta(30)
>>> extended_timedelta(years=1)
datetime.timedelta(365)
>>> extended_timedelta(days=1, months=1, years=1)
datetime.timedelta(396)
>>> extended_timedelta(hours=1)
datetime.timedelta(0, 3600)

Table Of Contents

Previous topic

Hooks

Next topic

Contributing

This Page