145 lines
3.9 KiB
Python
145 lines
3.9 KiB
Python
"""Test the Product Service Entry Points.
|
|
|
|
Simple Grocery List (Sigl) | sigl.app
|
|
Copyright (c) 2022 Asymworks, LLC. All Rights Reserved.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from sigl.domain.service import (
|
|
list_addItem,
|
|
list_by_id,
|
|
list_create,
|
|
list_deleteCrossedOff,
|
|
list_entry_set_crossedOff,
|
|
product_by_id,
|
|
product_create,
|
|
)
|
|
|
|
# Always use 'app' fixture so ORM gets initialized
|
|
pytestmark = pytest.mark.usefixtures('app')
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_list_create_defaults(session):
|
|
"""Test newly created Lists are empty."""
|
|
lc = list_create(session, 'Test')
|
|
list = list_by_id(session, lc.id)
|
|
|
|
assert list.name == 'Test'
|
|
assert not list.entries
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_list_add_product_defaults(session):
|
|
"""Test adding a Product to a List."""
|
|
list = list_create(session, 'Test')
|
|
entry = list_addItem(session, list.id, productName='Eggs', productCategory='Dairy')
|
|
|
|
assert entry.id is not None
|
|
assert entry.product is not None
|
|
assert entry.product.name == 'Eggs'
|
|
assert entry.product.category == 'Dairy'
|
|
assert entry.product.remember is True
|
|
|
|
assert len(list.entries) == 1
|
|
assert list.entries[0] == entry
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_list_add_product_by_id(session):
|
|
"""Test adding an existing Product to a List by Id."""
|
|
p1 = product_create(session, 'Eggs', category='Dairy')
|
|
|
|
list = list_create(session, 'Test')
|
|
entry = list_addItem(session, list.id, productId=p1.id)
|
|
|
|
assert entry.id is not None
|
|
assert entry.product is not None
|
|
assert entry.product.name == 'Eggs'
|
|
assert entry.product.category == 'Dairy'
|
|
assert entry.product.remember is True
|
|
|
|
assert len(list.entries) == 1
|
|
assert list.entries[0] == entry
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_list_add_product_by_name(session):
|
|
"""Test adding an existing Product to a List by Name."""
|
|
product_create(session, 'Eggs', category='Dairy')
|
|
|
|
list = list_create(session, 'Test')
|
|
entry = list_addItem(session, list.id, productName='eggs')
|
|
|
|
assert entry.id is not None
|
|
assert entry.product is not None
|
|
assert entry.product.name == 'Eggs'
|
|
assert entry.product.category == 'Dairy'
|
|
assert entry.product.remember is True
|
|
|
|
assert len(list.entries) == 1
|
|
assert list.entries[0] == entry
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_list_add_product_no_remember(session):
|
|
"""Test adding a Product to a List without remembering it."""
|
|
list = list_create(session, 'Test')
|
|
entry = list_addItem(
|
|
session,
|
|
list.id,
|
|
productName='Eggs',
|
|
productCategory='Dairy',
|
|
remember=False,
|
|
)
|
|
|
|
assert entry.id is not None
|
|
assert entry.product is not None
|
|
assert entry.product.name == 'Eggs'
|
|
assert entry.product.category == 'Dairy'
|
|
assert entry.product.remember is False
|
|
|
|
assert len(list.entries) == 1
|
|
assert list.entries[0] == entry
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_list_removes_product_with_remember(session):
|
|
"""Test that checking off and deleting a remembered Product does not delete the Product Entry."""
|
|
list = list_create(session, 'Test')
|
|
entry = list_addItem(
|
|
session,
|
|
list.id,
|
|
productName='Eggs',
|
|
productCategory='Dairy',
|
|
remember=True,
|
|
)
|
|
|
|
pid = entry.product.id
|
|
|
|
list_entry_set_crossedOff(session, list.id, entry.id, True)
|
|
list_deleteCrossedOff(session, list.id)
|
|
|
|
assert product_by_id(session, pid) is not None
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_list_removes_product_no_remember(session):
|
|
"""Test that checking off and deleting a non-remembered Product deletes the Product Entry also."""
|
|
list = list_create(session, 'Test')
|
|
entry = list_addItem(
|
|
session,
|
|
list.id,
|
|
productName='Eggs',
|
|
productCategory='Dairy',
|
|
remember=False,
|
|
)
|
|
|
|
pid = entry.product.id
|
|
|
|
list_entry_set_crossedOff(session, list.id, entry.id, True)
|
|
list_deleteCrossedOff(session, list.id)
|
|
|
|
assert product_by_id(session, pid) is None
|