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

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

1 точки общо

0 успешни теста
0 неуспешни теста
Код (changed to phonetic traditional)
Скрий всички коментари

 1import unittest
 2from itertools import product
 3from secret import validate_recipe, RuinedNikuldenDinnerError
 4from unittest.mock import mock_open, patch
 5
 6VALID_STRINGS_IN_FILE = ("риба", "рибена", "шаран", "сьонга")
 7FILENAME = "something.txt"
 8INVALID_FILENAME = "baba.txt"
 9
10
11class TestNikuldenValidator(unittest.TestCase):
12    def test_valid_recipe(self):
13        self.word_at_random_position_match(FILENAME, "{} is in the recipe")
14        self.word_at_random_position_match(FILENAME, "is in the middle {} bla bla")
15        self.word_at_random_position_match(FILENAME, "bla bla in the end {}")
16
17    def test_bad_recipe_file(self):
18        with patch("builtins.open", side_effect=OSError()):
19            with self.assertRaises(RuinedNikuldenDinnerError):
20                validate_recipe(INVALID_FILENAME)
21        with patch("builtins.open", side_effect=IOError()):
22            with self.assertRaises(RuinedNikuldenDinnerError):
23                validate_recipe(INVALID_FILENAME)
24
25    def word_at_random_position(self, filename, write_string, assert_function):
26        for word in VALID_STRINGS_IN_FILE:
27            for comb in self.generate_case_combinations(word):
28                with patch("builtins.open", mock_open(read_data=write_string.format(comb))):
29                    assert_function(validate_recipe(filename))
30
31    def word_at_random_position_match(self, filename, write_string):
32        self.word_at_random_position(filename, write_string, self.assertTrue)
33
34    def word_at_random_position_mismatch(self, filename, write_string):
35        self.word_at_random_position(filename, write_string, self.assertFalse)
36
37    @staticmethod
38    def generate_case_combinations(word):
39        char_cases = [(word.lower(), word.upper()) for word in word]
40        combinations = [''.join(combo) for combo in product(*char_cases)]
41        return combinations
42
43    def test_invalid_recipe(self):
44        self.word_at_random_position_mismatch(FILENAME, "s invalid")
45        self.word_at_random_position_mismatch(FILENAME, "bla bla  s")
46        self.word_at_random_position_mismatch(FILENAME, "bla bla s")

Timed out.

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

Нищо, "Точката си е точка", както е казал Чингиз Хан... Май, може и да си спомням грешно.
Павел Петков
07.12.2024 13:47

Защото съм качил грешен commit без да искам :)
Виктор Бечев
07.12.2024 13:23

Не знам защо би било различно, буквата в unicode е една и съща. Като си говорихме за бавните тестове, бях забравил, че нашият runner има timeout. :grin: Пуснах ти тестовете ръчно, имаш 3/4 минаващи. Единственият ти пропуск е, че не си тествал с поведение, където думата, която се търси е част от друга дума - `"Любимото ястие на Рибарска е пиле с ориз"`. А даже си питал.
Павел Петков
06.12.2024 14:43

Тествах с моя функция validate_recipe и забелязах че има значение как изписваме "риба" и останалите, защото ако пишем примерно на phonetic и phonetic traditional а-то ima различна unicode репрезенация. Тъй като не знам как се тества това нещо просто се надявам да е вкарано като логика в validate_recipe. ![Alt Image](https://gcdnb.pbrd.co/images/g2Ok8fwmNgbM.png?o=1)
История

f1import unittestf1import unittest
2from itertools import product2from itertools import product
3from secret import validate_recipe, RuinedNikuldenDinnerError3from secret import validate_recipe, RuinedNikuldenDinnerError
4from unittest.mock import mock_open, patch4from unittest.mock import mock_open, patch
55
n6VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга")n6VALID_STRINGS_IN_FILE = ("риба", "рибена", "шаран", "сьонга")
7FILENAME = "something.txt"7FILENAME = "something.txt"
8INVALID_FILENAME = "baba.txt"8INVALID_FILENAME = "baba.txt"
99
1010
11class TestNikuldenValidator(unittest.TestCase):11class TestNikuldenValidator(unittest.TestCase):
12    def test_valid_recipe(self):12    def test_valid_recipe(self):
13        self.word_at_random_position_match(FILENAME, "{} is in the recipe")13        self.word_at_random_position_match(FILENAME, "{} is in the recipe")
14        self.word_at_random_position_match(FILENAME, "is in the middle {} bla bla")14        self.word_at_random_position_match(FILENAME, "is in the middle {} bla bla")
15        self.word_at_random_position_match(FILENAME, "bla bla in the end {}")15        self.word_at_random_position_match(FILENAME, "bla bla in the end {}")
nn16 
17    def test_bad_recipe_file(self):
18        with patch("builtins.open", side_effect=OSError()):
19            with self.assertRaises(RuinedNikuldenDinnerError):
20                validate_recipe(INVALID_FILENAME)
21        with patch("builtins.open", side_effect=IOError()):
22            with self.assertRaises(RuinedNikuldenDinnerError):
23                validate_recipe(INVALID_FILENAME)
1624
17    def word_at_random_position(self, filename, write_string, assert_function):25    def word_at_random_position(self, filename, write_string, assert_function):
18        for word in VALID_STRINGS_IN_FILE:26        for word in VALID_STRINGS_IN_FILE:
19            for comb in self.generate_case_combinations(word):27            for comb in self.generate_case_combinations(word):
20                with patch("builtins.open", mock_open(read_data=write_string.format(comb))):28                with patch("builtins.open", mock_open(read_data=write_string.format(comb))):
21                    assert_function(validate_recipe(filename))29                    assert_function(validate_recipe(filename))
2230
23    def word_at_random_position_match(self, filename, write_string):31    def word_at_random_position_match(self, filename, write_string):
24        self.word_at_random_position(filename, write_string, self.assertTrue)32        self.word_at_random_position(filename, write_string, self.assertTrue)
2533
26    def word_at_random_position_mismatch(self, filename, write_string):34    def word_at_random_position_mismatch(self, filename, write_string):
27        self.word_at_random_position(filename, write_string, self.assertFalse)35        self.word_at_random_position(filename, write_string, self.assertFalse)
2836
29    @staticmethod37    @staticmethod
30    def generate_case_combinations(word):38    def generate_case_combinations(word):
31        char_cases = [(word.lower(), word.upper()) for word in word]39        char_cases = [(word.lower(), word.upper()) for word in word]
32        combinations = [''.join(combo) for combo in product(*char_cases)]40        combinations = [''.join(combo) for combo in product(*char_cases)]
33        return combinations41        return combinations
3442
35    def test_invalid_recipe(self):43    def test_invalid_recipe(self):
t36        self.word_at_random_position_mismatch(FILENAME, "{}s invalid")t44        self.word_at_random_position_mismatch(FILENAME, "s invalid")
37        self.word_at_random_position_mismatch(FILENAME, "bla bla{}  s")45        self.word_at_random_position_mismatch(FILENAME, "bla bla  s")
38        self.word_at_random_position_mismatch(FILENAME, "bla bla s{}")46        self.word_at_random_position_mismatch(FILENAME, "bla bla s")
39 
40    def test_bad_recipe_file(self):
41        with patch("builtins.open", side_effect=OSError()):
42            with self.assertRaises(RuinedNikuldenDinnerError):
43                validate_recipe(INVALID_FILENAME)
44        with patch("builtins.open", side_effect=IOError()):
45            with self.assertRaises(RuinedNikuldenDinnerError):
46                validate_recipe(INVALID_FILENAME)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2from itertools import product2from itertools import product
3from secret import validate_recipe, RuinedNikuldenDinnerError3from secret import validate_recipe, RuinedNikuldenDinnerError
4from unittest.mock import mock_open, patch4from unittest.mock import mock_open, patch
55
6VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга")6VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга")
7FILENAME = "something.txt"7FILENAME = "something.txt"
nn8INVALID_FILENAME = "baba.txt"
89
910
10class TestNikuldenValidator(unittest.TestCase):11class TestNikuldenValidator(unittest.TestCase):
11    def test_valid_recipe(self):12    def test_valid_recipe(self):
12        self.word_at_random_position_match(FILENAME, "{} is in the recipe")13        self.word_at_random_position_match(FILENAME, "{} is in the recipe")
13        self.word_at_random_position_match(FILENAME, "is in the middle {} bla bla")14        self.word_at_random_position_match(FILENAME, "is in the middle {} bla bla")
14        self.word_at_random_position_match(FILENAME, "bla bla in the end {}")15        self.word_at_random_position_match(FILENAME, "bla bla in the end {}")
1516
16    def word_at_random_position(self, filename, write_string, assert_function):17    def word_at_random_position(self, filename, write_string, assert_function):
17        for word in VALID_STRINGS_IN_FILE:18        for word in VALID_STRINGS_IN_FILE:
18            for comb in self.generate_case_combinations(word):19            for comb in self.generate_case_combinations(word):
19                with patch("builtins.open", mock_open(read_data=write_string.format(comb))):20                with patch("builtins.open", mock_open(read_data=write_string.format(comb))):
20                    assert_function(validate_recipe(filename))21                    assert_function(validate_recipe(filename))
2122
22    def word_at_random_position_match(self, filename, write_string):23    def word_at_random_position_match(self, filename, write_string):
23        self.word_at_random_position(filename, write_string, self.assertTrue)24        self.word_at_random_position(filename, write_string, self.assertTrue)
2425
25    def word_at_random_position_mismatch(self, filename, write_string):26    def word_at_random_position_mismatch(self, filename, write_string):
26        self.word_at_random_position(filename, write_string, self.assertFalse)27        self.word_at_random_position(filename, write_string, self.assertFalse)
2728
28    @staticmethod29    @staticmethod
29    def generate_case_combinations(word):30    def generate_case_combinations(word):
30        char_cases = [(word.lower(), word.upper()) for word in word]31        char_cases = [(word.lower(), word.upper()) for word in word]
31        combinations = [''.join(combo) for combo in product(*char_cases)]32        combinations = [''.join(combo) for combo in product(*char_cases)]
32        return combinations33        return combinations
3334
34    def test_invalid_recipe(self):35    def test_invalid_recipe(self):
35        self.word_at_random_position_mismatch(FILENAME, "{}s invalid")36        self.word_at_random_position_mismatch(FILENAME, "{}s invalid")
36        self.word_at_random_position_mismatch(FILENAME, "bla bla{}  s")37        self.word_at_random_position_mismatch(FILENAME, "bla bla{}  s")
37        self.word_at_random_position_mismatch(FILENAME, "bla bla s{}")38        self.word_at_random_position_mismatch(FILENAME, "bla bla s{}")
3839
39    def test_bad_recipe_file(self):40    def test_bad_recipe_file(self):
tt41        with patch("builtins.open", side_effect=OSError()):
40        with self.assertRaises(RuinedNikuldenDinnerError):42            with self.assertRaises(RuinedNikuldenDinnerError):
41            validate_recipe("baba.txt")43                validate_recipe(INVALID_FILENAME)
44        with patch("builtins.open", side_effect=IOError()):
45            with self.assertRaises(RuinedNikuldenDinnerError):
46                validate_recipe(INVALID_FILENAME)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import unittestf1import unittest
2from itertools import product2from itertools import product
3from secret import validate_recipe, RuinedNikuldenDinnerError3from secret import validate_recipe, RuinedNikuldenDinnerError
4from unittest.mock import mock_open, patch4from unittest.mock import mock_open, patch
55
6VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга")6VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга")
7FILENAME = "something.txt"7FILENAME = "something.txt"
88
99
10class TestNikuldenValidator(unittest.TestCase):10class TestNikuldenValidator(unittest.TestCase):
11    def test_valid_recipe(self):11    def test_valid_recipe(self):
12        self.word_at_random_position_match(FILENAME, "{} is in the recipe")12        self.word_at_random_position_match(FILENAME, "{} is in the recipe")
13        self.word_at_random_position_match(FILENAME, "is in the middle {} bla bla")13        self.word_at_random_position_match(FILENAME, "is in the middle {} bla bla")
14        self.word_at_random_position_match(FILENAME, "bla bla in the end {}")14        self.word_at_random_position_match(FILENAME, "bla bla in the end {}")
1515
n16    def word_at_random_position(self, filename, write_string, assertFunction):n16    def word_at_random_position(self, filename, write_string, assert_function):
17        for word in VALID_STRINGS_IN_FILE:17        for word in VALID_STRINGS_IN_FILE:
18            for comb in self.generate_case_combinations(word):18            for comb in self.generate_case_combinations(word):
19                with patch("builtins.open", mock_open(read_data=write_string.format(comb))):19                with patch("builtins.open", mock_open(read_data=write_string.format(comb))):
t20                    assertFunction(validate_recipe(filename))t20                    assert_function(validate_recipe(filename))
2121
22    def word_at_random_position_match(self, filename, write_string):22    def word_at_random_position_match(self, filename, write_string):
23        self.word_at_random_position(filename, write_string, self.assertTrue)23        self.word_at_random_position(filename, write_string, self.assertTrue)
2424
25    def word_at_random_position_mismatch(self, filename, write_string):25    def word_at_random_position_mismatch(self, filename, write_string):
26        self.word_at_random_position(filename, write_string, self.assertFalse)26        self.word_at_random_position(filename, write_string, self.assertFalse)
2727
28    @staticmethod28    @staticmethod
29    def generate_case_combinations(word):29    def generate_case_combinations(word):
30        char_cases = [(word.lower(), word.upper()) for word in word]30        char_cases = [(word.lower(), word.upper()) for word in word]
31        combinations = [''.join(combo) for combo in product(*char_cases)]31        combinations = [''.join(combo) for combo in product(*char_cases)]
32        return combinations32        return combinations
3333
34    def test_invalid_recipe(self):34    def test_invalid_recipe(self):
35        self.word_at_random_position_mismatch(FILENAME, "{}s invalid")35        self.word_at_random_position_mismatch(FILENAME, "{}s invalid")
36        self.word_at_random_position_mismatch(FILENAME, "bla bla{}  s")36        self.word_at_random_position_mismatch(FILENAME, "bla bla{}  s")
37        self.word_at_random_position_mismatch(FILENAME, "bla bla s{}")37        self.word_at_random_position_mismatch(FILENAME, "bla bla s{}")
3838
39    def test_bad_recipe_file(self):39    def test_bad_recipe_file(self):
40        with self.assertRaises(RuinedNikuldenDinnerError):40        with self.assertRaises(RuinedNikuldenDinnerError):
41            validate_recipe("baba.txt")41            validate_recipe("baba.txt")
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op