Skip to content

Commit 55703bf

Browse files
tests/: improved handling of annot_stem.
tests/conftest.py: Assert that tests do not change annot_stem. tests/test_annots.py Do not change annot_stem to 'jorj' when imported. test_1645(): temporarily change annot_stem to 'jorj' to match expected files. tests/test_toc.py Expect default annot_stem, 'fitz', not 'jorj' previously set by tests/test_annots.py.
1 parent ad6fa83 commit 55703bf

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

tests/conftest.py

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def get_fds():
4242
pymupdf._log_items_clear()
4343
pymupdf._log_items_active(True)
4444

45+
JM_annot_id_stem = pymupdf.JM_annot_id_stem
46+
4547
# Run the test.
4648
rep = yield
4749

@@ -57,6 +59,9 @@ def get_fds():
5759
log_items = pymupdf._log_items()
5860
assert not log_items, f'log() was called; {len(log_items)=}.'
5961

62+
assert pymupdf.JM_annot_id_stem == JM_annot_id_stem, \
63+
f'pymupdf.JM_annot_id_stem has changed from {JM_annot_id_stem!r} to {pymupdf.JM_annot_id_stem!r}'
64+
6065
if platform.system() == 'Linux':
6166
# Show detailed information about leaked fds.
6267
open_fds_after, open_fds_after_l = get_fds()

tests/test_annots.py

+40-34
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import gentle_compare
1111

1212

13-
pymupdf.TOOLS.set_annot_stem("jorj")
14-
1513
red = (1, 0, 0)
1614
blue = (0, 0, 1)
1715
gold = (1, 1, 0)
@@ -226,38 +224,46 @@ def test_1645():
226224
'''
227225
Test fix for #1645.
228226
'''
229-
path_in = os.path.abspath( f'{__file__}/../resources/symbol-list.pdf')
230-
231-
if pymupdf.mupdf_version_tuple >= (1, 26):
232-
path_expected = os.path.abspath( f'{__file__}/../../tests/resources/test_1645_expected_1.26.pdf')
233-
elif pymupdf.mupdf_version_tuple >= (1, 25):
234-
path_expected = os.path.abspath( f'{__file__}/../../tests/resources/test_1645_expected_1.25.pdf')
235-
elif pymupdf.mupdf_version_tuple >= (1, 24, 2):
236-
path_expected = os.path.abspath( f'{__file__}/../../tests/resources/test_1645_expected_1.24.2.pdf')
237-
else:
238-
path_expected = os.path.abspath( f'{__file__}/../../tests/resources/test_1645_expected_1.24.pdf')
239-
path_out = os.path.abspath( f'{__file__}/../test_1645_out.pdf')
240-
doc = pymupdf.open(path_in)
241-
page = doc[0]
242-
page_bounds = page.bound()
243-
annot_loc = pymupdf.Rect(page_bounds.x0, page_bounds.y0, page_bounds.x0 + 75, page_bounds.y0 + 15)
244-
# Check type of page.derotation_matrix - this is #2911.
245-
assert isinstance(page.derotation_matrix, pymupdf.Matrix), \
246-
f'Bad type for page.derotation_matrix: {type(page.derotation_matrix)=} {page.derotation_matrix=}.'
247-
page.add_freetext_annot(
248-
annot_loc * page.derotation_matrix,
249-
"TEST",
250-
fontsize=18,
251-
fill_color=pymupdf.utils.getColor("FIREBRICK1"),
252-
rotate=page.rotation,
253-
)
254-
doc.save(path_out, garbage=1, deflate=True, no_new_id=True)
255-
print(f'Have created {path_out}. comparing with {path_expected}.')
256-
with open( path_out, 'rb') as f:
257-
out = f.read()
258-
with open( path_expected, 'rb') as f:
259-
expected = f.read()
260-
assert out == expected, f'Files differ: {path_out} {path_expected}'
227+
# The expected output files assume annot_stem is 'jorj'. We need to always
228+
# restore this before returning (this is checked by conftest.py).
229+
annot_stem = pymupdf.JM_annot_id_stem
230+
pymupdf.TOOLS.set_annot_stem('jorj')
231+
try:
232+
path_in = os.path.abspath( f'{__file__}/../resources/symbol-list.pdf')
233+
234+
if pymupdf.mupdf_version_tuple >= (1, 26):
235+
path_expected = os.path.abspath( f'{__file__}/../../tests/resources/test_1645_expected_1.26.pdf')
236+
elif pymupdf.mupdf_version_tuple >= (1, 25):
237+
path_expected = os.path.abspath( f'{__file__}/../../tests/resources/test_1645_expected_1.25.pdf')
238+
elif pymupdf.mupdf_version_tuple >= (1, 24, 2):
239+
path_expected = os.path.abspath( f'{__file__}/../../tests/resources/test_1645_expected_1.24.2.pdf')
240+
else:
241+
path_expected = os.path.abspath( f'{__file__}/../../tests/resources/test_1645_expected_1.24.pdf')
242+
path_out = os.path.abspath( f'{__file__}/../test_1645_out.pdf')
243+
doc = pymupdf.open(path_in)
244+
page = doc[0]
245+
page_bounds = page.bound()
246+
annot_loc = pymupdf.Rect(page_bounds.x0, page_bounds.y0, page_bounds.x0 + 75, page_bounds.y0 + 15)
247+
# Check type of page.derotation_matrix - this is #2911.
248+
assert isinstance(page.derotation_matrix, pymupdf.Matrix), \
249+
f'Bad type for page.derotation_matrix: {type(page.derotation_matrix)=} {page.derotation_matrix=}.'
250+
page.add_freetext_annot(
251+
annot_loc * page.derotation_matrix,
252+
"TEST",
253+
fontsize=18,
254+
fill_color=pymupdf.utils.getColor("FIREBRICK1"),
255+
rotate=page.rotation,
256+
)
257+
doc.save(path_out, garbage=1, deflate=True, no_new_id=True)
258+
print(f'Have created {path_out}. comparing with {path_expected}.')
259+
with open( path_out, 'rb') as f:
260+
out = f.read()
261+
with open( path_expected, 'rb') as f:
262+
expected = f.read()
263+
assert out == expected, f'Files differ: {path_out} {path_expected}'
264+
finally:
265+
# Restore annot_stem.
266+
pymupdf.TOOLS.set_annot_stem(annot_stem)
261267

262268
def test_1824():
263269
'''

tests/test_toc.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ def test_3347():
187187
})
188188

189189
links_expected = [
190-
(0, {'kind': 1, 'xref': 11, 'from': pymupdf.Rect(10.0, 20.0, 50.0, 40.0), 'page': 0, 'to': pymupdf.Point(300.0, 350.0), 'zoom': 0.0, 'id': 'jorj-L0'}),
191-
(0, {'kind': 1, 'xref': 12, 'from': pymupdf.Rect(300.0, 350.0, 400.0, 450.0), 'page': 1, 'to': pymupdf.Point(20.0, 30.0), 'zoom': 0.0, 'id': 'jorj-L1'}),
192-
(1, {'kind': 1, 'xref': 13, 'from': pymupdf.Rect(20.0, 30.0, 40.0, 50.0), 'page': 1, 'to': pymupdf.Point(350.0, 300.0), 'zoom': 0.0, 'id': 'jorj-L0'}),
193-
(1, {'kind': 1, 'xref': 14, 'from': pymupdf.Rect(350.0, 300.0, 450.0, 400.0), 'page': 0, 'to': pymupdf.Point(10.0, 20.0), 'zoom': 0.0, 'id': 'jorj-L1'}),
190+
(0, {'kind': 1, 'xref': 11, 'from': pymupdf.Rect(10.0, 20.0, 50.0, 40.0), 'page': 0, 'to': pymupdf.Point(300.0, 350.0), 'zoom': 0.0, 'id': 'fitz-L0'}),
191+
(0, {'kind': 1, 'xref': 12, 'from': pymupdf.Rect(300.0, 350.0, 400.0, 450.0), 'page': 1, 'to': pymupdf.Point(20.0, 30.0), 'zoom': 0.0, 'id': 'fitz-L1'}),
192+
(1, {'kind': 1, 'xref': 13, 'from': pymupdf.Rect(20.0, 30.0, 40.0, 50.0), 'page': 1, 'to': pymupdf.Point(350.0, 300.0), 'zoom': 0.0, 'id': 'fitz-L0'}),
193+
(1, {'kind': 1, 'xref': 14, 'from': pymupdf.Rect(350.0, 300.0, 450.0, 400.0), 'page': 0, 'to': pymupdf.Point(10.0, 20.0), 'zoom': 0.0, 'id': 'fitz-L1'}),
194194
]
195195

196196
path = os.path.normpath(f'{__file__}/../../tests/test_3347_out.pdf')
@@ -264,7 +264,7 @@ def test_3400():
264264
print(f'Saved to {path=}.')
265265

266266
links_expected = [
267-
(1, {'kind': 1, 'xref': 1120, 'from': pymupdf.Rect(10.0, 10.0, 100.0, 50.0), 'page': 0, 'to': pymupdf.Point(187.5, 472.5), 'zoom': 0.0, 'id': 'jorj-L0'})
267+
(1, {'kind': 1, 'xref': 1120, 'from': pymupdf.Rect(10.0, 10.0, 100.0, 50.0), 'page': 0, 'to': pymupdf.Point(187.5, 472.5), 'zoom': 0.0, 'id': 'fitz-L0'})
268268
]
269269

270270
links_actual = list()

0 commit comments

Comments
 (0)