forked from anjesh/pdf-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdfProcessorTest.py
88 lines (76 loc) · 3.59 KB
/
PdfProcessorTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/local/bin/python
import unittest
import sys
import os.path
import os
import glob
import ConfigParser
from PdfProcessor import *
class PdfProcessorTest(unittest.TestCase):
def setUp(self):
"""
create dir if not present,
delete files if dir present
"""
self.outdir = "tests/out/pdfprocessor"
if not os.path.exists(self.outdir):
os.makedirs(self.outdir)
else:
files = glob.glob(self.outdir)
for f in files:
if os.path.isfile(f):
os.remove(f)
self.configParser = ConfigParser.RawConfigParser()
self.configParser.read('settings.config')
def testStructuredPdf(self):
pdfProcessor = PDFProcessor('tests/sample.pdf', self.outdir)
self.assertTrue(pdfProcessor.isStructured())
def testScannedPdf(self):
pdfProcessor = PDFProcessor('tests/sample-scanned.pdf', self.outdir)
self.assertFalse(pdfProcessor.isStructured())
def testScannedPdfStats(self):
pdfProcessor = PDFProcessor('tests/sample-scanned.pdf', self.outdir)
pdfProcessor.writeStats()
with open(os.path.join(self.outdir,"stats.json")) as json_file:
json_data = json.load(json_file)
self.assertEqual(json_data['status'], "Scanned")
self.assertEqual(json_data['pages'], 2)
def testEncryptedScannedPdfStats(self):
try:
pdfProcessor = PDFProcessor('tests/sample-scanned-encrypted.pdf', self.outdir)
except Exception as e:
self.assertEqual("Pdf is encrypted. Can't do processing.", str(e))
with open(os.path.join(self.outdir,"stats.json")) as json_file:
json_data = json.load(json_file)
self.assertEqual(json_data['status'], "Encrypted")
self.assertEqual(json_data['pages'], 69)
def testStructuredPdfStats(self):
pdfProcessor = PDFProcessor('tests/sample.pdf', self.outdir)
pdfProcessor.writeStats()
with open(os.path.join(self.outdir,"stats.json")) as json_file:
json_data = json.load(json_file)
self.assertEqual(json_data['status'], "Structured")
self.assertEqual(json_data['pages'], 5)
def testStructuredPdfExtractPages(self):
pdfProcessor = PDFProcessor('tests/sample.pdf', self.outdir)
self.assertTrue(pdfProcessor.isStructured())
pdfProcessor.extractTextFromStructuredDoc()
self.assertTrue(os.path.isdir(os.path.join(self.outdir,"text")))
self.assertTrue(os.path.isfile(os.path.join(self.outdir,"text","1.txt")))
self.assertTrue(os.path.isfile(os.path.join(self.outdir,"text","5.txt")))
def testSeparatePdfPages(self):
pdfProcessor = PDFProcessor('tests/sample.pdf', self.outdir)
pdfProcessor.separatePdfPages()
self.assertTrue(os.path.isdir(os.path.join(self.outdir,"pages")))
self.assertTrue(os.path.isfile(os.path.join(self.outdir,"pages","1.pdf")))
self.assertTrue(os.path.isfile(os.path.join(self.outdir,"pages","5.pdf")))
def testScannedPdfExtractPages(self):
try:
pdfProcessor = PDFProcessor('tests/sample-scanned-1.pdf', self.outdir)
pdfProcessor.setConfigParser(self.configParser)
self.assertFalse(pdfProcessor.isStructured())
pdfProcessor.extractTextFromScannedDoc()
self.assertTrue(os.path.isdir(os.path.join(self.outdir,"text")))
self.assertTrue(os.path.isfile(os.path.join(self.outdir,"text","1.txt")))
except Exception:
pass