-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter_test.py
More file actions
102 lines (84 loc) · 3.59 KB
/
converter_test.py
File metadata and controls
102 lines (84 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Tests for the docxplain.converter module."""
import shutil
from pathlib import Path
from docxplain.converter import convert_file, trim_trailing_whitespace
from docxplain.formats import get_format
def test_unchanged(tmp_path: Path) -> None:
"""Test case where the plain text conversion matches the docx source."""
repo_data = Path(__file__).parent.joinpath("data/unchanged")
work_dir = tmp_path / "unchanged"
shutil.copytree(repo_data, work_dir)
docxpath = work_dir.joinpath("test_doc.docx")
assert convert_file(str(docxpath), get_format("plain")) is False
def test_changed(tmp_path: Path) -> None:
"""Test the case where the existing plain text conversion is different."""
repo_data = Path(__file__).parent.joinpath("data/changed")
work_dir = tmp_path / "changed"
shutil.copytree(repo_data, work_dir)
docxpath = work_dir.joinpath("test_doc.docx")
assert convert_file(str(docxpath), get_format("plain")) is True
def test_new(tmp_path: Path) -> None:
"""Test the case where the existing plain text conversion is different."""
repo_data = Path(__file__).parent.joinpath("data/new")
work_dir = tmp_path / "new"
shutil.copytree(repo_data, work_dir)
docxpath = work_dir.joinpath("test_doc.docx")
assert convert_file(str(docxpath), get_format("plain")) is True
def test_suffix(tmp_path: Path) -> None:
"""Test the case of a custom plain text file suffix."""
repo_data = Path(__file__).parent.joinpath("data/new")
work_dir = tmp_path / "suffix"
shutil.copytree(repo_data, work_dir)
docxpath = work_dir.joinpath("test_doc.docx")
assert (
convert_file(
str(docxpath), get_format("plain"), suffix=".extracted.txt"
)
is True
)
plain_path = work_dir.joinpath("test_doc.extracted.txt")
assert plain_path.is_file()
def test_header(tmp_path: Path) -> None:
"""Test the case of a customized plain text file header."""
repo_data = Path(__file__).parent.joinpath("data/new")
work_dir = tmp_path / "header"
shutil.copytree(repo_data, work_dir)
docxpath = work_dir.joinpath("test_doc.docx")
header = "This file is autogenerated."
assert (
convert_file(str(docxpath), get_format("plain"), header=header) is True
)
plain_path = docxpath.with_suffix(".txt")
assert plain_path.is_file()
content = plain_path.read_text().splitlines()
assert content[0] == "This file is autogenerated."
def test_header_templating(tmp_path: Path) -> None:
"""Test the case of a templated custom file header that includes the
document name.
"""
repo_data = Path(__file__).parent.joinpath("data/new")
work_dir = tmp_path / "header"
shutil.copytree(repo_data, work_dir)
docxpath = work_dir.joinpath("test_doc.docx")
header = "This file is autogenerated from {docx}."
assert (
convert_file(str(docxpath), get_format("plain"), header=header) is True
)
plain_path = docxpath.with_suffix(".txt")
assert plain_path.is_file()
content = plain_path.read_text().splitlines()
assert content[0] == "This file is autogenerated from test_doc.docx."
def test_trim_trailing_whitespace(tmp_path: Path) -> None:
data_file = Path(__file__).parent.joinpath(
"data/trailing-whitespace/input.txt"
)
working_path = tmp_path / "trailing_whitespace.txt"
shutil.copyfile(data_file, working_path)
trim_trailing_whitespace(working_path)
output = working_path.read_text()
expected = (
Path(__file__)
.parent.joinpath("data/trailing-whitespace/output.txt")
.read_text()
)
assert output == expected