1import unittest
2from secret import validate_recipe, RuinedNikuldenDinnerError
3from unittest.mock import patch, mock_open
4
5class TestNikuldenValidator(unittest.TestCase):
6 def test_valid_recipe(self):
7 valid_file = "доста доста риба лала Рибена глава съм аз" # fake file content
8 with patch("builtins.open", mock_open(read_data = valid_file)): # immitates open built in func and gives my fake file content
9 self.assertTrue(validate_recipe("someFile.txt"))
10
11 def test_invalid_recipe(self):
12 invalid_file = "рецепта с рЙба няма нищо гладни сме малко соона"
13 with patch("builtins.open", mock_open(read_data = invalid_file)):
14 self.assertFalse(validate_recipe("somefile.txt"))
15
16 def test_bad_recipe_file(self):
17 errors = [OSError, IOError]
18 for error in errors:
19 with patch("builtins.open", side_effect = error):
20 with self.assertRaises(RuinedNikuldenDinnerError):
21 validate_recipe("file.txt")
22
23if __name__ == '__main__':
24 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 | from secret import validate_recipe, RuinedNikuldenDinnerError | 2 | from secret import validate_recipe, RuinedNikuldenDinnerError | ||
n | 3 | from unittest.mock import patch, Mock | n | 3 | from unittest.mock import patch, mock_open |
4 | 4 | ||||
5 | class TestNikuldenValidator(unittest.TestCase): | 5 | class TestNikuldenValidator(unittest.TestCase): | ||
6 | def test_valid_recipe(self): | 6 | def test_valid_recipe(self): | ||
7 | valid_file = "доста доста риба лала Рибена глава съм аз" # fake file content | 7 | valid_file = "доста доста риба лала Рибена глава съм аз" # fake file content | ||
n | 8 | mock_open = Mock(read_data = valid_file) # immitates open built in func and gives my fake file content | n | 8 | with patch("builtins.open", mock_open(read_data = valid_file)): # immitates open built in func and gives my fake file content |
9 | with patch("builtins.open", mock_open): | ||||
10 | self.assertTrue(validate_recipe("someFile.txt")) | 9 | self.assertTrue(validate_recipe("someFile.txt")) | ||
11 | 10 | ||||
12 | def test_invalid_recipe(self): | 11 | def test_invalid_recipe(self): | ||
13 | invalid_file = "рецепта с рЙба няма нищо гладни сме малко соона" | 12 | invalid_file = "рецепта с рЙба няма нищо гладни сме малко соона" | ||
t | 14 | mock_open = Mock(read_data = invalid_file) | t | ||
15 | with patch("builtins.open", mock_open): | 13 | with patch("builtins.open", mock_open(read_data = invalid_file)): | ||
16 | self.assertFalse(validate_recipe("somefile.txt")) | 14 | self.assertFalse(validate_recipe("somefile.txt")) | ||
17 | 15 | ||||
18 | def test_bad_recipe_file(self): | 16 | def test_bad_recipe_file(self): | ||
19 | errors = [OSError, IOError] | 17 | errors = [OSError, IOError] | ||
20 | for error in errors: | 18 | for error in errors: | ||
21 | with patch("builtins.open", side_effect = error): | 19 | with patch("builtins.open", side_effect = error): | ||
22 | with self.assertRaises(RuinedNikuldenDinnerError): | 20 | with self.assertRaises(RuinedNikuldenDinnerError): | ||
23 | validate_recipe("file.txt") | 21 | validate_recipe("file.txt") | ||
24 | 22 | ||||
25 | if __name__ == '__main__': | 23 | if __name__ == '__main__': | ||
26 | unittest.main() | 24 | unittest.main() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
07.12.2024 14:40
07.12.2024 11:57
07.12.2024 11:58