1import unittest
2from secret import validate_recipe, RuinedNikuldenDinnerError
3from unittest.mock import mock_open, patch
4
5class TestNikuldenValidator(unittest.TestCase):
6
7 def test_valid_recipe(self):
8 VALID_MOCK_FILE_CONTENT = "Днес ще ядем ли риба или рибена супа?"
9 mock_object = mock_open(read_data=VALID_MOCK_FILE_CONTENT)
10 with patch("builtins.open", mock_object):
11 lines = validate_recipe("valid_recipe.txt")
12 self.assertTrue(lines)
13
14 def test_invalid_recipe(self):
15 INVALID_MOCK_FILE_CONTENT = "Какво ще ядем?"
16 mock_object = mock_open(read_data=INVALID_MOCK_FILE_CONTENT)
17 with patch("builtins.open", mock_object):
18 lines = validate_recipe("invalid_recipe.txt")
19 self.assertFalse(lines)
20
21 def test_bad_recipe_file(self):
22 with patch("builtins.open", side_effect=OSError):
23 with self.assertRaises(RuinedNikuldenDinnerError):
24 validate_recipe("not_found_recipe.txt")
25
26 VALID_MOCK_FILE_CONTENT = "Днес ще ядем ли риба или рибена супа?"
27 mock_object = mock_open(read_data=VALID_MOCK_FILE_CONTENT)
28 with patch("builtins.open", mock_object) as mock_file:
29 mock_file.return_value.read.side_effect = IOError
30 with self.assertRaises(RuinedNikuldenDinnerError):
31 validate_recipe("not_found_recipe.txt")
32
33if __name__ == "__main__":
34 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.016s
FAILED (failures=2)
07.12.2024 14:12