from unittest import mock from unittest.mock import patch import faker import pytest from fansifter_common.adapters.poeditor import POEditorClient from fansifter_common.translation.services import ( ExportTranslationService, TranslationService, ) from fansifter_common.translation.types import Terms @pytest.fixture def poeditor_client_mock() -> mock.MagicMock: return mock.MagicMock(spec=POEditorClient) @pytest.fixture def export_translation_service( poeditor_client_mock: mock.MagicMock, ) -> ExportTranslationService: return ExportTranslationService(poeditor_client=poeditor_client_mock) def test_get_translations_for_export( export_translation_service: ExportTranslationService, poeditor_client_mock: mock.MagicMock, faker: faker.Faker, ) -> None: languages = {"en", "uk"} terms = {faker.word(): faker.word, faker.word(): faker.word()} poeditor_client_mock.get_available_project_languages.return_value = languages poeditor_client_mock.get_project_terms_translation_for_lang.return_value = terms translations = export_translation_service.get_project_translations( project_id=faker.pyint() ) assert translations == {"en": terms, "uk": terms} @patch("fansifter_common.translation.services.get_translations") def test_get_translation_for_country_code( mock_get_translations: mock.MagicMock, ) -> None: mock_translations = { "en": Terms( optInInfo="optIn_en", unsubscribe="unsub_en", optInInfoExternal="optIn_ext_en", ), "es": Terms( optInInfo="optIn_es", unsubscribe="unsub_es", optInInfoExternal="optIn_ext_es", ), } mock_get_translations.return_value = mock_translations translation_service = TranslationService(translations_path="fake/path") trans_for_us = translation_service.get_translations_by_country_code("ES") assert trans_for_us["es"] == mock_translations.get("es") trans_for_missing_country = translation_service.get_translations_by_country_code( "RAND" ) assert trans_for_missing_country["en"] == mock_translations.get("en")