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)
|
f | 1 | import unittest | f | 1 | import unittest |
2 | from itertools import product | 2 | from itertools import product | ||
3 | from secret import validate_recipe, RuinedNikuldenDinnerError | 3 | from secret import validate_recipe, RuinedNikuldenDinnerError | ||
4 | from unittest.mock import mock_open, patch | 4 | from unittest.mock import mock_open, patch | ||
5 | 5 | ||||
n | 6 | VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга") | n | 6 | VALID_STRINGS_IN_FILE = ("риба", "рибена", "шаран", "сьонга") |
7 | FILENAME = "something.txt" | 7 | FILENAME = "something.txt" | ||
8 | INVALID_FILENAME = "baba.txt" | 8 | INVALID_FILENAME = "baba.txt" | ||
9 | 9 | ||||
10 | 10 | ||||
11 | class TestNikuldenValidator(unittest.TestCase): | 11 | class 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 {}") | ||
n | n | 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) | ||||
16 | 24 | ||||
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)) | ||
22 | 30 | ||||
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) | ||
25 | 33 | ||||
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) | ||
28 | 36 | ||||
29 | @staticmethod | 37 | @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 combinations | 41 | return combinations | ||
34 | 42 | ||||
35 | def test_invalid_recipe(self): | 43 | def test_invalid_recipe(self): | ||
t | 36 | self.word_at_random_position_mismatch(FILENAME, "{}s invalid") | t | 44 | 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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import unittest | f | 1 | import unittest |
2 | from itertools import product | 2 | from itertools import product | ||
3 | from secret import validate_recipe, RuinedNikuldenDinnerError | 3 | from secret import validate_recipe, RuinedNikuldenDinnerError | ||
4 | from unittest.mock import mock_open, patch | 4 | from unittest.mock import mock_open, patch | ||
5 | 5 | ||||
6 | VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга") | 6 | VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга") | ||
7 | FILENAME = "something.txt" | 7 | FILENAME = "something.txt" | ||
n | n | 8 | INVALID_FILENAME = "baba.txt" | ||
8 | 9 | ||||
9 | 10 | ||||
10 | class TestNikuldenValidator(unittest.TestCase): | 11 | class 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 {}") | ||
15 | 16 | ||||
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)) | ||
21 | 22 | ||||
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) | ||
24 | 25 | ||||
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) | ||
27 | 28 | ||||
28 | @staticmethod | 29 | @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 combinations | 33 | return combinations | ||
33 | 34 | ||||
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{}") | ||
38 | 39 | ||||
39 | def test_bad_recipe_file(self): | 40 | def test_bad_recipe_file(self): | ||
t | t | 41 | 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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import unittest | f | 1 | import unittest |
2 | from itertools import product | 2 | from itertools import product | ||
3 | from secret import validate_recipe, RuinedNikuldenDinnerError | 3 | from secret import validate_recipe, RuinedNikuldenDinnerError | ||
4 | from unittest.mock import mock_open, patch | 4 | from unittest.mock import mock_open, patch | ||
5 | 5 | ||||
6 | VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга") | 6 | VALID_STRINGS_IN_FILE = ("рибa", "рибена", "шаран", "сьонга") | ||
7 | FILENAME = "something.txt" | 7 | FILENAME = "something.txt" | ||
8 | 8 | ||||
9 | 9 | ||||
10 | class TestNikuldenValidator(unittest.TestCase): | 10 | class 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 {}") | ||
15 | 15 | ||||
n | 16 | def word_at_random_position(self, filename, write_string, assertFunction): | n | 16 | 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))): | ||
t | 20 | assertFunction(validate_recipe(filename)) | t | 20 | assert_function(validate_recipe(filename)) |
21 | 21 | ||||
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) | ||
24 | 24 | ||||
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) | ||
27 | 27 | ||||
28 | @staticmethod | 28 | @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 combinations | 32 | return combinations | ||
33 | 33 | ||||
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{}") | ||
38 | 38 | ||||
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
07.12.2024 13:24