-
Notifications
You must be signed in to change notification settings - Fork 1.1k
UNIT tests was wrote for Stellar Burgers #646
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
Open
Niplou
wants to merge
2
commits into
Yandex-Practicum:main
Choose a base branch
from
Niplou:develop1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| import pytest | ||
| from unittest.mock import Mock | ||
| from burger import Burger | ||
| from bun import Bun | ||
| from ingredient import Ingredient | ||
| from ingredient_types import INGREDIENT_TYPE_SAUCE, INGREDIENT_TYPE_FILLING | ||
| from test_data import GET_RECEIPT_TEST_DATA, GET_PRICE_TEST_DATA | ||
|
|
||
|
|
||
| class TestBurger: | ||
|
|
||
| # Тесты для метода set_buns | ||
| def test_set_buns(self): | ||
| burger = Burger() | ||
| mock_bun = Mock(spec=Bun) | ||
|
|
||
| burger.set_buns(mock_bun) | ||
|
|
||
| assert burger.bun == mock_bun | ||
|
|
||
| # Тесты для метода add_ingredient | ||
| def test_add_ingredient(self): | ||
| burger = Burger() | ||
| mock_ingredient = Mock(spec=Ingredient) | ||
|
|
||
| burger.add_ingredient(mock_ingredient) | ||
|
|
||
| assert len(burger.ingredients) == 1 | ||
| assert burger.ingredients[0] == mock_ingredient | ||
|
|
||
| # Тесты для метода remove_ingredient | ||
| def test_remove_ingredient(self): | ||
| burger = Burger() | ||
| mock_ingredient1 = Mock(spec=Ingredient) | ||
| mock_ingredient2 = Mock(spec=Ingredient) | ||
|
|
||
| burger.add_ingredient(mock_ingredient1) | ||
| burger.add_ingredient(mock_ingredient2) | ||
|
|
||
| burger.remove_ingredient(0) | ||
|
|
||
| assert len(burger.ingredients) == 1 | ||
| assert burger.ingredients[0] == mock_ingredient2 | ||
|
|
||
| # Тесты для метода move_ingredient | ||
| def test_move_ingredient(self): | ||
| burger = Burger() | ||
| mock_ingredient1 = Mock(spec=Ingredient) | ||
| mock_ingredient2 = Mock(spec=Ingredient) | ||
| mock_ingredient3 = Mock(spec=Ingredient) | ||
|
|
||
| burger.add_ingredient(mock_ingredient1) | ||
| burger.add_ingredient(mock_ingredient2) | ||
| burger.add_ingredient(mock_ingredient3) | ||
|
|
||
| burger.move_ingredient(0, 2) | ||
|
|
||
| assert burger.ingredients[0] == mock_ingredient2 | ||
| assert burger.ingredients[1] == mock_ingredient3 | ||
| assert burger.ingredients[2] == mock_ingredient1 | ||
|
|
||
| @pytest.mark.parametrize("test_data", GET_PRICE_TEST_DATA) | ||
| def test_get_price_with_different_combinations(self, test_data): | ||
| burger = Burger() | ||
|
|
||
| # Мок булки | ||
| mock_bun = Mock(spec=Bun) | ||
| mock_bun.get_price.return_value = test_data["bun_price"] | ||
| burger.set_buns(mock_bun) | ||
|
|
||
| # Моки ингредиентов | ||
| for price in test_data["ingredient_prices"]: | ||
| mock_ingredient = Mock(spec=Ingredient) | ||
| mock_ingredient.get_price.return_value = price | ||
| burger.add_ingredient(mock_ingredient) | ||
|
|
||
| result = burger.get_price() | ||
|
|
||
| # Убираем лишние строки после assert | ||
| assert result == test_data["expected_total"] | ||
|
|
||
| # Тест для метода get_price с проверкой вызовов методов | ||
| def test_get_price_calls_methods(self): | ||
| burger = Burger() | ||
|
|
||
| mock_bun = Mock(spec=Bun) | ||
| mock_bun.get_price.return_value = 100 | ||
| burger.set_buns(mock_bun) | ||
|
|
||
| mock_ingredient1 = Mock(spec=Ingredient) | ||
| mock_ingredient1.get_price.return_value = 50 | ||
| burger.add_ingredient(mock_ingredient1) | ||
|
|
||
| mock_ingredient2 = Mock(spec=Ingredient) | ||
| mock_ingredient2.get_price.return_value = 75 | ||
| burger.add_ingredient(mock_ingredient2) | ||
|
|
||
| result = burger.get_price() | ||
|
|
||
| assert result == 325 | ||
|
|
||
| # Параметризованные тесты для метода get_receipt | ||
| @pytest.mark.parametrize("test_data", GET_RECEIPT_TEST_DATA) | ||
| def test_get_receipt_format(self, test_data): | ||
| burger = Burger() | ||
|
|
||
| # Мок булки | ||
| mock_bun = Mock(spec=Bun) | ||
| mock_bun.get_name.return_value = test_data["bun_name"] | ||
| mock_bun.get_price.return_value = 0 | ||
| burger.set_buns(mock_bun) | ||
|
|
||
| # Моки ингредиентов | ||
| for ingredient_type, ingredient_name in test_data["ingredients"]: | ||
| mock_ingredient = Mock(spec=Ingredient) | ||
| mock_ingredient.get_type.return_value = ingredient_type | ||
| mock_ingredient.get_name.return_value = ingredient_name | ||
| mock_ingredient.get_price.return_value = 0 | ||
| burger.add_ingredient(mock_ingredient) | ||
|
|
||
| receipt = burger.get_receipt() | ||
| receipt_lines = receipt.split('\n') | ||
|
|
||
| # Убираем лишние строки после assert | ||
| assert len(receipt_lines) == len(test_data["expected_lines"]) | ||
| for i, expected_line in enumerate(test_data["expected_lines"]): | ||
| assert receipt_lines[i] == expected_line | ||
|
|
||
| # Тест для метода get_receipt с правильной ценой | ||
| def test_get_receipt_with_price(self): | ||
| burger = Burger() | ||
|
|
||
| mock_bun = Mock(spec=Bun) | ||
| mock_bun.get_name.return_value = "red bun" | ||
| mock_bun.get_price.return_value = 100 | ||
| burger.set_buns(mock_bun) | ||
|
|
||
| mock_ingredient = Mock(spec=Ingredient) | ||
| mock_ingredient.get_type.return_value = INGREDIENT_TYPE_SAUCE | ||
| mock_ingredient.get_name.return_value = "chili sauce" | ||
| mock_ingredient.get_price.return_value = 50 | ||
| burger.add_ingredient(mock_ingredient) | ||
|
|
||
| receipt = burger.get_receipt() | ||
|
|
||
| assert "Price: 250" in receipt # 100*2 + 50 = 250 | ||
| assert "(==== red bun ====)" in receipt | ||
| assert "= sauce chili sauce =" in receipt | ||
|
|
||
| # Тест для пустого бургера (только булки) | ||
| def test_get_receipt_only_bun(self): | ||
| burger = Burger() | ||
|
|
||
| mock_bun = Mock(spec=Bun) | ||
| mock_bun.get_name.return_value = "white bun" | ||
| mock_bun.get_price.return_value = 200 | ||
| burger.set_buns(mock_bun) | ||
|
|
||
| receipt = burger.get_receipt() | ||
| receipt_lines = receipt.split('\n') | ||
|
|
||
| expected_lines = [ | ||
| "(==== white bun ====)", | ||
| "(==== white bun ====)", | ||
| "", | ||
| "Price: 400" | ||
| ] | ||
|
|
||
| assert len(receipt_lines) == len(expected_lines) | ||
| for i, expected_line in enumerate(expected_lines): | ||
| assert receipt_lines[i] == expected_line | ||
|
|
||
| # Тест на удаление ингредиента с несуществующим индексом | ||
| def test_remove_ingredient_invalid_index(self): | ||
| burger = Burger() | ||
| mock_ingredient = Mock(spec=Ingredient) | ||
| burger.add_ingredient(mock_ingredient) | ||
|
|
||
| # Должен поднять IndexError при попытке удалить несуществующий индекс | ||
| with pytest.raises(IndexError): | ||
| burger.remove_ingredient(5) | ||
|
|
||
| # Тест на перемещение ингредиента с несуществующим индексом | ||
| def test_move_ingredient_invalid_index(self): | ||
| burger = Burger() | ||
| mock_ingredient = Mock(spec=Ingredient) | ||
| burger.add_ingredient(mock_ingredient) | ||
|
|
||
| # Сохраняем исходный список ингредиентов | ||
| original_ingredients = burger.ingredients.copy() | ||
|
|
||
| # Проверяем, что при попытке переместить несуществующий индекс возникает ошибка | ||
| try: | ||
| burger.move_ingredient(5, 0) | ||
| # Если не возникла ошибка, тест должен упасть | ||
| assert False, "Expected IndexError but no exception was raised" | ||
| except IndexError as e: | ||
| # Проверяем, что список ингредиентов не изменился после ошибки | ||
| assert burger.ingredients == original_ingredients | ||
| # Можно также проверить текст ошибки, если нужно | ||
| assert "pop index out of range" in str(e) | ||
|
|
||
| # Дополнительный тест для проверки преобразования типов ингредиентов в нижний регистр | ||
| def test_get_receipt_ingredient_types_lowercase(self): | ||
| burger = Burger() | ||
|
|
||
| mock_bun = Mock(spec=Bun) | ||
| mock_bun.get_name.return_value = "test bun" | ||
| mock_bun.get_price.return_value = 0 | ||
| burger.set_buns(mock_bun) | ||
|
|
||
| # Добавляем ингредиенты с разными типами | ||
| mock_sauce = Mock(spec=Ingredient) | ||
| mock_sauce.get_type.return_value = INGREDIENT_TYPE_SAUCE | ||
| mock_sauce.get_name.return_value = "test sauce" | ||
| mock_sauce.get_price.return_value = 0 | ||
|
|
||
| mock_filling = Mock(spec=Ingredient) | ||
| mock_filling.get_type.return_value = INGREDIENT_TYPE_FILLING | ||
| mock_filling.get_name.return_value = "test filling" | ||
| mock_filling.get_price.return_value = 0 | ||
|
|
||
| burger.add_ingredient(mock_sauce) | ||
| burger.add_ingredient(mock_filling) | ||
|
|
||
| receipt = burger.get_receipt() | ||
|
|
||
| # Проверяем, что типы ингредиентов преобразованы в нижний регистр | ||
| assert "= sauce test sauce =" in receipt | ||
| assert "= filling test filling =" in receipt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| from ingredient_types import INGREDIENT_TYPE_SAUCE, INGREDIENT_TYPE_FILLING | ||
|
|
||
| # Тестовые данные для get_receipt | ||
| GET_RECEIPT_TEST_DATA = [ | ||
| { | ||
| "name": "Бургер с соусом и котлетой", | ||
| "bun_name": "black bun", | ||
| "ingredients": [ | ||
| (INGREDIENT_TYPE_SAUCE, "hot sauce"), | ||
| (INGREDIENT_TYPE_FILLING, "cutlet") | ||
| ], | ||
| "expected_lines": [ | ||
| "(==== black bun ====)", | ||
| "= sauce hot sauce =", | ||
| "= filling cutlet =", | ||
| "(==== black bun ====)", | ||
| "", | ||
| "Price: 0" | ||
| ] | ||
| }, | ||
| { | ||
| "name": "Бургер с динозавром и сметаной", | ||
| "bun_name": "white bun", | ||
| "ingredients": [ | ||
| (INGREDIENT_TYPE_FILLING, "dinosaur"), | ||
| (INGREDIENT_TYPE_SAUCE, "sour cream") | ||
| ], | ||
| "expected_lines": [ | ||
| "(==== white bun ====)", | ||
| "= filling dinosaur =", | ||
| "= sauce sour cream =", | ||
| "(==== white bun ====)", | ||
| "", | ||
| "Price: 0" | ||
| ] | ||
| } | ||
| ] | ||
|
|
||
| # Тестовые данные для get_price | ||
| GET_PRICE_TEST_DATA = [ | ||
| { | ||
| "name": "Булочка за 100 и два ингредиента", | ||
| "bun_price": 100, | ||
| "ingredient_prices": [50, 75], | ||
| "expected_total": 325 | ||
| }, | ||
| { | ||
| "name": "Булочка за 50 и три ингредиента", | ||
| "bun_price": 50, | ||
| "ingredient_prices": [25, 30, 45], | ||
| "expected_total": 200 | ||
| }, | ||
| { | ||
| "name": "Только булочка", | ||
| "bun_price": 200, | ||
| "ingredient_prices": [], | ||
| "expected_total": 400 | ||
| }, | ||
| { | ||
| "name": "Булочка бесплатная с платными ингредиентами", | ||
| "bun_price": 0, | ||
| "ingredient_prices": [10, 20], | ||
| "expected_total": 30 | ||
| } | ||
| ] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
нужно исправить: нужно протестировать используя assert