1import unittest
2
3from secret import validate_recipe, RuinedNikuldenDinnerError
4from unittest.mock import mock_open, patch
5
6class TestNikuldenValidator(unittest.TestCase):
7 def test_valid_recipe(self):
8 nikulden_mock = "Рибата (шаран, сьонга или друга) върви с чаша ракия за добър апетит!"
9 with patch("builtins.open", mock_open(read_data=nikulden_mock)):
10 self.assertTrue(validate_recipe("nevermind.txt"))
11
12 def test_invalid_recipe(self):
13 nikulden_mock = "За съжаление рецептата не е валидна, ако само пием ракия."
14 with patch("builtins.open", mock_open(read_data=nikulden_mock)):
15 self.assertFalse(validate_recipe("nevermind.txt"))
16
17 def test_bad_recipe_file(self):
18 nikulden_errors = [IOError, OSError]
19 for error in nikulden_errors:
20 with patch("builtins.open", side_effect=error):
21 with self.assertRaises(RuinedNikuldenDinnerError):
22 validate_recipe("nevermind.txt")
23
24
25if __name__ == "__main__":
26 unittest.main()
F.F.
======================================================================
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 92, in test_naive_in_validator
self.assertFalse(result.wasSuccessful(),
AssertionError: True is not false : Expecting test_invalid_recipe to fail with an implementation missing word splits.
======================================================================
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 62, in test_no_lower_validator
self.assertFalse(result.wasSuccessful(),
AssertionError: True is not false : Expecting test_valid_recipe to fail with an implementation missing lower.
----------------------------------------------------------------------
Ran 4 tests in 0.014s
FAILED (failures=2)
f | 1 | import unittest | f | 1 | import unittest |
2 | 2 | ||||
3 | from secret import validate_recipe, RuinedNikuldenDinnerError | 3 | from secret import validate_recipe, RuinedNikuldenDinnerError | ||
4 | from unittest.mock import mock_open, patch | 4 | from unittest.mock import mock_open, patch | ||
5 | 5 | ||||
6 | class TestNikuldenValidator(unittest.TestCase): | 6 | class TestNikuldenValidator(unittest.TestCase): | ||
7 | def test_valid_recipe(self): | 7 | def test_valid_recipe(self): | ||
8 | nikulden_mock = "Рибата (шаран, сьонга или друга) върви с чаша ракия за добър апетит!" | 8 | nikulden_mock = "Рибата (шаран, сьонга или друга) върви с чаша ракия за добър апетит!" | ||
9 | with patch("builtins.open", mock_open(read_data=nikulden_mock)): | 9 | with patch("builtins.open", mock_open(read_data=nikulden_mock)): | ||
10 | self.assertTrue(validate_recipe("nevermind.txt")) | 10 | self.assertTrue(validate_recipe("nevermind.txt")) | ||
11 | 11 | ||||
12 | def test_invalid_recipe(self): | 12 | def test_invalid_recipe(self): | ||
13 | nikulden_mock = "За съжаление рецептата не е валидна, ако само пием ракия." | 13 | nikulden_mock = "За съжаление рецептата не е валидна, ако само пием ракия." | ||
14 | with patch("builtins.open", mock_open(read_data=nikulden_mock)): | 14 | with patch("builtins.open", mock_open(read_data=nikulden_mock)): | ||
15 | self.assertFalse(validate_recipe("nevermind.txt")) | 15 | self.assertFalse(validate_recipe("nevermind.txt")) | ||
16 | 16 | ||||
17 | def test_bad_recipe_file(self): | 17 | def test_bad_recipe_file(self): | ||
t | 18 | nikulden_error = IOError | t | 18 | nikulden_errors = [IOError, OSError] |
19 | for error in nikulden_errors: | ||||
19 | with patch("builtins.open", side_effect=nikulden_error): | 20 | with patch("builtins.open", side_effect=error): | ||
20 | with self.assertRaises(RuinedNikuldenDinnerError): | 21 | with self.assertRaises(RuinedNikuldenDinnerError): | ||
21 | validate_recipe("nevermind.txt") | 22 | validate_recipe("nevermind.txt") | ||
22 | 23 | ||||
23 | 24 | ||||
24 | if __name__ == "__main__": | 25 | if __name__ == "__main__": | ||
25 | unittest.main() | 26 | unittest.main() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
07.12.2024 14:40
07.12.2024 11:57