From e6a6d0cd679cde05efad2f7cb5eb0ca7802e700b Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 14 Apr 2022 10:43:50 -0400 Subject: [PATCH] TST: Rewrite JS tests to pytest Signed-off-by: Matthew Peveler --- .github/workflows/unit-tests.yaml | 2 +- Makefile | 2 +- Tests/__init__.py | 5 ++ Tests/test_javascript.py | 45 ++++++++++ Tests/tests.py | 135 ------------------------------ tox.ini | 2 +- 6 files changed, 53 insertions(+), 138 deletions(-) create mode 100644 Tests/test_javascript.py delete mode 100644 Tests/tests.py diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml index e7e405a88..31179af76 100644 --- a/.github/workflows/unit-tests.yaml +++ b/.github/workflows/unit-tests.yaml @@ -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 diff --git a/Makefile b/Makefile index 84c93a264..8516f1e25 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/Tests/__init__.py b/Tests/__init__.py index e69de29bb..e4404b2df 100644 --- a/Tests/__init__.py +++ b/Tests/__init__.py @@ -0,0 +1,5 @@ +import os + +TESTS_ROOT = os.path.abspath(os.path.dirname(__file__)) +PROJECT_ROOT = os.path.dirname(TESTS_ROOT) +RESOURCE_ROOT = os.path.join(PROJECT_ROOT, "Resources") diff --git a/Tests/test_javascript.py b/Tests/test_javascript.py new file mode 100644 index 000000000..38fccd190 --- /dev/null +++ b/Tests/test_javascript.py @@ -0,0 +1,45 @@ +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." diff --git a/Tests/tests.py b/Tests/tests.py deleted file mode 100644 index 036fe0af3..000000000 --- a/Tests/tests.py +++ /dev/null @@ -1,135 +0,0 @@ -import os -import sys -import unittest -import binascii - -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") - -sys.path.append(PROJECT_ROOT) - - -if sys.version_info[0] < 3: - - def u_(s): - return s.decode("utf-8") - -else: - - def u_(s): - return s - - -class PdfReaderTestCases(unittest.TestCase): - def test_PdfReaderFileLoad(self): - """ - Test loading and parsing of a file. Extract text of the file and compare to expected - textual output. Expected outcome: file loads, text matches expected. - """ - - with open(os.path.join(RESOURCE_ROOT, "crazyones.pdf"), "rb") as inputfile: - # Load PDF file from file - ipdf = PdfFileReader(inputfile) - ipdf_p1 = ipdf.getPage(0) - - # Retrieve the text of the PDF - with open( - os.path.join(RESOURCE_ROOT, "crazyones.txt"), "rb" - ) as pdftext_file: - pdftext = pdftext_file.read() - - ipdf_p1_text = ipdf_p1.extractText().replace("\n", "").encode("utf-8") - - # Compare the text of the PDF to a known source - self.assertEqual( - ipdf_p1_text, - pdftext, - msg="PDF extracted text differs from expected value.\n\nExpected:\n\n%r\n\nExtracted:\n\n%r\n\n" - % (pdftext, ipdf_p1_text), - ) - - def test_PdfReaderJpegImage(self): - """ - Test loading and parsing of a file. Extract the image of the file and compare to expected - textual output. Expected outcome: file loads, image matches expected. - """ - - with open(os.path.join(RESOURCE_ROOT, "jpeg.pdf"), "rb") as inputfile: - # Load PDF file from file - ipdf = PdfFileReader(inputfile) - - # Retrieve the text of the image - with open(os.path.join(RESOURCE_ROOT, "jpeg.txt"), "r") as pdftext_file: - imagetext = pdftext_file.read() - - ipdf_p0 = ipdf.getPage(0) - xObject = ipdf_p0["/Resources"]["/XObject"].getObject() - data = xObject["/Im4"].getData() - - # Compare the text of the PDF to a known source - self.assertEqual( - binascii.hexlify(data).decode(), - imagetext, - msg="PDF extracted image differs from expected value.\n\nExpected:\n\n%r\n\nExtracted:\n\n%r\n\n" - % (imagetext, binascii.hexlify(data).decode()), - ) - - -class AddJsTestCase(unittest.TestCase): - def setUp(self): - ipdf = PdfFileReader(os.path.join(RESOURCE_ROOT, "crazyones.pdf")) - self.pdf_file_writer = PdfFileWriter() - self.pdf_file_writer.appendPagesFromReader(ipdf) - - def test_add(self): - - self.pdf_file_writer.addJS( - "this.print({bUI:true,bSilent:false,bShrinkToFit:true});" - ) - - self.assertIn( - "/Names", - self.pdf_file_writer._root_object, - "addJS should add a name catalog in the root object.", - ) - self.assertIn( - "/JavaScript", - self.pdf_file_writer._root_object["/Names"], - "addJS should add a JavaScript name tree under the name catalog.", - ) - self.assertIn( - "/OpenAction", - self.pdf_file_writer._root_object, - "addJS should add an OpenAction to the catalog.", - ) - - def test_overwrite(self): - - self.pdf_file_writer.addJS( - "this.print({bUI:true,bSilent:false,bShrinkToFit:true});" - ) - first_js = self.get_javascript_name() - - self.pdf_file_writer.addJS( - "this.print({bUI:true,bSilent:false,bShrinkToFit:true});" - ) - second_js = self.get_javascript_name() - - self.assertNotEqual( - first_js, - second_js, - "addJS should overwrite the previous script in the catalog.", - ) - - def get_javascript_name(self): - self.assertIn("/Names", self.pdf_file_writer._root_object) - self.assertIn("/JavaScript", self.pdf_file_writer._root_object["/Names"]) - self.assertIn( - "/Names", self.pdf_file_writer._root_object["/Names"]["/JavaScript"] - ) - return self.pdf_file_writer._root_object["/Names"]["/JavaScript"]["/Names"][0] diff --git a/tox.ini b/tox.ini index bf2ceb3a8..517e48eba 100644 --- a/tox.ini +++ b/tox.ini @@ -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