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
n | 1 | import os | n | ||
2 | import unittest | 1 | import unittest | ||
n | n | 2 | from unittest.mock import mock_open, patch | ||
3 | |||||
3 | from secret import validate_recipe, RuinedNikuldenDinnerError | 4 | from secret import validate_recipe, RuinedNikuldenDinnerError | ||
4 | 5 | ||||
5 | nik_recipe = "test_file_nikulden_rada.txt" | 6 | nik_recipe = "test_file_nikulden_rada.txt" | ||
6 | 7 | ||||
7 | class TestNikuldenValidator(unittest.TestCase): | 8 | class TestNikuldenValidator(unittest.TestCase): | ||
n | n | 9 | def test_valid_recipe(self): | ||
10 | with patch("builtins.open", mock_open(read_data="Рибата е тук, Рибари бъдете нащрек! Риба винаги ще бие рибаря...")): | ||||
11 | self.assertTrue(validate_recipe(nik_recipe)) | ||||
8 | 12 | ||||
n | 9 | # def setUp(self): | n | 13 | 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)) | ||
11 | 15 | ||||
n | 12 | # def tearDown(self): | n | 16 | 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) | ||||
15 | 18 | ||||
n | 16 | # safe writing to test_files | n | 19 | 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 | |||||
50 | 21 | ||||
51 | def test_invalid_recipe(self): | 22 | def test_invalid_recipe(self): | ||
n | 52 | # self.recipe_file = open(nik_recipe, "w", encoding="utf-8") | n | 23 | 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 | |||||
62 | 28 | ||||
63 | def test_bad_recipe_file(self): | 29 | def test_bad_recipe_file(self): | ||
n | 64 | # self.recipe_file.close() | n | 30 | 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") | ||
67 | 33 | ||||
t | 68 | self.write_to_test_file("На никулден - рибарят какво ли прави?") | t | 34 | 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) | ||||
73 | 37 | ||||
74 | 38 | ||||
75 | if __name__ == "__main__": | 39 | if __name__ == "__main__": | ||
76 | unittest.main() | 40 | unittest.main() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import os | f | 1 | import os |
2 | import unittest | 2 | import unittest | ||
3 | from secret import validate_recipe, RuinedNikuldenDinnerError | 3 | from secret import validate_recipe, RuinedNikuldenDinnerError | ||
4 | 4 | ||||
5 | nik_recipe = "test_file_nikulden_rada.txt" | 5 | nik_recipe = "test_file_nikulden_rada.txt" | ||
6 | 6 | ||||
t | 7 | class TestSecret(unittest.TestCase): | t | 7 | class TestNikuldenValidator(unittest.TestCase): |
8 | 8 | ||||
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") | ||
11 | 11 | ||||
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) | ||
15 | 15 | ||||
16 | # safe writing to test_files | 16 | # 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 | ||||
28 | 28 | ||||
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)) | ||
34 | 34 | ||||
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)) | ||
39 | 39 | ||||
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)) | ||
44 | 44 | ||||
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 | ||||
50 | 50 | ||||
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)) | ||
56 | 56 | ||||
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)) | ||
61 | 61 | ||||
62 | 62 | ||||
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") | ||
67 | 67 | ||||
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) | ||
73 | 73 | ||||
74 | 74 | ||||
75 | if __name__ == "__main__": | 75 | if __name__ == "__main__": | ||
76 | unittest.main() | 76 | unittest.main() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
07.12.2024 12:47