Задание 5: модульные тесты product_calculator
This commit is contained in:
commit
70c0e18a77
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.__pycache__
|
||||||
|
.pytest_cache/
|
||||||
18
README.md
Normal file
18
README.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Вариативная часть. Задание 5: тестирование программных модулей
|
||||||
|
|
||||||
|
Модуль logic/product_calculator.py (Приложение 4), модульные тесты - папка tests.
|
||||||
|
|
||||||
|
## Протокол тестирования
|
||||||
|
|
||||||
|
Фреймворк модульного тестирования: pytest (Python).
|
||||||
|
|
||||||
|
Порядок запуска тестов:
|
||||||
|
1. Открыть папку shoe_store в терминале.
|
||||||
|
2. Выполнить команду: python -m pytest -v
|
||||||
|
|
||||||
|
| № | Проверяемый случай | Входные данные | Ожидаемый результат | Фактический результат | Статус |
|
||||||
|
|---|---|---|---|---|---|
|
||||||
|
| 1 | Заказов в предыдущем месяце нет | Цена 2000; заказов нет; дата расчета 15.06.2027 | 1500 | 1500 | Пройден |
|
||||||
|
| 2 | Есть заказ в предыдущем месяце | Цена 2000; заказ 10.05.2027; дата расчета 15.06.2027 | 2000 | 2000 | Пройден |
|
||||||
|
| 3 | Граничное значение количества | Количество пар: 5 | мало | мало | Пройден |
|
||||||
|
| 4 | Количество больше порогового | Количество пар: 6 | много | много | Пройден |
|
||||||
0
logic/__init__.py
Normal file
0
logic/__init__.py
Normal file
44
logic/product_calculator.py
Normal file
44
logic/product_calculator.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
"""Расчет показателей товара для каталога."""
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
DISCOUNT_PERCENT = 25
|
||||||
|
MANY_QUANTITY_THRESHOLD = 5
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_discounted_price(price, order_dates, calculation_date):
|
||||||
|
"""Возвращает цену товара с учетом скидки.
|
||||||
|
|
||||||
|
Скидка 25% предоставляется, если в предыдущем календарном месяце
|
||||||
|
(относительно даты расчета) по товару не было заказов.
|
||||||
|
|
||||||
|
:param price: цена товара в рублях
|
||||||
|
:param order_dates: список дат заказов товара (datetime.date), может быть пустым
|
||||||
|
:param calculation_date: дата расчета (datetime.date)
|
||||||
|
:return: цена с учетом скидки, округленная до копеек
|
||||||
|
"""
|
||||||
|
if price < 0:
|
||||||
|
raise ValueError("Цена не может быть отрицательной.")
|
||||||
|
|
||||||
|
current_month_start = calculation_date.replace(day=1)
|
||||||
|
previous_month_start = (current_month_start - timedelta(days=1)).replace(day=1)
|
||||||
|
has_orders_in_previous_month = any(
|
||||||
|
previous_month_start <= order_date < current_month_start
|
||||||
|
for order_date in order_dates
|
||||||
|
)
|
||||||
|
|
||||||
|
if has_orders_in_previous_month:
|
||||||
|
return price
|
||||||
|
return round(price * (100 - DISCOUNT_PERCENT) / 100, 2)
|
||||||
|
|
||||||
|
|
||||||
|
def get_quantity_label(total_quantity):
|
||||||
|
"""Возвращает «много», если пар больше пяти, иначе «мало».
|
||||||
|
|
||||||
|
:param total_quantity: количество пар суммарно по всем размерам
|
||||||
|
"""
|
||||||
|
if total_quantity < 0:
|
||||||
|
raise ValueError("Количество не может быть отрицательным.")
|
||||||
|
if total_quantity > MANY_QUANTITY_THRESHOLD:
|
||||||
|
return "много"
|
||||||
|
return "мало"
|
||||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
34
tests/test_product_calculator.py
Normal file
34
tests/test_product_calculator.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
from datetime import date
|
||||||
|
from logic.product_calculator import calculate_discounted_price, get_quantity_label
|
||||||
|
|
||||||
|
|
||||||
|
def test_discounted_price_no_orders_in_previous_month_returns_price_with_discount():
|
||||||
|
price = 2000
|
||||||
|
order_dates = []
|
||||||
|
calculation_date = date(2027, 6, 15)
|
||||||
|
|
||||||
|
result = calculate_discounted_price(price, order_dates, calculation_date)
|
||||||
|
|
||||||
|
assert result == 1500
|
||||||
|
|
||||||
|
|
||||||
|
def test_discounted_price_order_in_previous_month_returns_price_without_discount():
|
||||||
|
price = 2000
|
||||||
|
order_dates = [date(2027, 5, 10)]
|
||||||
|
calculation_date = date(2027, 6, 15)
|
||||||
|
|
||||||
|
result = calculate_discounted_price(price, order_dates, calculation_date)
|
||||||
|
|
||||||
|
assert result == 2000
|
||||||
|
|
||||||
|
|
||||||
|
def test_quantity_label_five_pairs_returns_few():
|
||||||
|
result = get_quantity_label(5)
|
||||||
|
|
||||||
|
assert result == "мало"
|
||||||
|
|
||||||
|
|
||||||
|
def test_quantity_label_six_pairs_returns_many():
|
||||||
|
result = get_quantity_label(6)
|
||||||
|
|
||||||
|
assert result == "много"
|
||||||
BIN
tests_result.png
Normal file
BIN
tests_result.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 37 KiB |
Loading…
Reference in New Issue
Block a user