Skip to content

Commit 9594338

Browse files
committed
Fix transliteration tests.
1 parent a5531d0 commit 9594338

2 files changed

Lines changed: 38 additions & 78 deletions

File tree

test/integration.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@
1010
logger = getLogger(__name__)
1111

1212

13-
def test_sample(dset):
13+
def test_sample(dset, report=True):
1414
"""
1515
Test an individual sample set and produce a human-readable report.
1616
1717
Used outside of automated tests.
1818
1919
@param dset (str): sample set name (without the .csv extension) found in
2020
the `data/script_samples` directory.
21+
22+
@param report (bool): if True (the default), print fail/success ticks and
23+
write out a report to file at the end. Otherwise, raise an exception on
24+
the first error encountered.
2125
"""
22-
deltas = []
26+
deltas = [] if report else None
2327
dset_fpath = path.join(TEST_DATA_DIR, "script_samples", dset + ".csv")
2428
log_fpath = path.join(TEST_DATA_DIR, "log", f"test_{dset}.log")
2529

@@ -41,29 +45,36 @@ def test_sample(dset):
4145
_trans(rom, lang, "r2s", opts, script, deltas)
4246
i += 1
4347

44-
with open(log_fpath, "w") as fh:
45-
# If no deltas, just truncate the file.
46-
for lang, script, delta in deltas:
47-
fh.write(f"Language: {lang}\n")
48-
fh.write(f"Original: {script}\nDiff (result vs. expected):\n")
49-
for dline in delta:
50-
fh.write(dline.strip() + "\n")
51-
fh.write("\n\n")
48+
if report:
49+
with open(log_fpath, "w") as fh:
50+
# If no deltas, just truncate the file.
51+
for lang, script, delta in deltas:
52+
fh.write(f"Language: {lang}\n")
53+
fh.write(f"Original: {script}\nDiff (result vs. expected):\n")
54+
for dline in delta:
55+
fh.write(dline.strip() + "\n")
56+
fh.write("\n\n")
5257

53-
ct = len(deltas)
54-
if ct > 0:
55-
print(f"\n\n{ct} failed tests. See report at {log_fpath}")
56-
else:
57-
print("All tests passed.")
58+
ct = len(deltas)
59+
if ct > 0:
60+
print(f"\n\n{ct} failed tests. See report at {log_fpath}")
61+
else:
62+
print("All tests passed.")
5863

5964

6065
def _trans(script, lang, t_dir, opts, rom, deltas):
6166
logger.debug(f"Transliterating {lang}: {t_dir}")
6267
trans, warnings = transliterate(
6368
script, lang, t_dir=t_dir,
6469
capitalize=opts.get("capitalize"), options=opts)
65-
if (trans == rom):
66-
print(".", end="")
70+
try:
71+
assert trans == rom
72+
except AssertionError as e:
73+
if deltas is not None:
74+
print("F", end="")
75+
deltas.append((lang, script, ndiff([trans], [rom])))
76+
else:
77+
raise e
6778
else:
68-
print("F", end="")
69-
deltas.append((lang, script, ndiff([trans], [rom])))
79+
if deltas:
80+
print(".", end="")
Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import logging
22

3-
from unittest import TestCase, TestSuite, TextTestRunner
4-
from csv import reader
5-
from json import loads as jloads
6-
from os import environ, path, unlink
3+
from unittest import TestCase
4+
from os import environ, unlink
75

8-
from scriptshifter.trans import transliterate
9-
from scriptshifter.tables import get_language, init_db
10-
from test import TEST_DATA_DIR
6+
from scriptshifter.tables import init_db
7+
from test.integration import test_sample
118

129

1310
logger = logging.getLogger(__name__)
@@ -23,58 +20,10 @@ def tearDownModule():
2320

2421
class TestTrans(TestCase):
2522
"""
26-
Test S2R transliteration.
23+
Test transliteration.
2724
28-
Modified test case class to run independent tests for each CSV row.
29-
30-
TODO use a comprehensive sample table and report errors for unsupported
31-
languages.
32-
"""
33-
def sample(self):
34-
"""
35-
Test transliteration for one CSV row.
36-
37-
This function name won't start with `test_` otherwise will be
38-
automatically run without parameters.
39-
"""
40-
config = get_language(self.tbl)
41-
t_dir = self.options.get("t_dir", "s2r")
42-
if (
43-
t_dir == "s2r" and config["has_s2r"]
44-
or t_dir == "r2s" and config["has_r2s"]):
45-
txl = transliterate(
46-
self.script, self.tbl,
47-
t_dir=t_dir,
48-
capitalize=self.options.get("capitalize", False),
49-
options=self.options)[0]
50-
self.assertEqual(
51-
txl, self.roman,
52-
f"S2R transliteration error for {self.tbl}!\n"
53-
f"Original: {self.script}")
54-
55-
56-
def make_suite():
25+
Use "unittest" sample table.
5726
"""
58-
Build parametrized test cases.
59-
"""
60-
suite = TestSuite()
61-
62-
with open(path.join(
63-
TEST_DATA_DIR, "script_samples", "unittest.csv"
64-
), newline="") as fh:
65-
csv = reader(fh)
66-
for row in csv:
67-
if len(row[0]):
68-
# Inject transliteration info in the test case.
69-
tcase = TestTrans("sample")
70-
tcase.tbl = row[0]
71-
tcase.script = row[1].strip()
72-
tcase.roman = row[2].strip()
73-
tcase.options = jloads(row[3]) if len(row[3]) else {}
74-
75-
suite.addTest(tcase)
76-
77-
return suite
78-
7927

80-
TextTestRunner().run(make_suite())
28+
def test_integration_sample(self):
29+
test_sample("unittest", False)

0 commit comments

Comments
 (0)