"""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') l = ProductLocation(product=p, store='Pavilions', aisle='Back Wall') session.add(p) session.add(l) session.commit() assert l.aisle == 'Back Wall' assert l.bin is None assert l.product == p assert l 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()