Пример #1
0
class JobClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    job_client = JobClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()
    json_converter = JsonConverter()
class ImageClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    image_client = ImageClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_create_images(self):
        create_images = CreateImages(
            document=Document(doc_data=self.test_files.pdf_data),
            image_action=ImageAction(
                width_pixel=4000,
                page_selection=PageSelection(page_nrs=[1]),
                image_extension='jpg'))

        return create_images

    def test_create_images_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.image_client.create_images(None)

        assert_equal(e.exception.msg,
                     'The create_images parameter cannot be None.')

    def test_create_images_document_throws_exception(self):
        # prepare args
        create_images = self.create_create_images()
        create_images.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.image_client.create_images(create_images=create_images)

        assert_equal(e.exception.msg,
                     'The create_images document cannot be None.')

    def test_create_images_document_data_throws_exception(self):
        # prepare args
        create_images = self.create_create_images()
        create_images.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.image_client.create_images(create_images=create_images)

        assert_equal(e.exception.msg,
                     'The create_images document cannot be None.')

    def test_create_images_action_throws_exception(self):
        # prepare args
        create_images = self.create_create_images()
        create_images.image_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.image_client.create_images(create_images=create_images)

        assert_equal(e.exception.msg, 'The image_action cannot be None.')

    def test_create_images_action_page_selection_throws_exception(self):
        # prepare args
        create_images = self.create_create_images()
        create_images.image_action.page_selection = None

        with assert_raises(Pdf4meClientException) as e:
            self.image_client.create_images(create_images=create_images)

        assert_equal(e.exception.msg,
                     'The page_selection of the image_action cannot be None.')

    def test_create_images_no_none_response(self):
        # request
        create_images = self.create_create_images()
        res = self.image_client.create_images(create_images=create_images)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['pages'])
        self.assertIsNotNone(res['document']['pages'][0]['thumbnail'])

    def test_create_images_doc_length(self):
        # request
        create_images = self.create_create_images()
        res = self.image_client.create_images(create_images=create_images)

        # validation
        thumbnail_length = len(res['document']['pages'][0]['thumbnail'])
        self.assertIsNotNone(self.check.not_zero(thumbnail_length))

    def test_create_thumbnail_no_none_response(self):
        # request
        res = self.image_client.create_thumbnail(
            width=4000,
            page_nr='1',
            image_format='jpg',
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        self.assertIsNotNone(res)

    def test_create_thumbnail_doc_length(self):
        # request
        res = self.image_client.create_thumbnail(
            width=4000,
            page_nr='1',
            image_format='jpg',
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        self.assertTrue(self.check.not_zero(len(res)))
Пример #3
0
class BarcodeClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    barcode_client = BarcodeClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_read_barcode(self):
        read_barcodes = ReadBarcodes(
            document=Document(
                doc_data=self.test_files.pdf_barcode_data
            ),
            read_barcode_action=ReadBarcodeAction(
                barcode_types=['all']
            )
        )

        return read_barcodes

    def test_barcode_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.read_barcodes(None)

        assert_equal(e.exception.msg,
                     'The read_barcodes parameter cannot be None.')

    def test_barcode_document_throws_exception(self):
        # prepare args
        read_barcode = self.create_read_barcode()
        read_barcode.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.read_barcodes(read_barcodes=read_barcode)

        assert_equal(e.exception.msg,
                     'The read_barcodes document cannot be None.')

    def test_barcode_document_data_throws_exception(self):
        # prepare args
        read_barcodes = self.create_read_barcode()
        read_barcodes.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.read_barcodes(read_barcodes=read_barcodes)

        assert_equal(e.exception.msg,
                     'The read_barcodes document cannot be None.')

    def test_barcode_action_throws_exception(self):
        # prepare args
        read_barcodes = self.create_read_barcode()
        read_barcodes.read_barcode_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.read_barcodes(read_barcodes=read_barcodes)

        assert_equal(e.exception.msg,
                     'The read_barcode_action cannot be None.')

    def test_barcode_no_none_response(self):
        # request
        read_barcodes = self.create_read_barcode()
        res = self.barcode_client.read_barcodes(read_barcodes=read_barcodes)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['barcodes'])
        self.assertIsNotNone(res['barcodes'][0]['barcode_data'])

    def test_barcode_doc_length(self):
        # request
        read_barcodes = self.create_read_barcode()
        res = self.barcode_client.read_barcodes(read_barcodes=read_barcodes)

        # validation
        read_barcodes_text = len(res['barcodes'][0]['barcode_data'])

        self.assertTrue(self.check.not_zero(read_barcodes_text))

    def test_barcode_by_profile_no_none_response(self):
        # request
        res = self.barcode_client.read_barcodes_by_type(
            barcode_type='all',
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_barcode_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_barcode_by_profile_doc_length(self):
        # request
        res = self.barcode_client.read_barcodes_by_type(
            barcode_type='all',
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_barcode_path)
        )

        # validation
        read_barcodes_text = len(res['barcodes'][0]['barcode_data'])

        self.assertTrue(self.check.not_zero(read_barcodes_text))

    def create_add_barcode(self):
        add_barcode = AddBarcode(
            document=Document(
                doc_data=self.test_files.pdf_data
            ),
            add_barcode_action=AddBarcodeAction(
                barcode_type='qrCode',
                value='welcome to pdf4me',
                height=100,
                width=100,
                align_x='center',
                align_y='middle',
                margin_x=0,
                margin_y=0,
                page_sequence='1',
                rotate='30',
                barcode_color=BarcodeColor(
                    red=0,
                    green=0,
                    blue=0,
                    alpha=100,
                ),
                background_color=BarcodeColor(
                    red=1,
                    green=1,
                    blue=1,
                    alpha=100,
                ),
            )
        )

        return add_barcode

    def test_add_barcode_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.add_barcode(None)

        assert_equal(e.exception.msg,
                     'The add_barcode parameter cannot be None.')

    def test_add_barcode_document_throws_exception(self):
        # prepare args
        add_barcode = self.create_add_barcode()
        add_barcode.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.add_barcode(add_barcode=add_barcode)

        assert_equal(e.exception.msg,
                     'The add_barcode document cannot be None.')

    def test_add_barcode_document_data_throws_exception(self):
        # prepare args
        add_barcode = self.create_add_barcode()
        add_barcode.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.add_barcode(add_barcode=add_barcode)

        assert_equal(e.exception.msg,
                     'The add_barcode document cannot be None.')

    def test_add_barcode_action_throws_exception(self):
        # prepare args
        add_barcode = self.create_add_barcode()
        add_barcode.add_barcode_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.add_barcode(add_barcode=add_barcode)

        assert_equal(e.exception.msg, 'The add_barcode_action cannot be None.')

    def test_add_barcode_no_none_response(self):
        # request
        add_barcode = self.create_add_barcode()
        res = self.barcode_client.add_barcode(add_barcode=add_barcode)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_add_barcode_doc_length(self):
        # request
        add_barcode = self.create_add_barcode()
        res = self.barcode_client.add_barcode(add_barcode=add_barcode)

        # validation
        add_barcode_pdf = len(res['document']['doc_data'])

        self.assertTrue(self.check.not_zero(add_barcode_pdf))

    def test_add_barcode_by_type_no_none_respoonse(self):
        # request
        res = self.barcode_client.add_barcode_by_type(
            barcode_type='qrCode',
            text='EXAMPLE TEXT',
            pages='all',
            align_x='center',
            align_y='middle',
            height=100,
            width=100,
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path),
        )

        # validation
        self.assertIsNotNone(res)

    def test_add_barcode_by_type_doc_length(self):
        # request
        res = self.barcode_client.add_barcode_by_type(
            barcode_type='qrCode',
            text='EXAMPLE TEXT',
            pages='all',
            align_x='center',
            align_y='middle',
            height=100,
            width=100,
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path),
        )

        # validation
        barcoded_pdf_length = self.check.get_doc_base64_length(res)

        self.check.not_zero(barcoded_pdf_length)

    def test_create_barcode_by_type_no_none_respoonse(self):
        # request
        res = self.barcode_client.create_barcode_by_type(
            barcode_type='qrCode',
            content='EXAMPLE TEXT',
        )

        # validation
        self.assertIsNotNone(res)

    def test_create_barcode_by_type_doc_length(self):
        # request
        res = self.barcode_client.create_barcode_by_type(
            barcode_type='qrCode',
            content='EXAMPLE TEXT',
        )

        # validation
        barcode_length = self.check.get_doc_base64_length(res)

        self.check.not_zero(barcode_length)

    def create_split_by_barcode_req(self):
        split_by_barcode = SplitByBarcodeReq(
            document=Document(
                doc_data=self.test_files.pdf_barcode_data
            ),
            split_by_barcode_action=SplitByBarcodeAction(
                barcode_string='ara',
                barcode_filter='contains',
                barcode_type='any',
                split_barcode_page='after',
            )
        )

        return split_by_barcode

    def test_split_by_barcode_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.split_by_barcode(None)

        assert_equal(e.exception.msg,
                     'The split_by_barcode_req parameter cannot be None.')

    def test_asplit_by_barcode_document_throws_exception(self):
        # prepare args
        split_by_barcode_req = self.create_split_by_barcode_req()
        split_by_barcode_req.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.split_by_barcode(
                split_by_barcode_req=split_by_barcode_req)

        assert_equal(e.exception.msg,
                     'The split_by_barcode_req document cannot be None.')

    def test_split_by_barcode_document_data_throws_exception(self):
        # prepare args
        split_by_barcode_req = self.create_split_by_barcode_req()
        split_by_barcode_req.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.split_by_barcode(
                split_by_barcode_req=split_by_barcode_req)

        assert_equal(e.exception.msg,
                     'The split_by_barcode_req document cannot be None.')

    def test_split_by_barcode_action_throws_exception(self):
        # prepare args
        split_by_barcode_req = self.create_split_by_barcode_req()
        split_by_barcode_req.split_by_barcode_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.barcode_client.split_by_barcode(
                split_by_barcode_req=split_by_barcode_req)

        assert_equal(e.exception.msg,
                     'The split_by_barcode_action cannot be None.')

    def test_split_by_barcode_no_none_response(self):
        # request
        split_by_barcode_req = self.create_split_by_barcode_req()
        res = self.barcode_client.split_by_barcode(
            split_by_barcode_req=split_by_barcode_req)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['splitted_documents'])
        self.assertIsNotNone(res['splitted_documents'][0]['doc_data'])
        self.assertIsNotNone(res['splitted_documents'][1]['doc_data'])

    def test_split_by_barcode_doc_length(self):
        # request
        split_by_barcode_req = self.create_split_by_barcode_req()
        res = self.barcode_client.split_by_barcode(
            split_by_barcode_req=split_by_barcode_req)

        # validation
        doc_count = len(res['splitted_documents'])

        self.assertTrue(doc_count == 2)

        split_by_barcode_pdf_1 = len(res['splitted_documents'][0]['doc_data'])
        split_by_barcode_pdf_2 = len(res['splitted_documents'][1]['doc_data'])

        self.assertTrue(self.check.not_zero(split_by_barcode_pdf_1))
        self.assertTrue(self.check.not_zero(split_by_barcode_pdf_2))
class SplitClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    split_client = SplitClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_split(self):
        split = Split(document=Document(doc_data=self.test_files.pdf_data),
                      split_action=SplitAction(split_after_page=2))

        return split

    def test_split_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.split_client.split(None)

        assert_equal(e.exception.msg, 'The split parameter cannot be None.')

    def test_split_document_throws_exception(self):
        # prepare args
        split = self.create_split()
        split.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.split_client.split(split=split)

        assert_equal(e.exception.msg, 'The split document cannot be None.')

    def test_split_document_data_throws_exception(self):
        # prepare args
        split = self.create_split()
        split.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.split_client.split(split=split)

        assert_equal(e.exception.msg, 'The split document cannot be None.')

    def test_split_action_throws_exception(self):
        # prepare args
        split = self.create_split()
        split.split_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.split_client.split(split=split)

        assert_equal(e.exception.msg, 'The split_action cannot be None.')

    def test_split_action_split_after_page_1_throws_exception(self):
        # prepare args
        split = self.create_split()
        split.split_action.split_after_page = None

        with assert_raises(Pdf4meClientException) as e:
            self.split_client.split(split=split)

        assert_equal(
            e.exception.msg,
            'The split_after_page of split_action cannot be None or zero.'
            'The first page of a PDF corresponds to page number one.')

    def test_split_action_split_after_page_2_throws_exception(self):
        # prepare args
        split = self.create_split()
        split.split_action.split_after_page = 0

        with assert_raises(Pdf4meClientException) as e:
            self.split_client.split(split=split)

        assert_equal(
            e.exception.msg,
            'The split_after_page of split_action cannot be None or zero.'
            'The first page of a PDF corresponds to page number one.')

    def test_split_no_none_response(self):
        # request
        split = self.create_split()
        res = self.split_client.split(split=split)

        print(res)
        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['documents'])
        self.assertIsNotNone(res['documents'][0])
        self.assertIsNotNone(res['documents'][0]['doc_data'])
        self.assertIsNotNone(res['documents'][1])
        self.assertIsNotNone(res['documents'][1]['doc_data'])

    def test_split_doc_length(self):
        # request
        split = self.create_split()
        res = self.split_client.split(split=split)

        # validation
        original_length = self.test_files.pdf_length
        pdf1 = len(res['documents'][0]['doc_data'])
        pdf2 = len(res['documents'][1]['doc_data'])

        self.assertTrue(self.check.below_not_zero(pdf1, original_length))
        self.assertTrue(self.check.below_not_zero(pdf2, original_length))

    def test_split_by_page_nr_no_none_response(self):
        # request
        pdf_1, pdf_2 = self.split_client.split_by_page_nr(
            page_nr=2,
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        self.assertIsNotNone(pdf_1)
        self.assertIsNotNone(pdf_2)

    def test_split_by_page_nr_doc_length(self):
        # request
        pdf_1, pdf_2 = self.split_client.split_by_page_nr(
            page_nr=2,
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        original_length = self.test_files.pdf_length
        pdf_1_length = len(pdf_1)
        pdf_2_length = len(pdf_2)

        self.assertTrue(
            self.check.below_not_zero(pdf_1_length, original_length))
        self.assertTrue(
            self.check.below_not_zero(pdf_2_length, original_length))
class StampClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    stamp_client = StampClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_stamp(self):
        stamp = Stamp(
            document=Document(
                doc_data=self.test_files.pdf_data
            ),
            stamp_action=StampAction(
                text=Text(
                    value='EXAMPLE TEXT'
                ),
                alpha=1.0,
                page_sequence='all',
                align_x='center',
                align_y='middle'
            )
        )

        return stamp

    def test_stamp_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.stamp_client.stamp(None)

        assert_equal(e.exception.msg, 'The stamp parameter cannot be None.')

    def test_stamp_document_throws_exception(self):
        # prepare args
        stamp = self.create_stamp()
        stamp.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.stamp_client.stamp(stamp=stamp)

        assert_equal(e.exception.msg, 'The stamp document cannot be None.')

    def test_stamp_document_data_throws_exception(self):
        # prepare args
        stamp = self.create_stamp()
        stamp.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.stamp_client.stamp(stamp=stamp)

        assert_equal(e.exception.msg, 'The stamp document cannot be None.')

    def test_stamp_action_throws_exception(self):
        # prepare args
        stamp = self.create_stamp()
        stamp.stamp_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.stamp_client.stamp(stamp=stamp)

        assert_equal(e.exception.msg, 'The stamp_action cannot be None.')

    def test_stamp_action_alpha_throws_exception(self):
        # prepare args
        stamp = self.create_stamp()
        stamp.stamp_action.alpha = None

        with assert_raises(Pdf4meClientException) as e:
            self.stamp_client.stamp(stamp=stamp)

        assert_equal(e.exception.msg, 'The alpha parameter of stamp_action cannot be None.')

    def test_stamp_action_text_and_image_throws_exception(self):
        # prepare args
        stamp = self.create_stamp()
        stamp.stamp_action.text = None
        stamp.stamp_action.image = None

        with assert_raises(Pdf4meClientException) as e:
            self.stamp_client.stamp(stamp=stamp)

        assert_equal(e.exception.msg, 'The image and text parameter of stampAction cannot both be None.')

    def test_stamp_no_none_response(self):
        # request
        stamp = self.create_stamp()
        res = self.stamp_client.stamp(stamp)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_stamp_doc_length(self):
        # request
        stamp = self.create_stamp()
        res = self.stamp_client.stamp(stamp)

        # validation
        stamped_pdf_length = len(res['document']['doc_data'])

        self.check.not_zero(stamped_pdf_length)

    def test_text_stamp_no_none_respoonse(self):
        # request
        res = self.stamp_client.text_stamp(
            text='EXAMPLE TEXT',
            pages='all',
            align_x='center',
            align_y='middle',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_text_stamp_doc_length(self):
        # request
        res = self.stamp_client.text_stamp(
            text='EXAMPLE TEXT',
            pages='all',
            align_x='center',
            align_y='middle',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        stamped_pdf_length = self.check.get_doc_base64_length(res)

        self.check.not_zero(stamped_pdf_length)
class PdfAClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    pdfA_client = PdfAClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_create_pdfA(self):
        create_pdfA = CreatePdfA(
            document=Document(
                doc_data=self.test_files.pdf_data
            ),
            pdf_a_action=PdfAAction(
                compliance='pdfA2u'
            )
        )

        return create_pdfA

    def test_pdfA_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.pdfA(None)

        assert_equal(e.exception.msg, 'The create_pdfA parameter cannot be None.')

    def test_pdfA_document_throws_exception(self):
        # prepare args
        create_pdfA = self.create_create_pdfA()
        create_pdfA.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.pdfA(create_pdfA=create_pdfA)

        assert_equal(e.exception.msg, 'The create_pdfA document cannot be None.')

    def test_pdfA_document_data_throws_exception(self):
        # prepare args
        create_pdfA = self.create_create_pdfA()
        create_pdfA.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.pdfA(create_pdfA=create_pdfA)

        assert_equal(e.exception.msg, 'The create_pdfA document cannot be None.')

    def test_pdfA_action_data_throws_exception(self):
        # prepare args
        create_pdfA = self.create_create_pdfA()
        create_pdfA.pdf_a_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.pdfA(create_pdfA=create_pdfA)

        assert_equal(e.exception.msg, 'The pdf_a_action cannot be None.')

    def test_pdfA_no_none_response(self):
        # request
        create_pdfA = self.create_create_pdfA()
        res = self.pdfA_client.pdfA(create_pdfA)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_pdfA_doc_length(self):
        # request
        create_pdfA = self.create_create_pdfA()
        res = self.pdfA_client.pdfA(create_pdfA)

        # validation
        original_length = self.test_files.pdf_length
        pdfA = len(res['document']['doc_data'])

        self.assertTrue(self.check.below_not_zero(pdfA, original_length))

    def test_create_pdfA_no_none_response(self):
        # request
        res = self.pdfA_client.create_pdfA(
            pdf_compliance='pdfA2u',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_create_pdfA_doc_length(self):
        # request
        res = self.pdfA_client.create_pdfA(
            pdf_compliance='pdfA2u',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        original_length = self.test_files.pdf_length
        pdfA = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(pdfA, original_length))

    def test_create_pdfA_without_compliance_no_none_response(self):
        # request
        res = self.pdfA_client.create_pdfA(
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_create_pdfA_without_compliance_doc_length(self):
        # request
        res = self.pdfA_client.create_pdfA(
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        original_length = self.test_files.pdf_length
        pdfA = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(pdfA, original_length))

    # Rotate
    def create_rotate(self):
        rotate = Rotate(
            document=Document(
                doc_data=self.test_files.pdf_data
            ),
            rotate_action=RotateAction(
                rotation_list=[
                    PdfRotate(
                        page_nr=2,
                        rotation_type='clockwise'
                    )
                ]
            )
        )

        return rotate

    def test_rotate_throws_exception(self):
        create_rotate: Rotate = None
        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.rotate(rotate=create_rotate)

        assert_equal(e.exception.msg, 'The rotate parameter cannot be None.')

    def test_rotate_document_throws_exception(self):
        # prepare args
        create_rotate = self.create_rotate()
        create_rotate.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.rotate(rotate=create_rotate)

        assert_equal(e.exception.msg, 'The rotate document cannot be None.')

    def test_rotate_document_data_throws_exception(self):
        # prepare args
        create_rotate = self.create_rotate()
        create_rotate.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.rotate(rotate=create_rotate)

        assert_equal(e.exception.msg, 'The rotate document cannot be None.')

    def test_rotate_action_data_throws_exception(self):
        # prepare args
        create_rotate = self.create_rotate()
        create_rotate.rotate_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.rotate(rotate=create_rotate)

        assert_equal(e.exception.msg, 'The rotate_action cannot be None.')

    def test_rotate_no_none_response(self):
        # request
        create_rotate = self.create_rotate()
        res = self.pdfA_client.rotate(create_rotate)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_rotate_doc_length(self):
        # request
        create_rotate = self.create_rotate()
        res = self.pdfA_client.rotate(create_rotate)

        # validation
        original_length = self.test_files.pdf_length
        rotate = len(res['document']['doc_data'])

        self.assertTrue(self.check.below_not_zero(rotate, original_length))

    def test_rotate_page_no_none_response(self):
        # request
        res = self.pdfA_client.rotate_page(
            page_nr='2',
            rotate='clockwise',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_rotate_page_doc_length(self):
        # request
        res = self.pdfA_client.rotate_page(
            page_nr='2',
            rotate='clockwise',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        original_length = self.test_files.pdf_length
        rotate = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(rotate, original_length))

    # def test_rotate_page_without_compliance_no_none_response(self):
    #     # request
    #     res = self.pdfA_client.rotate_page(
    #         file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
    #     )
    #
    #     # validation
    #     self.assertIsNotNone(res)
    #
    # def test_rotate_page_without_compliance_doc_length(self):
    #     # request
    #     res = self.pdfA_client.rotate_page(
    #         file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
    #     )
    #
    #     # validation
    #     original_length = self.test_files.pdf_length
    #     pdfA = self.check.get_doc_base64_length(res)
    #
    #     self.assertTrue(self.check.below_not_zero(pdfA, original_length))

    def test_rotate_document_no_none_response(self):
        # request
        res = self.pdfA_client.rotate_document(
            rotate='clockwise',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_rotate_document_doc_length(self):
        # request
        res = self.pdfA_client.rotate_document(
            rotate='clockwise',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        original_length = self.test_files.pdf_length
        rotate = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(rotate, original_length))

    # Protect
    def create_protect(self):
        protect = Protect(
            document=Document(
                doc_data=self.test_files.pdf_data
            ),
            protect_action=ProtectAction(
                user_password='******',
                owner_password='******',
                # unlock=None,
                # permissions=None
            )
        )

        return protect

    def test_protect_throws_exception(self):
        create_protect: Protect = None
        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.protect(protect=create_protect)

        assert_equal(e.exception.msg, 'The protect parameter cannot be None.')

    def test_protect_document_throws_exception(self):
        # prepare args
        create_protect = self.create_protect()
        create_protect.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.protect(protect=create_protect)

        assert_equal(e.exception.msg, 'The protect document cannot be None.')

    def test_protect_document_data_throws_exception(self):
        # prepare args
        create_protect = self.create_protect()
        create_protect.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.protect(protect=create_protect)

        assert_equal(e.exception.msg, 'The protect document cannot be None.')

    def test_protect_action_data_throws_exception(self):
        # prepare args
        create_protect = self.create_protect()
        create_protect.protect_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.protect(protect=create_protect)

        assert_equal(e.exception.msg, 'The protect_action cannot be None.')

    def test_protect_no_none_response(self):
        # request
        create_protect = self.create_protect()
        res = self.pdfA_client.protect(create_protect)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_protect_doc_length(self):
        # request
        create_protect = self.create_protect()
        res = self.pdfA_client.protect(create_protect)

        # validation
        original_length = self.test_files.pdf_length
        protect = len(res['document']['doc_data'])

        self.assertTrue(self.check.below_not_zero(protect, original_length))

    def test_protect_document_no_none_response(self):
        # request
        res = self.pdfA_client.protect_document(
            password='******',
            permissions='all',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_protect_document_doc_length(self):
        # request
        res = self.pdfA_client.protect_document(
            password='******',
            permissions='all',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        original_length = self.test_files.pdf_length
        protect = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(protect, original_length))

    def test_unlock_document_no_none_response(self):
        # request
        res = self.pdfA_client.unlock_document(
            password='******',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_unlock_document_doc_length(self):
        # request
        res = self.pdfA_client.unlock_document(
            password='******',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        original_length = self.test_files.pdf_length
        unlock = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(unlock, original_length))

    # Validate
    def create_validate(self):
        validate = Validate(
            document=Document(
                doc_data=self.test_files.pdf_data
            ),
            validate_action=ValidateAction(
                pdf_conformance='pdfA2u'
            )
        )

        return validate

    def test_validate_throws_exception(self):
        create_validate: Validate = None
        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.validate(validate=create_validate)

        assert_equal(e.exception.msg, 'The validate parameter cannot be None.')

    def test_validate_document_throws_exception(self):
        # prepare args
        create_validate = self.create_validate()
        create_validate.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.validate(validate=create_validate)

        assert_equal(e.exception.msg, 'The validate document cannot be None.')

    def test_validate_document_data_throws_exception(self):
        # prepare args
        create_validate = self.create_validate()
        create_validate.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.validate(validate=create_validate)

        assert_equal(e.exception.msg, 'The validate document cannot be None.')

    def test_validate_action_data_throws_exception(self):
        # prepare args
        create_validate = self.create_validate()
        create_validate.validate_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.validate(validate=create_validate)

        assert_equal(e.exception.msg, 'The validate_action cannot be None.')

    def test_validate_no_none_response(self):
        # request
        create_validate = self.create_validate()
        res = self.pdfA_client.validate(create_validate)

        # validation
        self.assertIsNotNone(res)

    def test_validate_pdfValidation_length(self):
        # request
        create_validate = self.create_validate()
        res = self.pdfA_client.validate(create_validate)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['pdf_validation'])
        validate = len(res['pdf_validation']['validations'])
        self.assertTrue(self.check.not_zero(validate))

    def test_validate_document_no_none_response(self):
        # request
        res = self.pdfA_client.validate_document(
            pdf_compliance='pdfA2u',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_validate_document_pdfValidation_length(self):
        # request
        res = self.pdfA_client.validate_document(
            pdf_compliance='pdfA2u',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['pdf_validation'])
        validate = len(res['pdf_validation']['validations'])
        self.assertTrue(self.check.not_zero(validate))

    # Repair
    def create_repair(self):
        repair = Repair(
            document=Document(
                doc_data=self.test_files.pdf_data
            ),
            repair_action=RepairAction(

            )
        )

        return repair

    def test_repair_throws_exception(self):
        create_repair: Repair = None
        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.repair(repair=create_repair)

        assert_equal(e.exception.msg, 'The repair parameter cannot be None.')

    def test_repair_document_throws_exception(self):
        # prepare args
        create_repair = self.create_repair()
        create_repair.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.repair(repair=create_repair)

        assert_equal(e.exception.msg, 'The repair document cannot be None.')

    def test_repair_document_data_throws_exception(self):
        # prepare args
        create_repair = self.create_repair()
        create_repair.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.repair(repair=create_repair)

        assert_equal(e.exception.msg, 'The repair document cannot be None.')

    def test_repair_no_none_response(self):
        # request
        create_repair = self.create_repair()
        res = self.pdfA_client.repair(create_repair)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_repair_doc_length(self):
        # request
        create_repair = self.create_repair()
        res = self.pdfA_client.repair(create_repair)

        # validation
        original_length = self.test_files.pdf_length
        repair = len(res['document']['doc_data'])

        self.assertTrue(self.check.below_not_zero(repair, original_length))

    def test_repair_document_no_none_response(self):
        # request
        res = self.pdfA_client.repair_document(
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_repair_document_doc_length(self):
        # request
        res = self.pdfA_client.repair_document(
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        original_length = self.test_files.pdf_length
        repair = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(repair, original_length))

    # SignPdf
    def create_sign_pdf(self):
        sign_pdf = SignPdf(
            document=Document(
                doc_data=self.test_files.pdf_data
            ),
            sign_action=SignAction(

            )
        )

        return sign_pdf

    def test_sign_pdf_throws_exception(self):
        create_sign_pdf: SignPdf = None
        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.sign_pdf(sign_pdf=create_sign_pdf)

        assert_equal(e.exception.msg, 'The sign_pdf parameter cannot be None.')

    def test_sign_pdf_document_throws_exception(self):
        # prepare args
        create_sign_pdf = self.create_sign_pdf()
        create_sign_pdf.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.sign_pdf(sign_pdf=create_sign_pdf)

        assert_equal(e.exception.msg, 'The sign_pdf document cannot be None.')

    def test_sign_pdf_document_data_throws_exception(self):
        # prepare args
        create_sign_pdf = self.create_sign_pdf()
        create_sign_pdf.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.sign_pdf(sign_pdf=create_sign_pdf)

        assert_equal(e.exception.msg, 'The sign_pdf document cannot be None.')

    # def test_sign_pdf_no_none_response(self):
    #     # request
    #     create_sign_pdf = self.create_sign_pdf()
    #     res = self.pdfA_client.sign_pdf(create_sign_pdf)
    #
    #     # validation
    #     self.assertIsNotNone(res)
    #     self.assertIsNotNone(res['document'])
    #     self.assertIsNotNone(res['document']['doc_data'])
    #
    # def test_sign_pdf_doc_length(self):
    #     # request
    #     create_sign_pdf = self.create_sign_pdf()
    #     res = self.pdfA_client.sign_pdf(create_sign_pdf)
    #
    #     # validation
    #     original_length = self.test_files.pdf_length
    #     sign_pdf = len(res['document']['doc_data'])
    #
    #     self.assertTrue(self.check.below_not_zero(sign_pdf, original_length))

    # Metadata

    def test_metadata_no_none_response(self):
        # request
        res = self.pdfA_client.metadata(
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)
class MergeClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    merge_client = MergeClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_merge(self):
        merge = Merge(documents=[
            Document(doc_data=self.test_files.pdf_data),
            Document(doc_data=self.test_files.pdf_data_2)
        ],
                      merge_action=MergeAction())

        return merge

    def test_merge_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.merge_client.merge(None)

        assert_equal(e.exception.msg, 'The merge parameter cannot be None.')

    def test_merge_documents_throws_exception(self):
        # prepare args
        merge = self.create_merge()
        merge.documents = None

        with assert_raises(Pdf4meClientException) as e:
            self.merge_client.merge(merge=merge)

        assert_equal(e.exception.msg, 'The merge documents cannot be None.')

    def test_merge_documents_less_than_2_throws_exception(self):
        # prepare args
        merge = self.create_merge()
        merge.documents = [Document(doc_data=self.test_files.pdf_data)]

        with assert_raises(Pdf4meClientException) as e:
            self.merge_client.merge(merge=merge)

        assert_equal(
            e.exception.msg,
            'The merge documents must contain at least two documents.')

    def test_merge_document1_throws_exception(self):
        # prepare args
        merge = self.create_merge()
        merge.documents[0] = None

        with assert_raises(Pdf4meClientException) as e:
            self.merge_client.merge(merge=merge)

        assert_equal(
            e.exception.msg,
            'The merge documents cannot be None nor can the document.docData.')

    def test_merge_document2_throws_exception(self):
        # prepare args
        merge = self.create_merge()
        merge.documents[1] = None

        with assert_raises(Pdf4meClientException) as e:
            self.merge_client.merge(merge=merge)

        assert_equal(
            e.exception.msg,
            'The merge documents cannot be None nor can the document.docData.')

    def test_merge_document1_data_throws_exception(self):
        # prepare args
        merge = self.create_merge()
        merge.documents[0].doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.merge_client.merge(merge=merge)

        assert_equal(
            e.exception.msg,
            'The merge documents cannot be None nor can the document.docData.')

    def test_merge_document2_data_throws_exception(self):
        # prepare args
        merge = self.create_merge()
        merge.documents[1].doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.merge_client.merge(merge=merge)

        assert_equal(
            e.exception.msg,
            'The merge documents cannot be None nor can the document.docData.')

    def test_merge_action_throws_exception(self):
        # prepare args
        merge = self.create_merge()
        merge.merge_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.merge_client.merge(merge=merge)

        assert_equal(e.exception.msg, 'The merge_action cannot be None.')

    def test_merge_no_none_response(self):
        # request
        merge = self.create_merge()
        res = self.merge_client.merge(merge=merge)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_merge_doc_length(self):
        # request
        merge = self.create_merge()
        res = self.merge_client.merge(merge=merge)

        # validation
        mergeLength = len(res['document']['doc_data'])
        originals = self.test_files.pdf_length + self.test_files.pdf_length_2
        shorterDoc = min(self.test_files.pdf_length,
                         self.test_files.pdf_length_2)

        self.assertTrue(self.check.below_not_zero(mergeLength, originals))
        self.assertTrue(self.check.above(mergeLength, shorterDoc))

    def test_merge_2_pdfs_no_none_response(self):
        # request
        res = self.merge_client.merge_2_pdfs(
            file1=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path),
            file2=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path_2))

        # validation
        self.assertIsNotNone(res)

    def test_merge_2_pdfs_doc_length(self):
        # request
        res = self.merge_client.merge_2_pdfs(
            file1=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path),
            file2=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path_2))

        # validation
        mergeLength = self.check.get_doc_base64_length(res)
        originals = self.test_files.pdf_length + self.test_files.pdf_length_2
        shorterDoc = min(self.test_files.pdf_length,
                         self.test_files.pdf_length_2)

        self.assertTrue(self.check.below_not_zero(mergeLength, originals))
        self.assertTrue(self.check.above(mergeLength, shorterDoc))
Пример #8
0
class OptimizeClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    optimize_client = OptimizeClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_optimize(self):
        optimize = Optimize(
            document=Document(
                doc_data=self.test_files.pdf_data
            ),
            optimize_action=OptimizeAction(
                use_profile=True,
                profile='default'
            )
        )

        return optimize

    def test_optimize_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.optimize_client.optimize(None)

        assert_equal(e.exception.msg, 'The optimize parameter cannot be None.')

    def test_optimize_document_throws_exception(self):
        # prepare args
        optimize = self.create_optimize()
        optimize.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.optimize_client.optimize(optimize=optimize)

        assert_equal(e.exception.msg, 'The optimize document cannot be None.')

    def test_optimize_document_data_throws_exception(self):
        # prepare args
        optimize = self.create_optimize()
        optimize.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.optimize_client.optimize(optimize=optimize)

        assert_equal(e.exception.msg, 'The optimize document cannot be None.')

    def test_optimize_action_throws_exception(self):
        # prepare args
        optimize = self.create_optimize()
        optimize.optimize_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.optimize_client.optimize(optimize=optimize)

        assert_equal(e.exception.msg, 'The optimize_action cannot be None.')

    def test_optimize_action_use_profile_throws_exception(self):
        # prepare args
        optimize = self.create_optimize()
        optimize.optimize_action.use_profile = False

        with assert_raises(Pdf4meClientException) as e:
            self.optimize_client.optimize(optimize=optimize)

        assert_equal(e.exception.msg, 'The use_profile parameter of optimize_action has to be set to true.')

    def test_optimize_no_none_response(self):
        # request
        optimize = self.create_optimize()
        res = self.optimize_client.optimize(optimize=optimize)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_optimize_doc_length(self):
        # request
        optimize = self.create_optimize()
        res = self.optimize_client.optimize(optimize=optimize)

        # validation
        original_length = self.test_files.pdf_length
        optimized_pdf = len(res['document']['doc_data'])

        self.assertTrue(self.check.below_not_zero(optimized_pdf, original_length))

    def test_optimize_by_profile_no_none_response(self):
        # request
        res = self.optimize_client.optimize_by_profile(
            profile='default',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        self.assertIsNotNone(res)

    def test_optimize_by_profile_doc_length(self):
        # request
        res = self.optimize_client.optimize_by_profile(
            profile='default',
            file=self.file_reader.get_file_handler(path=self.test_files.pdf_path)
        )

        # validation
        original_length = self.test_files.pdf_length
        optimized_pdf = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(optimized_pdf, original_length))
class ExtractClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    extract_client = ExtractClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_extract(self):
        extract = Extract(document=Document(doc_data=self.test_files.pdf_data),
                          extract_action=ExtractAction(extract_pages=[1, 4]))

        return extract

    def test_extract_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.extract_client.extract(None)

        assert_equal(e.exception.msg, 'The extract parameter cannot be None.')

    def test_extract_document_throws_exception(self):
        # prepare args
        extract = self.create_extract()
        extract.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.extract_client.extract(extract=extract)

        assert_equal(e.exception.msg, 'The extract document cannot be None.')

    def test_extract_document_data_throws_exception(self):
        # prepare args
        extract = self.create_extract()
        extract.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.extract_client.extract(extract=extract)

        assert_equal(e.exception.msg, 'The extract document cannot be None.')

    def test_extract_action_throws_exception(self):
        # prepare args
        extract = self.create_extract()
        extract.extract_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.extract_client.extract(extract=extract)

        assert_equal(e.exception.msg, 'The extract_action cannot be None.')

    def test_extract_action_extract_pages_throws_exception(self):
        # prepare args
        extract = self.create_extract()
        extract.extract_action.extract_pages = None

        with assert_raises(Pdf4meClientException) as e:
            self.extract_client.extract(extract=extract)

        assert_equal(
            e.exception.msg,
            'The extract_pages of extract_action cannot be None or empty.')

    def test_extract_action_extract_pages_throws_exception2(self):
        # prepare args
        extract = self.create_extract()
        extract.extract_action.extract_pages = []

        with assert_raises(Pdf4meClientException) as e:
            self.extract_client.extract(extract=extract)

        assert_equal(
            e.exception.msg,
            'The extract_pages of extract_action cannot be None or empty.')

    def test_extract_no_none_response(self):
        # request
        extract = self.create_extract()
        res = self.extract_client.extract(extract=extract)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_extract_doc_length(self):
        # request
        extract = self.create_extract()
        res = self.extract_client.extract(extract=extract)

        # validation
        original_length = self.test_files.pdf_length
        shortened_pdf = len(res['document']['doc_data'])

        self.assertTrue(
            self.check.below_not_zero(shortened_pdf, original_length))

    def test_extract_pages_no_none_response(self):
        # request
        res = self.extract_client.extract_pages(
            page_nrs='1,4',
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        self.assertIsNotNone(res)

    def test_extract_pages_doc_length(self):
        # request
        res = self.extract_client.extract_pages(
            page_nrs='1,4',
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        original_length = self.test_files.pdf_length
        shortened_pdf = self.check.get_doc_base64_length(res)

        self.assertTrue(
            self.check.below_not_zero(shortened_pdf, original_length))
Пример #10
0
class ConvertClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    convert_client = ConvertClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_convert_to_pdf(self, file, file_name):
        convert_to_pdf = ConvertToPdf(
            document=Document(doc_data=file, name=file_name),
            convert_to_pdf_action=ConvertToPdfAction())

        return convert_to_pdf

    def test_convert_to_pdf_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.convert_client.convert_to_pdf(None)

        assert_equal(e.exception.msg,
                     'The convert_to_pdf parameter cannot be None.')

    def test_convert_to_pdf_document_throws_exception(self):
        # prepare args
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.text_data,
                                                    self.test_files.text_name)
        convert_to_pdf._document = None

        with assert_raises(Pdf4meClientException) as e:
            self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        assert_equal(e.exception.msg,
                     'The convert_to_pdf document cannot be None.')

    def test_convert_to_pdf_document_data_throws_exception(self):
        # prepare args
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.text_data,
                                                    self.test_files.text_name)
        convert_to_pdf.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        assert_equal(e.exception.msg,
                     'The convert_to_pdf document cannot be None.')

    def test_convert_to_pdf_document_name_throws_exception(self):
        # prepare args
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.text_data,
                                                    self.test_files.text_name)
        convert_to_pdf.document.name = None

        with assert_raises(Pdf4meClientException) as e:
            self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        assert_equal(
            e.exception.msg,
            'The name field of convertToPdf\'s document cannot be None (name must incl. file extension).'
        )

    def test_convert_to_pdf_action_throws_exception(self):
        # prepare args
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.text_data,
                                                    self.test_files.text_name)
        convert_to_pdf.convert_to_pdf_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        assert_equal(e.exception.msg,
                     'The convert_to_pdf_action cannot be None.')

    """text file"""

    def test_convert_to_pdf_text_no_none_response(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.text_data,
                                                    self.test_files.text_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_convert_to_pdf_text_doc_length(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.text_data,
                                                    self.test_files.text_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        original_length = self.test_files.text_length
        pdf_length = len(res['document']['doc_data'])

        self.assertTrue(self.check.above(pdf_length, original_length))

    """word doc"""

    def test_convert_to_pdf_word_no_none_response(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.word_data,
                                                    self.test_files.word_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_convert_to_pdf_word_doc_length(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.word_data,
                                                    self.test_files.word_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        original_length = self.test_files.word_length
        pdf_length = len(res['document']['doc_data'])

        self.assertTrue(self.check.above(pdf_length, original_length))

    """excel doc"""

    def test_convert_to_pdf_excel_no_none_response(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.excel_data,
                                                    self.test_files.excel_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_convert_to_pdf_excel_doc_length(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.excel_data,
                                                    self.test_files.excel_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        original_length = self.test_files.excel_length
        pdf_length = len(res['document']['doc_data'])

        self.assertTrue(self.check.above(pdf_length, original_length))

    """eml"""

    def test_convert_to_pdf_eml_no_none_response(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.eml_data,
                                                    self.test_files.eml_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_convert_to_pdf_eml_doc_length(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.eml_data,
                                                    self.test_files.eml_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        original_length = self.test_files.eml_length
        pdf_length = len(res['document']['doc_data'])

        self.assertTrue(self.check.above(pdf_length, original_length))

    """msg"""

    def test_convert_to_pdf_msg_no_none_response(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.msg_data,
                                                    self.test_files.msg_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_convert_to_pdf_msg_doc_length(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.msg_data,
                                                    self.test_files.msg_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        original_length = self.test_files.msg_length
        pdf_length = len(res['document']['doc_data'])

        self.assertTrue(self.check.above(pdf_length, original_length))

    """jpg"""

    def test_convert_to_pdf_jpg_no_none_response(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.jpg_data,
                                                    self.test_files.jpg_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_convert_to_pdf_jpg_doc_length(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.jpg_data,
                                                    self.test_files.jpg_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        original_length = self.test_files.jpg_length
        pdf_length = len(res['document']['doc_data'])

        self.assertTrue(self.check.above(pdf_length, original_length))

    """zip"""

    def test_convert_to_pdf_zip_no_none_response(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.zip_data,
                                                    self.test_files.zip_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_convert_to_pdf_zip_doc_length(self):
        # request
        convert_to_pdf = self.create_convert_to_pdf(self.test_files.zip_data,
                                                    self.test_files.zip_name)
        res = self.convert_client.convert_to_pdf(convert_to_pdf=convert_to_pdf)

        # validation
        original_length = self.test_files.zip_length
        pdf_length = len(res['document']['doc_data'])

        self.assertTrue(self.check.below_not_zero(pdf_length, original_length))

    """convert_file_to_pdf"""
    """text file"""

    def test_convert_file_to_pdf_text_no_none_response(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.text_name,
            file=self.file_reader.get_file_handler(self.test_files.text_path))

        # validation
        self.assertIsNotNone(res)

    def test_convert_file_to_pdf_text_doc_length(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.text_name,
            file=self.file_reader.get_file_handler(self.test_files.text_path))

        # validation
        original_length = self.test_files.text_length
        pdf_length = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.above(pdf_length, original_length))

    """word doc"""

    def test_convert_file_to_pdf_word_no_none_response(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.word_name,
            file=self.file_reader.get_file_handler(self.test_files.word_path))

        # validation
        self.assertIsNotNone(res)

    def test_convert_file_to_pdf_word_doc_length(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.word_name,
            file=self.file_reader.get_file_handler(self.test_files.word_path))

        # validation
        original_length = self.test_files.word_length
        pdf_length = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.above(pdf_length, original_length))

    """excel doc"""

    def test_convert_file_to_pdf_excel_no_none_response(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.excel_name,
            file=self.file_reader.get_file_handler(self.test_files.excel_path))

        # validation
        self.assertIsNotNone(res)

    def test_convert_file_to_pdf_excel_doc_length(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.excel_name,
            file=self.file_reader.get_file_handler(self.test_files.excel_path))

        # validation
        original_length = self.test_files.excel_length
        pdf_length = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.above(pdf_length, original_length))

    """eml"""

    def test_convert_file_to_pdf_eml_no_none_response(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.eml_name,
            file=self.file_reader.get_file_handler(self.test_files.eml_path))

        # validation
        self.assertIsNotNone(res)

    def test_convert_file_to_pdf_eml_doc_length(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.eml_name,
            file=self.file_reader.get_file_handler(self.test_files.eml_path))

        # validation
        original_length = self.test_files.eml_length
        pdf_length = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.above(pdf_length, original_length))

    """msg"""

    def test_convert_file_to_pdf_msg_no_none_response(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.msg_name,
            file=self.file_reader.get_file_handler(self.test_files.msg_path))

        # validation
        self.assertIsNotNone(res)

    def test_convert_file_to_pdf_msg_doc_length(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.msg_name,
            file=self.file_reader.get_file_handler(self.test_files.msg_path))

        # validation
        original_length = self.test_files.msg_length
        pdf_length = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.above(pdf_length, original_length))

    """jpg"""

    def test_convert_file_to_pdf_jpg_no_none_response(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.jpg_name,
            file=self.file_reader.get_file_handler(self.test_files.jpg_path))

        # validation
        self.assertIsNotNone(res)

    def test_convert_file_to_pdf_jpg_doc_length(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.jpg_name,
            file=self.file_reader.get_file_handler(self.test_files.jpg_path))

        # validation
        original_length = self.test_files.jpg_length
        pdf_length = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.above(pdf_length, original_length))

    """zip"""

    def test_convert_file_to_pdf_zip_no_none_response(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.zip_name,
            file=self.file_reader.get_file_handler(self.test_files.zip_path))

        # validation
        self.assertIsNotNone(res)

    def test_convert_file_to_pdf_zip_doc_length(self):
        # request
        res = self.convert_client.convert_file_to_pdf(
            file_name=self.test_files.zip_name,
            file=self.file_reader.get_file_handler(self.test_files.zip_path))

        # validation
        original_length = self.test_files.zip_length
        pdf_length = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(pdf_length, original_length))
Пример #11
0
class PdfAClientTest(unittest.TestCase):
    pdf4me_client = Pdf4meClient()
    pdfA_client = PdfAClient(pdf4me_client)

    test_files = TestFiles()
    check = Check()
    file_reader = FileReader()

    def create_craete_pdfA(self):
        create_pdfA = CreatePdfA(
            document=Document(doc_data=self.test_files.pdf_data),
            pdf_a_action=PdfAAction(compliance='pdfA2u'))

        return create_pdfA

    def test_pdfA_throws_exception(self):
        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.pdfA(None)

        assert_equal(e.exception.msg,
                     'The create_pdfA parameter cannot be None.')

    def test_pdfA_document_throws_exception(self):
        # prepare args
        create_pdfA = self.create_craete_pdfA()
        create_pdfA.document = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.pdfA(create_pdfA=create_pdfA)

        assert_equal(e.exception.msg,
                     'The create_pdfA document cannot be None.')

    def test_pdfA_document_data_throws_exception(self):
        # prepare args
        create_pdfA = self.create_craete_pdfA()
        create_pdfA.document.doc_data = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.pdfA(create_pdfA=create_pdfA)

        assert_equal(e.exception.msg,
                     'The create_pdfA document cannot be None.')

    def test_pdfA_action_data_throws_exception(self):
        # prepare args
        create_pdfA = self.create_craete_pdfA()
        create_pdfA.pdf_a_action = None

        with assert_raises(Pdf4meClientException) as e:
            self.pdfA_client.pdfA(create_pdfA=create_pdfA)

        assert_equal(e.exception.msg, 'The pdf_a_action cannot be None.')

    def test_pdfA_no_none_response(self):
        # request
        create_pdfA = self.create_craete_pdfA()
        res = self.pdfA_client.pdfA(create_pdfA)

        # validation
        self.assertIsNotNone(res)
        self.assertIsNotNone(res['document'])
        self.assertIsNotNone(res['document']['doc_data'])

    def test_pdfA_doc_length(self):
        # request
        create_pdfA = self.create_craete_pdfA()
        res = self.pdfA_client.pdfA(create_pdfA)

        # validation
        original_length = self.test_files.pdf_length
        pdfA = len(res['document']['doc_data'])

        self.assertTrue(self.check.below_not_zero(pdfA, original_length))

    def test_create_pdfA_no_none_response(self):
        # request
        res = self.pdfA_client.create_pdfA(
            pdf_compliance='pdfA2u',
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        self.assertIsNotNone(res)

    def test_create_pdfA_doc_length(self):
        # request
        res = self.pdfA_client.create_pdfA(
            pdf_compliance='pdfA2u',
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        original_length = self.test_files.pdf_length
        pdfA = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(pdfA, original_length))

    def test_create_pdfA_without_compliance_no_none_response(self):
        # request
        res = self.pdfA_client.create_pdfA(
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        self.assertIsNotNone(res)

    def test_create_pdfA_without_compliance_doc_length(self):
        # request
        res = self.pdfA_client.create_pdfA(
            file=self.file_reader.get_file_handler(
                path=self.test_files.pdf_path))

        # validation
        original_length = self.test_files.pdf_length
        pdfA = self.check.get_doc_base64_length(res)

        self.assertTrue(self.check.below_not_zero(pdfA, original_length))