-
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for translations (#833)
* refactor: simplify pathlib imports * feat: add tests for invalid format keys and missing / unnecessary translations * feat: parametrise format key test * feat: parametrise completeness test * fix: include more useful information in the error message * refactor: remove unused method * refactor: extract translation loading to a separate function * fix: include more useful information in the error message * fix: don't fail test when language hasn't been completely translated
- Loading branch information
1 parent
4fe76f1
commit 6d1ff90
Showing
6 changed files
with
77 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import string | ||
from pathlib import Path | ||
|
||
import pytest | ||
import ujson as json | ||
|
||
CWD = Path(__file__).parent | ||
TRANSLATION_DIR = CWD / ".." / "resources" / "translations" | ||
|
||
|
||
def load_translation(filename: str) -> dict[str, str]: | ||
with open(TRANSLATION_DIR / filename, encoding="utf-8") as f: | ||
return json.load(f) | ||
|
||
|
||
def get_translation_filenames() -> list[tuple[str]]: | ||
return [(a.name,) for a in TRANSLATION_DIR.glob("*.json")] | ||
|
||
|
||
def find_format_keys(format_string: str) -> set[str]: | ||
formatter = string.Formatter() | ||
return set([field[1] for field in formatter.parse(format_string) if field[1] is not None]) | ||
|
||
|
||
@pytest.mark.parametrize(["translation_filename"], get_translation_filenames()) | ||
def test_format_key_validity(translation_filename: str): | ||
default_translation = load_translation("en.json") | ||
translation = load_translation(translation_filename) | ||
for key in default_translation: | ||
if key not in translation: | ||
continue | ||
default_keys = find_format_keys(default_translation[key]) | ||
translation_keys = find_format_keys(translation[key]) | ||
assert default_keys.issuperset( | ||
translation_keys | ||
), f"Translation {translation_filename} for key {key} is using an invalid format key ({translation_keys.difference(default_keys)})" # noqa: E501 | ||
assert translation_keys.issuperset( | ||
default_keys | ||
), f"Translation {translation_filename} for key {key} is missing format keys ({default_keys.difference(translation_keys)})" # noqa: E501 | ||
|
||
|
||
@pytest.mark.parametrize(["translation_filename"], get_translation_filenames()) | ||
def test_for_unnecessary_translations(translation_filename: str): | ||
default_translation = load_translation("en.json") | ||
translation = load_translation(translation_filename) | ||
assert set( | ||
default_translation.keys() | ||
).issuperset( | ||
translation.keys() | ||
), f"Translation {translation_filename} has unnecessary keys ({set(translation.keys()).difference(default_translation.keys())})" # noqa: E501 |