def test_add_notify_tag_to_letter_correct_margins(mocker): pdf_original = PyPDF2.PdfFileReader(BytesIO(multi_page_pdf)) can = NotifyCanvas(white) can.drawString = MagicMock(return_value=3) mocker.patch('app.precompiled.NotifyCanvas', return_value=can) # It fails because we are mocking but by that time the drawString method has been called so just carry on try: add_notify_tag_to_letter(BytesIO(multi_page_pdf)) except Exception: pass mm_from_top_of_the_page = 4.3 mm_from_left_of_page = 3.44 x = mm_from_left_of_page * mm # page.mediaBox[3] Media box is an array with the four corners of the page # We want height so can use that co-ordinate which is located in [3] # The lets take away the margin and the ont size y = float(pdf_original.getPage(0).mediaBox[3]) - ( float(mm_from_top_of_the_page * mm + 6 - 1.75)) assert len(can.drawString.call_args_list) == 1 positional_args = can.drawString.call_args[0] assert len(positional_args) == 3 assert positional_args[0] == pytest.approx(x, 0.01) assert positional_args[1] == y assert positional_args[2] == "NOTIFY"
def test_add_notify_tag_to_letter(mocker): pdf_original = PyPDF2.PdfFileReader(BytesIO(multi_page_pdf)) assert 'NOTIFY' not in pdf_original.getPage(0).extractText() pdf_page = add_notify_tag_to_letter(BytesIO(multi_page_pdf)) pdf_new = PyPDF2.PdfFileReader(BytesIO(pdf_page.read())) assert pdf_new.numPages == pdf_original.numPages assert pdf_new.getPage(0).extractText() != pdf_original.getPage( 0).extractText() assert 'NOTIFY' in pdf_new.getPage(0).extractText() assert pdf_new.getPage(1).extractText() == pdf_original.getPage( 1).extractText() assert pdf_new.getPage(2).extractText() == pdf_original.getPage( 2).extractText() assert pdf_new.getPage(3).extractText() == pdf_original.getPage( 3).extractText()