Re-factor the ORM tests to use randomized fake data
- create `*Factory` classes with fakerboy and faker that generate randomized instances of the ORM models - add new pytest marker: "db" are the integration tests involving the database whereas "e2e" will be all other integration tests - streamline the docstrings in the ORM models
This commit is contained in:
parent
416a58f9dc
commit
78dba23d5d
19 changed files with 1092 additions and 721 deletions
105
tests/db/fake_data/fixture_makers.py
Normal file
105
tests/db/fake_data/fixture_makers.py
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
"""Fixture factories for testing the ORM layer with fake data."""
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.db.fake_data import factories
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_address(city):
|
||||
"""Replaces `AddressFactory.build()`: Create an `Address` in the `city`."""
|
||||
# Reset the identifiers before every test.
|
||||
factories.AddressFactory.reset_sequence(1)
|
||||
|
||||
def func(**kwargs):
|
||||
"""Create an `Address` object in the `city`."""
|
||||
return factories.AddressFactory.build(city=city, **kwargs)
|
||||
|
||||
return func
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_courier():
|
||||
"""Replaces `CourierFactory.build()`: Create a `Courier`."""
|
||||
# Reset the identifiers before every test.
|
||||
factories.CourierFactory.reset_sequence(1)
|
||||
|
||||
def func(**kwargs):
|
||||
"""Create a new `Courier` object."""
|
||||
return factories.CourierFactory.build(**kwargs)
|
||||
|
||||
return func
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_customer():
|
||||
"""Replaces `CustomerFactory.build()`: Create a `Customer`."""
|
||||
# Reset the identifiers before every test.
|
||||
factories.CustomerFactory.reset_sequence(1)
|
||||
|
||||
def func(**kwargs):
|
||||
"""Create a new `Customer` object."""
|
||||
return factories.CustomerFactory.build(**kwargs)
|
||||
|
||||
return func
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_restaurant(make_address):
|
||||
"""Replaces `RestaurantFactory.build()`: Create a `Restaurant`."""
|
||||
# Reset the identifiers before every test.
|
||||
factories.RestaurantFactory.reset_sequence(1)
|
||||
|
||||
def func(address=None, **kwargs):
|
||||
"""Create a new `Restaurant` object.
|
||||
|
||||
If no `address` is provided, a new `Address` is created.
|
||||
"""
|
||||
if address is None:
|
||||
address = make_address()
|
||||
|
||||
return factories.RestaurantFactory.build(address=address, **kwargs)
|
||||
|
||||
return func
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_order(make_address, make_courier, make_customer, make_restaurant):
|
||||
"""Replaces `OrderFactory.build()`: Create a `Order`."""
|
||||
# Reset the identifiers before every test.
|
||||
factories.AdHocOrderFactory.reset_sequence(1)
|
||||
|
||||
def func(scheduled=False, restaurant=None, courier=None, **kwargs):
|
||||
"""Create a new `Order` object.
|
||||
|
||||
Each `Order` is made by a new `Customer` with a unique `Address` for delivery.
|
||||
|
||||
Args:
|
||||
scheduled: if an `Order` is a pre-order
|
||||
restaurant: who receives the `Order`; defaults to a new `Restaurant`
|
||||
courier: who delivered the `Order`; defaults to a new `Courier`
|
||||
kwargs: additional keyword arguments forwarded to the `OrderFactory`
|
||||
|
||||
Returns:
|
||||
order
|
||||
"""
|
||||
if scheduled:
|
||||
factory_cls = factories.ScheduledOrderFactory
|
||||
else:
|
||||
factory_cls = factories.AdHocOrderFactory
|
||||
|
||||
if restaurant is None:
|
||||
restaurant = make_restaurant()
|
||||
if courier is None:
|
||||
courier = make_courier()
|
||||
|
||||
return factory_cls.build(
|
||||
customer=make_customer(), # assume a unique `Customer` per order
|
||||
restaurant=restaurant,
|
||||
courier=courier,
|
||||
pickup_address=restaurant.address, # no `Address` history
|
||||
delivery_address=make_address(), # unique `Customer` => new `Address`
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
return func
|
||||
Loading…
Add table
Add a link
Reference in a new issue