Skip to content

Commit 720cecc

Browse files
committed
Fixing unit tests
1 parent ad28b22 commit 720cecc

21 files changed

+47
-24
lines changed

.github/workflows/continuous-integration-workflow.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
python-version: ${{ matrix.python-version }}
2525
- name: Install system dependencies ⚙️
2626
if: matrix.platform == 'ubuntu-latest'
27-
run: sudo apt-get install libjpeg-dev
27+
run: sudo apt-get install ghostscript libjpeg-dev
2828
- name: Install qpdf ⚙️
2929
if: matrix.platform == 'ubuntu-latest' && matrix.python-version != '3.9' # This allows us to run the unit tests WITHOUT qpdf in just one parallel execution
3030
run: sudo apt-get install qpdf
@@ -35,9 +35,9 @@ jobs:
3535
- name: Statically checking code 🔎
3636
if: matrix.python-version == '3.10' && matrix.platform == 'ubuntu-latest'
3737
run: |
38-
pylint fpdf test tutorial/tuto*.py
39-
bandit -c .banditrc.yml -r contributors/ fpdf/ tutorial/
40-
semgrep scan --config auto --lang=py --error --strict --exclude-rule=python.lang.security.insecure-hash-function.insecure-hash-function fpdf
38+
pylint fpdf test tutorial/tuto*.py
39+
bandit -c .banditrc.yml -r contributors/ fpdf/ tutorial/
40+
semgrep scan --config auto --lang=py --error --strict --exclude-rule=python.lang.security.insecure-hash-function.insecure-hash-function fpdf
4141
- name: Ensure code has been autoformatted with black 🖌️
4242
if: matrix.python-version == '3.10' && matrix.platform == 'ubuntu-latest'
4343
run: black --check .
@@ -59,7 +59,7 @@ jobs:
5959
# Ensuring there is no `generate=True` left remaining in calls to assert_pdf_equal:
6060
grep -IRF generate=True test/ && exit 1
6161
# Executing all tests:
62-
pytest -vv --final-memory-usage
62+
pytest -vv --trace-memory-usage
6363
- name: Uploading coverage report to codecov.io ☑
6464
if: matrix.python-version == '3.10' && matrix.platform == 'ubuntu-latest'
6565
run: bash <(curl -s https://codecov.io/bash)

fpdf/util.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ def get_mem_usage(prefix) -> str:
9797
# heap_size, stack_size = get_process_heap_and_stack_sizes()
9898
# objs_size_sum = get_gc_managed_objs_total_size()
9999
pillow = get_pillow_allocated_memory()
100+
# malloc_stats = "Malloc stats: " + get_pymalloc_allocated_over_total_size()
101+
malloc_stats = ""
100102
if is_tracing():
101-
malloc_stats = get_tracemalloc_traced_memory()
102-
else:
103-
malloc_stats = get_pymalloc_allocated_over_total_size()
104-
return f"{prefix:<40} Malloc stats: {malloc_stats} | Pillow: {pillow} | Process RSS: {rss}"
103+
malloc_stats = "Malloc stats: " + get_tracemalloc_traced_memory()
104+
return f"{prefix:<40} {malloc_stats} | Pillow: {pillow} | Process RSS: {rss}"
105105

106106

107107
def get_process_rss() -> str:

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ ignore = E203, E221, E241, E251, E701, E731, W503
1616

1717
[tool:pytest]
1818
addopts = --cov=fpdf --cov-report=xml
19+
log_cli = 1
20+
log_cli_level= WARN

test/conftest.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,20 @@ def handler(_, __):
261261

262262

263263
def ensure_rss_memory_below(max_in_mib):
264+
"""
265+
Enure there is no unexpected / significant increase between
266+
the process RSS memory BEFORE executing the test, and AFTER.
267+
"""
268+
264269
def actual_decorator(test_func):
265270
@functools.wraps(test_func)
266271
def wrapper(*args, **kwargs):
272+
start_rss_in_mib = get_process_rss_as_mib()
267273
test_func(*args, **kwargs)
268-
rss_in_mib = get_process_rss_as_mib()
269-
if rss_in_mib:
270-
assert rss_in_mib < max_in_mib
274+
if not start_rss_in_mib:
275+
return # not available under Windows
276+
end_rss_in_mib = get_process_rss_as_mib()
277+
assert (end_rss_in_mib - start_rss_in_mib) < max_in_mib
271278

272279
return wrapper
273280

@@ -278,9 +285,9 @@ def wrapper(*args, **kwargs):
278285
# so we require an opt-in through a CLI argument:
279286
def pytest_addoption(parser):
280287
parser.addoption(
281-
"--final-memory-usage",
288+
"--trace-memory-usage",
282289
action="store_true",
283-
help="At the end of the tests execution, display the current memory usage",
290+
help="Trace the memory usage during tests execution",
284291
)
285292
parser.addoption(
286293
"--pympler-summary",
@@ -294,15 +301,26 @@ def pytest_addoption(parser):
294301
)
295302

296303

304+
@pytest.fixture(scope="module", autouse=True)
305+
def module_memory_usage(request):
306+
if request.config.getoption("trace_memory_usage"):
307+
gc.collect()
308+
capmanager = request.config.pluginmanager.getplugin("capturemanager")
309+
with capmanager.global_and_fixture_disabled():
310+
print("\n")
311+
print_mem_usage("Memory usage:")
312+
yield
313+
314+
297315
@pytest.fixture(scope="session", autouse=True)
298316
def final_memory_usage(request):
299317
yield
300-
if request.config.getoption("final_memory_usage"):
318+
if request.config.getoption("trace_memory_usage"):
301319
gc.collect()
302320
capmanager = request.config.pluginmanager.getplugin("capturemanager")
303321
with capmanager.global_and_fixture_disabled():
304322
print("\n")
305-
print_mem_usage("Final memory usage:")
323+
print_mem_usage("Memory usage:")
306324

307325

308326
@pytest.fixture(scope="session", autouse=True)

test/embed_file_all_optionals.pdf

145 Bytes
Binary file not shown.

test/embed_file_self.pdf

338 Bytes
Binary file not shown.

test/file_attachment_annotation.pdf

337 Bytes
Binary file not shown.

test/html/html_customize_ul.pdf

1.28 KB
Binary file not shown.

test/html/html_features.pdf

27 Bytes
Binary file not shown.
10.9 KB
Binary file not shown.
135 Bytes
Binary file not shown.
138 Bytes
Binary file not shown.

test/html/html_table_with_border.pdf

137 Bytes
Binary file not shown.
145 Bytes
Binary file not shown.
1.84 KB
Binary file not shown.

test/image/test_load_image.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_load_invalid_base64_data():
4242

4343

4444
# ensure memory usage does not get too high - this value depends on Python version:
45-
@ensure_rss_memory_below(max_in_mib=190)
45+
@ensure_rss_memory_below(max_in_mib=6)
4646
def test_share_images_cache(tmp_path):
4747
images_cache = {}
4848
icc_profiles_cache = {}

test/image/test_oversized.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
HERE = Path(__file__).resolve().parent
99
IMAGE_PATH = HERE / "png_images/6c853ed9dacd5716bc54eb59cec30889.png"
10-
MAX_MEMORY_MB = 190 # memory usage depends on Python version
10+
MAX_MEMORY_MB = 12 # memory usage depends on Python version
1111

1212

1313
def test_oversized_images_warn(caplog):

test/requirements.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
bandit
22
black
3-
camelot-py[base]
3+
# camelot-py[base] : does not work due to https://github.com/vinayak-mehta/pdftopng/issues/11
4+
camelot-py
45
endesive
6+
ghostscript # required by camelot-py[base]: https://github.com/camelot-dev/camelot/blob/master/setup.py#L27
7+
opencv-python # required by camelot-py[base]: https://github.com/camelot-dev/camelot/blob/master/setup.py#L27
58
pylint
69
pytest
710
pytest-cov

test/table/table_with_cell_fill.pdf

4 Bytes
Binary file not shown.

test/table/test_table_extraction.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
from test.table.test_table import TABLE_DATA
1515

1616
HERE = Path(__file__).resolve().parent
17-
TABLE_DATA_AS_DF = DataFrame(TABLE_DATA)
18-
TABLE_DATA_AS_DF = DataFrame(
19-
TABLE_DATA_AS_DF.values[1:], columns=TABLE_DATA_AS_DF.iloc[0]
20-
).astype({"Age": int})
17+
_TMP_DF = DataFrame(TABLE_DATA)
18+
TABLE_DATA_AS_DF = DataFrame(_TMP_DF.values[1:], columns=_TMP_DF.iloc[0])
2119

2220

2321
###############################################################################
@@ -97,6 +95,7 @@ def test_tabula_extract_simple_table(filename):
9795
dataframes = tabula.read_pdf(HERE / filename, pages="all")
9896
assert len(dataframes) == 1
9997
for df in dataframes:
98+
df = df.astype({"Age": str})
10099
assert_frame_equal(df, TABLE_DATA_AS_DF, check_names=False)
101100

102101

@@ -111,6 +110,7 @@ def test_tabula_extract_two_tables(filename):
111110
dataframes = tabula.read_pdf(HERE / filename, pages="all")
112111
assert len(dataframes) == 2
113112
for df in dataframes:
113+
df = df.astype({"Age": str})
114114
assert_frame_equal(df, TABLE_DATA_AS_DF, check_names=False)
115115

116116

test/test_perfs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
HERE = Path(__file__).resolve().parent
99

1010

11-
@ensure_rss_memory_below(max_in_mib=175)
11+
@ensure_rss_memory_below(max_in_mib=6)
1212
@pytest.mark.timeout(40)
1313
def test_intense_image_rendering():
1414
png_file_paths = []

0 commit comments

Comments
 (0)