-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from JSv4/JSv4/fix-parsing-issue-improve-test…
…s-add-ocr-detection Dynamically Apply OCR, Improve PDF Utilities and Tests
- Loading branch information
Showing
6 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import io | ||
import os | ||
import tempfile | ||
|
||
from django.test import TestCase | ||
|
||
from opencontractserver.tests.fixtures import ( | ||
NLM_INGESTOR_SAMPLE_PDF, | ||
NLM_INGESTOR_SAMPLE_PDF_NEEDS_OCR, | ||
) | ||
from opencontractserver.utils.pdf import ( | ||
base_64_encode_bytes, | ||
check_if_pdf_needs_ocr, | ||
convert_hex_to_rgb_tuple, | ||
createHighlight, | ||
split_pdf_into_images, | ||
) | ||
|
||
|
||
class PDFUtilsTestCase(TestCase): | ||
def setUp(self): | ||
# Create a sample PDF file for testing | ||
self.sample_pdf_content = NLM_INGESTOR_SAMPLE_PDF.read_bytes() | ||
self.need_ocr_pdf_content = NLM_INGESTOR_SAMPLE_PDF_NEEDS_OCR.read_bytes() | ||
|
||
def test_check_if_pdf_needs_ocr_with_text(self): | ||
needs_ocr = check_if_pdf_needs_ocr(io.BytesIO(self.sample_pdf_content)) | ||
self.assertFalse(needs_ocr) | ||
|
||
def test_check_if_pdf_needs_ocr_without_text(self): | ||
# Create a PDF without extractable text | ||
needs_ocr = check_if_pdf_needs_ocr(io.BytesIO(self.need_ocr_pdf_content)) | ||
self.assertTrue(needs_ocr) | ||
|
||
def test_base_64_encode_bytes(self): | ||
test_bytes = b"Hello, World!" | ||
encoded = base_64_encode_bytes(test_bytes) | ||
self.assertEqual(encoded, "SGVsbG8sIFdvcmxkIQ==") | ||
|
||
def test_convert_hex_to_rgb_tuple(self): | ||
hex_color = "FF8000" | ||
rgb_tuple = convert_hex_to_rgb_tuple(hex_color) | ||
self.assertEqual(rgb_tuple, (255, 128, 0)) | ||
|
||
def test_create_highlight(self): | ||
highlight = createHighlight( | ||
x1=10, | ||
y1=20, | ||
x2=30, | ||
y2=40, | ||
meta={"author": "Test Author", "contents": "Test Contents"}, | ||
color=(1.0, 0.5, 0.0), | ||
) | ||
self.assertEqual(highlight["/Type"], "/Annot") | ||
self.assertEqual(highlight["/Subtype"], "/Highlight") | ||
self.assertEqual(highlight["/T"], "Test Author") | ||
self.assertEqual(highlight["/Contents"], "Test Contents") | ||
|
||
def test_split_pdf_into_images(self): | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
# Call the function | ||
result = split_pdf_into_images(self.need_ocr_pdf_content, temp_dir) | ||
print(f"Result: {result}") | ||
# Check the results | ||
self.assertEqual(len(result), 1) | ||
self.assertTrue(all(path.endswith(".png") for path in result)) | ||
|
||
# Verify that files were actually created | ||
for path in result: | ||
self.assertTrue(os.path.exists(path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters