Skip to content

Added type hints and pytest tests #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions patterns/behavioral/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,77 @@
Enables selecting an algorithm at runtime.
"""

from __future__ import annotations

from typing import Callable, Type


class DiscountStrategyValidator: # Descriptor class for check perform
def validate(self, obj: Order, value: Callable) -> bool:
try:
if obj.price - value(obj) < 0:
raise ValueError(f"Discount cannot be applied due to negative price resulting. {value.__name__}")
return True
except ValueError as ex:
print(str(ex))
return False

def __set_name__(self, owner, name: str) -> None:
self.private_name = '_' + name

def __set__(self, obj: Order, value: Callable = None) -> None:
if value and self.validate(obj, value):
setattr(obj, self.private_name, value)
else:
setattr(obj, self.private_name, None)

def __get__(self, obj: object, objtype: Type = None):
return getattr(obj, self.private_name)


class Order:
def __init__(self, price, discount_strategy=None):
self.price = price
discount_strategy = DiscountStrategyValidator()

def __init__(self, price: float, discount_strategy: Callable = None) -> None:
self.price: float = price
self.discount_strategy = discount_strategy

def price_after_discount(self):
def apply_discount(self) -> float:
if self.discount_strategy:
discount = self.discount_strategy(self)
else:
discount = 0

return self.price - discount

def __repr__(self):
fmt = "<Price: {}, price after discount: {}>"
return fmt.format(self.price, self.price_after_discount())
def __repr__(self) -> str:
return f"<Order price: {self.price} with discount strategy: {getattr(self.discount_strategy,'__name__',None)}>"


def ten_percent_discount(order):
def ten_percent_discount(order: Order) -> float:
return order.price * 0.10


def on_sale_discount(order):
def on_sale_discount(order: Order) -> float:
return order.price * 0.25 + 20


def main():
"""
>>> Order(100)
<Price: 100, price after discount: 100>

>>> Order(100, discount_strategy=ten_percent_discount)
<Price: 100, price after discount: 90.0>

>>> Order(1000, discount_strategy=on_sale_discount)
<Price: 1000, price after discount: 730.0>
>>> order = Order(100, discount_strategy=ten_percent_discount)
>>> print(order)
<Order price: 100 with discount strategy: ten_percent_discount>
>>> print(order.apply_discount())
90.0
>>> order = Order(100, discount_strategy=on_sale_discount)
>>> print(order)
<Order price: 100 with discount strategy: on_sale_discount>
>>> print(order.apply_discount())
55.0
>>> order = Order(10, discount_strategy=on_sale_discount)
Discount cannot be applied due to negative price resulting. on_sale_discount
>>> print(order)
<Order price: 10 with discount strategy: None>
"""


Expand Down
52 changes: 52 additions & 0 deletions tests/behavioral/test_strategy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest

from patterns.behavioral.strategy import Order, ten_percent_discount, on_sale_discount


@pytest.fixture
def order():
return Order(100)


@pytest.mark.parametrize(
"func, discount",
[
(ten_percent_discount, 10.0),
(on_sale_discount, 45.0)
]
)
def test_discount_function_return(func, order, discount):
assert func(order) == discount


@pytest.mark.parametrize(
"func, price",
[
(ten_percent_discount, 100),
(on_sale_discount, 100)
]
)
def test_order_discount_strategy_validate_success(func, price):
order = Order(price, func)

assert order.price == price
assert order.discount_strategy == func


def test_order_discount_strategy_validate_error():
order = Order(10, discount_strategy=on_sale_discount)

assert order.discount_strategy is None


@pytest.mark.parametrize(
"func, price, discount",
[
(ten_percent_discount, 100, 90.0),
(on_sale_discount, 100, 55.0)
]
)
def test_discount_apply_success(func, price, discount):
order = Order(price, func)

assert order.apply_discount() == discount