Skip to content

Commit 6d1ff90

Browse files
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
1 parent 4fe76f1 commit 6d1ff90

File tree

6 files changed

+77
-27
lines changed

6 files changed

+77
-27
lines changed

tagstudio/tests/conftest.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import pathlib
21
import sys
2+
from pathlib import Path
33
from tempfile import TemporaryDirectory
44
from unittest.mock import Mock, patch
55

66
import pytest
77

8-
CWD = pathlib.Path(__file__).parent
8+
CWD = Path(__file__).parent
99
# this needs to be above `src` imports
1010
sys.path.insert(0, str(CWD.parent))
1111

@@ -23,24 +23,24 @@ def cwd():
2323
def file_mediatypes_library():
2424
lib = Library()
2525

26-
status = lib.open_library(pathlib.Path(""), ":memory:")
26+
status = lib.open_library(Path(""), ":memory:")
2727
assert status.success
2828

2929
entry1 = Entry(
3030
folder=lib.folder,
31-
path=pathlib.Path("foo.png"),
31+
path=Path("foo.png"),
3232
fields=lib.default_fields,
3333
)
3434

3535
entry2 = Entry(
3636
folder=lib.folder,
37-
path=pathlib.Path("bar.png"),
37+
path=Path("bar.png"),
3838
fields=lib.default_fields,
3939
)
4040

4141
entry3 = Entry(
4242
folder=lib.folder,
43-
path=pathlib.Path("baz.apng"),
43+
path=Path("baz.apng"),
4444
fields=lib.default_fields,
4545
)
4646

@@ -61,7 +61,7 @@ def library(request):
6161
library_path = request.param
6262

6363
lib = Library()
64-
status = lib.open_library(pathlib.Path(library_path), ":memory:")
64+
status = lib.open_library(Path(library_path), ":memory:")
6565
assert status.success
6666

6767
tag = Tag(
@@ -92,15 +92,15 @@ def library(request):
9292
entry = Entry(
9393
id=1,
9494
folder=lib.folder,
95-
path=pathlib.Path("foo.txt"),
95+
path=Path("foo.txt"),
9696
fields=lib.default_fields,
9797
)
9898
assert lib.add_tags_to_entries(entry.id, tag.id)
9999

100100
entry2 = Entry(
101101
id=2,
102102
folder=lib.folder,
103-
path=pathlib.Path("one/two/bar.md"),
103+
path=Path("one/two/bar.md"),
104104
fields=lib.default_fields,
105105
)
106106
assert lib.add_tags_to_entries(entry2.id, tag2.id)
@@ -114,7 +114,7 @@ def library(request):
114114
@pytest.fixture
115115
def search_library() -> Library:
116116
lib = Library()
117-
lib.open_library(pathlib.Path(CWD / "fixtures" / "search_library"))
117+
lib.open_library(Path(CWD / "fixtures" / "search_library"))
118118
return lib
119119

120120

@@ -133,8 +133,8 @@ def qt_driver(qtbot, library):
133133
with TemporaryDirectory() as tmp_dir:
134134

135135
class Args:
136-
config_file = pathlib.Path(tmp_dir) / "tagstudio.ini"
137-
open = pathlib.Path(tmp_dir)
136+
config_file = Path(tmp_dir) / "tagstudio.ini"
137+
open = Path(tmp_dir)
138138
ci = True
139139

140140
with patch("src.qt.ts_qt.Consumer"), patch("src.qt.ts_qt.CustomRunnable"):
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import pathlib
1+
from pathlib import Path
22

33
from src.core.library import Entry
44
from src.core.utils.dupe_files import DupeRegistry
55

6-
CWD = pathlib.Path(__file__).parent
6+
CWD = Path(__file__).parent
77

88

99
def test_refresh_dupe_files(library):
1010
library.library_dir = "/tmp/"
1111
entry = Entry(
1212
folder=library.folder,
13-
path=pathlib.Path("bar/foo.txt"),
13+
path=Path("bar/foo.txt"),
1414
fields=library.default_fields,
1515
)
1616

1717
entry2 = Entry(
1818
folder=library.folder,
19-
path=pathlib.Path("foo/foo.txt"),
19+
path=Path("foo/foo.txt"),
2020
fields=library.default_fields,
2121
)
2222

@@ -30,7 +30,7 @@ def test_refresh_dupe_files(library):
3030
assert len(registry.groups) == 1
3131
paths = [entry.path for entry in registry.groups[0]]
3232
assert paths == [
33-
pathlib.Path("bar/foo.txt"),
34-
pathlib.Path("foo.txt"),
35-
pathlib.Path("foo/foo.txt"),
33+
Path("bar/foo.txt"),
34+
Path("foo.txt"),
35+
Path("foo/foo.txt"),
3636
]

tagstudio/tests/macros/test_missing_files.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import pathlib
1+
from pathlib import Path
22
from tempfile import TemporaryDirectory
33

44
import pytest
55
from src.core.library import Library
66
from src.core.library.alchemy.enums import FilterState
77
from src.core.utils.missing_files import MissingRegistry
88

9-
CWD = pathlib.Path(__file__).parent
9+
CWD = Path(__file__).parent
1010

1111

1212
# NOTE: Does this test actually work?
@@ -28,4 +28,4 @@ def test_refresh_missing_files(library: Library):
2828

2929
# `bar.md` should be relinked to new correct path
3030
results = library.search_library(FilterState.from_path("bar.md"))
31-
assert results[0].path == pathlib.Path("bar.md")
31+
assert results[0].path == Path("bar.md")

tagstudio/tests/macros/test_refresh_dir.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import pathlib
1+
from pathlib import Path
22
from tempfile import TemporaryDirectory
33

44
import pytest
55
from src.core.enums import LibraryPrefs
66
from src.core.utils.refresh_dir import RefreshDirTracker
77

8-
CWD = pathlib.Path(__file__).parent
8+
CWD = Path(__file__).parent
99

1010

1111
@pytest.mark.parametrize("exclude_mode", [True, False])
@@ -22,4 +22,4 @@ def test_refresh_new_files(library, exclude_mode):
2222
assert len(list(registry.refresh_dir(library.library_dir))) == 1
2323

2424
# Then
25-
assert registry.files_not_in_library == [pathlib.Path("FOO.MD")]
25+
assert registry.files_not_in_library == [Path("FOO.MD")]

tagstudio/tests/test_json_migration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# Licensed under the GPL-3.0 License.
33
# Created for TagStudio: https://github.com/CyanVoxel/TagStudio
44

5-
import pathlib
5+
from pathlib import Path
66
from time import time
77

88
from src.core.enums import LibraryPrefs
99
from src.qt.widgets.migration_modal import JsonMigrationModal
1010

11-
CWD = pathlib.Path(__file__)
11+
CWD = Path(__file__)
1212

1313

1414
def test_json_migration():

tagstudio/tests/test_translations.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import string
2+
from pathlib import Path
3+
4+
import pytest
5+
import ujson as json
6+
7+
CWD = Path(__file__).parent
8+
TRANSLATION_DIR = CWD / ".." / "resources" / "translations"
9+
10+
11+
def load_translation(filename: str) -> dict[str, str]:
12+
with open(TRANSLATION_DIR / filename, encoding="utf-8") as f:
13+
return json.load(f)
14+
15+
16+
def get_translation_filenames() -> list[tuple[str]]:
17+
return [(a.name,) for a in TRANSLATION_DIR.glob("*.json")]
18+
19+
20+
def find_format_keys(format_string: str) -> set[str]:
21+
formatter = string.Formatter()
22+
return set([field[1] for field in formatter.parse(format_string) if field[1] is not None])
23+
24+
25+
@pytest.mark.parametrize(["translation_filename"], get_translation_filenames())
26+
def test_format_key_validity(translation_filename: str):
27+
default_translation = load_translation("en.json")
28+
translation = load_translation(translation_filename)
29+
for key in default_translation:
30+
if key not in translation:
31+
continue
32+
default_keys = find_format_keys(default_translation[key])
33+
translation_keys = find_format_keys(translation[key])
34+
assert default_keys.issuperset(
35+
translation_keys
36+
), f"Translation {translation_filename} for key {key} is using an invalid format key ({translation_keys.difference(default_keys)})" # noqa: E501
37+
assert translation_keys.issuperset(
38+
default_keys
39+
), f"Translation {translation_filename} for key {key} is missing format keys ({default_keys.difference(translation_keys)})" # noqa: E501
40+
41+
42+
@pytest.mark.parametrize(["translation_filename"], get_translation_filenames())
43+
def test_for_unnecessary_translations(translation_filename: str):
44+
default_translation = load_translation("en.json")
45+
translation = load_translation(translation_filename)
46+
assert set(
47+
default_translation.keys()
48+
).issuperset(
49+
translation.keys()
50+
), f"Translation {translation_filename} has unnecessary keys ({set(translation.keys()).difference(default_translation.keys())})" # noqa: E501

0 commit comments

Comments
 (0)