-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Diplom_1. Tests for Burger ClassV1 #656
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
levspivak63-ship-it
wants to merge
1
commit into
Yandex-Practicum:main
Choose a base branch
from
levspivak63-ship-it: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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # -------------------------------------------------------------------------------- | ||
| # File created using 'AnGitIgnored' extensions for Visual Studio Code: | ||
| # https://marketplace.visualstudio.com/items?itemName=AnAppWiLos.angitignored | ||
| #------------------------------------------------------------------------------- | ||
| # Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode | ||
| # Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode | ||
|
|
||
| ### VisualStudioCode ### | ||
| .vscode/* | ||
| !.vscode/settings.json | ||
| !.vscode/tasks.json | ||
| !.vscode/launch.json | ||
| !.vscode/extensions.json | ||
| !.vscode/*.code-snippets | ||
|
|
||
| # Local History for Visual Studio Code | ||
| .history/ | ||
|
|
||
| # Built Visual Studio Code Extensions | ||
| *.vsix | ||
|
|
||
| ### VisualStudioCode Patch ### | ||
| # Ignore all local history of files | ||
| .history | ||
| .ionide | ||
|
|
||
| ### Windows ### | ||
| # Windows thumbnail cache files | ||
| Thumbs.db | ||
| Thumbs.db:encryptable | ||
| ehthumbs.db | ||
| ehthumbs_vista.db | ||
|
|
||
| # Dump file | ||
| *.stackdump | ||
|
|
||
| # Folder config file | ||
| [Dd]esktop.ini | ||
|
|
||
| # Recycle Bin used on file shares | ||
| $RECYCLE.BIN/ | ||
|
|
||
| # Windows Installer files | ||
| *.cab | ||
| *.msi | ||
| *.msix | ||
| *.msm | ||
| *.msp | ||
|
|
||
| # Windows shortcuts | ||
| *.lnk | ||
|
|
||
| # End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode | ||
|
|
||
| # Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option) | ||
|
|
||
| /tests/__pycache__ | ||
|
|
||
| # Coverage | ||
| htmlcov/ | ||
| .coverage | ||
| .coverage.* | ||
| *.cover | ||
| coverage.xml | ||
|
|
||
| # Отредактируйте .gitignore чтобы было: | ||
| echo "__pycache__/" >> .gitignore | ||
| echo "*.pyc" >> .gitignore |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
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,27 @@ | ||
| # conftest.py | ||
|
|
||
| import pytest | ||
| from unittest.mock import Mock | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_bun(): | ||
| mock = Mock() | ||
| mock.get_name.return_value = "black bun" | ||
| mock.get_price.return_value = 100.0 | ||
| return mock | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_ingredient(): | ||
| mock = Mock() | ||
| mock.get_type.return_value = "SAUCE" | ||
| mock.get_name.return_value = "hot sauce" | ||
| mock.get_price.return_value = 100.0 | ||
| return mock | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def empty_burger(): | ||
| from praktikum.burger import Burger | ||
| return Burger() | ||
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,83 @@ | ||
| #data.py | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
|
|
||
| def get_price_test_cases(): | ||
| return [ | ||
| (100.0, [100.0, 200.0], 500.0), # 100*2 + 100 + 200 = 500 (а не 400!) | ||
| (200.0, [100.0, 200.0, 300.0], 1000.0), # 200*2 + 100 + 200 + 300 = 1000 (а не 800!) | ||
| (300.0, [], 600.0), # 300*2 = 600 ✓ | ||
| (150.0, [50.0], 350.0), # 150*2 + 50 = 350 ✓ | ||
| ] | ||
|
|
||
|
|
||
| def get_receipt_test_cases(): | ||
| return [ | ||
| ( | ||
| "black bun", | ||
| 100.0, | ||
| [], | ||
| ["(==== black bun ====)", "(==== black bun ====)", "", "Price: 200.0"] # Пустая строка есть! | ||
| ), | ||
| ( | ||
| "white bun", | ||
| 200.0, | ||
| [("SAUCE", "hot sauce", 100.0)], | ||
| ["(==== white bun ====)", "= sauce hot sauce =", "(==== white bun ====)", "", "Price: 500.0"] # Пустая строка есть! | ||
| ), | ||
| ( | ||
| "red bun", | ||
| 300.0, | ||
| [ | ||
| ("SAUCE", "sour cream", 200.0), | ||
| ("FILLING", "cutlet", 100.0) | ||
| ], | ||
| ["(==== red bun ====)", "= sauce sour cream =", "= filling cutlet =", "(==== red bun ====)", "", "Price: 900.0"] # Пустая строка есть! | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| def create_burger_for_price_test(bun_price, ingredient_prices): | ||
| """Создает бургер для теста цены""" | ||
| from praktikum.burger import Burger | ||
|
|
||
| burger = Burger() | ||
|
|
||
| # Создаем булку | ||
| mock_bun = Mock() | ||
| mock_bun.get_price.return_value = bun_price | ||
|
|
||
| # Настраиваем бургер | ||
| burger.set_buns(mock_bun) | ||
|
|
||
| # Добавляем ингредиенты | ||
| for price in ingredient_prices: | ||
| mock_ingredient = Mock() | ||
| mock_ingredient.get_price.return_value = price | ||
| burger.add_ingredient(mock_ingredient) | ||
|
|
||
| return burger | ||
|
|
||
|
|
||
| def create_burger_for_receipt_test(bun_name, bun_price, ingredients_data): | ||
| """Создает бургер для теста рецепта""" | ||
| from praktikum.burger import Burger | ||
|
|
||
| burger = Burger() | ||
|
|
||
| # Создаем булку | ||
| mock_bun = Mock() | ||
| mock_bun.get_name.return_value = bun_name | ||
| mock_bun.get_price.return_value = bun_price | ||
| burger.set_buns(mock_bun) | ||
|
|
||
| # Добавляем ингредиенты | ||
| for ingredient_type, name, price in ingredients_data: | ||
| mock_ingredient = Mock() | ||
| mock_ingredient.get_type.return_value = ingredient_type | ||
| mock_ingredient.get_name.return_value = name | ||
| mock_ingredient.get_price.return_value = price | ||
| burger.add_ingredient(mock_ingredient) | ||
|
|
||
| return burger |
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,80 @@ | ||
| #test_burger.py | ||
|
|
||
| import pytest | ||
| from unittest.mock import Mock | ||
| from .data import ( | ||
| get_price_test_cases, | ||
| get_receipt_test_cases, | ||
| create_burger_for_price_test, | ||
| create_burger_for_receipt_test | ||
| ) | ||
|
|
||
|
|
||
| class TestBurger: | ||
|
|
||
| def test_burger_initialization(self, empty_burger): | ||
| assert empty_burger.bun is None | ||
| assert empty_burger.ingredients == [] | ||
|
|
||
| def test_set_buns(self, empty_burger, mock_bun): | ||
| empty_burger.set_buns(mock_bun) | ||
| assert empty_burger.bun == mock_bun | ||
|
|
||
| def test_add_ingredient(self, empty_burger, mock_ingredient): | ||
| empty_burger.add_ingredient(mock_ingredient) | ||
| assert len(empty_burger.ingredients) == 1 | ||
| assert empty_burger.ingredients[0] == mock_ingredient | ||
|
|
||
|
|
||
| def test_remove_ingredient(self, empty_burger): | ||
| mock_ingredient = Mock() | ||
| empty_burger.add_ingredient(mock_ingredient) | ||
| empty_burger.remove_ingredient(0) | ||
| assert len(empty_burger.ingredients) == 0 | ||
|
|
||
| def test_remove_ingredient_invalid_index(self, empty_burger): | ||
| mock_ingredient = Mock() | ||
| empty_burger.add_ingredient(mock_ingredient) | ||
| with pytest.raises(IndexError): | ||
| empty_burger.remove_ingredient(10) | ||
|
|
||
| def test_move_ingredient(self, empty_burger): | ||
| mock_ingredient1 = Mock() | ||
| mock_ingredient2 = Mock() | ||
|
|
||
| empty_burger.add_ingredient(mock_ingredient1) | ||
| empty_burger.add_ingredient(mock_ingredient2) | ||
|
|
||
| empty_burger.move_ingredient(0, 1) | ||
| assert empty_burger.ingredients[0] == mock_ingredient2 | ||
| assert empty_burger.ingredients[1] == mock_ingredient1 | ||
|
|
||
| def test_move_ingredient_invalid_index(self, empty_burger): | ||
| mock_ingredient = Mock() | ||
| empty_burger.add_ingredient(mock_ingredient) | ||
| with pytest.raises(IndexError): | ||
| empty_burger.move_ingredient(10, 0) | ||
|
|
||
| @pytest.mark.parametrize("bun_price,ingredient_prices,expected", get_price_test_cases()) | ||
| def test_get_price(self, bun_price, ingredient_prices, expected): | ||
| burger = create_burger_for_price_test(bun_price, ingredient_prices) | ||
| assert burger.get_price() == expected | ||
|
|
||
| def test_get_price_no_bun(self, empty_burger): | ||
| mock_ingredient = Mock() | ||
| empty_burger.add_ingredient(mock_ingredient) | ||
| with pytest.raises(AttributeError): | ||
| empty_burger.get_price() | ||
|
|
||
| @pytest.mark.parametrize("bun_name,bun_price,ingredients_data,expected_lines", get_receipt_test_cases()) | ||
| def test_get_receipt(self, bun_name, bun_price, ingredients_data, expected_lines): | ||
| burger = create_burger_for_receipt_test(bun_name, bun_price, ingredients_data) | ||
| receipt = burger.get_receipt() | ||
| lines = receipt.split('\n') | ||
|
|
||
| assert len(lines) == len(expected_lines) | ||
| assert lines == expected_lines | ||
|
|
||
| def test_get_receipt_no_bun(self, empty_burger): | ||
| with pytest.raises(AttributeError): | ||
| empty_burger.get_receipt() |
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.
Можно лучше: во всех тестах лучше выносить тестовые данные во внешний модуль (например, data). Это позволит облегчить их поддержку