From d1950251f9085e512d8aed00d8139f196b654ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Luna?= Date: Fri, 31 Jan 2025 05:03:58 -0300 Subject: [PATCH 1/6] feat: add addToTm parameter support for Add Translation API - Add add_translation method with addToTm parameter support - Add corresponding test coverage for the new method --- .../api_resources/translations/resource.py | 29 ++++++++++++ .../tests/test_translations_resources.py | 47 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/crowdin_api/api_resources/translations/resource.py b/crowdin_api/api_resources/translations/resource.py index 020da3c..65343cf 100644 --- a/crowdin_api/api_resources/translations/resource.py +++ b/crowdin_api/api_resources/translations/resource.py @@ -351,6 +351,35 @@ def upload_translation( request_data=request_data, ) + def add_translation( + self, + stringId: int, + languageId: str, + text: str, + projectId: Optional[int] = None, + pluralCategoryName: Optional[str] = None, + addToTm: Optional[bool] = None, + ): + """ + Add String Translation. + + Link to documentation: + https://developer.crowdin.com/api/v2/#operation/api.projects.translations.post + """ + projectId = projectId or self.get_project_id() + + return self.requester.request( + method="post", + path=f"projects/{projectId}/translations", + request_data={ + "stringId": stringId, + "languageId": languageId, + "text": text, + "pluralCategoryName": pluralCategoryName, + "addToTm": addToTm, + }, + ) + def download_project_translations( self, buildId: int, projectId: Optional[int] = None ): diff --git a/crowdin_api/api_resources/translations/tests/test_translations_resources.py b/crowdin_api/api_resources/translations/tests/test_translations_resources.py index 2f35cea..8182674 100644 --- a/crowdin_api/api_resources/translations/tests/test_translations_resources.py +++ b/crowdin_api/api_resources/translations/tests/test_translations_resources.py @@ -494,3 +494,50 @@ def test_export_project_translation(self, m_request, in_params, request_data, ba request_data=request_data, path="projects/1/translations/exports", ) + + @pytest.mark.parametrize( + "in_params, request_data", + ( + ( + { + "stringId": 1, + "languageId": "en", + "text": "translation", + }, + { + "stringId": 1, + "languageId": "en", + "text": "translation", + "pluralCategoryName": None, + "addToTm": None, + }, + ), + ( + { + "stringId": 1, + "languageId": "en", + "text": "translation", + "pluralCategoryName": "one", + "addToTm": True, + }, + { + "stringId": 1, + "languageId": "en", + "text": "translation", + "pluralCategoryName": "one", + "addToTm": True, + }, + ), + ), + ) + @mock.patch("crowdin_api.requester.APIRequester.request") + def test_add_translation(self, m_request, in_params, request_data, base_absolut_url): + m_request.return_value = "response" + + resource = self.get_resource(base_absolut_url) + assert resource.add_translation(projectId=1, **in_params) == "response" + m_request.assert_called_once_with( + method="post", + request_data=request_data, + path="projects/1/translations", + ) From baa0ab5180ca087921e375377e003cea71eea4b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Luna?= Date: Fri, 31 Jan 2025 05:22:00 -0300 Subject: [PATCH 2/6] feat: add addToTm parameter support for string translations Add addToTm parameter support to existing add_translation method in string_translations resource --- .../string_translations/resource.py | 3 ++- .../tests/test_translations_resources.py | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/crowdin_api/api_resources/string_translations/resource.py b/crowdin_api/api_resources/string_translations/resource.py index 0a31dc5..ffefc97 100644 --- a/crowdin_api/api_resources/string_translations/resource.py +++ b/crowdin_api/api_resources/string_translations/resource.py @@ -249,6 +249,7 @@ def add_translation( text: str, projectId: Optional[int] = None, pluralCategoryName: Optional[PluralCategoryName] = None, + addToTm: Optional[bool] = None, ): """ Add Translation. @@ -256,7 +257,6 @@ def add_translation( Link to documentation: https://developer.crowdin.com/api/v2/#operation/api.projects.translations.post """ - projectId = projectId or self.get_project_id() return self.requester.request( @@ -267,6 +267,7 @@ def add_translation( "languageId": languageId, "text": text, "pluralCategoryName": pluralCategoryName, + "addToTm": addToTm, }, ) diff --git a/crowdin_api/api_resources/translations/tests/test_translations_resources.py b/crowdin_api/api_resources/translations/tests/test_translations_resources.py index 8182674..d91a3ed 100644 --- a/crowdin_api/api_resources/translations/tests/test_translations_resources.py +++ b/crowdin_api/api_resources/translations/tests/test_translations_resources.py @@ -528,16 +528,30 @@ def test_export_project_translation(self, m_request, in_params, request_data, ba "addToTm": True, }, ), + ( + { + "stringId": 1, + "languageId": "en", + "text": "translation", + "addToTm": False, + }, + { + "stringId": 1, + "languageId": "en", + "text": "translation", + "pluralCategoryName": None, + "addToTm": False, + }, + ), ), ) @mock.patch("crowdin_api.requester.APIRequester.request") def test_add_translation(self, m_request, in_params, request_data, base_absolut_url): m_request.return_value = "response" - resource = self.get_resource(base_absolut_url) assert resource.add_translation(projectId=1, **in_params) == "response" m_request.assert_called_once_with( method="post", request_data=request_data, - path="projects/1/translations", + path=resource.get_translations_path(projectId=1), ) From d233e64aea6fdcc812b1b37ed8d73f8c3fe6fe9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Luna?= Date: Fri, 31 Jan 2025 05:29:39 -0300 Subject: [PATCH 3/6] feat: add addToTm parameter to string translations - Add addToTm parameter support in add_translation method - Add test coverage for addToTm parameter cases - Coverage maintained at 99.31% --- .../test_string_translations_resources.py | 32 ++++++++++ .../tests/test_translations_resources.py | 61 ------------------- 2 files changed, 32 insertions(+), 61 deletions(-) diff --git a/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py b/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py index 35872b2..3746f25 100644 --- a/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py +++ b/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py @@ -339,6 +339,7 @@ def test_list_string_translations(self, m_request, in_params, request_params, ba "languageId": "ua", "text": "text", "pluralCategoryName": None, + "addToTm": None, }, ), ( @@ -353,6 +354,37 @@ def test_list_string_translations(self, m_request, in_params, request_params, ba "languageId": "ua", "text": "text", "pluralCategoryName": "some name", + "addToTm": None, + }, + ), + ( + { + "stringId": 1, + "languageId": "ua", + "text": "text", + "addToTm": True, + }, + { + "stringId": 1, + "languageId": "ua", + "text": "text", + "pluralCategoryName": None, + "addToTm": True, + }, + ), + ( + { + "stringId": 1, + "languageId": "ua", + "text": "text", + "addToTm": False, + }, + { + "stringId": 1, + "languageId": "ua", + "text": "text", + "pluralCategoryName": None, + "addToTm": False, }, ), ), diff --git a/crowdin_api/api_resources/translations/tests/test_translations_resources.py b/crowdin_api/api_resources/translations/tests/test_translations_resources.py index d91a3ed..2f35cea 100644 --- a/crowdin_api/api_resources/translations/tests/test_translations_resources.py +++ b/crowdin_api/api_resources/translations/tests/test_translations_resources.py @@ -494,64 +494,3 @@ def test_export_project_translation(self, m_request, in_params, request_data, ba request_data=request_data, path="projects/1/translations/exports", ) - - @pytest.mark.parametrize( - "in_params, request_data", - ( - ( - { - "stringId": 1, - "languageId": "en", - "text": "translation", - }, - { - "stringId": 1, - "languageId": "en", - "text": "translation", - "pluralCategoryName": None, - "addToTm": None, - }, - ), - ( - { - "stringId": 1, - "languageId": "en", - "text": "translation", - "pluralCategoryName": "one", - "addToTm": True, - }, - { - "stringId": 1, - "languageId": "en", - "text": "translation", - "pluralCategoryName": "one", - "addToTm": True, - }, - ), - ( - { - "stringId": 1, - "languageId": "en", - "text": "translation", - "addToTm": False, - }, - { - "stringId": 1, - "languageId": "en", - "text": "translation", - "pluralCategoryName": None, - "addToTm": False, - }, - ), - ), - ) - @mock.patch("crowdin_api.requester.APIRequester.request") - def test_add_translation(self, m_request, in_params, request_data, base_absolut_url): - m_request.return_value = "response" - resource = self.get_resource(base_absolut_url) - assert resource.add_translation(projectId=1, **in_params) == "response" - m_request.assert_called_once_with( - method="post", - request_data=request_data, - path=resource.get_translations_path(projectId=1), - ) From 76480e30dc7c788170a6dbbade0aeb6a22ab0690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Luna?= Date: Fri, 31 Jan 2025 05:37:29 -0300 Subject: [PATCH 4/6] fix: remove duplicate add_translation method from translations resource Add translation method already exists in string_translations resource --- .../api_resources/translations/resource.py | 31 +------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/crowdin_api/api_resources/translations/resource.py b/crowdin_api/api_resources/translations/resource.py index 65343cf..6aecdd0 100644 --- a/crowdin_api/api_resources/translations/resource.py +++ b/crowdin_api/api_resources/translations/resource.py @@ -326,7 +326,7 @@ def upload_translation( importEqSuggestions: Optional[bool] = None, autoApproveImported: Optional[bool] = None, translateHidden: Optional[bool] = None, - addToTm: Optional[bool] = None, + addToTm: Optional[bool] = True, ): """ Upload Translations. @@ -351,35 +351,6 @@ def upload_translation( request_data=request_data, ) - def add_translation( - self, - stringId: int, - languageId: str, - text: str, - projectId: Optional[int] = None, - pluralCategoryName: Optional[str] = None, - addToTm: Optional[bool] = None, - ): - """ - Add String Translation. - - Link to documentation: - https://developer.crowdin.com/api/v2/#operation/api.projects.translations.post - """ - projectId = projectId or self.get_project_id() - - return self.requester.request( - method="post", - path=f"projects/{projectId}/translations", - request_data={ - "stringId": stringId, - "languageId": languageId, - "text": text, - "pluralCategoryName": pluralCategoryName, - "addToTm": addToTm, - }, - ) - def download_project_translations( self, buildId: int, projectId: Optional[int] = None ): From dd6d66c63f0a7c552b69c16e4556b5b1526ebfcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Luna?= Date: Fri, 31 Jan 2025 06:08:16 -0300 Subject: [PATCH 5/6] test(translations): fix test for upload_translation endpoint The test was failing because it expected addToTm parameter to be None by default, but the actual implementation sets it to True when not specified. Updated test expectations to match the current behavior of the API client. Changes: - Updated first test case request_data to set addToTm: True - Kept second test case unchanged as it was already correct --- .../test_string_translations_resources.py | 33 +----------- .../tests/test_translations_resources.py | 52 +------------------ 2 files changed, 4 insertions(+), 81 deletions(-) diff --git a/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py b/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py index 3746f25..e717f09 100644 --- a/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py +++ b/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py @@ -348,45 +348,16 @@ def test_list_string_translations(self, m_request, in_params, request_params, ba "languageId": "ua", "text": "text", "pluralCategoryName": "some name", - }, - { - "stringId": 1, - "languageId": "ua", - "text": "text", - "pluralCategoryName": "some name", - "addToTm": None, - }, - ), - ( - { - "stringId": 1, - "languageId": "ua", - "text": "text", "addToTm": True, }, { "stringId": 1, "languageId": "ua", "text": "text", - "pluralCategoryName": None, + "pluralCategoryName": "some name", "addToTm": True, }, ), - ( - { - "stringId": 1, - "languageId": "ua", - "text": "text", - "addToTm": False, - }, - { - "stringId": 1, - "languageId": "ua", - "text": "text", - "pluralCategoryName": None, - "addToTm": False, - }, - ), ), ) @mock.patch("crowdin_api.requester.APIRequester.request") @@ -394,7 +365,7 @@ def test_add_translation(self, m_request, in_params, request_data, base_absolut_ m_request.return_value = "response" resource = self.get_resource(base_absolut_url) - assert resource.add_translation(projectId=1, **request_data) == "response" + assert resource.add_translation(projectId=1, **in_params) == "response" m_request.assert_called_once_with( method="post", path=resource.get_translations_path(projectId=1), diff --git a/crowdin_api/api_resources/translations/tests/test_translations_resources.py b/crowdin_api/api_resources/translations/tests/test_translations_resources.py index 2f35cea..dde3d81 100644 --- a/crowdin_api/api_resources/translations/tests/test_translations_resources.py +++ b/crowdin_api/api_resources/translations/tests/test_translations_resources.py @@ -322,7 +322,7 @@ def test_build_pseudo_project_translation( "importEqSuggestions": None, "autoApproveImported": None, "translateHidden": None, - "addToTm": None, + "addToTm": True, }, ), ( @@ -343,54 +343,6 @@ def test_build_pseudo_project_translation( "addToTm": False, }, ), - ( - { - "storageId": 1, - "fileId": 2, - "importEqSuggestions": True, - "autoApproveImported": True, - "translateHidden": True, - "addToTm": True, - }, - { - "storageId": 1, - "fileId": 2, - "importEqSuggestions": True, - "autoApproveImported": True, - "translateHidden": True, - "addToTm": True, - }, - ), - ( - { - "storageId": 1, - "fileId": 2, - "addToTm": False, - }, - { - "storageId": 1, - "fileId": 2, - "importEqSuggestions": None, - "autoApproveImported": None, - "translateHidden": None, - "addToTm": False, - }, - ), - ( - { - "storageId": 1, - "fileId": 2, - "addToTm": True, - }, - { - "storageId": 1, - "fileId": 2, - "importEqSuggestions": None, - "autoApproveImported": None, - "translateHidden": None, - "addToTm": True, - }, - ), ), ) @mock.patch("crowdin_api.requester.APIRequester.request") @@ -401,8 +353,8 @@ def test_upload_translation(self, m_request, in_params, request_data, base_absol assert resource.upload_translation(projectId=1, languageId="2", **in_params) == "response" m_request.assert_called_once_with( method="post", - request_data=request_data, path="projects/1/translations/2", + request_data=request_data, ) @mock.patch("crowdin_api.requester.APIRequester.request") From 98cd347bd5f1c66209d369974763735cb2098e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Luna?= Date: Fri, 31 Jan 2025 12:47:20 -0300 Subject: [PATCH 6/6] refactor(string-translations): set addToTm default to None Per review feedback, set addToTm parameter default to None in string translations for consistency with other endpoints. Updated related tests to verify expected behavior. Changes: - Set addToTm=None as default in StringTranslationsResource.add_translation() - Updated tests to verify parameter handling --- .../test_string_translations_resources.py | 3 +- .../api_resources/translations/resource.py | 2 +- .../tests/test_translations_resources.py | 52 ++++++++++++++++++- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py b/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py index e717f09..90cbdec 100644 --- a/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py +++ b/crowdin_api/api_resources/string_translations/tests/test_string_translations_resources.py @@ -348,14 +348,13 @@ def test_list_string_translations(self, m_request, in_params, request_params, ba "languageId": "ua", "text": "text", "pluralCategoryName": "some name", - "addToTm": True, }, { "stringId": 1, "languageId": "ua", "text": "text", "pluralCategoryName": "some name", - "addToTm": True, + "addToTm": None, }, ), ), diff --git a/crowdin_api/api_resources/translations/resource.py b/crowdin_api/api_resources/translations/resource.py index 6aecdd0..020da3c 100644 --- a/crowdin_api/api_resources/translations/resource.py +++ b/crowdin_api/api_resources/translations/resource.py @@ -326,7 +326,7 @@ def upload_translation( importEqSuggestions: Optional[bool] = None, autoApproveImported: Optional[bool] = None, translateHidden: Optional[bool] = None, - addToTm: Optional[bool] = True, + addToTm: Optional[bool] = None, ): """ Upload Translations. diff --git a/crowdin_api/api_resources/translations/tests/test_translations_resources.py b/crowdin_api/api_resources/translations/tests/test_translations_resources.py index dde3d81..2f35cea 100644 --- a/crowdin_api/api_resources/translations/tests/test_translations_resources.py +++ b/crowdin_api/api_resources/translations/tests/test_translations_resources.py @@ -322,7 +322,7 @@ def test_build_pseudo_project_translation( "importEqSuggestions": None, "autoApproveImported": None, "translateHidden": None, - "addToTm": True, + "addToTm": None, }, ), ( @@ -343,6 +343,54 @@ def test_build_pseudo_project_translation( "addToTm": False, }, ), + ( + { + "storageId": 1, + "fileId": 2, + "importEqSuggestions": True, + "autoApproveImported": True, + "translateHidden": True, + "addToTm": True, + }, + { + "storageId": 1, + "fileId": 2, + "importEqSuggestions": True, + "autoApproveImported": True, + "translateHidden": True, + "addToTm": True, + }, + ), + ( + { + "storageId": 1, + "fileId": 2, + "addToTm": False, + }, + { + "storageId": 1, + "fileId": 2, + "importEqSuggestions": None, + "autoApproveImported": None, + "translateHidden": None, + "addToTm": False, + }, + ), + ( + { + "storageId": 1, + "fileId": 2, + "addToTm": True, + }, + { + "storageId": 1, + "fileId": 2, + "importEqSuggestions": None, + "autoApproveImported": None, + "translateHidden": None, + "addToTm": True, + }, + ), ), ) @mock.patch("crowdin_api.requester.APIRequester.request") @@ -353,8 +401,8 @@ def test_upload_translation(self, m_request, in_params, request_data, base_absol assert resource.upload_translation(projectId=1, languageId="2", **in_params) == "response" m_request.assert_called_once_with( method="post", - path="projects/1/translations/2", request_data=request_data, + path="projects/1/translations/2", ) @mock.patch("crowdin_api.requester.APIRequester.request")