Skip to content

Commit e6a6d0c

Browse files
committed
TST: Rewrite JS tests to pytest
Signed-off-by: Matthew Peveler <[email protected]>
1 parent 38d5ec4 commit e6a6d0c

File tree

6 files changed

+53
-138
lines changed

6 files changed

+53
-138
lines changed

.github/workflows/unit-tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ jobs:
4848

4949
- name: Test with pytest
5050
run: |
51-
pytest Tests/tests.py Tests --cov --cov-report term-missing -vv
51+
pytest Tests --cov --cov-report term-missing -vv

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ clean:
1414
rm -rf Tests/__pycache__ PyPDF2/__pycache__ Image9.png htmlcov docs/_build dist dont_commit_merged.pdf dont_commit_writer.pdf PyPDF2.egg-info PyPDF2_pdfLocation.txt
1515

1616
test:
17-
pytest Tests/tests.py Tests --cov --cov-report term-missing -vv --cov-report html
17+
pytest Tests --cov --cov-report term-missing -vv --cov-report html

Tests/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
3+
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
4+
PROJECT_ROOT = os.path.dirname(TESTS_ROOT)
5+
RESOURCE_ROOT = os.path.join(PROJECT_ROOT, "Resources")

Tests/test_javascript.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import os
2+
import pytest
3+
4+
from PyPDF2 import PdfFileReader, PdfFileWriter
5+
6+
# Configure path environment
7+
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
8+
PROJECT_ROOT = os.path.dirname(TESTS_ROOT)
9+
RESOURCE_ROOT = os.path.join(PROJECT_ROOT, "Resources")
10+
11+
@pytest.fixture
12+
def pdf_file_writer():
13+
ipdf = PdfFileReader(os.path.join(RESOURCE_ROOT, "crazyones.pdf"))
14+
pdf_file_writer = PdfFileWriter()
15+
pdf_file_writer.appendPagesFromReader(ipdf)
16+
yield pdf_file_writer
17+
18+
def test_add_js(pdf_file_writer):
19+
20+
pdf_file_writer.addJS(
21+
"this.print({bUI:true,bSilent:false,bShrinkToFit:true});"
22+
)
23+
24+
assert "/Names" in pdf_file_writer._root_object, "addJS should add a name catalog in the root object."
25+
assert "/JavaScript" in pdf_file_writer._root_object["/Names"], "addJS should add a JavaScript name tree under the name catalog."
26+
assert "/OpenAction" in pdf_file_writer._root_object, "addJS should add an OpenAction to the catalog."
27+
28+
def test_overwrite_js(pdf_file_writer):
29+
def get_javascript_name():
30+
assert "/Names" in pdf_file_writer._root_object
31+
assert "/JavaScript" in pdf_file_writer._root_object["/Names"]
32+
assert "/Names" in pdf_file_writer._root_object["/Names"]["/JavaScript"]
33+
return pdf_file_writer._root_object["/Names"]["/JavaScript"]["/Names"][0]
34+
35+
pdf_file_writer.addJS(
36+
"this.print({bUI:true,bSilent:false,bShrinkToFit:true});"
37+
)
38+
first_js = get_javascript_name()
39+
40+
pdf_file_writer.addJS(
41+
"this.print({bUI:true,bSilent:false,bShrinkToFit:true});"
42+
)
43+
second_js = get_javascript_name()
44+
45+
assert first_js != second_js, "addJS should overwrite the previous script in the catalog."

Tests/tests.py

Lines changed: 0 additions & 135 deletions
This file was deleted.

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ deps =
77
pillow
88
pytest
99
pytest-cov
10-
commands = pytest Tests/tests.py Tests --cov --cov-report term-missing -vv
10+
commands = pytest Tests --cov --cov-report term-missing -vv

0 commit comments

Comments
 (0)