Skip to content

Commit

Permalink
TST: Rewrite JS tests from unittest to pytest (#746)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
  • Loading branch information
MasterOdin committed Apr 14, 2022
1 parent 7771fad commit 0ea2301
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 138 deletions.
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

0 comments on commit 0ea2301

Please sign in to comment.