diff --git a/apps/librelingo_fakes/README.md b/apps/librelingo_fakes/README.md index e2dcd7eef0c7..6d6902849db5 100644 --- a/apps/librelingo_fakes/README.md +++ b/apps/librelingo_fakes/README.md @@ -31,3 +31,13 @@ fake_course = fakes.customize(fakes.course1, modules=[ ]) ``` + +#### number + +```python +number(max_value=10000) +``` + +Returns a 'random' non-negative integer. +It is NOT cryptographically-secure. + diff --git a/apps/librelingo_fakes/librelingo_fakes/fakes.py b/apps/librelingo_fakes/librelingo_fakes/fakes.py index 3565a31d1d59..2e0dc5f1679d 100644 --- a/apps/librelingo_fakes/librelingo_fakes/fakes.py +++ b/apps/librelingo_fakes/librelingo_fakes/fakes.py @@ -267,9 +267,17 @@ ) +def number(max_value=10000): + """ + Returns a 'random' non-negative integer. + It is NOT cryptographically-secure. + """ + return random.randint(0, max_value) # NOSONAR + + def fake_value(): FakeValue = namedtuple("FakeValue", ["id"]) - return FakeValue(random.randint(0, 10000)) + return FakeValue(number()) def customize(fake, **kwargs): @@ -279,4 +287,4 @@ def customize(fake, **kwargs): def path(): - return Path(f"./path{random.randint(0, 5000)}") + return Path(f"./path{number(max_value=5000)}") diff --git a/apps/librelingo_yaml_loader/tests/test_yaml_loader.py b/apps/librelingo_yaml_loader/tests/test_yaml_loader.py index e33c055f0b7f..24718a1f61b8 100644 --- a/apps/librelingo_yaml_loader/tests/test_yaml_loader.py +++ b/apps/librelingo_yaml_loader/tests/test_yaml_loader.py @@ -1,5 +1,4 @@ import os -import random import re from pathlib import Path from unittest.mock import Mock, patch @@ -26,10 +25,6 @@ _load_modules, _load_skills, _load_skill, - _convert_words, - _convert_word, - _convert_phrases, - _convert_phrase, _load_dictionary, ) from librelingo_fakes import fakes @@ -40,12 +35,12 @@ def create_patch(self, module): patcher = patch(module) self.addCleanup(patcher.stop) mock = patcher.start() - mock.return_value = random.randint(0, 1000) + mock.return_value = fakes.number() return mock def setUp(self): self.setUpPyfakefs() - self.fake_path = Path(".") / str(random.randint(0, 1000)) + self.fake_path = fakes.path() self.fake_path.mkdir() self.fake_values = self.get_fake_values() self.result = None @@ -713,162 +708,9 @@ def test_calls_convert_phrases_with_correct_values(self): self.convert_phrases.assert_called_with(self.fake_values["fake_phrases"]) -class TestConvertWords(TestCase): - def test_returns_a_list(self): - self.assertIsInstance(_convert_words([]), list) - - @patch("librelingo_yaml_loader.yaml_loader._convert_word") - def test_converts_every_word(self, _convert_word): - raw_words = [None] * random.randint(0, 1000) - self.assertEqual(len(_convert_words(raw_words)), len(raw_words)) - - @patch("librelingo_yaml_loader.yaml_loader._convert_word") - def test_returns_correct_value(self, convert_word): - convert_word.return_value = fakes.fake_value() - self.assertEqual(_convert_words([None]), [convert_word.return_value]) - - @patch("librelingo_yaml_loader.yaml_loader._convert_word") - def test_calls_convert_word_with_correct_values(self, convert_word): - # pylint: disable=no-self-use - word1 = fakes.fake_value() - word2 = fakes.fake_value() - _convert_words([word1, word2]) - convert_word.assert_any_call(word1) - convert_word.assert_any_call(word2) - - -class TestConvertWord(TestCase): - def setUp(self): - self.fakeWord = { - "Images": fakes.fake_value(), - "Word": fakes.fake_value(), - "Synonyms": [ - fakes.fake_value(), - fakes.fake_value(), - ], - "Translation": fakes.fake_value(), - "Also accepted": [ - fakes.fake_value(), - fakes.fake_value(), - ], - } - - def test_returns_a_word_object(self): - self.assertIsInstance(_convert_word(self.fakeWord), Word) - - def test_includes_the_correct_pictures(self): - self.assertEqual(_convert_word(self.fakeWord).pictures, self.fakeWord["Images"]) - - def test_pictures_are_optional(self): - del self.fakeWord["Images"] - self.assertIsNone(_convert_word(self.fakeWord).pictures) - - def test_includes_main_word(self): - self.assertEqual( - _convert_word(self.fakeWord).in_target_language[0], self.fakeWord["Word"] - ) - - def test_includes_synonyms(self): - result = _convert_word(self.fakeWord).in_target_language - self.assertIn(self.fakeWord["Synonyms"][0], result) - self.assertIn(self.fakeWord["Synonyms"][1], result) - - def test_synonyms_are_optional(self): - del self.fakeWord["Synonyms"] - self.assertEqual(len(_convert_word(self.fakeWord).in_target_language), 1) - - def test_includes_translation(self): - self.assertEqual( - _convert_word(self.fakeWord).in_source_language[0], - self.fakeWord["Translation"], - ) - - def test_includes_alternative_translations(self): - result = _convert_word(self.fakeWord).in_source_language - self.assertIn(self.fakeWord["Also accepted"][0], result) - self.assertIn(self.fakeWord["Also accepted"][1], result) - - def test_alternative_translations_are_optional(self): - del self.fakeWord["Also accepted"] - self.assertEqual(len(_convert_word(self.fakeWord).in_source_language), 1) - - -class TestConvertPhrases(TestCase): - def test_returns_a_list(self): - self.assertIsInstance(_convert_phrases([]), list) - - @patch("librelingo_yaml_loader.yaml_loader._convert_phrase") - def test_converts_every_word(self, convert_phrase): - raw_words = [None] * random.randint(0, 1000) - self.assertEqual(len(_convert_phrases(raw_words)), len(raw_words)) - - @patch("librelingo_yaml_loader.yaml_loader._convert_phrase") - def test_returns_correct_value(self, convert_phrase): - convert_phrase.return_value = fakes.fake_value() - self.assertEqual(_convert_phrases([None]), [convert_phrase.return_value]) - - @patch("librelingo_yaml_loader.yaml_loader._convert_phrase") - def test_calls_convert_phrases_with_correct_values(self, convert_phrase): - # pylint: disable=no-self-use - word1 = fakes.fake_value() - word2 = fakes.fake_value() - _convert_phrases([word1, word2]) - convert_phrase.assert_any_call(word1) - convert_phrase.assert_any_call(word2) - - -class TestConvertPhrase(TestCase): - def setUp(self): - self.fakePhrase = { - "Phrase": fakes.fake_value(), - "Alternative versions": [ - fakes.fake_value(), - fakes.fake_value(), - ], - "Translation": fakes.fake_value(), - "Alternative translations": [ - fakes.fake_value(), - fakes.fake_value(), - ], - } - - def test_returns_a_phrase_object(self): - self.assertIsInstance(_convert_phrase(self.fakePhrase), Phrase) - - def test_includes_main_version(self): - self.assertEqual( - _convert_phrase(self.fakePhrase).in_target_language[0], - self.fakePhrase["Phrase"], - ) - - def test_includes_alternative_versions(self): - result = _convert_phrase(self.fakePhrase).in_target_language - self.assertIn(self.fakePhrase["Alternative versions"][0], result) - self.assertIn(self.fakePhrase["Alternative versions"][1], result) - - def test_alternative_versions_are_optional(self): - del self.fakePhrase["Alternative versions"] - self.assertEqual(len(_convert_phrase(self.fakePhrase).in_target_language), 1) - - def test_includes_translation(self): - self.assertEqual( - _convert_phrase(self.fakePhrase).in_source_language[0], - self.fakePhrase["Translation"], - ) - - def test_includes_alternative_translations(self): - result = _convert_phrase(self.fakePhrase).in_source_language - self.assertIn(self.fakePhrase["Alternative translations"][0], result) - self.assertIn(self.fakePhrase["Alternative translations"][1], result) - - def test_alternative_translations_are_optional(self): - del self.fakePhrase["Alternative translations"] - self.assertEqual(len(_convert_phrase(self.fakePhrase).in_source_language), 1) - - def get_fake_word_values(): - in_target_language = [str(random.randint(0, 1000))] - in_source_language = [str(random.randint(0, 1000))] + in_target_language = [str(fakes.number())] + in_source_language = [str(fakes.number())] return in_source_language, in_target_language @@ -983,149 +825,144 @@ def test_load_dictionary_includes_duplicate_words_includes_multiple_definitions( @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_module_complains_about_an_empty_file(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = None with pytest.raises( RuntimeError, - match=f'Module file "{randomPath}/module.yaml" is empty or does not exist', + match=f'Module file "{random_path}/module.yaml" is empty or does not exist', ): - _load_module(randomPath, fakes.course1) + _load_module(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_module_complains_missing_module_key(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = {} expected_error = ( - f'Module file "{randomPath}/module.yaml" needs to have a "Module" key' + f'Module file "{random_path}/module.yaml" needs to have a "Module" key' ) with pytest.raises(RuntimeError, match=expected_error): - _load_module(randomPath, fakes.course1) + _load_module(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_module_complains_missing_skills_key(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = {"Module": {}} expected_error = ( - f'Module file "{randomPath}/module.yaml" needs to have a "Skills" key' + f'Module file "{random_path}/module.yaml" needs to have a "Skills" key' ) with pytest.raises(RuntimeError, match=expected_error): - _load_module(randomPath, fakes.course1) + _load_module(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_module_complains_missing_module_name(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = {"Module": {}, "Skills": []} - expected_error = f'Module file "{randomPath}/module.yaml" needs to have module name' + expected_error = ( + f'Module file "{random_path}/module.yaml" needs to have module name' + ) with pytest.raises(RuntimeError, match=expected_error): - _load_module(randomPath, fakes.course1) + _load_module(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skills_complains_missing_skills(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() expected_error = ( - f'Module file "{randomPath}/module.yaml" needs to have a list of skills' + f'Module file "{random_path}/module.yaml" needs to have a list of skills' ) with pytest.raises(RuntimeError, match=expected_error): - _load_skills(randomPath, skills=None, course=fakes.course1) + _load_skills(random_path, skills=None, course=fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_about_an_empty_file(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = None with pytest.raises( - RuntimeError, match=f'Skill file "{randomPath}" is empty or does not exist' + RuntimeError, match=f'Skill file "{random_path}" is empty or does not exist' ): - _load_skill(randomPath, fakes.course1) + _load_skill(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_missing_skills_key(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = {} - expected_error = f'Skill file "{randomPath}" needs to have a "Skill" key' + expected_error = f'Skill file "{random_path}" needs to have a "Skill" key' with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fakes.course1) + _load_skill(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_missing_new_words_key(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = {"Skill": []} - expected_error = f'Skill file "{randomPath}" needs to have a "New words" key' + expected_error = f'Skill file "{random_path}" needs to have a "New words" key' with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fakes.course1) + _load_skill(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_missing_skill_name(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = {"Skill": {}, "New words": [], "Phrases": []} - expected_error = f'Skill file "{randomPath}" needs to have skill name' + expected_error = f'Skill file "{random_path}" needs to have skill name' with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fakes.course1) + _load_skill(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_missing_skill_id(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = {"Skill": {"Name": "asd"}, "New words": [], "Phrases": []} - expected_error = f'Skill file "{randomPath}" needs to have skill id' + expected_error = f'Skill file "{random_path}" needs to have skill id' with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fakes.course1) + _load_skill(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_doesnt_fail_without_thumnails(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = { "Skill": {"Name": "asd", "Id": "4234234"}, "New words": [], "Phrases": [], } - _load_skill(randomPath, fakes.course1) + _load_skill(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_about_invalid_phrase(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = { "Skill": {"Name": "asd", "Id": 32423423}, "New words": [], "Phrases": [""], } - expected_error = f'Skill file "{randomPath}" has an invalid phrase' + expected_error = f'Skill file "{random_path}" has an invalid phrase' with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fakes.course1) + _load_skill(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_about_invalid_word(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() load_yaml.return_value = { "Skill": {"Name": "asd", "Id": 32423423}, "Phrases": [], "New words": [""], } - expected_error = f'Skill file "{randomPath}" has an invalid word' - with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fakes.course1) - - -def test_convert_phrase_complains_about_missing_translation(): - randomPhrase = str(random.randint(0, 1000)) - expected_error = f'Phrase "{randomPhrase}" needs to have a "Translation".' + expected_error = f'Skill file "{random_path}" has an invalid word' with pytest.raises(RuntimeError, match=expected_error): - _convert_phrase({"Phrase": randomPhrase}) + _load_skill(random_path, fakes.course1) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_about_misspelled_word_in_source_language(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() fake_word_value = str(fakes.fake_value()) load_yaml.return_value = { "Skill": {"Name": "asd", "Id": 32423423}, @@ -1153,12 +990,12 @@ def test_load_skill_complains_about_misspelled_word_in_source_language(load_yaml f'The {fake_course.source_language.name} word "{fake_word_value}" is misspelled.' ) with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fake_course) + _load_skill(random_path, fake_course) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_about_misspelled_word_in_target_language(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() fake_word_value_simple = str(fakes.fake_value()) fake_word_value = f"the {fake_word_value_simple}" load_yaml.return_value = { @@ -1187,12 +1024,12 @@ def test_load_skill_complains_about_misspelled_word_in_target_language(load_yaml f'The {fake_course.target_language.name} word "{fake_word_value}" is misspelled.' ) with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fake_course) + _load_skill(random_path, fake_course) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_about_misspelled_phrase_in_target_language(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() fake_word = str(fakes.fake_value()) fake_phrase = f"the {fake_word} foo bar" load_yaml.return_value = { @@ -1221,12 +1058,12 @@ def test_load_skill_complains_about_misspelled_phrase_in_target_language(load_ya f'The {fake_course.target_language.name} phrase "{fake_phrase}" is misspelled. The word "{fake_word}" is unknown.' ) with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fake_course) + _load_skill(random_path, fake_course) @patch("librelingo_yaml_loader.yaml_loader._load_yaml") def test_load_skill_complains_about_misspelled_phrase_in_source_language(load_yaml): - randomPath = str(random.randint(0, 1000)) + random_path = fakes.path() fake_word = str(fakes.fake_value()) fake_phrase = f"the {fake_word} foo bar" load_yaml.return_value = { @@ -1255,4 +1092,4 @@ def test_load_skill_complains_about_misspelled_phrase_in_source_language(load_ya f'The {fake_course.source_language.name} phrase "{fake_phrase}" is misspelled. The word "{fake_word}" is unknown.' ) with pytest.raises(RuntimeError, match=expected_error): - _load_skill(randomPath, fake_course) + _load_skill(random_path, fake_course) diff --git a/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_phrase.py b/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_phrase.py new file mode 100644 index 000000000000..b1ec0f5a086c --- /dev/null +++ b/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_phrase.py @@ -0,0 +1,91 @@ +""" +this file contains tests of the funcion +librelingo_yaml_loader.yaml_loader._convert_phrase +""" +import pytest + +from librelingo_types import Phrase +from librelingo_yaml_loader.yaml_loader import _convert_phrase + +from librelingo_fakes import fakes + + +def _access_functions(in_key): + def getter(raw_phrase): + return raw_phrase[in_key] + + def remover(raw_phrase): + del raw_phrase[in_key] + + return getter, remover + + +_GET_ALTERNATIVE_VERSIONS, _REMOVE_ALTERNATIVE_VERSIONS = _access_functions( + "Alternative versions" +) + +_GET_ALTERNATIVE_TRANSLATIONS, _REMOVE_ALTERNATIVE_TRANSLATIONS = _access_functions( + "Alternative translations" +) + + +@pytest.fixture +def raw_fake_phrase(): + """returns the raw data describing the phrase used in tests in this file""" + return { + "Phrase": fakes.fake_value(), + "Alternative versions": [ + fakes.fake_value(), + fakes.fake_value(), + ], + "Translation": fakes.fake_value(), + "Alternative translations": [ + fakes.fake_value(), + fakes.fake_value(), + ], + } + + +def test_returns_a_phrase_object(raw_fake_phrase): + assert isinstance(_convert_phrase(raw_fake_phrase), Phrase) + + +def test_includes_main_version(raw_fake_phrase): + assert ( + _convert_phrase(raw_fake_phrase).in_target_language[0] + == raw_fake_phrase["Phrase"] + ) + + +def test_includes_alternative_versions(raw_fake_phrase): + result = _convert_phrase(raw_fake_phrase).in_target_language + assert all(_ in result for _ in _GET_ALTERNATIVE_VERSIONS(raw_fake_phrase)) + + +def test_alternative_versions_are_optional(raw_fake_phrase): + _REMOVE_ALTERNATIVE_VERSIONS(raw_fake_phrase) + assert len(_convert_phrase(raw_fake_phrase).in_target_language) == 1 + + +def test_includes_translation(raw_fake_phrase): + assert ( + _convert_phrase(raw_fake_phrase).in_source_language[0] + == raw_fake_phrase["Translation"] + ) + + +def test_includes_alternative_translations(raw_fake_phrase): + result = _convert_phrase(raw_fake_phrase).in_source_language + assert all(_ in result for _ in _GET_ALTERNATIVE_TRANSLATIONS(raw_fake_phrase)) + + +def test_alternative_translations_are_optional(raw_fake_phrase): + _REMOVE_ALTERNATIVE_TRANSLATIONS(raw_fake_phrase) + assert len(_convert_phrase(raw_fake_phrase).in_source_language) == 1 + + +def test_convert_phrase_complains_about_missing_translation(): + random_phrase = fakes.number() + expected_error = f'Phrase "{random_phrase}" needs to have a "Translation".' + with pytest.raises(RuntimeError, match=expected_error): + _convert_phrase({"Phrase": random_phrase}) diff --git a/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_phrases.py b/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_phrases.py new file mode 100644 index 000000000000..dc68128253e5 --- /dev/null +++ b/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_phrases.py @@ -0,0 +1,39 @@ +""" +this file contains tests of the funcion +librelingo_yaml_loader.yaml_loader._convert_phrases +""" +import pytest + +from librelingo_yaml_loader.yaml_loader import _convert_phrases + +from librelingo_fakes import fakes + + +def test_returns_a_list(): + assert isinstance(_convert_phrases([]), list) + + +@pytest.fixture +def mock_convert_phrase(mocker): + return mocker.patch("librelingo_yaml_loader.yaml_loader._convert_phrase") + + +def test_converts_every_word(mock_convert_phrase): + # pylint: disable=unused-argument + # mock_convert_phrase is needed here becasue + # _convert_phrase crashes for input None + raw_words = [None] * fakes.number() + assert len(_convert_phrases(raw_words)) == len(raw_words) + + +def test_returns_correct_value(mock_convert_phrase): + mock_convert_phrase.return_value = fakes.fake_value() + assert _convert_phrases([None]) == [mock_convert_phrase.return_value] + + +def test_calls_convert_phrases_with_correct_values(mock_convert_phrase): + word1 = fakes.fake_value() + word2 = fakes.fake_value() + _convert_phrases([word1, word2]) + mock_convert_phrase.assert_any_call(word1) + mock_convert_phrase.assert_any_call(word2) diff --git a/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_word.py b/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_word.py new file mode 100644 index 000000000000..f419b9d68ce1 --- /dev/null +++ b/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_word.py @@ -0,0 +1,85 @@ +""" +this file contains tests of the funcion +librelingo_yaml_loader.yaml_loader._convert_word +""" +import pytest + +from librelingo_types import Word +from librelingo_yaml_loader.yaml_loader import _convert_word + +from librelingo_fakes import fakes + + +def _access_functions(in_key): + def getter(raw_word): + return raw_word[in_key] + + def remover(raw_word): + del raw_word[in_key] + + return getter, remover + + +_GET_ALSO_ACCEPTED, _REMOVE_ALSO_ACCEPTED = _access_functions("Also accepted") + + +@pytest.fixture +def raw_fake_word(): + """returns the raw data describing the word used in tests in this file""" + return { + "Images": fakes.fake_value(), + "Word": fakes.fake_value(), + "Synonyms": [ + fakes.fake_value(), + fakes.fake_value(), + ], + "Translation": fakes.fake_value(), + "Also accepted": [ + fakes.fake_value(), + fakes.fake_value(), + ], + } + + +def test_returns_a_word_object(raw_fake_word): + assert isinstance(_convert_word(raw_fake_word), Word) + + +def test_includes_the_correct_pictures(raw_fake_word): + assert _convert_word(raw_fake_word).pictures == raw_fake_word["Images"] + + +def test_pictures_are_optional(raw_fake_word): + del raw_fake_word["Images"] + assert _convert_word(raw_fake_word).pictures is None + + +def test_includes_main_word(raw_fake_word): + assert _convert_word(raw_fake_word).in_target_language[0] == raw_fake_word["Word"] + + +def test_includes_synonyms(raw_fake_word): + result = _convert_word(raw_fake_word).in_target_language + assert all(_ in result for _ in raw_fake_word["Synonyms"]) + + +def test_synonyms_are_optional(raw_fake_word): + del raw_fake_word["Synonyms"] + assert len(_convert_word(raw_fake_word).in_target_language) == 1 + + +def test_includes_translation(raw_fake_word): + assert ( + _convert_word(raw_fake_word).in_source_language[0] + == raw_fake_word["Translation"] + ) + + +def test_includes_alternative_translations(raw_fake_word): + result = _convert_word(raw_fake_word).in_source_language + assert all(_ in result for _ in _GET_ALSO_ACCEPTED(raw_fake_word)) + + +def test_alternative_translations_are_optional(raw_fake_word): + _REMOVE_ALSO_ACCEPTED(raw_fake_word) + assert len(_convert_word(raw_fake_word).in_source_language) == 1 diff --git a/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_words.py b/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_words.py new file mode 100644 index 000000000000..c7b21eb618d9 --- /dev/null +++ b/apps/librelingo_yaml_loader/tests/test_yaml_loader_convert_words.py @@ -0,0 +1,37 @@ +""" +this file contains tests of the funcion +librelingo_yaml_loader.yaml_loader._convert_words +""" +import pytest + +from librelingo_yaml_loader.yaml_loader import _convert_words + +from librelingo_fakes import fakes + + +def test_returns_a_list(): + assert isinstance(_convert_words([]), list) + + +@pytest.fixture +def mock_convert_word(mocker): + return mocker.patch("librelingo_yaml_loader.yaml_loader._convert_word") + + +def test_converts_every_word(mock_convert_word): + # pylint: disable=unused-argument + raw_words = [None] * fakes.number() + assert len(_convert_words(raw_words)) == len(raw_words) + + +def test_returns_correct_value(mock_convert_word): + mock_convert_word.return_value = fakes.fake_value() + assert _convert_words([None]) == [mock_convert_word.return_value] + + +def test_calls_convert_word_with_correct_values(mock_convert_word): + word1 = fakes.fake_value() + word2 = fakes.fake_value() + _convert_words([word1, word2]) + mock_convert_word.assert_any_call(word1) + mock_convert_word.assert_any_call(word2)