Skip to content

Commit

Permalink
Unified duplicated logic
Browse files Browse the repository at this point in the history
- Able to unify some of the duplicated logic for merging files
- Fixed test cases and added more assertions to check correctness
  • Loading branch information
Ayush-Kaushik committed Jun 18, 2024
1 parent c446aeb commit 69a2325
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ report.html
.DS_Store
tests/.DS_Store
tests/data/.DS_Store
tests/output/*.pdf
tests/output/*
5 changes: 3 additions & 2 deletions pdf_merger/src/services/pdf_merger_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ def __init__(

def merge_files(self):
for file in self.file_list:
self.merger.append(file.as_uri())
self.merger.write(self.target_file_path)
self.merger.append(str(file)) # solution: https://docs.python.org/3/library/pathlib.html#general-properties
self.merger.write(str(self.target_file_path))

27 changes: 15 additions & 12 deletions tests/test_image_merger_service.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# Author: Ayush Kaushik

import unittest

import PyPDF2
import img2pdf

from PyPDF2 import PdfReader
from pathlib import Path
from pdf_merger.src.services import ImageMergerService


class TestImagesToPdfService(unittest.TestCase):
def setUp(self):
test_path = str(Path(__file__).parent.resolve())
self.target_file_path = Path(test_path + "/output/test_output.pdf")
self.target_file_path = Path(test_path + "/output/test_image_merge_output.pdf")
self.file_list = [
Path(test_path + "/data/image/file_example_JPG_1MB.jpg"),
Path(test_path + "/data/image/file_example_JPG_1MB.jpg")
]

self.service = ImageMergerService(
img2pdf,
[
Path(test_path + "/data/image/file_example_JPG_1MB.jpg"),
Path(test_path + "/data/image/file_example_JPG_1MB.jpg")
],
self.file_list,
self.target_file_path)

def test_merge_files(self):
Expand All @@ -29,14 +30,16 @@ def test_merge_files(self):
def validate_pdf(self) -> bool:
try:
with open(self.target_file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
reader = PdfReader(file)
if len(reader.pages) != len(self.file_list): # easy to assert since there is one image per file
return False

first_page = reader.pages[0]
text = first_page.extract_text()
if text is None:
raise ValueError("Merged file is invalid.")
return False
return True
except Exception as e:
return False
return True
except:
return False

def test_set_target_path(self):
Expand Down
24 changes: 19 additions & 5 deletions tests/test_pdf_merger_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import unittest
from pathlib import Path
from PyPDF2 import PdfMerger


from PyPDF2 import PdfMerger, PdfReader

from pdf_merger.src.services.pdf_merger_service import PdfMergerService

Expand All @@ -13,15 +15,27 @@ def setUp(self):
merger = PdfMerger()
path_list = [
Path(test_path + '/data/pdf/FREE_Test_Data_1MB_PDF.pdf'),
Path(test_path + '/data/pdf/FREE_Test_Data_100KB_PDF')
Path(test_path + '/data/pdf/FREE_Test_Data_100KB_PDF.pdf')
]
target_path = Path(test_path + '/output/test_output.pdf')
self.service = PdfMergerService(merger, path_list, target_path)
self.target_file_path = Path(test_path + '/output/test_pdf_merge_output.pdf')
self.service = PdfMergerService(merger, path_list, self.target_file_path)

def test_merge_files(self):
self.service.merge_files()
# TODO check if file exists in the location specified with specific name
self.assertTrue(self.service.target_file_path.exists())
self.assertTrue(self.validate_pdf())

def validate_pdf(self) -> bool:
try:
with open(self.target_file_path, 'rb') as file:
reader = PdfReader(file)
first_page = reader.pages[0]
text = first_page.extract_text()
if text is None:
return False
return True
except:
return False

def test_clear_list(self):
self.service.clear_list()
Expand Down

0 comments on commit 69a2325

Please sign in to comment.