Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Rewrite JS tests to pytest #746

Merged
merged 2 commits into from Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yaml
Expand Up @@ -48,4 +48,4 @@ jobs:

- name: Test with pytest
run: |
pytest Tests/tests.py Tests --cov --cov-report term-missing -vv
pytest Tests --cov --cov-report term-missing -vv
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -14,4 +14,4 @@ clean:
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

test:
pytest Tests/tests.py Tests --cov --cov-report term-missing -vv --cov-report html
pytest Tests --cov --cov-report term-missing -vv --cov-report html
44 changes: 44 additions & 0 deletions Tests/test_javascript.py
@@ -0,0 +1,44 @@
import os
import pytest

from PyPDF2 import PdfFileReader, PdfFileWriter

# Configure path environment
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(TESTS_ROOT)
RESOURCE_ROOT = os.path.join(PROJECT_ROOT, "Resources")

@pytest.fixture
def pdf_file_writer():
ipdf = PdfFileReader(os.path.join(RESOURCE_ROOT, "crazyones.pdf"))
pdf_file_writer = PdfFileWriter()
pdf_file_writer.appendPagesFromReader(ipdf)
yield pdf_file_writer

def test_add_js(pdf_file_writer):
pdf_file_writer.addJS(
"this.print({bUI:true,bSilent:false,bShrinkToFit:true});"
)

assert "/Names" in pdf_file_writer._root_object, "addJS should add a name catalog in the root object."
assert "/JavaScript" in pdf_file_writer._root_object["/Names"], "addJS should add a JavaScript name tree under the name catalog."
assert "/OpenAction" in pdf_file_writer._root_object, "addJS should add an OpenAction to the catalog."

def test_overwrite_js(pdf_file_writer):
def get_javascript_name():
assert "/Names" in pdf_file_writer._root_object
assert "/JavaScript" in pdf_file_writer._root_object["/Names"]
assert "/Names" in pdf_file_writer._root_object["/Names"]["/JavaScript"]
return pdf_file_writer._root_object["/Names"]["/JavaScript"]["/Names"][0]

pdf_file_writer.addJS(
"this.print({bUI:true,bSilent:false,bShrinkToFit:true});"
)
first_js = get_javascript_name()

pdf_file_writer.addJS(
"this.print({bUI:true,bSilent:false,bShrinkToFit:true});"
)
second_js = get_javascript_name()

assert first_js != second_js, "addJS should overwrite the previous script in the catalog."
135 changes: 0 additions & 135 deletions Tests/tests.py

This file was deleted.

2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -7,4 +7,4 @@ deps =
pillow
pytest
pytest-cov
commands = pytest Tests/tests.py Tests --cov --cov-report term-missing -vv
commands = pytest Tests --cov --cov-report term-missing -vv