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

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

1 точки общо

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

 1import unittest
 2from unittest.mock import mock_open, patch
 3from secret import validate_recipe, RuinedNikuldenDinnerError
 4
 5
 6class TestNikuldenValidator(unittest.TestCase):
 7    def setUp(self):
 8        self.valid_contents = [
 9            "Рецепта за Рибена глава и СьОНГА по врачански.",
10            "Рецепта за рибена чорба.",
11            "Сьонга",
12            "Cьонга с рибена глава за Никулден."
13            "Шаран и РИБА"
14        ]
15
16        self.invalid_contents = [
17            "Врачанска рецепта за вегани на Никулден.",
18            "Зелена салата с домати и краставици.",
19            "Печени картофи с чесън и магданоз.",
20            "Супа от гъби по селски."
21        ]
22
23    def test_valid_recipe(self):
24        for content in self.valid_contents:
25            with patch("builtins.open", mock_open(read_data=content)):
26                result = validate_recipe("random_name.txt")
27                self.assertTrue(result)
28
29    def test_invalid_recipe(self):
30        for content in self.invalid_contents:
31            with patch("builtins.open", mock_open(read_data=content)):
32                result = validate_recipe("random_name.txt")
33                self.assertFalse(result)
34
35    def test_bad_recipe_file(self):
36        with patch("builtins.open", side_effect=OSError):
37            with self.assertRaises(RuinedNikuldenDinnerError):
38                validate_recipe("random_name.txt")
39
40        content = "Рецепта за Рибена глава и СьОНГА по врачански."
41        with patch("builtins.open", mock_open(read_data=content)) as mocket_file:
42            mocket_file.return_value.read.side_effect = IOError
43            with self.assertRaises(RuinedNikuldenDinnerError):
44                validate_recipe("random_name.txt")
45
46if __name__ == '__main__':
47    unittest.main()

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.

----------------------------------------------------------------------
Ran 4 tests in 0.039s

FAILED (failures=1)

Дискусия
История

f1import unittestf1import unittest
2from unittest.mock import mock_open, patch2from unittest.mock import mock_open, patch
t3from secret import validate_recipe, RuinedNikuldenDinnerErrort3from secret import validate_recipe, RuinedNikuldenDinnerError
4from Challenge6_real import validate_recipe, RuinedNikuldenDinnerError
54
65
7class TestNikuldenValidator(unittest.TestCase):6class TestNikuldenValidator(unittest.TestCase):
8    def setUp(self):7    def setUp(self):
9        self.valid_contents = [8        self.valid_contents = [
10            "Рецепта за Рибена глава и СьОНГА по врачански.",9            "Рецепта за Рибена глава и СьОНГА по врачански.",
11            "Рецепта за рибена чорба.",10            "Рецепта за рибена чорба.",
12            "Сьонга",11            "Сьонга",
13            "Cьонга с рибена глава за Никулден."12            "Cьонга с рибена глава за Никулден."
14            "Шаран и РИБА"13            "Шаран и РИБА"
15        ]14        ]
1615
17        self.invalid_contents = [16        self.invalid_contents = [
18            "Врачанска рецепта за вегани на Никулден.",17            "Врачанска рецепта за вегани на Никулден.",
19            "Зелена салата с домати и краставици.",18            "Зелена салата с домати и краставици.",
20            "Печени картофи с чесън и магданоз.",19            "Печени картофи с чесън и магданоз.",
21            "Супа от гъби по селски."20            "Супа от гъби по селски."
22        ]21        ]
2322
24    def test_valid_recipe(self):23    def test_valid_recipe(self):
25        for content in self.valid_contents:24        for content in self.valid_contents:
26            with patch("builtins.open", mock_open(read_data=content)):25            with patch("builtins.open", mock_open(read_data=content)):
27                result = validate_recipe("random_name.txt")26                result = validate_recipe("random_name.txt")
28                self.assertTrue(result)27                self.assertTrue(result)
2928
30    def test_invalid_recipe(self):29    def test_invalid_recipe(self):
31        for content in self.invalid_contents:30        for content in self.invalid_contents:
32            with patch("builtins.open", mock_open(read_data=content)):31            with patch("builtins.open", mock_open(read_data=content)):
33                result = validate_recipe("random_name.txt")32                result = validate_recipe("random_name.txt")
34                self.assertFalse(result)33                self.assertFalse(result)
3534
36    def test_bad_recipe_file(self):35    def test_bad_recipe_file(self):
37        with patch("builtins.open", side_effect=OSError):36        with patch("builtins.open", side_effect=OSError):
38            with self.assertRaises(RuinedNikuldenDinnerError):37            with self.assertRaises(RuinedNikuldenDinnerError):
39                validate_recipe("random_name.txt")38                validate_recipe("random_name.txt")
4039
41        content = "Рецепта за Рибена глава и СьОНГА по врачански."40        content = "Рецепта за Рибена глава и СьОНГА по врачански."
42        with patch("builtins.open", mock_open(read_data=content)) as mocket_file:41        with patch("builtins.open", mock_open(read_data=content)) as mocket_file:
43            mocket_file.return_value.read.side_effect = IOError42            mocket_file.return_value.read.side_effect = IOError
44            with self.assertRaises(RuinedNikuldenDinnerError):43            with self.assertRaises(RuinedNikuldenDinnerError):
45                validate_recipe("random_name.txt")44                validate_recipe("random_name.txt")
4645
47if __name__ == '__main__':46if __name__ == '__main__':
48    unittest.main()47    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2from unittest.mock import mock_open, patch2from unittest.mock import mock_open, patch
n3from secret import validate_recipe, RuinedNikuldenDinnerErrorn3from secret import validate_recipe, RuinedNikuldenDinnerError
4from Challenge6_real import validate_recipe, RuinedNikuldenDinnerError
5 
46
5class TestNikuldenValidator(unittest.TestCase):7class TestNikuldenValidator(unittest.TestCase):
6    def setUp(self):8    def setUp(self):
7        self.valid_contents = [9        self.valid_contents = [
n8             "Рецепта за Рибена глава и СьОНГА по врачански.",n10            "Рецепта за Рибена глава и СьОНГА по врачански.",
9             "Рецепта за рибена чорба.",11            "Рецепта за рибена чорба.",
10             "Сьонга",12            "Сьонга",
11             "Cьонга с рибена глава за Никулден."13            "Cьонга с рибена глава за Никулден."
12             "Шаран и РИБА"14            "Шаран и РИБА"
13        ]15        ]
n14        n16 
15        self.invalid_contents = [17        self.invalid_contents = [
16            "Врачанска рецепта за вегани на Никулден.",18            "Врачанска рецепта за вегани на Никулден.",
17            "Зелена салата с домати и краставици.",19            "Зелена салата с домати и краставици.",
18            "Печени картофи с чесън и магданоз.",20            "Печени картофи с чесън и магданоз.",
19            "Супа от гъби по селски."21            "Супа от гъби по селски."
20        ]22        ]
n21        n23 
22    def test_valid_recipe(self):24    def test_valid_recipe(self):
23        for content in self.valid_contents:25        for content in self.valid_contents:
24            with patch("builtins.open", mock_open(read_data=content)):26            with patch("builtins.open", mock_open(read_data=content)):
25                result = validate_recipe("random_name.txt")27                result = validate_recipe("random_name.txt")
26                self.assertTrue(result)28                self.assertTrue(result)
2729
28    def test_invalid_recipe(self):30    def test_invalid_recipe(self):
29        for content in self.invalid_contents:31        for content in self.invalid_contents:
30            with patch("builtins.open", mock_open(read_data=content)):32            with patch("builtins.open", mock_open(read_data=content)):
31                result = validate_recipe("random_name.txt")33                result = validate_recipe("random_name.txt")
32                self.assertFalse(result)34                self.assertFalse(result)
n33    n35 
34    def test_bad_recipe_file(self):36    def test_bad_recipe_file(self):
35        with patch("builtins.open", side_effect=OSError):37        with patch("builtins.open", side_effect=OSError):
36            with self.assertRaises(RuinedNikuldenDinnerError):38            with self.assertRaises(RuinedNikuldenDinnerError):
37                validate_recipe("random_name.txt")39                validate_recipe("random_name.txt")
n38                n40 
39        with patch("builtins.open", side_effect=IOError):41        content = "Рецепта за Рибена глава и СьОНГА по врачански."
42        with patch("builtins.open", mock_open(read_data=content)) as mocket_file:
43            mocket_file.return_value.read.side_effect = IOError
40            with self.assertRaises(RuinedNikuldenDinnerError):44            with self.assertRaises(RuinedNikuldenDinnerError):
41                validate_recipe("random_name.txt")45                validate_recipe("random_name.txt")
t42                t46 
43if __name__ == '__main__':47if __name__ == '__main__':
44    unittest.main()48    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2from unittest.mock import mock_open, patch2from unittest.mock import mock_open, patch
t3#from secret import validate_recipe, RuinedNikuldenDinnerErrort3from secret import validate_recipe, RuinedNikuldenDinnerError
4from Challenge6_real import validate_recipe, RuinedNikuldenDinnerError
54
6class TestNikuldenValidator(unittest.TestCase):5class TestNikuldenValidator(unittest.TestCase):
7    def setUp(self):6    def setUp(self):
8        self.valid_contents = [7        self.valid_contents = [
9             "Рецепта за Рибена глава и СьОНГА по врачански.",8             "Рецепта за Рибена глава и СьОНГА по врачански.",
10             "Рецепта за рибена чорба.",9             "Рецепта за рибена чорба.",
11             "Сьонга",10             "Сьонга",
12             "Cьонга с рибена глава за Никулден."11             "Cьонга с рибена глава за Никулден."
13             "Шаран и РИБА"12             "Шаран и РИБА"
14        ]13        ]
15        14        
16        self.invalid_contents = [15        self.invalid_contents = [
17            "Врачанска рецепта за вегани на Никулден.",16            "Врачанска рецепта за вегани на Никулден.",
18            "Зелена салата с домати и краставици.",17            "Зелена салата с домати и краставици.",
19            "Печени картофи с чесън и магданоз.",18            "Печени картофи с чесън и магданоз.",
20            "Супа от гъби по селски."19            "Супа от гъби по селски."
21        ]20        ]
22        21        
23    def test_valid_recipe(self):22    def test_valid_recipe(self):
24        for content in self.valid_contents:23        for content in self.valid_contents:
25            with patch("builtins.open", mock_open(read_data=content)):24            with patch("builtins.open", mock_open(read_data=content)):
26                result = validate_recipe("random_name.txt")25                result = validate_recipe("random_name.txt")
27                self.assertTrue(result)26                self.assertTrue(result)
2827
29    def test_invalid_recipe(self):28    def test_invalid_recipe(self):
30        for content in self.invalid_contents:29        for content in self.invalid_contents:
31            with patch("builtins.open", mock_open(read_data=content)):30            with patch("builtins.open", mock_open(read_data=content)):
32                result = validate_recipe("random_name.txt")31                result = validate_recipe("random_name.txt")
33                self.assertFalse(result)32                self.assertFalse(result)
34    33    
35    def test_bad_recipe_file(self):34    def test_bad_recipe_file(self):
36        with patch("builtins.open", side_effect=OSError):35        with patch("builtins.open", side_effect=OSError):
37            with self.assertRaises(RuinedNikuldenDinnerError):36            with self.assertRaises(RuinedNikuldenDinnerError):
38                validate_recipe("random_name.txt")37                validate_recipe("random_name.txt")
39                38                
40        with patch("builtins.open", side_effect=IOError):39        with patch("builtins.open", side_effect=IOError):
41            with self.assertRaises(RuinedNikuldenDinnerError):40            with self.assertRaises(RuinedNikuldenDinnerError):
42                validate_recipe("random_name.txt")41                validate_recipe("random_name.txt")
43                42                
44if __name__ == '__main__':43if __name__ == '__main__':
45    unittest.main()44    unittest.main()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op