79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
"""Test the Product and ProductLocation Models.
|
|
|
|
Simple Grocery List (Sigl) | sigl.app
|
|
Copyright (c) 2022 Asymworks, LLC. All Rights Reserved.
|
|
"""
|
|
|
|
import pytest
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
from sigl.domain.models import Product, ProductLocation
|
|
|
|
# Always use 'app' fixture so ORM gets initialized
|
|
pytestmark = pytest.mark.usefixtures('app')
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_product_model_init(session):
|
|
"""Test newly created Products have no Locations."""
|
|
p = Product(name='Eggs', category='Dairy')
|
|
|
|
session.add(p)
|
|
session.commit()
|
|
|
|
assert p.id is not None
|
|
assert p.name == 'Eggs'
|
|
assert p.category == 'Dairy'
|
|
assert not p.locations
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_product_model_can_add_location(session):
|
|
"""Test that a Location can be added to a Product."""
|
|
p = Product(name='Eggs', category='Dairy')
|
|
loc = ProductLocation(product=p, store='Pavilions', aisle='Back Wall')
|
|
|
|
session.add(p)
|
|
session.add(loc)
|
|
session.commit()
|
|
|
|
assert loc.aisle == 'Back Wall'
|
|
assert loc.bin is None
|
|
assert loc.product == p
|
|
|
|
assert loc in p.locations
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_product_model_can_add_multiple_stores(session):
|
|
"""Test that multiple Locations can be added to a Product."""
|
|
p = Product(name='Eggs', category='Dairy')
|
|
l1 = ProductLocation(product=p, store='Pavilions', aisle='Back Wall')
|
|
l2 = ProductLocation(product=p, store='Stater Brothers', aisle='Left Wall')
|
|
|
|
session.add(p)
|
|
session.add(l1)
|
|
session.add(l2)
|
|
session.commit()
|
|
|
|
assert l1.product == p
|
|
assert l1 in p.locations
|
|
|
|
assert l2.product == p
|
|
assert l2 in p.locations
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_product_model_same_store_fails(session):
|
|
"""Test that two Locations with the same Store cannot be added to a Product."""
|
|
p = Product(name='Eggs', category='Dairy')
|
|
l1 = ProductLocation(product=p, store='Pavilions', aisle='Back Wall')
|
|
l2 = ProductLocation(product=p, store='Pavilions', aisle='Left Wall')
|
|
|
|
session.add(p)
|
|
session.add(l1)
|
|
session.add(l2)
|
|
|
|
with pytest.raises(IntegrityError):
|
|
session.commit()
|