example-code-2e/10-dp-1class-func/classic_strategy_test.py

66 lines
2.0 KiB
Python
Raw Normal View History

2021-08-07 05:44:01 +02:00
from decimal import Decimal
2020-06-11 19:58:15 +02:00
import pytest # type: ignore
from classic_strategy import Customer, LineItem, Order
from classic_strategy import FidelityPromo, BulkItemPromo, LargeOrderPromo
@pytest.fixture
def customer_fidelity_0() -> Customer:
return Customer('John Doe', 0)
@pytest.fixture
def customer_fidelity_1100() -> Customer:
return Customer('Ann Smith', 1100)
@pytest.fixture
2021-08-07 05:44:01 +02:00
def cart_plain() -> tuple[LineItem, ...]:
return (
LineItem('banana', 4, Decimal('0.5')),
LineItem('apple', 10, Decimal('1.5')),
LineItem('watermelon', 5, Decimal('5.0')),
)
2020-06-11 19:58:15 +02:00
def test_fidelity_promo_no_discount(customer_fidelity_0, cart_plain) -> None:
order = Order(customer_fidelity_0, cart_plain, FidelityPromo())
2021-08-07 05:44:01 +02:00
assert order.total() == 42
assert order.due() == 42
2020-06-11 19:58:15 +02:00
def test_fidelity_promo_with_discount(customer_fidelity_1100, cart_plain) -> None:
order = Order(customer_fidelity_1100, cart_plain, FidelityPromo())
2021-08-07 05:44:01 +02:00
assert order.total() == 42
assert order.due() == Decimal('39.9')
2020-06-11 19:58:15 +02:00
def test_bulk_item_promo_no_discount(customer_fidelity_0, cart_plain) -> None:
order = Order(customer_fidelity_0, cart_plain, BulkItemPromo())
2021-08-07 05:44:01 +02:00
assert order.total() == 42
assert order.due() == 42
2020-06-11 19:58:15 +02:00
def test_bulk_item_promo_with_discount(customer_fidelity_0) -> None:
2021-08-07 05:44:01 +02:00
cart = [LineItem('banana', 30, Decimal('0.5')),
LineItem('apple', 10, Decimal('1.5'))]
2020-06-11 19:58:15 +02:00
order = Order(customer_fidelity_0, cart, BulkItemPromo())
2021-08-07 05:44:01 +02:00
assert order.total() == 30
assert order.due() == Decimal('28.5')
2020-06-11 19:58:15 +02:00
def test_large_order_promo_no_discount(customer_fidelity_0, cart_plain) -> None:
order = Order(customer_fidelity_0, cart_plain, LargeOrderPromo())
2021-08-07 05:44:01 +02:00
assert order.total() == 42
assert order.due() == 42
2020-06-11 19:58:15 +02:00
def test_large_order_promo_with_discount(customer_fidelity_0) -> None:
2021-08-07 05:44:01 +02:00
cart = [LineItem(str(item_code), 1, Decimal(1))
for item_code in range(10)]
2020-06-11 19:58:15 +02:00
order = Order(customer_fidelity_0, cart, LargeOrderPromo())
2021-08-07 05:44:01 +02:00
assert order.total() == 10
assert order.due() == Decimal('9.3')