Skip to content
Open

done #905

Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
__pycache__/
*.pyc
.pytest_cache/
.coverage
htmlcov/
*.egg-info/
.venv/
venv/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
### Структура проекта

- `praktikum` - пакет, содержащий код программы
- `tests` - пакет, содержащий тесты, разделенные по классам. Например, `bun_test.py`, `burger_test.py` и т.д.
- `tests` - пакет, содержащий тесты, разделенные по классам. Например, `test_bun.py`, `test_burger.py` и т.д.

### Запуск автотестов

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
testpaths = tests
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==8.3.4
pytest-cov==7.1.0
Empty file added tests/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest.mock import Mock

import pytest

from praktikum.bun import Bun
from praktikum.burger import Burger
from praktikum.database import Database
from praktikum.ingredient import Ingredient
from tests.data import BURGER_BUN, BURGER_INGREDIENTS


@pytest.fixture
def burger():
return Burger()


@pytest.fixture
def bun_mock():
bun = Mock(spec=Bun)
bun.get_name.return_value = BURGER_BUN[0]
bun.get_price.return_value = BURGER_BUN[1]
return bun


@pytest.fixture
def ingredient_mocks():
mocks = []
for ingredient_type, name, price in BURGER_INGREDIENTS:
ingredient = Mock(spec=Ingredient)
ingredient.get_type.return_value = ingredient_type
ingredient.get_name.return_value = name
ingredient.get_price.return_value = price
mocks.append(ingredient)
return mocks


@pytest.fixture
def database():
return Database()
38 changes: 38 additions & 0 deletions tests/data.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужно исправить: в директории tests хранятся только тесты и conftest, все остальное должно быть в корне

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from praktikum.ingredient_types import INGREDIENT_TYPE_FILLING, INGREDIENT_TYPE_SAUCE

BUN_CASES = (
("classic bun", 100),
("premium bun", 200.5),
)

INGREDIENT_CASES = (
(INGREDIENT_TYPE_SAUCE, "spicy sauce", 50),
(INGREDIENT_TYPE_FILLING, "beef patty", 150),
)

BURGER_BUN = ("test bun", 100)

BURGER_INGREDIENTS = (
(INGREDIENT_TYPE_SAUCE, "hot sauce", 30),
(INGREDIENT_TYPE_FILLING, "cutlet", 120),
(INGREDIENT_TYPE_SAUCE, "mayo", 25),
)

REMOVE_INGREDIENT_CASES = (
(0, (1, 2)),
(1, (0, 2)),
(2, (0, 1)),
(-1, (0, 1)),
)

MOVE_INGREDIENT_CASES = (
(0, 2, (1, 2, 0)),
(2, 0, (2, 0, 1)),
(1, 1, (0, 1, 2)),
)

DATABASE_BUN_NAMES = ("black bun", "white bun", "red bun")
DATABASE_BUN_PRICES = (100, 200, 300)
DATABASE_INGREDIENT_COUNT = 6
DATABASE_SAUCE_COUNT = 3
DATABASE_FILLING_COUNT = 3
18 changes: 18 additions & 0 deletions tests/test_bun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from praktikum.bun import Bun
from tests.data import BUN_CASES


class TestBun:
@pytest.mark.parametrize("name, price", BUN_CASES)
def test_get_name_returns_given_name(self, name, price):
bun = Bun(name, price)

assert bun.get_name() == name

@pytest.mark.parametrize("name, price", BUN_CASES)
def test_get_price_returns_given_price(self, name, price):
bun = Bun(name, price)

assert bun.get_price() == price
92 changes: 92 additions & 0 deletions tests/test_burger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import pytest

from tests.data import (
BURGER_BUN,
BURGER_INGREDIENTS,
MOVE_INGREDIENT_CASES,
REMOVE_INGREDIENT_CASES,
)


class TestBurger:
def test_get_price_without_ingredients_returns_double_bun_price(self, burger, bun_mock):
burger.set_buns(bun_mock)

assert burger.get_price() == BURGER_BUN[1] * 2
bun_mock.get_price.assert_called_once_with()

def test_add_ingredient_includes_ingredient_in_price(self, burger, bun_mock, ingredient_mocks):
burger.set_buns(bun_mock)
burger.add_ingredient(ingredient_mocks[0])

expected_price = BURGER_BUN[1] * 2 + BURGER_INGREDIENTS[0][2]
assert burger.get_price() == expected_price
assert burger.ingredients == [ingredient_mocks[0]]

def test_get_price_with_several_ingredients_returns_total(self, burger, bun_mock, ingredient_mocks):
burger.set_buns(bun_mock)
for ingredient in ingredient_mocks:
burger.add_ingredient(ingredient)

expected_price = BURGER_BUN[1] * 2 + sum(item[2] for item in BURGER_INGREDIENTS)
assert burger.get_price() == expected_price

@pytest.mark.parametrize("index, remaining_indices", REMOVE_INGREDIENT_CASES)
def test_remove_ingredient_excludes_item_from_price(
self, burger, bun_mock, ingredient_mocks, index, remaining_indices
):
burger.set_buns(bun_mock)
for ingredient in ingredient_mocks:
burger.add_ingredient(ingredient)

burger.remove_ingredient(index)

expected_price = BURGER_BUN[1] * 2 + sum(
BURGER_INGREDIENTS[item_index][2] for item_index in remaining_indices
)
assert burger.get_price() == expected_price
assert burger.ingredients == [ingredient_mocks[item_index] for item_index in remaining_indices]

@pytest.mark.parametrize("index, new_index, expected_order", MOVE_INGREDIENT_CASES)
def test_move_ingredient_changes_order(
self, burger, bun_mock, ingredient_mocks, index, new_index, expected_order
):
burger.set_buns(bun_mock)
for ingredient in ingredient_mocks:
burger.add_ingredient(ingredient)

burger.move_ingredient(index, new_index)

assert burger.ingredients == [ingredient_mocks[item_index] for item_index in expected_order]

def test_get_receipt_without_ingredients_returns_bun_only(self, burger, bun_mock):
burger.set_buns(bun_mock)

receipt = burger.get_receipt()

expected_receipt = "\n".join(
[
f"(==== {BURGER_BUN[0]} ====)",
f"(==== {BURGER_BUN[0]} ====)\n",
f"Price: {burger.get_price()}",
]
)
assert receipt == expected_receipt

def test_get_receipt_returns_formatted_receipt(self, burger, bun_mock, ingredient_mocks):
burger.set_buns(bun_mock)
for ingredient in ingredient_mocks[:2]:
burger.add_ingredient(ingredient)

receipt = burger.get_receipt()

expected_receipt = "\n".join(
[
f"(==== {BURGER_BUN[0]} ====)",
f"= {BURGER_INGREDIENTS[0][0].lower()} {BURGER_INGREDIENTS[0][1]} =",
f"= {BURGER_INGREDIENTS[1][0].lower()} {BURGER_INGREDIENTS[1][1]} =",
f"(==== {BURGER_BUN[0]} ====)\n",
f"Price: {burger.get_price()}",
]
)
assert receipt == expected_receipt
25 changes: 25 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from praktikum.ingredient_types import INGREDIENT_TYPE_FILLING, INGREDIENT_TYPE_SAUCE
from tests.data import (
DATABASE_BUN_NAMES,
DATABASE_BUN_PRICES,
DATABASE_FILLING_COUNT,
DATABASE_INGREDIENT_COUNT,
DATABASE_SAUCE_COUNT,
)


class TestDatabase:
def test_available_buns_returns_all_buns(self, database):
buns = database.available_buns()

assert len(buns) == len(DATABASE_BUN_NAMES)
assert tuple(bun.get_name() for bun in buns) == DATABASE_BUN_NAMES
assert tuple(bun.get_price() for bun in buns) == DATABASE_BUN_PRICES

def test_available_ingredients_returns_sauces_and_fillings(self, database):
ingredients = database.available_ingredients()
types = [ingredient.get_type() for ingredient in ingredients]

assert len(ingredients) == DATABASE_INGREDIENT_COUNT
assert types.count(INGREDIENT_TYPE_SAUCE) == DATABASE_SAUCE_COUNT
assert types.count(INGREDIENT_TYPE_FILLING) == DATABASE_FILLING_COUNT
24 changes: 24 additions & 0 deletions tests/test_ingredient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

from praktikum.ingredient import Ingredient
from tests.data import INGREDIENT_CASES


class TestIngredient:
@pytest.mark.parametrize("ingredient_type, name, price", INGREDIENT_CASES)
def test_get_type_returns_given_type(self, ingredient_type, name, price):
ingredient = Ingredient(ingredient_type, name, price)

assert ingredient.get_type() == ingredient_type

@pytest.mark.parametrize("ingredient_type, name, price", INGREDIENT_CASES)
def test_get_name_returns_given_name(self, ingredient_type, name, price):
ingredient = Ingredient(ingredient_type, name, price)

assert ingredient.get_name() == name

@pytest.mark.parametrize("ingredient_type, name, price", INGREDIENT_CASES)
def test_get_price_returns_given_price(self, ingredient_type, name, price):
ingredient = Ingredient(ingredient_type, name, price)

assert ingredient.get_price() == price