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

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

2 точки общо

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

 1import unittest
 2from unittest.mock import mock_open, patch
 3
 4from secret import validate_recipe, RuinedNikuldenDinnerError
 5
 6nik_recipe = "test_file_nikulden_rada.txt"
 7
 8class TestNikuldenValidator(unittest.TestCase):
 9    def test_valid_recipe(self):
10        with patch("builtins.open", mock_open(read_data="Рибата е тук, Рибари бъдете нащрек! Риба винаги ще бие рибаря...")): 
11            self.assertTrue(validate_recipe(nik_recipe))
12
13        with patch("builtins.open", mock_open(read_data="Рибена чорба, как да устоиш!")): 
14            self.assertTrue(validate_recipe(nik_recipe))
15
16        with patch("builtins.open", mock_open(read_data="На никулден - шаран???")): 
17            self.assertTrue(validate_recipe(nik_recipe))
18
19        with patch("builtins.open", mock_open(read_data="една Сьомга, или пък СьОНГА защото няма ер голямо, да видим и това.")): 
20            self.assertTrue(validate_recipe(nik_recipe))
21
22    def test_invalid_recipe(self):
23        with patch("builtins.open", mock_open(read_data="На никулден - рибарят какво ли прави?")): 
24            self.assertFalse(validate_recipe(nik_recipe))
25    
26        with patch("builtins.open", mock_open(read_data="да видим и дали има сьомга - нищо че не е необходима.")): 
27            self.assertFalse(validate_recipe(nik_recipe))
28
29    def test_bad_recipe_file(self):
30        with patch("builtins.open", side_effect=IOError):
31            with self.assertRaises(RuinedNikuldenDinnerError):
32                validate_recipe("recepta_za_velikdensko_agne.txt")
33
34        with patch("builtins.open", side_effect=OSError):
35            with self.assertRaises(RuinedNikuldenDinnerError):
36                validate_recipe(nik_recipe)
37
38
39if __name__ == "__main__":
40    unittest.main()

....
----------------------------------------------------------------------
Ran 4 tests in 0.067s

OK

Дискусия
История

n1import osn
2import unittest1import unittest
nn2from unittest.mock import mock_open, patch
3 
3from secret import validate_recipe, RuinedNikuldenDinnerError4from secret import validate_recipe, RuinedNikuldenDinnerError
45
5nik_recipe = "test_file_nikulden_rada.txt"6nik_recipe = "test_file_nikulden_rada.txt"
67
7class TestNikuldenValidator(unittest.TestCase):8class TestNikuldenValidator(unittest.TestCase):
nn9    def test_valid_recipe(self):
10        with patch("builtins.open", mock_open(read_data="Рибата е тук, Рибари бъдете нащрек! Риба винаги ще бие рибаря...")): 
11            self.assertTrue(validate_recipe(nik_recipe))
812
n9    # def setUp(self):n13        with patch("builtins.open", mock_open(read_data="Рибена чорба, как да устоиш!")): 
10    #     self.recipe_file = open(nik_recipe, "w", encoding="utf-8")14            self.assertTrue(validate_recipe(nik_recipe))
1115
n12    # def tearDown(self):n16        with patch("builtins.open", mock_open(read_data="На никулден - шаран???")): 
13    #     os.chmod(nik_recipe, 0o777)17            self.assertTrue(validate_recipe(nik_recipe))
14    #     os.remove(nik_recipe)
1518
n16    # safe writing to test_filesn19        with patch("builtins.open", mock_open(read_data="една Сьомга, или пък СьОНГА защото няма ер голямо, да видим и това.")): 
17    def write_to_test_file(self, recipe):
18        try:
19            with open(nik_recipe, 'w', encoding='utf-8') as file:
20                file.write(recipe)
21        except IOError:
22            print("can't write to file")
23        except OSError:
24            print("don't have permissions to write in file")
25        except Exception:
26            print("couldn't write to file due to an error")
27        
28 
29    def test_valid_recipe(self):
30        self.write_to_test_file("Рибата е тук, Рибари бъдете нащрек! Риба винаги ще бие рибаря...")
31        # self.recipe_file.write("Рибата е тук, Рибари бъдете нащрек! Риба винаги ще бие рибаря...")
32        # self.recipe_file.close()
33        self.assertTrue(validate_recipe(nik_recipe))20            self.assertTrue(validate_recipe(nik_recipe))
34 
35        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")
36        self.write_to_test_file("Рибена чорба, как да устоиш!")
37        # self.recipe_file.close()
38        self.assertTrue(validate_recipe(nik_recipe))
39 
40        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")
41        self.write_to_test_file("На никулден - шаран???")
42        # self.recipe_file.close()
43        self.assertTrue(validate_recipe(nik_recipe))
44 
45        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")
46        self.write_to_test_file("една Сьомга, или пък СьОНГА защото няма ер голямо, да видим и това.")
47        # self.recipe_file.close()
48        self.assertTrue(validate_recipe(nik_recipe))
49        
5021
51    def test_invalid_recipe(self):22    def test_invalid_recipe(self):
n52        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")n23        with patch("builtins.open", mock_open(read_data="На никулден - рибарят какво ли прави?")): 
53        self.write_to_test_file("На никулден - рибарят какво ли прави?")
54        # self.recipe_file.close()
55        self.assertFalse(validate_recipe(nik_recipe))24            self.assertFalse(validate_recipe(nik_recipe))
56 25    
57        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")26        with patch("builtins.open", mock_open(read_data="да видим и дали има сьомга - нищо че не е необходима.")): 
58        self.write_to_test_file("да видим и дали има сьомга - нищо че не е необходима")
59        # self.recipe_file.close()
60        self.assertTrue(validate_recipe(nik_recipe))27            self.assertFalse(validate_recipe(nik_recipe))
61 
6228
63    def test_bad_recipe_file(self):29    def test_bad_recipe_file(self):
n64        # self.recipe_file.close()n30        with patch("builtins.open", side_effect=IOError):
65        with self.assertRaises(RuinedNikuldenDinnerError):31            with self.assertRaises(RuinedNikuldenDinnerError):
66            validate_recipe("recepta_za_velikdensko_agne.txt")32                validate_recipe("recepta_za_velikdensko_agne.txt")
6733
t68        self.write_to_test_file("На никулден - рибарят какво ли прави?")t34        with patch("builtins.open", side_effect=OSError):
69        os.chmod(nik_recipe, 0o000)
70        with self.assertRaises(RuinedNikuldenDinnerError):35            with self.assertRaises(RuinedNikuldenDinnerError):
71            validate_recipe(nik_recipe)36                validate_recipe(nik_recipe)
72        os.chmod(nik_recipe, 0o777)
7337
7438
75if __name__ == "__main__":39if __name__ == "__main__":
76    unittest.main()40    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import osf1import os
2import unittest2import unittest
3from secret import validate_recipe, RuinedNikuldenDinnerError3from secret import validate_recipe, RuinedNikuldenDinnerError
44
5nik_recipe = "test_file_nikulden_rada.txt"5nik_recipe = "test_file_nikulden_rada.txt"
66
t7class TestSecret(unittest.TestCase):t7class TestNikuldenValidator(unittest.TestCase):
88
9    # def setUp(self):9    # def setUp(self):
10    #     self.recipe_file = open(nik_recipe, "w", encoding="utf-8")10    #     self.recipe_file = open(nik_recipe, "w", encoding="utf-8")
1111
12    # def tearDown(self):12    # def tearDown(self):
13    #     os.chmod(nik_recipe, 0o777)13    #     os.chmod(nik_recipe, 0o777)
14    #     os.remove(nik_recipe)14    #     os.remove(nik_recipe)
1515
16    # safe writing to test_files16    # safe writing to test_files
17    def write_to_test_file(self, recipe):17    def write_to_test_file(self, recipe):
18        try:18        try:
19            with open(nik_recipe, 'w', encoding='utf-8') as file:19            with open(nik_recipe, 'w', encoding='utf-8') as file:
20                file.write(recipe)20                file.write(recipe)
21        except IOError:21        except IOError:
22            print("can't write to file")22            print("can't write to file")
23        except OSError:23        except OSError:
24            print("don't have permissions to write in file")24            print("don't have permissions to write in file")
25        except Exception:25        except Exception:
26            print("couldn't write to file due to an error")26            print("couldn't write to file due to an error")
27        27        
2828
29    def test_valid_recipe(self):29    def test_valid_recipe(self):
30        self.write_to_test_file("Рибата е тук, Рибари бъдете нащрек! Риба винаги ще бие рибаря...")30        self.write_to_test_file("Рибата е тук, Рибари бъдете нащрек! Риба винаги ще бие рибаря...")
31        # self.recipe_file.write("Рибата е тук, Рибари бъдете нащрек! Риба винаги ще бие рибаря...")31        # self.recipe_file.write("Рибата е тук, Рибари бъдете нащрек! Риба винаги ще бие рибаря...")
32        # self.recipe_file.close()32        # self.recipe_file.close()
33        self.assertTrue(validate_recipe(nik_recipe))33        self.assertTrue(validate_recipe(nik_recipe))
3434
35        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")35        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")
36        self.write_to_test_file("Рибена чорба, как да устоиш!")36        self.write_to_test_file("Рибена чорба, как да устоиш!")
37        # self.recipe_file.close()37        # self.recipe_file.close()
38        self.assertTrue(validate_recipe(nik_recipe))38        self.assertTrue(validate_recipe(nik_recipe))
3939
40        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")40        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")
41        self.write_to_test_file("На никулден - шаран???")41        self.write_to_test_file("На никулден - шаран???")
42        # self.recipe_file.close()42        # self.recipe_file.close()
43        self.assertTrue(validate_recipe(nik_recipe))43        self.assertTrue(validate_recipe(nik_recipe))
4444
45        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")45        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")
46        self.write_to_test_file("една Сьомга, или пък СьОНГА защото няма ер голямо, да видим и това.")46        self.write_to_test_file("една Сьомга, или пък СьОНГА защото няма ер голямо, да видим и това.")
47        # self.recipe_file.close()47        # self.recipe_file.close()
48        self.assertTrue(validate_recipe(nik_recipe))48        self.assertTrue(validate_recipe(nik_recipe))
49        49        
5050
51    def test_invalid_recipe(self):51    def test_invalid_recipe(self):
52        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")52        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")
53        self.write_to_test_file("На никулден - рибарят какво ли прави?")53        self.write_to_test_file("На никулден - рибарят какво ли прави?")
54        # self.recipe_file.close()54        # self.recipe_file.close()
55        self.assertFalse(validate_recipe(nik_recipe))55        self.assertFalse(validate_recipe(nik_recipe))
5656
57        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")57        # self.recipe_file = open(nik_recipe, "w", encoding="utf-8")
58        self.write_to_test_file("да видим и дали има сьомга - нищо че не е необходима")58        self.write_to_test_file("да видим и дали има сьомга - нищо че не е необходима")
59        # self.recipe_file.close()59        # self.recipe_file.close()
60        self.assertTrue(validate_recipe(nik_recipe))60        self.assertTrue(validate_recipe(nik_recipe))
6161
6262
63    def test_bad_recipe_file(self):63    def test_bad_recipe_file(self):
64        # self.recipe_file.close()64        # self.recipe_file.close()
65        with self.assertRaises(RuinedNikuldenDinnerError):65        with self.assertRaises(RuinedNikuldenDinnerError):
66            validate_recipe("recepta_za_velikdensko_agne.txt")66            validate_recipe("recepta_za_velikdensko_agne.txt")
6767
68        self.write_to_test_file("На никулден - рибарят какво ли прави?")68        self.write_to_test_file("На никулден - рибарят какво ли прави?")
69        os.chmod(nik_recipe, 0o000)69        os.chmod(nik_recipe, 0o000)
70        with self.assertRaises(RuinedNikuldenDinnerError):70        with self.assertRaises(RuinedNikuldenDinnerError):
71            validate_recipe(nik_recipe)71            validate_recipe(nik_recipe)
72        os.chmod(nik_recipe, 0o777)72        os.chmod(nik_recipe, 0o777)
7373
7474
75if __name__ == "__main__":75if __name__ == "__main__":
76    unittest.main()76    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op