diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 000000000..e07ae6284 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,16 @@ +import pytest +from unittest.mock import Mock + +@pytest.fixture +def burger_with_mocks(): + burger = Burger() + mock_bun = Mock() + mock_bun.get_price.return_value = 50 + burger.set_buns(mock_bun) + ing1 = Mock() + ing1.get_price.return_value = 30 + ing2 = Mock() + ing2.get_price.return_value = 20 + burger.add_ingredient(ing1) + burger.add_ingredient(ing2) + return burger \ No newline at end of file diff --git a/tests/test_bun.py b/tests/test_bun.py new file mode 100644 index 000000000..220671b04 --- /dev/null +++ b/tests/test_bun.py @@ -0,0 +1,15 @@ +from praktikum.bun import Bun + +class TestBun: + def test_bun_creation(self): + bun = Bun("black bun", 100.0) + assert bun.name == "black bun" + assert bun.price == 100.0 + + def test_get_name(self): + bun = Bun("white bun", 200.0) + assert bun.get_name() == "white bun" + + def test_get_price(self): + bun = Bun("red bun", 300.0) + assert bun.get_price() == 300.0 \ No newline at end of file diff --git a/tests/test_burger.py b/tests/test_burger.py new file mode 100644 index 000000000..230866bb3 --- /dev/null +++ b/tests/test_burger.py @@ -0,0 +1,63 @@ +import pytest +from unittest.mock import Mock +from praktikum.burger import Burger + +class TestBurger: + def test_set_buns(self): + burger = Burger() + mock_bun = Mock() + burger.set_buns(mock_bun) + assert burger.bun == mock_bun + + def test_add_ingredient(self): + burger = Burger() + mock_ingredient = Mock() + burger.add_ingredient(mock_ingredient) + assert len(burger.ingredients) == 1 + assert burger.ingredients[0] == mock_ingredient + + def test_remove_ingredient(self): + burger = Burger() + ing1 = Mock() + ing2 = Mock() + burger.add_ingredient(ing1) + burger.add_ingredient(ing2) + burger.remove_ingredient(0) + assert len(burger.ingredients) == 1 + assert burger.ingredients[0] == ing2 + + @pytest.mark.parametrize("index, new_index, expected_order", [ + (0, 1, [1, 0, 2]), + (2, 0, [2, 0, 1]), + (1, 1, [0, 1, 2]) + ]) + def test_move_ingredient(self, index, new_index, expected_order): + burger = Burger() + ingredients = [Mock(), Mock(), Mock()] + for ing in ingredients: + burger.add_ingredient(ing) + burger.move_ingredient(index, new_index) + moved_order = [burger.ingredients[i] for i in range(3)] + expected_list = [ingredients[i] for i in expected_order] + assert moved_order == expected_list + + def test_get_price(self, burger_with_mocks): + assert burger_with_mocks.get_price() == 50 * 2 + 30 + 20 + + def test_get_receipt(self, burger_with_mocks): + mock_bun = burger_with_mocks.bun + mock_bun.get_name.return_value = "sesame bun" + mock_ing1 = burger_with_mocks.ingredients[0] + mock_ing1.get_type.return_value = "SAUCE" + mock_ing1.get_name.return_value = "ketchup" + mock_ing2 = burger_with_mocks.ingredients[1] + mock_ing2.get_type.return_value = "FILLING" + mock_ing2.get_name.return_value = "cheese" + expected_receipt = ( + "(==== sesame bun ====)\n" + "= sauce ketchup =\n" + "= filling cheese =\n" + "(==== sesame bun ====)\n" + "Price: 130" + ) + assert burger_with_mocks.get_receipt() == expected_receipt \ No newline at end of file diff --git a/tests/test_database.py b/tests/test_database.py new file mode 100644 index 000000000..0966b1d79 --- /dev/null +++ b/tests/test_database.py @@ -0,0 +1,41 @@ +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 + assert buns[0].get_name() == "black bun" + assert buns[0].get_price() == 100 + assert buns[1].get_name() == "white bun" + assert buns[1].get_price() == 200 + assert buns[2].get_name() == "red bun" + assert buns[2].get_price() == 300 + + def test_available_ingredients(self): + db = Database() + ingredients = db.available_ingredients() + assert len(ingredients) == 6 + + # Проверка соусов + assert ingredients[0].get_type() == INGREDIENT_TYPE_SAUCE + assert ingredients[0].get_name() == "hot sauce" + assert ingredients[0].get_price() == 100 + assert ingredients[1].get_type() == INGREDIENT_TYPE_SAUCE + assert ingredients[1].get_name() == "sour cream" + assert ingredients[1].get_price() == 200 + assert ingredients[2].get_type() == INGREDIENT_TYPE_SAUCE + assert ingredients[2].get_name() == "chili sauce" + assert ingredients[2].get_price() == 300 + + # Проверка начинок + assert ingredients[3].get_type() == INGREDIENT_TYPE_FILLING + assert ingredients[3].get_name() == "cutlet" + assert ingredients[3].get_price() == 100 + assert ingredients[4].get_type() == INGREDIENT_TYPE_FILLING + assert ingredients[4].get_name() == "dinosaur" + assert ingredients[4].get_price() == 200 + assert ingredients[5].get_type() == INGREDIENT_TYPE_FILLING + assert ingredients[5].get_name() == "sausage" + assert ingredients[5].get_price() == 300 \ No newline at end of file diff --git a/tests/test_ingredient.py b/tests/test_ingredient.py new file mode 100644 index 000000000..767ccfdcd --- /dev/null +++ b/tests/test_ingredient.py @@ -0,0 +1,21 @@ +from praktikum.ingredient import Ingredient +from praktikum.ingredient_types import INGREDIENT_TYPE_SAUCE, INGREDIENT_TYPE_FILLING + +class TestIngredient: + def test_ingredient_creation(self): + ingredient = Ingredient(INGREDIENT_TYPE_SAUCE, "hot sauce", 100.0) + assert ingredient.type == INGREDIENT_TYPE_SAUCE + assert ingredient.name == "hot sauce" + assert ingredient.price == 100.0 + + def test_get_price(self): + ingredient = Ingredient(INGREDIENT_TYPE_FILLING, "cutlet", 150.0) + assert ingredient.get_price() == 150.0 + + def test_get_name(self): + ingredient = Ingredient(INGREDIENT_TYPE_SAUCE, "sour cream", 200.0) + assert ingredient.get_name() == "sour cream" + + def test_get_type(self): + ingredient = Ingredient(INGREDIENT_TYPE_FILLING, "dinosaur", 200.0) + assert ingredient.get_type() == INGREDIENT_TYPE_FILLING \ No newline at end of file