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

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

1 точки общо

0 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

 1import unittest
 2from unittest.mock import mock_open, patch
 3from itertools import product
 4from secret import validate_recipe, RuinedNikuldenDinnerError
 5
 6
 7def permute(word):
 8    """Permute all case variations of a word."""
 9    letter_options = [(char.lower(), char.upper()) for char in word]
10    permutations = list(map(''.join, product(*letter_options)))
11    return permutations
12
13
14class TestNikuldenValidator(unittest.TestCase):
15    def setUp(self):
16        """Set up valid and invalid mock recipes."""
17        keywords = ['риба', 'рибена', 'шаран', 'сьонга']
18
19        self.mock_valid_recipes = []
20        for keyword in keywords:
21            self.mock_valid_recipes.extend(permute(keyword))
22
23        self.mock_invalid_recipes = list(
24            map(lambda x: 'a' + x + 'a', self.mock_valid_recipes)
25        )
26
27    def test_valid_recipe(self):
28        for mock_recipe in self.mock_valid_recipes:
29            with patch("builtins.open", mock_open(read_data=mock_recipe)):
30                self.assertTrue(validate_recipe("hacknah_vi.txt"))
31
32    def test_invalid_recipe(self):
33        mock_invalid_recipe = (
34            """Всеки ми казва – спри да я чакаш
35            Ако обича те, ще бъде тук
36            Месеци минаха, даже година
37            Може би има си друг
38            Всеки ми казва – не настоявай
39            И да умираш, дишай напук
40            Живея и дишам с тази надежда
41            Някога пак да си тук"""
42        )
43
44        self.mock_invalid_recipes.append(mock_invalid_recipe) #нямаше как
45
46        for mock_recipe in self.mock_invalid_recipes:
47            with patch("builtins.open", mock_open(read_data=mock_recipe)):
48                self.assertFalse(validate_recipe("hacknah_vi.txt"))
49
50    def test_bad_recipe_file(self):
51        with patch("builtins.open", side_effect=OSError("File error!")):
52            with self.assertRaises(RuinedNikuldenDinnerError):
53                validate_recipe("pak_vi_hacknah.txt")
54
55        with patch("builtins.open", side_effect=IOError("File error!")):
56            with self.assertRaises(RuinedNikuldenDinnerError):
57                validate_recipe("pak_vi_hacknah.txt")
58
59
60if __name__ == "__main__":
61    unittest.main()

Timed out.

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

Пуснах ти тестовете ръчно, защото имаме сложен timeout за да не ни хаквате. Минават всичките.
История

f1import unittestf1import unittest
2from unittest.mock import mock_open, patch2from unittest.mock import mock_open, patch
3from itertools import product3from itertools import product
4from secret import validate_recipe, RuinedNikuldenDinnerError4from secret import validate_recipe, RuinedNikuldenDinnerError
55
nn6 
6def permute(word):7def permute(word):
7    """Permute all case variations of a word."""8    """Permute all case variations of a word."""
n8    n
9    letter_options = [(char.lower(), char.upper()) for char in word]9    letter_options = [(char.lower(), char.upper()) for char in word]
10    permutations = list(map(''.join, product(*letter_options)))10    permutations = list(map(''.join, product(*letter_options)))
11    return permutations11    return permutations
1212
nn13 
13class TestNikuldenValidator(unittest.TestCase):14class TestNikuldenValidator(unittest.TestCase):
n14    n
15    def setUp(self):15    def setUp(self):
nn16        """Set up valid and invalid mock recipes."""
16        keywords = ['риба', 'рибена', 'шаран', 'сьонга']17        keywords = ['риба', 'рибена', 'шаран', 'сьонга']
n17        n18 
18        self.mock_valid_recipes = []19        self.mock_valid_recipes = []
19        for keyword in keywords:20        for keyword in keywords:
n20             self.mock_valid_recipes.extend(permute(keyword))n21            self.mock_valid_recipes.extend(permute(keyword))
21        
22        self.mock_invalid_recipes = list(map(lambda x: 'a' + x + 'a',  self.mock_valid_recipes))
2322
nn23        self.mock_invalid_recipes = list(
24            map(lambda x: 'a' + x + 'a', self.mock_valid_recipes)
25        )
2426
25    def test_valid_recipe(self):27    def test_valid_recipe(self):
26        for mock_recipe in self.mock_valid_recipes:28        for mock_recipe in self.mock_valid_recipes:
27            with patch("builtins.open", mock_open(read_data=mock_recipe)):29            with patch("builtins.open", mock_open(read_data=mock_recipe)):
28                self.assertTrue(validate_recipe("hacknah_vi.txt"))30                self.assertTrue(validate_recipe("hacknah_vi.txt"))
2931
30    def test_invalid_recipe(self):32    def test_invalid_recipe(self):
31        mock_invalid_recipe = (33        mock_invalid_recipe = (
32            """Всеки ми казва – спри да я чакаш34            """Всеки ми казва – спри да я чакаш
33            Ако обича те, ще бъде тук35            Ако обича те, ще бъде тук
34            Месеци минаха, даже година36            Месеци минаха, даже година
35            Може би има си друг37            Може би има си друг
36            Всеки ми казва – не настоявай38            Всеки ми казва – не настоявай
37            И да умираш, дишай напук39            И да умираш, дишай напук
38            Живея и дишам с тази надежда40            Живея и дишам с тази надежда
39            Някога пак да си тук"""41            Някога пак да си тук"""
40        )42        )
n41        n43 
42        self.mock_invalid_recipes.append(mock_invalid_recipe) #нямаше как44        self.mock_invalid_recipes.append(mock_invalid_recipe) #нямаше как
nn45 
43        for mock_recipe in self.mock_invalid_recipes:46        for mock_recipe in self.mock_invalid_recipes:
44            with patch("builtins.open", mock_open(read_data=mock_recipe)):47            with patch("builtins.open", mock_open(read_data=mock_recipe)):
45                self.assertFalse(validate_recipe("hacknah_vi.txt"))48                self.assertFalse(validate_recipe("hacknah_vi.txt"))
n46                n49 
47        
48    def test_bad_recipe_file(self):50    def test_bad_recipe_file(self):
n49        with patch("builtins.open", side_effect=OSError("Тук ви шаня, пазя си шарана!")):n51        with patch("builtins.open", side_effect=OSError("File error!")):
50            with self.assertRaises(RuinedNikuldenDinnerError):52            with self.assertRaises(RuinedNikuldenDinnerError):
51                validate_recipe("pak_vi_hacknah.txt")53                validate_recipe("pak_vi_hacknah.txt")
t52                t54 
53        with patch("builtins.open", side_effect=IOError("Тук ви шаня, пазя си шарана!")):55        with patch("builtins.open", side_effect=IOError("File error!")):
54            with self.assertRaises(RuinedNikuldenDinnerError):56            with self.assertRaises(RuinedNikuldenDinnerError):
55                validate_recipe("pak_vi_hacknah.txt")57                validate_recipe("pak_vi_hacknah.txt")
5658
5759
58if __name__ == "__main__":60if __name__ == "__main__":
59    unittest.main()61    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2from unittest.mock import mock_open, patch2from unittest.mock import mock_open, patch
3from itertools import product3from itertools import product
4from secret import validate_recipe, RuinedNikuldenDinnerError4from secret import validate_recipe, RuinedNikuldenDinnerError
55
6def permute(word):6def permute(word):
7    """Permute all case variations of a word."""7    """Permute all case variations of a word."""
8    8    
9    letter_options = [(char.lower(), char.upper()) for char in word]9    letter_options = [(char.lower(), char.upper()) for char in word]
10    permutations = list(map(''.join, product(*letter_options)))10    permutations = list(map(''.join, product(*letter_options)))
11    return permutations11    return permutations
1212
13class TestNikuldenValidator(unittest.TestCase):13class TestNikuldenValidator(unittest.TestCase):
nn14    
15    def setUp(self):
16        keywords = ['риба', 'рибена', 'шаран', 'сьонга']
17        
18        self.mock_valid_recipes = []
19        for keyword in keywords:
20             self.mock_valid_recipes.extend(permute(keyword))
21        
22        self.mock_invalid_recipes = list(map(lambda x: 'a' + x + 'a',  self.mock_valid_recipes))
23 
24 
14    def test_valid_recipe(self):25    def test_valid_recipe(self):
n15        keywords = ['риба', 'рибена', 'шаран', 'сьонга']n
16        mock_valid_recipes = []
17        for keyword in keywords:
18            mock_valid_recipes.extend(permute(keyword))
19 
20        for mock_recipe in mock_valid_recipes:26        for mock_recipe in self.mock_valid_recipes:
21            with patch("builtins.open", mock_open(read_data=mock_recipe)):27            with patch("builtins.open", mock_open(read_data=mock_recipe)):
22                self.assertTrue(validate_recipe("hacknah_vi.txt"))28                self.assertTrue(validate_recipe("hacknah_vi.txt"))
2329
24    def test_invalid_recipe(self):30    def test_invalid_recipe(self):
25        mock_invalid_recipe = (31        mock_invalid_recipe = (
26            """Всеки ми казва – спри да я чакаш32            """Всеки ми казва – спри да я чакаш
27            Ако обича те, ще бъде тук33            Ако обича те, ще бъде тук
28            Месеци минаха, даже година34            Месеци минаха, даже година
29            Може би има си друг35            Може би има си друг
30            Всеки ми казва – не настоявай36            Всеки ми казва – не настоявай
31            И да умираш, дишай напук37            И да умираш, дишай напук
32            Живея и дишам с тази надежда38            Живея и дишам с тази надежда
33            Някога пак да си тук"""39            Някога пак да си тук"""
34        )40        )
n35 n41        
42        self.mock_invalid_recipes.append(mock_invalid_recipe) #нямаше как
43        for mock_recipe in self.mock_invalid_recipes:
36        with patch("builtins.open", mock_open(read_data=mock_invalid_recipe)):44            with patch("builtins.open", mock_open(read_data=mock_recipe)):
37            self.assertFalse(validate_recipe("pak_hacknah_vi.txt"))45                self.assertFalse(validate_recipe("hacknah_vi.txt"))
38 46                
47        
39    def test_bad_recipe_file(self):48    def test_bad_recipe_file(self):
40        with patch("builtins.open", side_effect=OSError("Тук ви шаня, пазя си шарана!")):49        with patch("builtins.open", side_effect=OSError("Тук ви шаня, пазя си шарана!")):
41            with self.assertRaises(RuinedNikuldenDinnerError):50            with self.assertRaises(RuinedNikuldenDinnerError):
n42                validate_recipe("pak_pak_vi_hacknah.txt")n51                validate_recipe("pak_vi_hacknah.txt")
43                52                
44        with patch("builtins.open", side_effect=IOError("Тук ви шаня, пазя си шарана!")):53        with patch("builtins.open", side_effect=IOError("Тук ви шаня, пазя си шарана!")):
45            with self.assertRaises(RuinedNikuldenDinnerError):54            with self.assertRaises(RuinedNikuldenDinnerError):
t46                validate_recipe("pak_pak_vi_hacknah.txt")t55                validate_recipe("pak_vi_hacknah.txt")
4756
4857
49if __name__ == "__main__":58if __name__ == "__main__":
50    unittest.main()59    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2from unittest.mock import mock_open, patch2from unittest.mock import mock_open, patch
3from itertools import product3from itertools import product
4from secret import validate_recipe, RuinedNikuldenDinnerError4from secret import validate_recipe, RuinedNikuldenDinnerError
55
6def permute(word):6def permute(word):
7    """Permute all case variations of a word."""7    """Permute all case variations of a word."""
8    8    
9    letter_options = [(char.lower(), char.upper()) for char in word]9    letter_options = [(char.lower(), char.upper()) for char in word]
10    permutations = list(map(''.join, product(*letter_options)))10    permutations = list(map(''.join, product(*letter_options)))
11    return permutations11    return permutations
1212
13class TestNikuldenValidator(unittest.TestCase):13class TestNikuldenValidator(unittest.TestCase):
14    def test_valid_recipe(self):14    def test_valid_recipe(self):
t15        # mock_valid_recipes = [t
16        #     "За една сьоНГА продадох си душата,",
17        #     "за РИбеНа глава загубих свободата.",
18        #     "Сляп в самота в плен на шаран",
19        #     "да съм риба на света.",
20        # ]    
21        
22        keywords = ['риба', 'рибена', 'шаран', 'сьонга']15        keywords = ['риба', 'рибена', 'шаран', 'сьонга']
23        mock_valid_recipes = []16        mock_valid_recipes = []
24        for keyword in keywords:17        for keyword in keywords:
25            mock_valid_recipes.extend(permute(keyword))18            mock_valid_recipes.extend(permute(keyword))
2619
27        for mock_recipe in mock_valid_recipes:20        for mock_recipe in mock_valid_recipes:
28            with patch("builtins.open", mock_open(read_data=mock_recipe)):21            with patch("builtins.open", mock_open(read_data=mock_recipe)):
29                self.assertTrue(validate_recipe("hacknah_vi.txt"))22                self.assertTrue(validate_recipe("hacknah_vi.txt"))
3023
31    def test_invalid_recipe(self):24    def test_invalid_recipe(self):
32        mock_invalid_recipe = (25        mock_invalid_recipe = (
33            """Всеки ми казва – спри да я чакаш26            """Всеки ми казва – спри да я чакаш
34            Ако обича те, ще бъде тук27            Ако обича те, ще бъде тук
35            Месеци минаха, даже година28            Месеци минаха, даже година
36            Може би има си друг29            Може би има си друг
37            Всеки ми казва – не настоявай30            Всеки ми казва – не настоявай
38            И да умираш, дишай напук31            И да умираш, дишай напук
39            Живея и дишам с тази надежда32            Живея и дишам с тази надежда
40            Някога пак да си тук"""33            Някога пак да си тук"""
41        )34        )
4235
43        with patch("builtins.open", mock_open(read_data=mock_invalid_recipe)):36        with patch("builtins.open", mock_open(read_data=mock_invalid_recipe)):
44            self.assertFalse(validate_recipe("pak_hacknah_vi.txt"))37            self.assertFalse(validate_recipe("pak_hacknah_vi.txt"))
4538
46    def test_bad_recipe_file(self):39    def test_bad_recipe_file(self):
47        with patch("builtins.open", side_effect=OSError("Тук ви шаня, пазя си шарана!")):40        with patch("builtins.open", side_effect=OSError("Тук ви шаня, пазя си шарана!")):
48            with self.assertRaises(RuinedNikuldenDinnerError):41            with self.assertRaises(RuinedNikuldenDinnerError):
49                validate_recipe("pak_pak_vi_hacknah.txt")42                validate_recipe("pak_pak_vi_hacknah.txt")
50                43                
51        with patch("builtins.open", side_effect=IOError("Тук ви шаня, пазя си шарана!")):44        with patch("builtins.open", side_effect=IOError("Тук ви шаня, пазя си шарана!")):
52            with self.assertRaises(RuinedNikuldenDinnerError):45            with self.assertRaises(RuinedNikuldenDinnerError):
53                validate_recipe("pak_pak_vi_hacknah.txt")46                validate_recipe("pak_pak_vi_hacknah.txt")
5447
5548
56if __name__ == "__main__":49if __name__ == "__main__":
57    unittest.main()50    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2from unittest.mock import mock_open, patch2from unittest.mock import mock_open, patch
nn3from itertools import product
3from secret import validate_recipe, RuinedNikuldenDinnerError4from secret import validate_recipe, RuinedNikuldenDinnerError
45
nn6def permute(word):
7    """Permute all case variations of a word."""
8    
9    letter_options = [(char.lower(), char.upper()) for char in word]
10    permutations = list(map(''.join, product(*letter_options)))
11    return permutations
512
6class TestNikuldenValidator(unittest.TestCase):13class TestNikuldenValidator(unittest.TestCase):
7    def test_valid_recipe(self):14    def test_valid_recipe(self):
nn15        # mock_valid_recipes = [
16        #     "За една сьоНГА продадох си душата,",
17        #     "за РИбеНа глава загубих свободата.",
18        #     "Сляп в самота в плен на шаран",
19        #     "да съм риба на света.",
20        # ]    
21        
22        keywords = ['риба', 'рибена', 'шаран', 'сьонга']
8        mock_valid_recipes = [23        mock_valid_recipes = []
9            "За една сьоНГА продадох си душата,",24        for keyword in keywords:
10            "за РИбеНа глава загубих свободата.",25            mock_valid_recipes.extend(permute(keyword))
11            "Сляп в самота в плен на шаран",
12            "да съм риба на света.",
13        ]
1426
15        for mock_recipe in mock_valid_recipes:27        for mock_recipe in mock_valid_recipes:
16            with patch("builtins.open", mock_open(read_data=mock_recipe)):28            with patch("builtins.open", mock_open(read_data=mock_recipe)):
17                self.assertTrue(validate_recipe("hacknah_vi.txt"))29                self.assertTrue(validate_recipe("hacknah_vi.txt"))
1830
19    def test_invalid_recipe(self):31    def test_invalid_recipe(self):
20        mock_invalid_recipe = (32        mock_invalid_recipe = (
21            """Всеки ми казва – спри да я чакаш33            """Всеки ми казва – спри да я чакаш
22            Ако обича те, ще бъде тук34            Ако обича те, ще бъде тук
23            Месеци минаха, даже година35            Месеци минаха, даже година
24            Може би има си друг36            Може би има си друг
25            Всеки ми казва – не настоявай37            Всеки ми казва – не настоявай
26            И да умираш, дишай напук38            И да умираш, дишай напук
27            Живея и дишам с тази надежда39            Живея и дишам с тази надежда
28            Някога пак да си тук"""40            Някога пак да си тук"""
29        )41        )
3042
31        with patch("builtins.open", mock_open(read_data=mock_invalid_recipe)):43        with patch("builtins.open", mock_open(read_data=mock_invalid_recipe)):
32            self.assertFalse(validate_recipe("pak_hacknah_vi.txt"))44            self.assertFalse(validate_recipe("pak_hacknah_vi.txt"))
3345
34    def test_bad_recipe_file(self):46    def test_bad_recipe_file(self):
35        with patch("builtins.open", side_effect=OSError("Тук ви шаня, пазя си шарана!")):47        with patch("builtins.open", side_effect=OSError("Тук ви шаня, пазя си шарана!")):
36            with self.assertRaises(RuinedNikuldenDinnerError):48            with self.assertRaises(RuinedNikuldenDinnerError):
37                validate_recipe("pak_pak_vi_hacknah.txt")49                validate_recipe("pak_pak_vi_hacknah.txt")
38                50                
39        with patch("builtins.open", side_effect=IOError("Тук ви шаня, пазя си шарана!")):51        with patch("builtins.open", side_effect=IOError("Тук ви шаня, пазя си шарана!")):
40            with self.assertRaises(RuinedNikuldenDinnerError):52            with self.assertRaises(RuinedNikuldenDinnerError):
41                validate_recipe("pak_pak_vi_hacknah.txt")53                validate_recipe("pak_pak_vi_hacknah.txt")
4254
tt55 
43if __name__ == "__main__":56if __name__ == "__main__":
44    unittest.main()57    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2from unittest.mock import mock_open, patch2from unittest.mock import mock_open, patch
3from secret import validate_recipe, RuinedNikuldenDinnerError3from secret import validate_recipe, RuinedNikuldenDinnerError
44
55
6class TestNikuldenValidator(unittest.TestCase):6class TestNikuldenValidator(unittest.TestCase):
7    def test_valid_recipe(self):7    def test_valid_recipe(self):
8        mock_valid_recipes = [8        mock_valid_recipes = [
9            "За една сьоНГА продадох си душата,",9            "За една сьоНГА продадох си душата,",
10            "за РИбеНа глава загубих свободата.",10            "за РИбеНа глава загубих свободата.",
11            "Сляп в самота в плен на шаран",11            "Сляп в самота в плен на шаран",
12            "да съм риба на света.",12            "да съм риба на света.",
13        ]13        ]
1414
15        for mock_recipe in mock_valid_recipes:15        for mock_recipe in mock_valid_recipes:
16            with patch("builtins.open", mock_open(read_data=mock_recipe)):16            with patch("builtins.open", mock_open(read_data=mock_recipe)):
17                self.assertTrue(validate_recipe("hacknah_vi.txt"))17                self.assertTrue(validate_recipe("hacknah_vi.txt"))
1818
19    def test_invalid_recipe(self):19    def test_invalid_recipe(self):
20        mock_invalid_recipe = (20        mock_invalid_recipe = (
21            """Всеки ми казва – спри да я чакаш21            """Всеки ми казва – спри да я чакаш
22            Ако обича те, ще бъде тук22            Ако обича те, ще бъде тук
23            Месеци минаха, даже година23            Месеци минаха, даже година
24            Може би има си друг24            Може би има си друг
25            Всеки ми казва – не настоявай25            Всеки ми казва – не настоявай
26            И да умираш, дишай напук26            И да умираш, дишай напук
27            Живея и дишам с тази надежда27            Живея и дишам с тази надежда
28            Някога пак да си тук"""28            Някога пак да си тук"""
29        )29        )
3030
31        with patch("builtins.open", mock_open(read_data=mock_invalid_recipe)):31        with patch("builtins.open", mock_open(read_data=mock_invalid_recipe)):
32            self.assertFalse(validate_recipe("pak_hacknah_vi.txt"))32            self.assertFalse(validate_recipe("pak_hacknah_vi.txt"))
3333
34    def test_bad_recipe_file(self):34    def test_bad_recipe_file(self):
35        with patch("builtins.open", side_effect=OSError("Тук ви шаня, пазя си шарана!")):35        with patch("builtins.open", side_effect=OSError("Тук ви шаня, пазя си шарана!")):
36            with self.assertRaises(RuinedNikuldenDinnerError):36            with self.assertRaises(RuinedNikuldenDinnerError):
37                validate_recipe("pak_pak_vi_hacknah.txt")37                validate_recipe("pak_pak_vi_hacknah.txt")
tt38                
39        with patch("builtins.open", side_effect=IOError("Тук ви шаня, пазя си шарана!")):
40            with self.assertRaises(RuinedNikuldenDinnerError):
41                validate_recipe("pak_pak_vi_hacknah.txt")
3842
39if __name__ == "__main__":43if __name__ == "__main__":
40    unittest.main()44    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op