Skip to content

generic_mock_repository

A repository implementation for tests.

Uses a dict for storage.

GenericMockRepository

GenericMockRepository(id_factory=uuid4, **_)

Bases: AbstractRepository[ModelT], Generic[ModelT]

A repository implementation for tests.

Uses a dict for storage.

__class_getitem__ classmethod

__class_getitem__(item)

Add collection to _collections for the type.

Parameters:

Name Type Description Default
item type[ModelT]

The type that the class has been parametrized with.

required

add async

add(data, allow_id=False)

Add data to the collection.

Parameters:

Name Type Description Default
data ModelT

Instance to be added to the collection.

required
allow_id bool

disable the identified object check.

False

Returns:

Type Description
ModelT

The added instance.

clear_collection classmethod

clear_collection()

Empty the collection for repository type.

delete async

delete(id_)

Delete instance identified by id_.

Parameters:

Name Type Description Default
id_ Any

Identifier of instance to be deleted.

required

Returns:

Type Description
ModelT

The deleted instance.

Raises:

Type Description
RepositoryNotFoundException

If no instance found identified by id_.

filter_collection_by_kwargs

filter_collection_by_kwargs(**kwargs)

Filter the collection by kwargs.

Parameters:

Name Type Description Default
**kwargs Any

key/value pairs such that objects remaining in the collection after filtering have the property that their attribute named key has value equal to value.

{}

get async

get(id_)

Get instance identified by id_.

Parameters:

Name Type Description Default
id_ Any

Identifier of the instance to be retrieved.

required

Returns:

Type Description
ModelT

The retrieved instance.

Raises:

Type Description
RepositoryNotFoundException

If no instance found identified by id_.

list async

list(*filters, **kwargs)

Get a list of instances, optionally filtered.

Parameters:

Name Type Description Default
*filters FilterTypes

Types for specific filtering operations.

()
**kwargs Any

Instance attribute value filters.

{}

Returns:

Type Description
list[ModelT]

The list of instances, after filtering applied.

seed_collection classmethod

seed_collection(instances)

Seed the collection for repository type.

Parameters:

Name Type Description Default
instances Iterable[ModelT]

the instances to be added to the collection.

required

update async

update(data)

Update instance with the attribute values present on data.

Parameters:

Name Type Description Default
data ModelT

An instance that should have a value for self.id_attribute that exists in the collection.

required

Returns:

Type Description
ModelT

The updated instance.

Raises:

Type Description
RepositoryNotFoundException

If no instance found with same identifier as data.

upsert async

upsert(data)

Update or create instance.

Updates instance with the attribute values present on data, or creates a new instance if one doesn't exist.

Parameters:

Name Type Description Default
data ModelT

Instance to update existing, or be created. Identifier used to determine if an existing instance exists is the value of an attribute on data named as value of self.id_attribute.

required

Returns:

Type Description
ModelT

The updated or created instance.

Raises:

Type Description
RepositoryNotFoundException

If no instance found with same identifier as data.