Предизвикателства > Валидатор за Никулденска рецепта > Решения > Решението на Георги Балтиев

Резултати
0 точки от тестове
0 точки от учител

0 точки общо

0 успешни теста
4 неуспешни теста
Код

 1import unittest
 2import unittest.mock as mock
 3from secret import validate_recipe, RuinedNikuldenDinnerError
 4
 5FISH_VALUES = ['риба', 'рибена', 'шаран', 'сьонга']
 6
 7MOCKED_VALID_FILE_1 = """
 8    И ловец съм, и рИба р съм! 
 9"""
10
11MOCKED_VALID_FILE_2 = """
12    рецепта за РиБенА чорба. Съставки - РИбА сЬОНГА Шаран вода
13"""
14
15MOCKED_VALID_FILE_3 = """
16    шеф иван манчев тази вечер ще ни учи как се готви рибЕна чорба с ШаРан
17"""
18
19MOCKED_INVALID_FILE_1 = """
20    За съжаление, как ще отбележи рождения си ден в 'На Кафе'
21    Гергана няма да видите сега, защото докато вървеше видеото
22    колегите ми казаха, че Гергана няма да дойде в предаването.
23"""
24
25MOCKED_INVALID_FILE_2 = """
26    Тованеерибенарецепта!
27"""
28
29MOCKED_INVALID_FILE_3 = """
30    риба, обаче с тая запетая не знам дали е валидно
31"""
32
33MOCKED_INVALID_FILE_4 = """
34    Шаранннннннннннн
35"""
36
37
38class TestNikuldenValidator(unittest.TestCase):
39
40    def test_valid_recipe(self, valid_file_name):
41        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_VALID_FILE_1)):
42            self.assertEqual(validate_recipe("mock_open_file_1"), True)
43
44        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):
45            self.assertEqual(validate_recipe("mock_open_file_2"), True)
46
47        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):
48            self.assertEqual(validate_recipe("mock_open_file_3"), True)
49
50    def test_invalid_recipe(self, invalid_file_name):
51        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):
52            self.assertEqual(validate_recipe("mock_open_file_1"), False)
53
54        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):
55            self.assertEqual(validate_recipe("mock_open_file_2"), False)
56
57        with mock.patch('builtins.open', mock_open_file_3=mock.mock_open(read_data=MOCKED_INVALID_FILE_3)):
58            self.assertEqual(validate_recipe("mock_open_file_3"), False)
59
60        with mock.patch('builtins.open', mock_open_file_4=mock.mock_open(read_data=MOCKED_INVALID_FILE_4)):
61            self.assertEqual(validate_recipe("mock_open_file_4"), False)
62
63    def test_bad_recipe_file(self, bad_file_name):
64        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open()):
65            mock.mock_open.side_effect = IOError()
66            with self.assertRaises(RuinedNikuldenDinnerError):
67                validate_recipe("mock_open_file_1")
68
69        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open()):
70            mock.mock_open.side_effect = OSError()
71            with self.assertRaises(RuinedNikuldenDinnerError):
72                validate_recipe("mock_open_file_1")

FFFF
======================================================================
FAIL: test_naive_in_validator (test.TestTestNikuldenValidator.test_naive_in_validator)
Test with implementation missing word splits.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 97, in test_naive_in_validator
mock_validate_recipe.assert_called()
File "/usr/lib/python3.12/unittest/mock.py", line 913, in assert_called
raise AssertionError(msg)
AssertionError: Expected 'validate_recipe' to have been called.

======================================================================
FAIL: test_no_error_handling_validator (test.TestTestNikuldenValidator.test_no_error_handling_validator)
Test with implementation missing error handling.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 80, in test_no_error_handling_validator
self.assertTrue(result.wasSuccessful(),
AssertionError: False is not true : Expecting test_invalid_recipe to pass with an implementation with error handling.

======================================================================
FAIL: test_no_lower_validator (test.TestTestNikuldenValidator.test_no_lower_validator)
Test with implementation missing lower.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 65, in test_no_lower_validator
self.assertTrue(result.wasSuccessful(),
AssertionError: False is not true : Expecting test_invalid_recipe to pass with an implementation missing lower.

======================================================================
FAIL: test_valid_validator (test.TestTestNikuldenValidator.test_valid_validator)
Test with a valid implementation.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 50, in test_valid_validator
self.assertTrue(result.wasSuccessful(),
AssertionError: False is not true : Expecting test_invalid_recipe to pass with valid implementation.

----------------------------------------------------------------------
Ran 4 tests in 0.004s

FAILED (failures=4)

Дискусия
Виктор Бечев
07.12.2024 13:27

Тестовете ти не минават поради много причини, първата от която е излишният параметър на тестовите функции. Когато ползваш `patch` като декоратор - трябва ти, иначе - при извикване функцията не го намира.
История

f1import unittestf1import unittest
2import unittest.mock as mock2import unittest.mock as mock
3from secret import validate_recipe, RuinedNikuldenDinnerError3from secret import validate_recipe, RuinedNikuldenDinnerError
44
5FISH_VALUES = ['риба', 'рибена', 'шаран', 'сьонга']5FISH_VALUES = ['риба', 'рибена', 'шаран', 'сьонга']
66
7MOCKED_VALID_FILE_1 = """7MOCKED_VALID_FILE_1 = """
n8    И ловец съм, и рИба р съм!n8    И ловец съм, и рИба р съм! 
9"""9"""
1010
11MOCKED_VALID_FILE_2 = """11MOCKED_VALID_FILE_2 = """
n12    рецепта за РиБенА чорба. Съставки - РИбА, сЬОНГА, водаn12    рецепта за РиБенА чорба. Съставки - РИбА сЬОНГА Шаран вода
13"""13"""
1414
15MOCKED_VALID_FILE_3 = """15MOCKED_VALID_FILE_3 = """
n16    шеф иван манчев тази вечер ще ни учи как се готви сьОНгаn16    шеф иван манчев тази вечер ще ни учи как се готви рибЕна чорба с ШаРан
17"""17"""
1818
19MOCKED_INVALID_FILE_1 = """19MOCKED_INVALID_FILE_1 = """
20    За съжаление, как ще отбележи рождения си ден в 'На Кафе'20    За съжаление, как ще отбележи рождения си ден в 'На Кафе'
21    Гергана няма да видите сега, защото докато вървеше видеото21    Гергана няма да видите сега, защото докато вървеше видеото
22    колегите ми казаха, че Гергана няма да дойде в предаването.22    колегите ми казаха, че Гергана няма да дойде в предаването.
23"""23"""
2424
25MOCKED_INVALID_FILE_2 = """25MOCKED_INVALID_FILE_2 = """
26    Тованеерибенарецепта!26    Тованеерибенарецепта!
27"""27"""
2828
29MOCKED_INVALID_FILE_3 = """29MOCKED_INVALID_FILE_3 = """
30    риба, обаче с тая запетая не знам дали е валидно30    риба, обаче с тая запетая не знам дали е валидно
nn31"""
32 
33MOCKED_INVALID_FILE_4 = """
34    Шаранннннннннннн
31"""35"""
3236
3337
34class TestNikuldenValidator(unittest.TestCase):38class TestNikuldenValidator(unittest.TestCase):
3539
36    def test_valid_recipe(self, valid_file_name):40    def test_valid_recipe(self, valid_file_name):
37        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_VALID_FILE_1)):41        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_VALID_FILE_1)):
38            self.assertEqual(validate_recipe("mock_open_file_1"), True)42            self.assertEqual(validate_recipe("mock_open_file_1"), True)
3943
40        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):44        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):
41            self.assertEqual(validate_recipe("mock_open_file_2"), True)45            self.assertEqual(validate_recipe("mock_open_file_2"), True)
4246
43        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):47        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):
44            self.assertEqual(validate_recipe("mock_open_file_3"), True)48            self.assertEqual(validate_recipe("mock_open_file_3"), True)
4549
46    def test_invalid_recipe(self, invalid_file_name):50    def test_invalid_recipe(self, invalid_file_name):
47        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):51        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):
48            self.assertEqual(validate_recipe("mock_open_file_1"), False)52            self.assertEqual(validate_recipe("mock_open_file_1"), False)
4953
50        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):54        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):
51            self.assertEqual(validate_recipe("mock_open_file_2"), False)55            self.assertEqual(validate_recipe("mock_open_file_2"), False)
5256
53        with mock.patch('builtins.open', mock_open_file_3=mock.mock_open(read_data=MOCKED_INVALID_FILE_3)):57        with mock.patch('builtins.open', mock_open_file_3=mock.mock_open(read_data=MOCKED_INVALID_FILE_3)):
54            self.assertEqual(validate_recipe("mock_open_file_3"), False)58            self.assertEqual(validate_recipe("mock_open_file_3"), False)
5559
tt60        with mock.patch('builtins.open', mock_open_file_4=mock.mock_open(read_data=MOCKED_INVALID_FILE_4)):
61            self.assertEqual(validate_recipe("mock_open_file_4"), False)
62 
56    def test_bad_recipe_file(self, bad_file_name):63    def test_bad_recipe_file(self, bad_file_name):
57        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open()):64        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open()):
58            mock.mock_open.side_effect = IOError()65            mock.mock_open.side_effect = IOError()
59            with self.assertRaises(RuinedNikuldenDinnerError):66            with self.assertRaises(RuinedNikuldenDinnerError):
60                validate_recipe("mock_open_file_1")67                validate_recipe("mock_open_file_1")
6168
62        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open()):69        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open()):
63            mock.mock_open.side_effect = OSError()70            mock.mock_open.side_effect = OSError()
64            with self.assertRaises(RuinedNikuldenDinnerError):71            with self.assertRaises(RuinedNikuldenDinnerError):
65                validate_recipe("mock_open_file_1")72                validate_recipe("mock_open_file_1")
6673
6774
6875
6976
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2import unittest.mock as mock2import unittest.mock as mock
3from secret import validate_recipe, RuinedNikuldenDinnerError3from secret import validate_recipe, RuinedNikuldenDinnerError
44
5FISH_VALUES = ['риба', 'рибена', 'шаран', 'сьонга']5FISH_VALUES = ['риба', 'рибена', 'шаран', 'сьонга']
66
7MOCKED_VALID_FILE_1 = """7MOCKED_VALID_FILE_1 = """
8    И ловец съм, и рИба р съм!8    И ловец съм, и рИба р съм!
9"""9"""
1010
11MOCKED_VALID_FILE_2 = """11MOCKED_VALID_FILE_2 = """
12    рецепта за РиБенА чорба. Съставки - РИбА, сЬОНГА, вода12    рецепта за РиБенА чорба. Съставки - РИбА, сЬОНГА, вода
13"""13"""
1414
15MOCKED_VALID_FILE_3 = """15MOCKED_VALID_FILE_3 = """
16    шеф иван манчев тази вечер ще ни учи как се готви сьОНга16    шеф иван манчев тази вечер ще ни учи как се готви сьОНга
17"""17"""
1818
19MOCKED_INVALID_FILE_1 = """19MOCKED_INVALID_FILE_1 = """
20    За съжаление, как ще отбележи рождения си ден в 'На Кафе'20    За съжаление, как ще отбележи рождения си ден в 'На Кафе'
21    Гергана няма да видите сега, защото докато вървеше видеото21    Гергана няма да видите сега, защото докато вървеше видеото
22    колегите ми казаха, че Гергана няма да дойде в предаването.22    колегите ми казаха, че Гергана няма да дойде в предаването.
23"""23"""
2424
25MOCKED_INVALID_FILE_2 = """25MOCKED_INVALID_FILE_2 = """
26    Тованеерибенарецепта!26    Тованеерибенарецепта!
27"""27"""
2828
29MOCKED_INVALID_FILE_3 = """29MOCKED_INVALID_FILE_3 = """
30    риба, обаче с тая запетая не знам дали е валидно30    риба, обаче с тая запетая не знам дали е валидно
31"""31"""
3232
3333
34class TestNikuldenValidator(unittest.TestCase):34class TestNikuldenValidator(unittest.TestCase):
3535
36    def test_valid_recipe(self, valid_file_name):36    def test_valid_recipe(self, valid_file_name):
37        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_VALID_FILE_1)):37        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_VALID_FILE_1)):
38            self.assertEqual(validate_recipe("mock_open_file_1"), True)38            self.assertEqual(validate_recipe("mock_open_file_1"), True)
3939
40        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):40        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):
41            self.assertEqual(validate_recipe("mock_open_file_2"), True)41            self.assertEqual(validate_recipe("mock_open_file_2"), True)
4242
43        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):43        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):
44            self.assertEqual(validate_recipe("mock_open_file_3"), True)44            self.assertEqual(validate_recipe("mock_open_file_3"), True)
4545
46    def test_invalid_recipe(self, invalid_file_name):46    def test_invalid_recipe(self, invalid_file_name):
47        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):47        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):
48            self.assertEqual(validate_recipe("mock_open_file_1"), False)48            self.assertEqual(validate_recipe("mock_open_file_1"), False)
4949
50        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):50        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):
51            self.assertEqual(validate_recipe("mock_open_file_2"), False)51            self.assertEqual(validate_recipe("mock_open_file_2"), False)
5252
53        with mock.patch('builtins.open', mock_open_file_3=mock.mock_open(read_data=MOCKED_INVALID_FILE_3)):53        with mock.patch('builtins.open', mock_open_file_3=mock.mock_open(read_data=MOCKED_INVALID_FILE_3)):
54            self.assertEqual(validate_recipe("mock_open_file_3"), False)54            self.assertEqual(validate_recipe("mock_open_file_3"), False)
5555
56    def test_bad_recipe_file(self, bad_file_name):56    def test_bad_recipe_file(self, bad_file_name):
n57        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):n57        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open()):
58            mock.mock_open.side_effect = IOError()58            mock.mock_open.side_effect = IOError()
59            with self.assertRaises(RuinedNikuldenDinnerError):59            with self.assertRaises(RuinedNikuldenDinnerError):
60                validate_recipe("mock_open_file_1")60                validate_recipe("mock_open_file_1")
6161
t62        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):t62        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open()):
63            mock.mock_open.side_effect = OSError()63            mock.mock_open.side_effect = OSError()
64            with self.assertRaises(RuinedNikuldenDinnerError):64            with self.assertRaises(RuinedNikuldenDinnerError):
65                validate_recipe("mock_open_file_1")65                validate_recipe("mock_open_file_1")
6666
6767
6868
6969
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2import unittest.mock as mock2import unittest.mock as mock
3from secret import validate_recipe, RuinedNikuldenDinnerError3from secret import validate_recipe, RuinedNikuldenDinnerError
44
5FISH_VALUES = ['риба', 'рибена', 'шаран', 'сьонга']5FISH_VALUES = ['риба', 'рибена', 'шаран', 'сьонга']
66
7MOCKED_VALID_FILE_1 = """7MOCKED_VALID_FILE_1 = """
8    И ловец съм, и рИба р съм!8    И ловец съм, и рИба р съм!
9"""9"""
1010
11MOCKED_VALID_FILE_2 = """11MOCKED_VALID_FILE_2 = """
12    рецепта за РиБенА чорба. Съставки - РИбА, сЬОНГА, вода12    рецепта за РиБенА чорба. Съставки - РИбА, сЬОНГА, вода
13"""13"""
1414
15MOCKED_VALID_FILE_3 = """15MOCKED_VALID_FILE_3 = """
16    шеф иван манчев тази вечер ще ни учи как се готви сьОНга16    шеф иван манчев тази вечер ще ни учи как се готви сьОНга
17"""17"""
1818
19MOCKED_INVALID_FILE_1 = """19MOCKED_INVALID_FILE_1 = """
20    За съжаление, как ще отбележи рождения си ден в 'На Кафе'20    За съжаление, как ще отбележи рождения си ден в 'На Кафе'
21    Гергана няма да видите сега, защото докато вървеше видеото21    Гергана няма да видите сега, защото докато вървеше видеото
22    колегите ми казаха, че Гергана няма да дойде в предаването.22    колегите ми казаха, че Гергана няма да дойде в предаването.
23"""23"""
2424
25MOCKED_INVALID_FILE_2 = """25MOCKED_INVALID_FILE_2 = """
26    Тованеерибенарецепта!26    Тованеерибенарецепта!
27"""27"""
2828
29MOCKED_INVALID_FILE_3 = """29MOCKED_INVALID_FILE_3 = """
30    риба, обаче с тая запетая не знам дали е валидно30    риба, обаче с тая запетая не знам дали е валидно
31"""31"""
3232
3333
34class TestNikuldenValidator(unittest.TestCase):34class TestNikuldenValidator(unittest.TestCase):
3535
36    def test_valid_recipe(self, valid_file_name):36    def test_valid_recipe(self, valid_file_name):
37        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_VALID_FILE_1)):37        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_VALID_FILE_1)):
38            self.assertEqual(validate_recipe("mock_open_file_1"), True)38            self.assertEqual(validate_recipe("mock_open_file_1"), True)
3939
40        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):40        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):
41            self.assertEqual(validate_recipe("mock_open_file_2"), True)41            self.assertEqual(validate_recipe("mock_open_file_2"), True)
4242
43        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):43        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_VALID_FILE_2)):
44            self.assertEqual(validate_recipe("mock_open_file_3"), True)44            self.assertEqual(validate_recipe("mock_open_file_3"), True)
4545
46    def test_invalid_recipe(self, invalid_file_name):46    def test_invalid_recipe(self, invalid_file_name):
47        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):47        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):
48            self.assertEqual(validate_recipe("mock_open_file_1"), False)48            self.assertEqual(validate_recipe("mock_open_file_1"), False)
4949
50        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):50        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):
51            self.assertEqual(validate_recipe("mock_open_file_2"), False)51            self.assertEqual(validate_recipe("mock_open_file_2"), False)
5252
53        with mock.patch('builtins.open', mock_open_file_3=mock.mock_open(read_data=MOCKED_INVALID_FILE_3)):53        with mock.patch('builtins.open', mock_open_file_3=mock.mock_open(read_data=MOCKED_INVALID_FILE_3)):
54            self.assertEqual(validate_recipe("mock_open_file_3"), False)54            self.assertEqual(validate_recipe("mock_open_file_3"), False)
5555
t56    def test_bad_file(self, bad_file_name):t56    def test_bad_recipe_file(self, bad_file_name):
57        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):57        with mock.patch('builtins.open', mock_open_file_1=mock.mock_open(read_data=MOCKED_INVALID_FILE_1)):
58            mock.mock_open.side_effect = IOError()58            mock.mock_open.side_effect = IOError()
59            with self.assertRaises(RuinedNikuldenDinnerError):59            with self.assertRaises(RuinedNikuldenDinnerError):
60                validate_recipe("mock_open_file_1")60                validate_recipe("mock_open_file_1")
6161
62        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):62        with mock.patch('builtins.open', mock_open_file_2=mock.mock_open(read_data=MOCKED_INVALID_FILE_2)):
63            mock.mock_open.side_effect = OSError()63            mock.mock_open.side_effect = OSError()
64            with self.assertRaises(RuinedNikuldenDinnerError):64            with self.assertRaises(RuinedNikuldenDinnerError):
65                validate_recipe("mock_open_file_1")65                validate_recipe("mock_open_file_1")
6666
6767
6868
6969
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op