Files
pdf2epub/scripts/test_translate.py
T

83 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""Самопроверка моста без сети: python3 test_translate.py"""
import json
from pathlib import Path
import translate as t
def test_marks_roundtrip():
src = 'She thinks <i>this is bad</i> &amp; says &#x201c;no&#x201d;'
marked = t.to_marks(src)
assert "<i>" not in marked and "⟦i⟧" in marked
assert "&amp;" not in marked, "сущности должны раскрываться для переводчика"
body, ok = t.from_marks(marked)
assert ok
assert "<i>this is bad</i>" in body
assert "&amp;" in body, "спецсимволы должны экранироваться обратно"
def test_broken_marks_drop_tags():
body, ok = t.from_marks("текст ⟦i⟧без закрытия")
assert not ok and "⟦" not in body and "<i>" not in body
def test_language_detection():
assert t.detect_language("the quick brown fox " * 40)[0] == "en"
assert t.detect_language("быстрая бурая лиса " * 40)[0] == "ru"
assert t.detect_language("short")[0] == "unknown"
def test_split_and_rebuild(tmp: Path):
src = tmp / "book.html"
src.write_text("\n".join([
"<html><body>",
"<h1>CHAPTER 1</h1>",
"<p>He said <i>hello</i> loudly.</p>",
'<p class="figure"><img src="images/img01.png"/></p>',
"<h1>CHAPTER 2</h1>",
"<p>Second chapter text.</p>",
"</body></html>",
]), encoding="utf-8")
blocks = t.parse_blocks(src)
assert len(blocks) == 4, "картинка не должна попадать в перевод"
chapters = t.split_chapters(blocks)
assert [len(c) for c in chapters] == [2, 2]
extracted = tmp / "extracted"
t.write_input(chapters, extracted)
ch0 = json.loads((extracted / "chapter_000.json").read_text(encoding="utf-8"))
assert ch0["title"] == "CHAPTER 1"
assert ch0["paragraphs"][1] == "He said ⟦i⟧hello⟦/i⟧ loudly."
# подделываем ответ book_translator: тот же формат, тот же порядок
trans = tmp / "translations"
trans.mkdir()
for n, ch in enumerate(chapters):
paragraphs = [json.loads((extracted / ("chapter_%03d.json" % n))
.read_text(encoding="utf-8"))["paragraphs"][i]
.replace("He said", "Он сказал").replace("hello", "привет")
for i in range(len(ch))]
(trans / ("chapter_%03d_translated.json" % n)).write_text(
json.dumps({"number": n, "paragraphs": paragraphs}, ensure_ascii=False),
encoding="utf-8")
out = tmp / "book.ru.html"
assert t.rebuild(src, chapters, tmp, out)
result = out.read_text(encoding="utf-8")
assert "<p>Он сказал <i>привет</i> loudly.</p>" in result
assert '<img src="images/img01.png"/>' in result, "картинка должна уцелеть"
assert result.count("<h1>") == 2
if __name__ == "__main__":
import tempfile
test_marks_roundtrip()
test_broken_marks_drop_tags()
test_language_detection()
with tempfile.TemporaryDirectory() as d:
test_split_and_rebuild(Path(d))
print("OK")