f | import unittest | f | import unittest |
| import random | | import random |
| import string | | import string |
| from unittest.mock import mock_open, patch | | from unittest.mock import mock_open, patch |
| from secret import validate_recipe, RuinedNikuldenDinnerError | | from secret import validate_recipe, RuinedNikuldenDinnerError |
| | | |
| def text_generator(word = ''): | | def text_generator(word = ''): |
| letters = string.ascii_letters + string.digits + ' ' + word | | letters = string.ascii_letters + string.digits + ' ' + word |
| | | |
| text = '' | | text = '' |
| | | |
| for i in range(10): | | for i in range(10): |
| text += ''.join(random.choice(letters) for _ in range(20)) + ' ' | | text += ''.join(random.choice(letters) for _ in range(20)) + ' ' |
| | | |
| return text + word | | return text + word |
| | | |
| class TestNikuldenValidator(unittest.TestCase): | | class TestNikuldenValidator(unittest.TestCase): |
| def test_valid_recipe(self): | | def test_valid_recipe(self): |
| valid_words = ['риба', 'рибена', 'шаран', 'сьонга'] | | valid_words = ['риба', 'рибена', 'шаран', 'сьонга'] |
| | | |
| for word in valid_words: | | for word in valid_words: |
| content_lower = text_generator(word) | | content_lower = text_generator(word) |
| content_upper = text_generator(word.upper()) | | content_upper = text_generator(word.upper()) |
| content_random = text_generator(''.join(char.upper() if random.choice([True, False]) else char.lower() | | content_random = text_generator(''.join(char.upper() if random.choice([True, False]) else char.lower() |
| for char in word)) | | for char in word)) |
| | | |
| with patch("builtins.open", mock_open(read_data=content_lower)) as mock_file_lower: | | with patch("builtins.open", mock_open(read_data=content_lower)) as mock_file_lower: |
| result_lower = validate_recipe("test.txt") | | result_lower = validate_recipe("test.txt") |
| mock_file_lower.assert_called_once_with("test.txt") | | mock_file_lower.assert_called_once_with("test.txt") |
| self.assertTrue(result_lower) | | self.assertTrue(result_lower) |
| | | |
| | | |
| with patch("builtins.open", mock_open(read_data=content_upper)) as mock_file_upper: | | with patch("builtins.open", mock_open(read_data=content_upper)) as mock_file_upper: |
| result_upper = validate_recipe("test.txt") | | result_upper = validate_recipe("test.txt") |
| mock_file_upper.assert_called_once_with("test.txt") | | mock_file_upper.assert_called_once_with("test.txt") |
| self.assertTrue(result_upper) | | self.assertTrue(result_upper) |
| | | |
| with patch("builtins.open", mock_open(read_data=content_random)) as mock_file_random: | | with patch("builtins.open", mock_open(read_data=content_random)) as mock_file_random: |
| result_random = validate_recipe("test.txt") | | result_random = validate_recipe("test.txt") |
| mock_file_random.assert_called_once_with("test.txt") | | mock_file_random.assert_called_once_with("test.txt") |
| self.assertTrue(result_random) | | self.assertTrue(result_random) |
| | | |
| | | |
| def test_invalid_recipe(self): | | def test_invalid_recipe(self): |
n | with patch("builtins.open", mock_open(read_data='')) as mock_file_lower: | n | with patch("builtins.open", mock_open(read_data='')) as mock_file: |
| result = validate_recipe("test.txt") | | result = validate_recipe("test.txt") |
n | mock_file_lower.assert_called_once_with("test.txt") | n | mock_file.assert_called_once_with("test.txt") |
| self.assertFalse(result) | | self.assertFalse(result) |
| | | |
| random_text = text_generator() | | random_text = text_generator() |
n | with patch("builtins.open", mock_open(read_data=random_text)) as mock_file_lower: | n | with patch("builtins.open", mock_open(read_data=random_text)) as mock_file: |
| result = validate_recipe("test.txt") | | result = validate_recipe("test.txt") |
n | mock_file_lower.assert_called_once_with("test.txt") | n | mock_file.assert_called_once_with("test.txt") |
| self.assertFalse(result) | | self.assertFalse(result) |
| | | |
| invalid_text = text_generator('сьомга') | | invalid_text = text_generator('сьомга') |
n | with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file_lower: | n | with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file: |
| result = validate_recipe("test.txt") | | result = validate_recipe("test.txt") |
n | mock_file_lower.assert_called_once_with("test.txt") | n | mock_file.assert_called_once_with("test.txt") |
| self.assertFalse(result) | | self.assertFalse(result) |
| | | |
| invalid_text += 'риба' | | invalid_text += 'риба' |
n | with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file_lower: | n | with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file: |
| result = validate_recipe("test.txt") | | result = validate_recipe("test.txt") |
n | mock_file_lower.assert_called_once_with("test.txt") | n | mock_file.assert_called_once_with("test.txt") |
| self.assertFalse(result) | | self.assertFalse(result) |
| | | |
| def test_bad_recipe_file(self): | | def test_bad_recipe_file(self): |
| with self.assertRaises(RuinedNikuldenDinnerError): | | with self.assertRaises(RuinedNikuldenDinnerError): |
| with patch("builtins.open", side_effect=OSError): | | with patch("builtins.open", side_effect=OSError): |
| validate_recipe("test.txt") | | validate_recipe("test.txt") |
| | | |
| with self.assertRaises(RuinedNikuldenDinnerError): | | with self.assertRaises(RuinedNikuldenDinnerError): |
| with patch("builtins.open", side_effect=IOError): | | with patch("builtins.open", side_effect=IOError): |
| validate_recipe("test.txt") | | validate_recipe("test.txt") |
| | | |
t | | t | |
| | | |
| | | |
| | | |
| | | |
| | | |
| if __name__ == '__main__': | | if __name__ == '__main__': |
| unittest.main() | | unittest.main() |
| | | |
| | | |
07.12.2024 11:54