Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Extra warning for get_translation testdata vs. output non-equivalence
Browse files Browse the repository at this point in the history
  • Loading branch information
why-not-try-calmer committed Feb 19, 2023
1 parent ee79490 commit 5abcae2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
6 changes: 2 additions & 4 deletions pytransifex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ def get_translation(
) -> str:
"""Fetch the translation resource matching the given language"""
language = tx_api.Language.get(code=language_code)
path_to_output_dir_as_path = Path(path_to_output_dir)
Path.mkdir(path_to_output_dir_as_path, parents=True, exist_ok=True)

if project := self.get_project(project_slug=project_slug):
if resources := project.fetch("resources"):
Expand All @@ -202,13 +204,9 @@ def get_translation(
resource=resource, language=language
)
translated_content = requests.get(url).text

path_to_output_dir_as_path = Path(path_to_output_dir)
Path.mkdir(path_to_output_dir_as_path, parents=True, exist_ok=True)
path_to_file = path_to_output_dir_as_path.joinpath(
f"{resource_slug}_{language_code}"
)

with open(path_to_file, "w") as fh:
fh.write(translated_content)

Expand Down
27 changes: 21 additions & 6 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pytransifex.api import Transifex
from pytransifex.interfaces import Tx
from tests import test_config
from tests import logging, test_config


class TestNewApi(unittest.TestCase):
Expand All @@ -29,10 +29,10 @@ def setUpClass(cls):
)

if project := cls.tx.get_project(project_slug=cls.project_slug):
print("Found old project, removing.")
logging.info("Found old project, removing.")
project.delete()

print("Creating a brand new project")
logging.info("Creating a brand new project")
cls.tx.create_project(
project_name=cls.project_name, project_slug=cls.project_slug, private=True
)
Expand Down Expand Up @@ -80,13 +80,28 @@ def test7_list_languages(self):
assert languages is not None

def test8_get_translation(self):
path_to_ouput_file = self.tx.get_translation(
path_to_output_file = self.tx.get_translation(
project_slug=self.project_slug,
resource_slug=self.resource_slug,
language_code="fr_CH",
path_to_output_dir=str(self.output_dir),
)
assert Path.exists(Path(path_to_ouput_file))
assert Path.exists(Path(path_to_output_file))

from difflib import Differ
from filecmp import cmp
from sys import stdout

diff = Differ()
if not cmp(path_to_output_file, self.path_to_file):
with open(path_to_output_file, "r") as fh:
f1 = fh.readlines()
with open(path_to_output_file, "r") as fh:
f2 = fh.readlines()
res = list(diff.compare(f1, f2))
logging.warning(
f"Notice that the two files were found to differ: {stdout.writelines(res)}"
)

def test9_project_exists(self):
verdict = self.tx.project_exists(project_slug=self.project_slug)
Expand All @@ -98,7 +113,7 @@ def test10_ping(self):

def test11_stats(self):
stats = self.tx.get_project_stats(project_slug=self.project_slug)
print(str(stats))
logging.info(str(stats))
assert stats

def test12_stats(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pytransifex.api import Transifex
from pytransifex.cli import cli
from tests import test_config
from tests import logging, test_config


class TestCli(unittest.TestCase):
Expand Down Expand Up @@ -34,10 +34,10 @@ def setUpClass(cls):
)

if project := cls.tx.get_project(project_slug=cls.project_slug):
print("Found old project, removing.")
logging.info("Found old project, removing.")
project.delete()

print("Creating a brand new project")
logging.info("Creating a brand new project")
cls.tx.create_project(
project_name=cls.project_name, project_slug=cls.project_slug, private=True
)
Expand All @@ -60,13 +60,13 @@ def test1_init(self):
def test2_push(self):
result = self.runner.invoke(cli, ["push", "-in", str(self.path_to_file)])
passed = result.exit_code == 0
print(result.output)
logging.info(result.output)
assert passed

def test3_pull(self):
result = self.runner.invoke(cli, ["pull", "-l", "fr_CH,en_GB"])
passed = result.exit_code == 0
print(result.output)
logging.info(result.output)
assert passed


Expand Down

0 comments on commit 5abcae2

Please sign in to comment.