-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Stellar Burgers Task 1 (unit tests) #698
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
debatableoutcome
wants to merge
6
commits into
Yandex-Practicum:main
Choose a base branch
from
debatableoutcome: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
6 commits
Select commit
Hold shift + click to select a range
22928be
feat: implement unit tests for Task 1 using mocks and parametrization
debatableoutcome acdd876
fix after CR: fixture bun_mock() applied
debatableoutcome 0d6b512
fix after CRL: add Burger.get_receipt tests for empty and populated i…
debatableoutcome fba6a1b
fix after CR: ref: one method - one test
debatableoutcome a2033e4
fix after CR: ref: tests made class-based pytest layout across all mo…
debatableoutcome 7c33771
updated coverage report
debatableoutcome 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,27 @@ | ||
| # Virtual environment | ||
| venv/ | ||
| .env | ||
| .env.* | ||
|
|
||
| # Python cache | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
|
|
||
| # Pytest cache | ||
| .pytest_cache/ | ||
| .coverage | ||
| htmlcov/ | ||
| pytest_cov/ | ||
|
|
||
| # IDE | ||
| .idea/ | ||
| .vscode/ | ||
| *.iml | ||
|
|
||
| # Logs | ||
| *.log | ||
|
|
||
| # System files | ||
| .DS_Store | ||
| Thumbs.db |
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,19 @@ | ||
| import pytest | ||
| from unittest.mock import Mock | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def bun_mock(): | ||
| bun = Mock() | ||
| bun.get_name.return_value = 'black bun' | ||
| bun.get_price.return_value = 100 | ||
| return bun | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def ingredient_mock(): | ||
| ingredient = Mock() | ||
| ingredient.get_type.return_value = 'SAUCE' | ||
| ingredient.get_name.return_value = 'hot sauce' | ||
| ingredient.get_price.return_value = 50 | ||
| return ingredient | ||
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,108 @@ | ||
| <?xml version="1.0" ?> | ||
| <coverage version="7.13.3" timestamp="1771524148565" lines-valid="69" lines-covered="69" line-rate="1" branches-covered="0" branches-valid="0" branch-rate="0" complexity="0"> | ||
| <!-- Generated by coverage.py: https://coverage.readthedocs.io/en/7.13.3 --> | ||
| <!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd --> | ||
| <sources> | ||
| <source>C:\Users\masha\Documents\Education Practice\dev\Project\Task_1\praktikum</source> | ||
| </sources> | ||
| <packages> | ||
| <package name="." line-rate="1" branch-rate="0" complexity="0"> | ||
| <classes> | ||
| <class name="bun.py" filename="bun.py" complexity="0" line-rate="1" branch-rate="0"> | ||
| <methods/> | ||
| <lines> | ||
| <line number="1" hits="1"/> | ||
| <line number="7" hits="1"/> | ||
| <line number="8" hits="1"/> | ||
| <line number="9" hits="1"/> | ||
| <line number="11" hits="1"/> | ||
| <line number="12" hits="1"/> | ||
| <line number="14" hits="1"/> | ||
| <line number="15" hits="1"/> | ||
| </lines> | ||
| </class> | ||
| <class name="burger.py" filename="burger.py" complexity="0" line-rate="1" branch-rate="0"> | ||
| <methods/> | ||
| <lines> | ||
| <line number="1" hits="1"/> | ||
| <line number="3" hits="1"/> | ||
| <line number="4" hits="1"/> | ||
| <line number="7" hits="1"/> | ||
| <line number="15" hits="1"/> | ||
| <line number="16" hits="1"/> | ||
| <line number="17" hits="1"/> | ||
| <line number="19" hits="1"/> | ||
| <line number="20" hits="1"/> | ||
| <line number="22" hits="1"/> | ||
| <line number="23" hits="1"/> | ||
| <line number="25" hits="1"/> | ||
| <line number="26" hits="1"/> | ||
| <line number="28" hits="1"/> | ||
| <line number="29" hits="1"/> | ||
| <line number="31" hits="1"/> | ||
| <line number="32" hits="1"/> | ||
| <line number="34" hits="1"/> | ||
| <line number="35" hits="1"/> | ||
| <line number="37" hits="1"/> | ||
| <line number="39" hits="1"/> | ||
| <line number="40" hits="1"/> | ||
| <line number="42" hits="1"/> | ||
| <line number="43" hits="1"/> | ||
| <line number="45" hits="1"/> | ||
| <line number="46" hits="1"/> | ||
| <line number="48" hits="1"/> | ||
| </lines> | ||
| </class> | ||
| <class name="database.py" filename="database.py" complexity="0" line-rate="1" branch-rate="0"> | ||
| <methods/> | ||
| <lines> | ||
| <line number="1" hits="1"/> | ||
| <line number="3" hits="1"/> | ||
| <line number="4" hits="1"/> | ||
| <line number="5" hits="1"/> | ||
| <line number="8" hits="1"/> | ||
| <line number="13" hits="1"/> | ||
| <line number="14" hits="1"/> | ||
| <line number="15" hits="1"/> | ||
| <line number="17" hits="1"/> | ||
| <line number="18" hits="1"/> | ||
| <line number="19" hits="1"/> | ||
| <line number="21" hits="1"/> | ||
| <line number="22" hits="1"/> | ||
| <line number="23" hits="1"/> | ||
| <line number="25" hits="1"/> | ||
| <line number="26" hits="1"/> | ||
| <line number="27" hits="1"/> | ||
| <line number="29" hits="1"/> | ||
| <line number="30" hits="1"/> | ||
| <line number="32" hits="1"/> | ||
| <line number="33" hits="1"/> | ||
| </lines> | ||
| </class> | ||
| <class name="ingredient.py" filename="ingredient.py" complexity="0" line-rate="1" branch-rate="0"> | ||
| <methods/> | ||
| <lines> | ||
| <line number="1" hits="1"/> | ||
| <line number="8" hits="1"/> | ||
| <line number="9" hits="1"/> | ||
| <line number="10" hits="1"/> | ||
| <line number="11" hits="1"/> | ||
| <line number="13" hits="1"/> | ||
| <line number="14" hits="1"/> | ||
| <line number="16" hits="1"/> | ||
| <line number="17" hits="1"/> | ||
| <line number="19" hits="1"/> | ||
| <line number="20" hits="1"/> | ||
| </lines> | ||
| </class> | ||
| <class name="ingredient_types.py" filename="ingredient_types.py" complexity="0" line-rate="1" branch-rate="0"> | ||
| <methods/> | ||
| <lines> | ||
| <line number="6" hits="1"/> | ||
| <line number="7" hits="1"/> | ||
| </lines> | ||
| </class> | ||
| </classes> | ||
| </package> | ||
| </packages> | ||
| </coverage> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,3 @@ | ||
| [pytest] | ||
| pythonpath = . | ||
| testpaths = tests |
Binary file not shown.
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,24 @@ | ||
| import pytest | ||
| from praktikum.bun import Bun | ||
|
|
||
|
|
||
| class TestBun: | ||
| @pytest.mark.parametrize('name,price', [ | ||
| ('black bun', 100), | ||
| ('white bun', 200), | ||
| ('red bun', 300), | ||
| ]) | ||
| def test_bun_get_name(self, name, price): | ||
| bun = Bun(name, price) | ||
|
|
||
| assert bun.get_name() == name | ||
|
|
||
| @pytest.mark.parametrize('name,price', [ | ||
| ('black bun', 100), | ||
| ('white bun', 200), | ||
| ('red bun', 300), | ||
| ]) | ||
| def test_bun_get_price(self, name, price): | ||
| bun = Bun(name, price) | ||
|
|
||
| assert bun.get_price() == price |
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,98 @@ | ||
| from unittest.mock import Mock | ||
| import pytest | ||
| from praktikum.burger import Burger | ||
|
|
||
|
|
||
| class TestBurger: | ||
| def test_set_buns(self, bun_mock): | ||
| burger = Burger() | ||
| burger.set_buns(bun_mock) | ||
|
|
||
| assert burger.bun == bun_mock | ||
|
|
||
| def test_add_ingredient(self, ingredient_mock): | ||
| burger = Burger() | ||
| burger.add_ingredient(ingredient_mock) | ||
|
|
||
| assert burger.ingredients == [ingredient_mock] | ||
|
|
||
| def test_remove_ingredient(self): | ||
| burger = Burger() | ||
|
|
||
| i1 = Mock() | ||
| i2 = Mock() | ||
|
|
||
| burger.add_ingredient(i1) | ||
| burger.add_ingredient(i2) | ||
|
|
||
| burger.remove_ingredient(0) | ||
|
|
||
| assert burger.ingredients == [i2] | ||
|
|
||
| def test_move_ingredient(self): | ||
| burger = Burger() | ||
|
|
||
| i1 = Mock() | ||
| i2 = Mock() | ||
| i3 = Mock() | ||
|
|
||
| burger.ingredients = [i1, i2, i3] | ||
|
|
||
| burger.move_ingredient(0, 2) | ||
|
|
||
| assert burger.ingredients == [i2, i3, i1] | ||
|
|
||
| @pytest.mark.parametrize('bun_price, ing_prices, expected', [ | ||
| (100, [], 200), | ||
| (100, [50], 250), | ||
| (200, [10, 20], 430), | ||
| ]) | ||
| def test_get_price(self, bun_price, ing_prices, expected): | ||
| burger = Burger() | ||
|
|
||
| bun = Mock() | ||
| bun.get_price.return_value = bun_price | ||
| burger.set_buns(bun) | ||
|
|
||
| for price in ing_prices: | ||
| ingredient = Mock() | ||
| ingredient.get_price.return_value = price | ||
|
|
||
| burger.add_ingredient(ingredient) | ||
|
|
||
| assert burger.get_price() == expected | ||
|
|
||
| def test_get_receipt_without_ingredients(self, bun_mock): | ||
| burger = Burger() | ||
| burger.set_buns(bun_mock) | ||
|
|
||
| expected = '(==== black bun ====)\n(==== black bun ====)\n\nPrice: 200' | ||
|
|
||
| assert burger.get_receipt() == expected | ||
|
|
||
| def test_get_receipt_with_ingredients(self, bun_mock): | ||
| burger = Burger() | ||
| burger.set_buns(bun_mock) | ||
|
|
||
| sauce = Mock() | ||
| sauce.get_type.return_value = 'SAUCE' | ||
| sauce.get_name.return_value = 'hot sauce' | ||
| sauce.get_price.return_value = 50 | ||
|
|
||
| filling = Mock() | ||
| filling.get_type.return_value = 'FILLING' | ||
| filling.get_name.return_value = 'cutlet' | ||
| filling.get_price.return_value = 100 | ||
|
|
||
| burger.add_ingredient(sauce) | ||
| burger.add_ingredient(filling) | ||
|
|
||
| expected = ( | ||
| '(==== black bun ====)\n' | ||
| '= sauce hot sauce =\n' | ||
| '= filling cutlet =\n' | ||
| '(==== black bun ====)\n\n' | ||
| 'Price: 350' | ||
| ) | ||
|
|
||
| assert burger.get_receipt() == expected |
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,39 @@ | ||
| from praktikum.database import Database | ||
| from praktikum.ingredient_types import INGREDIENT_TYPE_SAUCE, INGREDIENT_TYPE_FILLING | ||
|
|
||
|
|
||
| class TestDatabase: | ||
| def test_available_buns(self): | ||
| db = Database() | ||
| buns = db.available_buns() | ||
|
|
||
| assert len(buns) == 3 | ||
|
|
||
| names = [] | ||
| for bun in buns: | ||
| names.append(bun.get_name()) | ||
|
|
||
| assert names == ['black bun', 'white bun', 'red bun'] | ||
|
|
||
| def test_available_ingredients(self): | ||
| db = Database() | ||
| ingredients = db.available_ingredients() | ||
|
|
||
| assert len(ingredients) == 6 | ||
|
|
||
| def test_ingredient_types_count(self): | ||
| db = Database() | ||
| ingredients = db.available_ingredients() | ||
|
|
||
| sauce_count = 0 | ||
| filling_count = 0 | ||
|
|
||
| for ing in ingredients: | ||
| if ing.get_type() == INGREDIENT_TYPE_SAUCE: | ||
| sauce_count += 1 | ||
|
|
||
| if ing.get_type() == INGREDIENT_TYPE_FILLING: | ||
| filling_count += 1 | ||
|
|
||
| assert sauce_count == 3 | ||
| assert filling_count == 3 |
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,32 @@ | ||
| import pytest | ||
| from praktikum.ingredient import Ingredient | ||
| from praktikum.ingredient_types import INGREDIENT_TYPE_SAUCE, INGREDIENT_TYPE_FILLING | ||
|
|
||
|
|
||
| class TestIngredient: | ||
| @pytest.mark.parametrize('ingredient_type, name, price', [ | ||
| (INGREDIENT_TYPE_SAUCE, 'hot sauce', 100), | ||
| (INGREDIENT_TYPE_FILLING, 'cutlet', 200), | ||
| ]) | ||
| def test_ingredient_get_type(self, ingredient_type, name, price): | ||
| ing = Ingredient(ingredient_type, name, price) | ||
|
|
||
| assert ing.get_type() == ingredient_type | ||
|
|
||
| @pytest.mark.parametrize('ingredient_type, name, price', [ | ||
| (INGREDIENT_TYPE_SAUCE, 'hot sauce', 100), | ||
| (INGREDIENT_TYPE_FILLING, 'cutlet', 200), | ||
| ]) | ||
| def test_ingredient_get_name(self, ingredient_type, name, price): | ||
| ing = Ingredient(ingredient_type, name, price) | ||
|
|
||
| assert ing.get_name() == name | ||
|
|
||
| @pytest.mark.parametrize('ingredient_type, name, price', [ | ||
| (INGREDIENT_TYPE_SAUCE, 'hot sauce', 100), | ||
| (INGREDIENT_TYPE_FILLING, 'cutlet', 200), | ||
| ]) | ||
| def test_ingredient_get_price(self, ingredient_type, name, price): | ||
| ing = Ingredient(ingredient_type, name, price) | ||
|
|
||
| assert ing.get_price() == price |
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.
Нужно исправить: фикстуры написаны но нигде не используются