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

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

1 точки общо

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

 1import unittest
 2from unittest.mock import patch, mock_open
 3import itertools
 4from  secret import validate_recipe, RuinedNikuldenDinnerError
 5
 6
 7class TestNikuldenValidator(unittest.TestCase):
 8
 9    def setUp(self):
10        self.func = validate_recipe 
11    
12    def _generate_case_insensitive_combinations(self, words):
13        combined_words = []
14        for word in words:
15            combined_words.extend([''.join(letters) for letters in itertools.product(*[(ch.lower(), ch.upper()) for ch in word])])
16        return combined_words
17    
18    def test_valid_recipe(self):
19        mock_file_content = self._generate_case_insensitive_combinations(["риба", "рибена", "шаран", "сьонга"])
20        for content in mock_file_content:
21            with patch("builtins.open", mock_open(read_data=content)):
22                self.assertTrue(self.func("test.txt"))
23
24    def test_invalid_recipe(self):
25        mock_file_content = ["Няма да ядем ри бена чорба.", "За рецептата ни трябва сьомга.", "Маран е вид вкусна миба."]
26        for content in mock_file_content:
27            with patch("builtins.open", mock_open(read_data=content)):
28                self.assertFalse(self.func("test.txt"))
29
30    def test_bad_recipe_file(self):
31        with patch("builtins.open", side_effect=OSError):
32            with self.assertRaises(RuinedNikuldenDinnerError):
33                self.func("test.txt")
34
35        with patch("builtins.open", side_effect=IOError):
36            with self.assertRaises(RuinedNikuldenDinnerError):
37                self.func("test.txt")
38
39
40if __name__ == '__main__':
41    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.627s

FAILED (failures=1)

Дискусия
История
Това решение има само една версия.