Предизвикателства > Валидатор за Никулденска рецепта > Решения > Решението на Силвана Николова

Резултати
1 точки от тестове
1 точки от учител

2 точки общо

4 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

 1import unittest
 2import random
 3import string
 4from unittest.mock import mock_open, patch
 5from secret import validate_recipe, RuinedNikuldenDinnerError
 6
 7def text_generator(word = ''):
 8    letters = string.ascii_letters + string.digits + ' ' + word
 9
10    text = ''
11
12    for i in range(10):
13        text += ''.join(random.choice(letters) for _ in range(20)) + ' '
14
15    return text + word
16
17class TestNikuldenValidator(unittest.TestCase):
18    def test_valid_recipe(self):
19        valid_words = ['риба', 'рибена', 'шаран', 'сьонга']
20
21        for word in valid_words:
22            content_lower = text_generator(word)
23            content_upper = text_generator(word.upper())
24            content_random = text_generator(''.join(char.upper() if random.choice([True, False]) else char.lower()
25                                            for char in word))
26            
27            with patch("builtins.open", mock_open(read_data=content_lower)) as mock_file_lower:
28                result_lower = validate_recipe("test.txt")
29                mock_file_lower.assert_called_once_with("test.txt")
30                self.assertTrue(result_lower)
31
32            
33            with patch("builtins.open", mock_open(read_data=content_upper)) as mock_file_upper:
34                result_upper = validate_recipe("test.txt")
35                mock_file_upper.assert_called_once_with("test.txt")
36                self.assertTrue(result_upper)
37
38            with patch("builtins.open", mock_open(read_data=content_random)) as mock_file_random:
39                result_random = validate_recipe("test.txt")
40                mock_file_random.assert_called_once_with("test.txt")
41                self.assertTrue(result_random)
42                
43    
44    def test_invalid_recipe(self):
45        with patch("builtins.open", mock_open(read_data='')) as mock_file:
46            result = validate_recipe("test.txt")
47            mock_file.assert_called_once_with("test.txt")
48            self.assertFalse(result)
49
50        random_text = text_generator()
51        with patch("builtins.open", mock_open(read_data=random_text)) as mock_file:
52            result = validate_recipe("test.txt")
53            mock_file.assert_called_once_with("test.txt")
54            self.assertFalse(result)
55
56        invalid_text = text_generator('сьомга')
57        with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file:
58            result = validate_recipe("test.txt")
59            mock_file.assert_called_once_with("test.txt")
60            self.assertFalse(result)
61
62        invalid_text += 'риба'
63        with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file:
64            result = validate_recipe("test.txt")
65            mock_file.assert_called_once_with("test.txt")
66            self.assertFalse(result)
67
68    def test_bad_recipe_file(self):
69        with self.assertRaises(RuinedNikuldenDinnerError):
70            with patch("builtins.open", side_effect=OSError):
71                validate_recipe("test.txt")
72        
73        with self.assertRaises(RuinedNikuldenDinnerError):
74            with patch("builtins.open", side_effect=IOError):
75                validate_recipe("test.txt")
76
77if __name__ == '__main__':
78    unittest.main()

....
----------------------------------------------------------------------
Ran 4 tests in 0.096s

OK

Дискусия
Виктор Бечев
07.12.2024 11:55

Отвъд дребната забележка по-горе - хубави тестове. Може би в повечето случаи бих препоръчал по-експлицитна дефиниция на входните стрингове, но в случая работи, без да е много объркващо.
История

f1import unittestf1import unittest
2import random2import random
3import string3import string
4from unittest.mock import mock_open, patch4from unittest.mock import mock_open, patch
5from secret import validate_recipe, RuinedNikuldenDinnerError5from secret import validate_recipe, RuinedNikuldenDinnerError
66
7def text_generator(word = ''):7def text_generator(word = ''):
8    letters = string.ascii_letters + string.digits + ' ' + word8    letters = string.ascii_letters + string.digits + ' ' + word
99
10    text = ''10    text = ''
1111
12    for i in range(10):12    for i in range(10):
13        text += ''.join(random.choice(letters) for _ in range(20)) + ' '13        text += ''.join(random.choice(letters) for _ in range(20)) + ' '
1414
15    return text + word15    return text + word
1616
17class TestNikuldenValidator(unittest.TestCase):17class TestNikuldenValidator(unittest.TestCase):
18    def test_valid_recipe(self):18    def test_valid_recipe(self):
19        valid_words = ['риба', 'рибена', 'шаран', 'сьонга']19        valid_words = ['риба', 'рибена', 'шаран', 'сьонга']
2020
21        for word in valid_words:21        for word in valid_words:
22            content_lower = text_generator(word)22            content_lower = text_generator(word)
23            content_upper = text_generator(word.upper())23            content_upper = text_generator(word.upper())
24            content_random = text_generator(''.join(char.upper() if random.choice([True, False]) else char.lower()24            content_random = text_generator(''.join(char.upper() if random.choice([True, False]) else char.lower()
25                                            for char in word))25                                            for char in word))
26            26            
27            with patch("builtins.open", mock_open(read_data=content_lower)) as mock_file_lower:27            with patch("builtins.open", mock_open(read_data=content_lower)) as mock_file_lower:
28                result_lower = validate_recipe("test.txt")28                result_lower = validate_recipe("test.txt")
29                mock_file_lower.assert_called_once_with("test.txt")29                mock_file_lower.assert_called_once_with("test.txt")
30                self.assertTrue(result_lower)30                self.assertTrue(result_lower)
3131
32            32            
33            with patch("builtins.open", mock_open(read_data=content_upper)) as mock_file_upper:33            with patch("builtins.open", mock_open(read_data=content_upper)) as mock_file_upper:
34                result_upper = validate_recipe("test.txt")34                result_upper = validate_recipe("test.txt")
35                mock_file_upper.assert_called_once_with("test.txt")35                mock_file_upper.assert_called_once_with("test.txt")
36                self.assertTrue(result_upper)36                self.assertTrue(result_upper)
3737
38            with patch("builtins.open", mock_open(read_data=content_random)) as mock_file_random:38            with patch("builtins.open", mock_open(read_data=content_random)) as mock_file_random:
39                result_random = validate_recipe("test.txt")39                result_random = validate_recipe("test.txt")
40                mock_file_random.assert_called_once_with("test.txt")40                mock_file_random.assert_called_once_with("test.txt")
41                self.assertTrue(result_random)41                self.assertTrue(result_random)
42                42                
43    43    
44    def test_invalid_recipe(self):44    def test_invalid_recipe(self):
n45        with patch("builtins.open", mock_open(read_data='')) as mock_file_lower:n45        with patch("builtins.open", mock_open(read_data='')) as mock_file:
46            result = validate_recipe("test.txt")46            result = validate_recipe("test.txt")
n47            mock_file_lower.assert_called_once_with("test.txt")n47            mock_file.assert_called_once_with("test.txt")
48            self.assertFalse(result)48            self.assertFalse(result)
4949
50        random_text = text_generator()50        random_text = text_generator()
n51        with patch("builtins.open", mock_open(read_data=random_text)) as mock_file_lower:n51        with patch("builtins.open", mock_open(read_data=random_text)) as mock_file:
52            result = validate_recipe("test.txt")52            result = validate_recipe("test.txt")
n53            mock_file_lower.assert_called_once_with("test.txt")n53            mock_file.assert_called_once_with("test.txt")
54            self.assertFalse(result)54            self.assertFalse(result)
5555
56        invalid_text = text_generator('сьомга')56        invalid_text = text_generator('сьомга')
n57        with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file_lower:n57        with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file:
58            result = validate_recipe("test.txt")58            result = validate_recipe("test.txt")
n59            mock_file_lower.assert_called_once_with("test.txt")n59            mock_file.assert_called_once_with("test.txt")
60            self.assertFalse(result)60            self.assertFalse(result)
6161
62        invalid_text += 'риба'62        invalid_text += 'риба'
n63        with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file_lower:n63        with patch("builtins.open", mock_open(read_data=invalid_text)) as mock_file:
64            result = validate_recipe("test.txt")64            result = validate_recipe("test.txt")
n65            mock_file_lower.assert_called_once_with("test.txt")n65            mock_file.assert_called_once_with("test.txt")
66            self.assertFalse(result)66            self.assertFalse(result)
6767
68    def test_bad_recipe_file(self):68    def test_bad_recipe_file(self):
69        with self.assertRaises(RuinedNikuldenDinnerError):69        with self.assertRaises(RuinedNikuldenDinnerError):
70            with patch("builtins.open", side_effect=OSError):70            with patch("builtins.open", side_effect=OSError):
71                validate_recipe("test.txt")71                validate_recipe("test.txt")
72        72        
73        with self.assertRaises(RuinedNikuldenDinnerError):73        with self.assertRaises(RuinedNikuldenDinnerError):
74            with patch("builtins.open", side_effect=IOError):74            with patch("builtins.open", side_effect=IOError):
75                validate_recipe("test.txt")75                validate_recipe("test.txt")
7676
t77 t
78        
79        
80 
81        
82 
83if __name__ == '__main__':77if __name__ == '__main__':
84    unittest.main()78    unittest.main()
8579
8680
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op