-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathtest_entrypoint.py
More file actions
243 lines (191 loc) · 8.46 KB
/
Copy pathtest_entrypoint.py
File metadata and controls
243 lines (191 loc) · 8.46 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""Testing the parse_file and write_file functions."""
import os
import tempfile
import warnings
import pytest
from bibtexparser import parse_file
from bibtexparser import write_file
from bibtexparser import write_string
from bibtexparser.library import Library
from bibtexparser.model import Entry
from bibtexparser.model import Field
def test_gbk():
library = parse_file("tests/resources/gbk_test.bib", encoding="gbk")
assert library.entries[0]["author"] == "凯撒"
assert library.entries[0]["title"] == "Test Title"
assert library.entries[0]["year"] == "2013"
assert library.entries[0]["journal"] == "测试期刊"
def test_write_file_default_encoding():
"""Test write_file uses UTF-8 by default."""
entry = Entry(
entry_type="article",
key="test2024",
fields=[
Field(key="author", value="Müller"),
Field(key="title", value="Ångström measurements"),
],
)
library = Library([entry])
with tempfile.NamedTemporaryFile(mode="w", suffix=".bib", delete=False) as f:
temp_path = f.name
try:
write_file(temp_path, library)
# Read back and verify
with open(temp_path, encoding="UTF-8") as f:
content = f.read()
assert "Müller" in content
assert "Ångström" in content
finally:
os.unlink(temp_path)
def test_write_file_gbk_encoding():
"""Test write_file with GBK encoding for Chinese characters."""
entry = Entry(
entry_type="article",
key="test2024",
fields=[
Field(key="author", value="凯撒"),
Field(key="title", value="Test Title"),
Field(key="journal", value="测试期刊"),
],
)
library = Library([entry])
with tempfile.NamedTemporaryFile(mode="w", suffix=".bib", delete=False) as f:
temp_path = f.name
try:
write_file(temp_path, library, encoding="gbk")
# Read back with GBK and verify
with open(temp_path, encoding="gbk") as f:
content = f.read()
assert "凯撒" in content
assert "测试期刊" in content
finally:
os.unlink(temp_path)
def test_write_file_roundtrip_gbk():
"""Test round-trip: parse GBK file, write with GBK, parse again."""
# Parse original GBK file
library = parse_file("tests/resources/gbk_test.bib", encoding="gbk")
original_author = library.entries[0]["author"]
original_journal = library.entries[0]["journal"]
with tempfile.NamedTemporaryFile(mode="w", suffix=".bib", delete=False) as f:
temp_path = f.name
try:
# Write with GBK encoding
write_file(temp_path, library, encoding="gbk")
# Parse back
library2 = parse_file(temp_path, encoding="gbk")
assert library2.entries[0]["author"] == original_author
assert library2.entries[0]["journal"] == original_journal
finally:
os.unlink(temp_path)
# Deprecation warning tests for write_file and write_string
def test_write_file_deprecated_parse_stack_parameter():
"""Test that using deprecated 'parse_stack' parameter issues a warning."""
library = Library([])
with tempfile.NamedTemporaryFile(mode="w", suffix=".bib", delete=False) as f:
temp_path = f.name
try:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
write_file(temp_path, library, parse_stack=[])
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert "parse_stack" in str(w[0].message)
assert "unparse_stack" in str(w[0].message)
finally:
os.unlink(temp_path)
def test_write_file_deprecated_append_middleware_parameter():
"""Test that using deprecated 'append_middleware' parameter issues a warning."""
library = Library([])
with tempfile.NamedTemporaryFile(mode="w", suffix=".bib", delete=False) as f:
temp_path = f.name
try:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
write_file(temp_path, library, append_middleware=[])
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert "append_middleware" in str(w[0].message)
assert "prepend_middleware" in str(w[0].message)
finally:
os.unlink(temp_path)
def test_write_file_both_parse_stack_and_unparse_stack_raises_error():
"""Test that providing both parse_stack and unparse_stack raises ValueError."""
library = Library([])
with tempfile.NamedTemporaryFile(mode="w", suffix=".bib", delete=False) as f:
temp_path = f.name
try:
with pytest.raises(ValueError) as excinfo:
write_file(temp_path, library, parse_stack=[], unparse_stack=[])
assert "parse_stack" in str(excinfo.value)
assert "unparse_stack" in str(excinfo.value)
assert "Use 'unparse_stack' instead" in str(excinfo.value)
finally:
os.unlink(temp_path)
def test_write_file_both_append_and_prepend_middleware_raises_error():
"""Test that providing both append_middleware and prepend_middleware raises ValueError."""
library = Library([])
with tempfile.NamedTemporaryFile(mode="w", suffix=".bib", delete=False) as f:
temp_path = f.name
try:
with pytest.raises(ValueError) as excinfo:
write_file(temp_path, library, append_middleware=[], prepend_middleware=[])
assert "append_middleware" in str(excinfo.value)
assert "prepend_middleware" in str(excinfo.value)
assert "Use 'prepend_middleware' instead" in str(excinfo.value)
finally:
os.unlink(temp_path)
def test_write_file_unexpected_keyword_argument_raises_error():
"""Test that unexpected keyword arguments raise TypeError."""
library = Library([])
with tempfile.NamedTemporaryFile(mode="w", suffix=".bib", delete=False) as f:
temp_path = f.name
try:
with pytest.raises(TypeError) as excinfo:
write_file(temp_path, library, unknown_param="value")
assert "unexpected keyword arguments" in str(excinfo.value)
assert "unknown_param" in str(excinfo.value)
finally:
os.unlink(temp_path)
def test_write_string_deprecated_parse_stack_parameter():
"""Test that using deprecated 'parse_stack' parameter issues a warning."""
library = Library([])
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
write_string(library, parse_stack=[])
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert "parse_stack" in str(w[0].message)
assert "unparse_stack" in str(w[0].message)
def test_write_string_deprecated_append_middleware_parameter():
"""Test that using deprecated 'append_middleware' parameter issues a warning."""
library = Library([])
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
write_string(library, append_middleware=[])
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert "append_middleware" in str(w[0].message)
assert "prepend_middleware" in str(w[0].message)
def test_write_string_both_parse_stack_and_unparse_stack_raises_error():
"""Test that providing both parse_stack and unparse_stack raises ValueError."""
library = Library([])
with pytest.raises(ValueError) as excinfo:
write_string(library, parse_stack=[], unparse_stack=[])
assert "parse_stack" in str(excinfo.value)
assert "unparse_stack" in str(excinfo.value)
assert "Use 'unparse_stack' instead" in str(excinfo.value)
def test_write_string_both_append_and_prepend_middleware_raises_error():
"""Test that providing both append_middleware and prepend_middleware raises ValueError."""
library = Library([])
with pytest.raises(ValueError) as excinfo:
write_string(library, append_middleware=[], prepend_middleware=[])
assert "append_middleware" in str(excinfo.value)
assert "prepend_middleware" in str(excinfo.value)
assert "Use 'prepend_middleware' instead" in str(excinfo.value)
def test_write_string_unexpected_keyword_argument_raises_error():
"""Test that unexpected keyword arguments raise TypeError."""
library = Library([])
with pytest.raises(TypeError) as excinfo:
write_string(library, unknown_param="value")
assert "unexpected keyword arguments" in str(excinfo.value)
assert "unknown_param" in str(excinfo.value)