-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ADD `test_data_path()` func * CUT password prompting from `Info.__init__` for encrypted pdfs - fix `Info().decrypted()` method * simplify `Info().encrypted()` method * add todo * ADD utils module test suite with test data * MAKE test_utils_path.py for testing `add_suffix()` function * MAKE test_utils_read.py for testing `pypdf3_reader()` function * reformat todo * reformat todo * CUT unused test function
- Loading branch information
Showing
10 changed files
with
251 additions
and
18 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
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.
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,143 @@ | ||
import unittest | ||
|
||
from looptools import Timer | ||
|
||
from pdfconduit import Info | ||
from tests import * | ||
|
||
|
||
class TestInfo(unittest.TestCase): | ||
@Timer.decorator | ||
def test_pypdf3_is_encrypted(self): | ||
info = self._get_info('encrypted.pdf') | ||
|
||
self.assertTrue(info.encrypted) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_is_decrypted(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertTrue(info.decrypted) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_is_not_encrypted(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertFalse(info.encrypted) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_is_not_decrypted(self): | ||
info = self._get_info('encrypted.pdf') | ||
|
||
self.assertFalse(info.decrypted) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_pages(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertIsInstance(info.pages, int) | ||
self.assertEqual(info.pages, 1) | ||
self.assertEqual(Info(test_data_path('document.pdf')).pages, 11) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_metadata(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertIsInstance(info.metadata, dict) | ||
self.assertEqual(info.metadata['/Creator'], 'This PDF is created by PDF4U Pro 2.0') | ||
self.assertEqual(info.metadata['/CreationDate'], 'D:20040120105826',) | ||
self.assertEqual(info.metadata['/Producer'], 'PDF4U Adobe PDF Creator 2.0',) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_resources(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertEqual(info.pages, len(info.resources())) | ||
|
||
resources = info.resources()[0] | ||
self.assertIsInstance(resources, dict) | ||
self.assertEqual(resources['/Type'], '/Page') | ||
|
||
mediabox = list(resources['/MediaBox']) | ||
self.assertEqual(mediabox[0], 0) | ||
self.assertEqual(mediabox[1], 0) | ||
self.assertEqual(float(mediabox[2]), 595.276) | ||
self.assertEqual(float(mediabox[3]), 841.89) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_security_encrypted_pdf(self): | ||
info = self._get_info('encrypted.pdf') | ||
|
||
self.assertIsInstance(info.security, dict) | ||
self.assertEqual(info.security, {}) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_security_decrypted_pdf(self): | ||
info = self._get_info('encrypted.pdf', 'foo') | ||
|
||
self.assertIsInstance(info.security, dict) | ||
self.assertTrue('/V' in info.security) | ||
self.assertTrue('/R' in info.security) | ||
self.assertTrue('/Length' in info.security) | ||
self.assertTrue('/P' in info.security) | ||
self.assertTrue('/Filter' in info.security) | ||
self.assertTrue('/O' in info.security) | ||
self.assertTrue('/U' in info.security) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_security_passwordless_pdf(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertIsInstance(info.security, dict) | ||
self.assertEqual(info.security, {}) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_dimensions(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertIsInstance(info.dimensions, dict) | ||
self.assertTrue('w' in info.dimensions) | ||
self.assertTrue('h' in info.dimensions) | ||
self.assertIsInstance(info.dimensions['w'], float) | ||
self.assertIsInstance(info.dimensions['h'], float) | ||
self.assertEqual(info.dimensions['w'], 595.276) | ||
self.assertEqual(info.dimensions['h'], 841.89) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_size(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertIsInstance(info.size, tuple) | ||
self.assertEqual(len(info.size), 2) | ||
self.assertIsInstance(info.size[0], float) | ||
self.assertIsInstance(info.size[1], float) | ||
self.assertEqual(info.size[0], 595.276) | ||
self.assertEqual(info.size[1], 841.89) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_size_and_dimensions_are_equal(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertEqual(info.size[0], info.dimensions['w']) | ||
self.assertEqual(info.size[1], info.dimensions['h']) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_rotate_no_rotation(self): | ||
info = self._get_info('article.pdf') | ||
|
||
self.assertEqual(info.rotate, None) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_rotate_rotated(self): | ||
info = self._get_info('rotated.pdf') | ||
|
||
self.assertIsInstance(info.rotate, int) | ||
self.assertEqual(info.rotate, 90) | ||
|
||
@staticmethod | ||
def _get_info(filename, password=None): | ||
return Info(test_data_path(filename), password=password) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,45 @@ | ||
import os.path | ||
import unittest | ||
|
||
from looptools import Timer | ||
|
||
from pdfconduit.utils import add_suffix, set_destination | ||
from tests import * | ||
|
||
|
||
class TestPath(unittest.TestCase): | ||
@Timer.decorator | ||
def test_add_suffix(self): | ||
file_path = test_data_path('article.pdf') | ||
with_suffix = add_suffix(file_path) | ||
|
||
self.assertIsInstance(with_suffix, str) | ||
self.assertEqual(with_suffix, os.path.join(os.path.dirname(file_path), 'article_modified.pdf')) | ||
|
||
@Timer.decorator | ||
def test_add_suffix_suffix(self): | ||
file_path = test_data_path('article.pdf') | ||
with_suffix = add_suffix(file_path, 'new') | ||
|
||
self.assertIsInstance(with_suffix, str) | ||
self.assertEqual(with_suffix, os.path.join(os.path.dirname(file_path), 'article_new.pdf')) | ||
|
||
@Timer.decorator | ||
def test_add_suffix_suffix_sep(self): | ||
file_path = test_data_path('article.pdf') | ||
with_suffix = add_suffix(file_path, 'old', '-') | ||
|
||
self.assertIsInstance(with_suffix, str) | ||
self.assertEqual(with_suffix, os.path.join(os.path.dirname(file_path), 'article-old.pdf')) | ||
|
||
@Timer.decorator | ||
def test_add_suffix_suffix_sep_ext(self): | ||
file_path = test_data_path('article.pdf') | ||
with_suffix = add_suffix(file_path, 'backup', '-', 'zip') | ||
|
||
self.assertIsInstance(with_suffix, str) | ||
self.assertEqual(with_suffix, os.path.join(os.path.dirname(file_path), 'article-backup.zip')) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,42 @@ | ||
import unittest | ||
|
||
from PyPDF3 import PdfFileReader | ||
from PyPDF3.utils import PdfReadError | ||
from looptools import Timer | ||
|
||
from pdfconduit.utils import pypdf3_reader | ||
from tests import * | ||
|
||
|
||
class TestRead(unittest.TestCase): | ||
@Timer.decorator | ||
def test_pypdf3_reader_can_read_unencrypted(self): | ||
file_path = test_data_path('document.pdf') | ||
reader = pypdf3_reader(file_path) | ||
|
||
self.assertIsInstance(reader, PdfFileReader) | ||
self.assertEqual(reader.getNumPages(), 11) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_reader_cant_read_encrypted(self): | ||
file_path = test_data_path('encrypted.pdf') | ||
reader = pypdf3_reader(file_path) | ||
|
||
with self.assertRaises(PdfReadError) as context: | ||
reader.getNumPages() | ||
|
||
self.assertIsInstance(context.exception, PdfReadError) | ||
self.assertEqual('File has not been decrypted', context.exception.__str__()) | ||
|
||
@Timer.decorator | ||
def test_pypdf3_reader_can_read_encrypted_with_password(self): | ||
file_path = test_data_path('encrypted.pdf') | ||
reader = pypdf3_reader(file_path, 'foo') | ||
|
||
self.assertIsInstance(reader, PdfFileReader) | ||
self.assertTrue(reader.isEncrypted) | ||
self.assertEqual(reader.getNumPages(), 11) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |