Skip to content

Commit

Permalink
Merge pull request #694 from Mara3l/master
Browse files Browse the repository at this point in the history
feat: Add tests for Localization methods
  • Loading branch information
pcerny authored Jun 11, 2024
2 parents 484ec40 + f1f0159 commit fbdd777
Show file tree
Hide file tree
Showing 7 changed files with 7,227 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gooddata-sdk/gooddata_sdk/catalog/workspace/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ def add_metadata_locale(
Args:
workspace_id (str): The ID of the workspace.
target_language (str): The target language for the metadata localization.
translator_func (Optional[Callable]): A function to translate the source text.
translator_func (Callable): A function to translate the source text.
set_locale (bool): Flag to indicate if the locale settings should be updated in the workspace.
Returns:
Expand Down
2,478 changes: 2,478 additions & 0 deletions gooddata-sdk/tests/catalog/fixtures/workspaces/add_metadata_locale.yaml

Large diffs are not rendered by default.

3,022 changes: 3,022 additions & 0 deletions gooddata-sdk/tests/catalog/fixtures/workspaces/clean_metadata_locale.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1,085 changes: 1,085 additions & 0 deletions gooddata-sdk/tests/catalog/fixtures/workspaces/set_metadata_localization.yaml

Large diffs are not rendered by default.

80 changes: 80 additions & 0 deletions gooddata-sdk/tests/catalog/test_catalog_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
from pathlib import Path
from typing import List
from xml.etree import ElementTree as ET

import yaml
from gooddata_sdk import (
Expand Down Expand Up @@ -840,3 +841,82 @@ def test_update_workspace_setting(test_config):
finally:
sdk.catalog_workspace.delete_workspace_setting(test_config["workspace"], setting_id)
assert len(sdk.catalog_workspace.list_workspace_settings(test_config["workspace"])) == 0


@gd_vcr.use_cassette(str(_fixtures_dir / "get_metadata_localization.yaml"))
def test_get_metadata_localization(test_config):
sdk = GoodDataSdk.create(host_=test_config["host"], token_=test_config["token"])
test_workspace = test_config["workspace"]
xliff = sdk.catalog_workspace.get_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

tree = ET.ElementTree(ET.fromstring(xliff))

# Check, if the returned xliff is valid.
assert tree is not None


@gd_vcr.use_cassette(str(_fixtures_dir / "set_metadata_localization.yaml"))
def test_set_metadata_localization(test_config):
sdk = GoodDataSdk.create(host_=test_config["host"], token_=test_config["token"])
test_workspace = test_config["workspace"]
xliff = sdk.catalog_workspace.get_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

sdk.catalog_workspace.set_metadata_localization(workspace_id=test_workspace, encoded_xml=xliff)


@gd_vcr.use_cassette(str(_fixtures_dir / "add_metadata_locale.yaml"))
def test_add_metadata_locale(test_config):
sdk = GoodDataSdk.create(host_=test_config["host"], token_=test_config["token"])
test_workspace = test_config["workspace"]

def translate(
to_translate: str,
already_translated: bool = False,
old_translation: str = "",
):
return f"{to_translate}."

sdk.catalog_workspace.clean_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

xliff_before = sdk.catalog_workspace.get_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

sdk.catalog_workspace.add_metadata_locale(
workspace_id=test_workspace, target_language="fr-FR", translator_func=translate, set_locale=False
)

xliff_after = sdk.catalog_workspace.get_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

sdk.catalog_workspace.clean_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

assert xliff_before != xliff_after


@gd_vcr.use_cassette(str(_fixtures_dir / "clean_metadata_locale.yaml"))
def test_clean_metadata_locale(test_config):
sdk = GoodDataSdk.create(host_=test_config["host"], token_=test_config["token"])
test_workspace = test_config["workspace"]

def translate(
to_translate: str,
already_translated: bool = False,
old_translation: str = "",
):
return f"{to_translate}."

sdk.catalog_workspace.clean_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

xliff_before = sdk.catalog_workspace.get_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

sdk.catalog_workspace.add_metadata_locale(
workspace_id=test_workspace, target_language="fr-FR", translator_func=translate, set_locale=False
)

xliff_after = sdk.catalog_workspace.get_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

assert xliff_before != xliff_after

sdk.catalog_workspace.clean_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

xliff_after = sdk.catalog_workspace.get_metadata_localization(workspace_id=test_workspace, target_language="fr-FR")

assert xliff_before == xliff_after
16 changes: 13 additions & 3 deletions tests-support/tests_support/vcrpy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ def deserialize(self, cassette_string: str) -> dict[str, Any]:
request_body = interaction["request"]["body"]
response_body = interaction["response"]["body"]
if request_body is not None:
interaction["request"]["body"] = json.dumps(request_body)
if isinstance(request_body, str) and request_body.startswith("<?xml"):
interaction["request"]["body"] = request_body
else:
interaction["request"]["body"] = json.dumps(request_body)
if response_body is not None and response_body["string"] != "":
try:
interaction["response"]["body"]["string"] = json.dumps(response_body["string"])
if isinstance(response_body["string"], str) and response_body["string"].startswith("<?xml"):
interaction["response"]["body"]["string"] = response_body["string"]
else:
interaction["response"]["body"]["string"] = json.dumps(response_body["string"])
except TypeError:
# this exception is expected while getting XLSX file content
continue
Expand All @@ -56,7 +62,11 @@ def serialize(self, cassette_dict: dict[str, Any]) -> str:
request_body = interaction["request"]["body"]
response_body = interaction["response"]["body"]
if request_body is not None:
interaction["request"]["body"] = json.loads(request_body)
try:
interaction["request"]["body"] = json.loads(request_body)
except (JSONDecodeError, UnicodeDecodeError):
# The response can be in XML
interaction["request"]["body"] = request_body
if response_body is not None and response_body["string"] != "":
try:
interaction["response"]["body"]["string"] = json.loads(response_body["string"])
Expand Down

0 comments on commit fbdd777

Please sign in to comment.