def test_should_return_cursor_position_after_drawing_line_of_text_with_char_space( self): canvas = Canvas() canvas.font.char_space_pts = 10 self.assertAlmostEqual(66.36, canvas.text('lorem ipsum', 10, 10), places=2)
def test_should_draw_text(self, mock_obj): canvas = Canvas() canvas.font.family = FontFamily.SANS canvas.font.weight = FontWeight.BOLD canvas.font.style = FontStyle.ITALIC canvas.font.size_pts = 72 canvas.font.render_mode = FontRenderMode.STROKE canvas.font.char_space_pts = 10.5 canvas.font.word_space_pts = 5.75 canvas.font.rise_pts = -5.0 canvas.text('lorem ipsum\ndolor sit\namet', 10, 10) mock_obj.assert_called_once_with(28.34645669291339, 755.943307086614) mock_obj.return_value.setFont.assert_called_once_with( 'FreeSansBoldItalic', 72.0, 86.39999999999999) mock_obj.return_value.setTextRenderMode.assert_called_once_with(1) mock_obj.return_value.setCharSpace.assert_called_once_with(10.5) mock_obj.return_value.setWordSpace.assert_called_once_with(5.75) mock_obj.return_value.setRise.assert_called_once_with(-5.0) mock_obj.return_value.textLine.assert_has_calls([ mock.call('lorem ipsum'), mock.call('dolor sit'), mock.call('amet') ])
def test_should_restore_state_on_add_page(self, mock_stroke_apply, mock_fill_apply): canvas = Canvas() canvas.add_page() canvas.add_page() self.assertTrue(mock_stroke_apply.called) self.assertTrue(mock_fill_apply.called)
def test_should_return_cursor_position_of_a_line_of_text(self): text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod' canvas = Canvas() canvas.font.size_pts = 10 canvas.font.char_space_pts = 10 canvas.font.word_space_pts = 5 self.assertAlmostEqual(379.66, canvas.text(text, 10, 10), places=2)
def test_should_store_page_size_in_points_internally(self): canvas = Canvas() self.assertAlmostEqual(595.275590551, canvas._width) self.assertAlmostEqual(841.88976378, canvas._height) canvas = Canvas(size=letter) self.assertAlmostEqual(612.0, canvas._width) self.assertAlmostEqual(792.0, canvas._height)
def test_should_apply_default_state(self, mock_stroke_apply, mock_fill_apply): canvas = Canvas() canvas.set_default_state() self.assertTrue(mock_stroke_apply.called) self.assertTrue(mock_fill_apply.called)
def test_should_return_cursor_position_of_longest_line_of_text_with_char_and_word_space( self): canvas = Canvas() canvas.font.size_pts = 10 canvas.font.char_space_pts = 10 canvas.font.word_space_pts = 5 self.assertAlmostEqual(406.15, canvas.text(self.TEXT, 10, 10), places=2)
def test_should_add_new_page_before_rendering(self, mock_canvas): mock_canvas.return_value.stringWidth.return_value = 0.0 canvas = Canvas() canvas.add_page() RearSide(canvas, mock.Mock()).render(0) mock_canvas.return_value.assert_has_calls([ mock.call.showPage(), mock.call.setStrokeColor('#000000', 1.0), mock.call.setLineWidth(0.2834645669291339), mock.call.setLineCap(0), mock.call.setLineJoin(0), mock.call.setMiterLimit(28.34645669291339), mock.call.setDash([]), mock.call.setFillColor('#000000', 1.0), mock.call.setStrokeColor('#000000', 1.0), mock.call.setLineWidth(0.2834645669291339), mock.call.setLineCap(0), mock.call.setLineJoin(0), mock.call.setMiterLimit(28.34645669291339), mock.call.setDash([]), mock.call.setStrokeColor('#000000', 1.0), mock.call.setLineWidth(0.2834645669291339), mock.call.setLineCap(0), mock.call.setLineJoin(0), mock.call.setMiterLimit(28.34645669291339), mock.call.setDash([]), mock.call.setFillColor('#000000', 1.0), mock.call.setFillColor('#000000', 1.0), mock.call.setStrokeColor('#000000', 1.0), mock.call.setLineWidth(0.2834645669291339), mock.call.setLineCap(0), mock.call.setLineJoin(0), mock.call.setMiterLimit(28.34645669291339), mock.call.setDash([]), mock.call.setFillColor('#000000', 1.0), mock.call.setLineCap(2), mock.call.setLineWidth(0.2834645669291339), mock.call.grid([ 49.60629921259835, 162.9921259842519, 233.85826771653538, 318.89763779527556, 389.763779527559, 474.8031496062992, 545.6692913385826 ], [ 758.2677165354331, 727.0866141732284, 695.9055118110236, 664.7244094488188, 633.5433070866142, 602.3622047244095, 571.1811023622047, 540.0, 508.81889763779526, 477.6377952755905, 446.4566929133858, 415.27559055118104, 384.09448818897636, 352.9133858267716, 321.7322834645669, 290.55118110236214, 259.37007874015745, 228.1889763779527, 197.00787401574797, 165.82677165354323, 134.64566929133852, 103.46456692913371, 72.28346456692898, 41.10236220472425 ]) ])
def test_should_add_page_if_document_has_previous_pages( self, mock_add_page): canvas = Canvas() canvas.add_page() FrontSide(canvas, mock.Mock()).render(self.mock_debtor, Chunk(1, 1, {}), 0) self.assertEqual(mock_add_page.call_count, 2)
def test_should_set_default_state(self): canvas = Canvas() set_state(canvas, STATE_1) # when canvas.set_default_state() # then assert_same(self, DEFAULT_STATE, get_state(canvas))
def test_should_return_text_width(self): canvas = Canvas() canvas.font.family = FontFamily.SERIF canvas.font.weight = FontWeight.NORMAL canvas.font.style = FontStyle.ITALIC canvas.font.size_pts = 10 canvas.font.char_space_pts = 1.5 canvas.font.word_space_pts = 2.5 canvas.font.leading = 1.5 width = canvas.get_text_width_mm('Lorem ipsum dolor sit amet.') self.assertAlmostEqual(55.44, width, 2)
def test_should_align_table_to_top_right(self, mock_grid): align = TableAlign(HAlign.RIGHT, VAlign.TOP, right=0.5, top=0.5, bottom=50) Table(Canvas(), self.get_header(), 10, align) self.assertListEqual([ mock.call([ 83.19685039370064, 210.75590551181088, 281.62204724409435, 366.6614173228345, 437.527559055118, 522.5669291338581, 593.4330708661416 ], [ 797.5275590551181, 769.1811023622047, 740.8346456692913, 712.4881889763778, 684.1417322834644, 655.7952755905511, 627.4488188976377, 599.1023622047243, 570.7559055118109, 542.4094488188975, 514.0629921259841, 485.71653543307076, 457.37007874015734, 429.023622047244, 400.67716535433055, 372.3307086614172, 343.98425196850377, 315.6377952755904, 287.29133858267704, 258.9448818897636, 230.59842519685023, 202.25196850393687, 173.90551181102347, 145.55905511811008 ]), mock.call([ 83.19685039370064, 210.75590551181088, 281.62204724409435, 366.6614173228345, 437.527559055118, 522.5669291338581, 593.4330708661416 ], [840.0472440944882, 797.5275590551181]) ], mock_grid.mock_calls)
def test_should_align_table_to_middle_left(self, mock_grid): align = TableAlign(HAlign.LEFT, VAlign.MIDDLE, left=0.5, top=10, bottom=50) Table(Canvas(), self.get_header(), 10, align) self.assertListEqual([ mock.call([ 1.8425196850393704, 129.40157480314963, 200.2677165354331, 285.3070866141733, 356.17322834645677, 441.2125984251969, 512.0787401574804 ], [ 711.496062992126, 683.1496062992126, 654.8031496062991, 626.4566929133858, 598.1102362204724, 569.763779527559, 541.4173228346457, 513.0708661417323, 484.72440944881885, 456.3779527559055, 428.03149606299206, 399.6850393700787, 371.33858267716533, 342.9921259842519, 314.64566929133855, 286.29921259842513, 257.95275590551176, 229.60629921259837, 201.25984251968498, 172.9133858267716, 144.5669291338582, 116.22047244094482, 87.87401574803134 ]), mock.call([ 1.8425196850393704, 129.40157480314963, 200.2677165354331, 285.3070866141733, 356.17322834645677, 441.2125984251969, 512.0787401574804 ], [754.0157480314962, 711.496062992126]) ], mock_grid.mock_calls)
def test_should_align_table_to_middle_right(self, mock_grid): align = TableAlign(HAlign.RIGHT, VAlign.MIDDLE, right=0.5, top=10, bottom=50) Table(Canvas(), self.get_header(), 10, align) self.assertListEqual([ mock.call([ 83.19685039370064, 210.75590551181088, 281.62204724409435, 366.6614173228345, 437.527559055118, 522.5669291338581, 593.4330708661416 ], [ 711.496062992126, 683.1496062992126, 654.8031496062991, 626.4566929133858, 598.1102362204724, 569.763779527559, 541.4173228346457, 513.0708661417323, 484.72440944881885, 456.3779527559055, 428.03149606299206, 399.6850393700787, 371.33858267716533, 342.9921259842519, 314.64566929133855, 286.29921259842513, 257.95275590551176, 229.60629921259837, 201.25984251968498, 172.9133858267716, 144.5669291338582, 116.22047244094482, 87.87401574803134 ]), mock.call([ 83.19685039370064, 210.75590551181088, 281.62204724409435, 366.6614173228345, 437.527559055118, 522.5669291338581, 593.4330708661416 ], [754.0157480314962, 711.496062992126]) ], mock_grid.mock_calls)
def test_should_align_table_to_top_center(self, mock_grid): align = TableAlign(HAlign.CENTER, VAlign.TOP, top=0.5, bottom=50) Table(Canvas(), self.get_header(), 10, align) self.assertListEqual([ mock.call([ 42.51968503937, 170.07874015748024, 240.94488188976374, 325.9842519685039, 396.8503937007874, 481.8897637795275, 552.755905511811 ], [ 797.5275590551181, 769.1811023622047, 740.8346456692913, 712.4881889763778, 684.1417322834644, 655.7952755905511, 627.4488188976377, 599.1023622047243, 570.7559055118109, 542.4094488188975, 514.0629921259841, 485.71653543307076, 457.37007874015734, 429.023622047244, 400.67716535433055, 372.3307086614172, 343.98425196850377, 315.6377952755904, 287.29133858267704, 258.9448818897636, 230.59842519685023, 202.25196850393687, 173.90551181102347, 145.55905511811008 ]), mock.call([ 42.51968503937, 170.07874015748024, 240.94488188976374, 325.9842519685039, 396.8503937007874, 481.8897637795275, 552.755905511811 ], [840.0472440944882, 797.5275590551181]) ], mock_grid.mock_calls)
def test_should_shorten_long_name_and_add_ellipsis(self, mock_canvas): it = iter(range(900, 300, -7)) def stringWidth(text, *args): if text.startswith('PESEL'): return 134 elif text.startswith('Ognivo'): return next(it) return 0 mock_canvas.return_value.stringWidth.side_effect = stringWidth mock_debtor = mock.Mock() mock_debtor.name = 'lorem ipsum dolor sit amet ' * 5 mock_debtor.identity.name = 'PESEL' mock_debtor.identity.value = '12345678901' FrontSide(Canvas(), mock.Mock()).render(mock_debtor, Chunk(1, 1, {}), 0) mock_canvas.return_value.assert_has_calls([ mock.call.beginText().textLine( 'lorem ipsum dolor sit amet lorem ipsum dolor sit amet lore\u2026' ) ], any_order=True)
def test_should_render_title(self, mock_canvas): mock_canvas.return_value.stringWidth.return_value = 120.0 FrontSide(Canvas(), mock.Mock()).render(self.mock_debtor, Chunk(1, 1, {}), 0) mock_canvas.return_value.beginText.return_value.assert_has_calls([ mock.call.setFont('FreeSansBold', 12.755905511811026, 15.30708661417323), mock.call.setTextRenderMode(0), mock.call.setCharSpace(0.0), mock.call.setWordSpace(0.0), mock.call.setRise(0.0), mock.call.textLine('Ognivo: '), mock.call.setFont('FreeSans', 12.755905511811026, 15.30708661417323), mock.call.setTextRenderMode(0), mock.call.setCharSpace(0.0), mock.call.setWordSpace(0.0), mock.call.setRise(0.0), mock.call.textLine('Jan Kowalski'), mock.call.setFont('FreeSans', 12.755905511811026, 15.30708661417323), mock.call.setTextRenderMode(0), mock.call.setCharSpace(0.0), mock.call.setWordSpace(0.0), mock.call.setRise(0.0), mock.call.getY(), mock.call.setTextOrigin(mock.ANY, mock.ANY), mock.call.textLine('PESEL # 12345678901') ])
def test_should_align_table_to_middle_center(self, mock_grid): align = TableAlign(HAlign.CENTER, VAlign.MIDDLE, top=10, bottom=50) Table(Canvas(), self.get_header(), 10, align) self.assertListEqual([ mock.call([ 42.51968503937, 170.07874015748024, 240.94488188976374, 325.9842519685039, 396.8503937007874, 481.8897637795275, 552.755905511811 ], [ 711.496062992126, 683.1496062992126, 654.8031496062991, 626.4566929133858, 598.1102362204724, 569.763779527559, 541.4173228346457, 513.0708661417323, 484.72440944881885, 456.3779527559055, 428.03149606299206, 399.6850393700787, 371.33858267716533, 342.9921259842519, 314.64566929133855, 286.29921259842513, 257.95275590551176, 229.60629921259837, 201.25984251968498, 172.9133858267716, 144.5669291338582, 116.22047244094482, 87.87401574803134 ]), mock.call([ 42.51968503937, 170.07874015748024, 240.94488188976374, 325.9842519685039, 396.8503937007874, 481.8897637795275, 552.755905511811 ], [754.0157480314962, 711.496062992126]) ], mock_grid.mock_calls)
def test_should_align_table_to_bottom_center(self, mock_grid): align = TableAlign(HAlign.CENTER, VAlign.BOTTOM, top=50, bottom=0.5) Table(Canvas(), self.get_header(), 10, align) self.assertListEqual([ mock.call([ 42.51968503937, 170.07874015748024, 240.94488188976374, 325.9842519685039, 396.8503937007874, 481.8897637795275, 552.755905511811 ], [ 653.5275590551182, 625.1811023622048, 596.8346456692914, 568.4881889763781, 540.1417322834646, 511.7952755905513, 483.44881889763786, 455.1023622047245, 426.7559055118111, 398.4094488188977, 370.0629921259843, 341.71653543307093, 313.37007874015757, 285.02362204724415, 256.6771653543308, 228.3307086614174, 199.984251968504, 171.6377952755906, 143.2913385826772, 114.94488188976382, 86.59842519685043, 58.25196850393704, 29.905511811023658, 1.5590551181102685 ]), mock.call([ 42.51968503937, 170.07874015748024, 240.94488188976374, 325.9842519685039, 396.8503937007874, 481.8897637795275, 552.755905511811 ], [696.0472440944883, 653.5275590551182]) ], mock_grid.mock_calls)
def test_should_make_deep_copies_on_push(self): canvas = Canvas() old_stroke = id(canvas.stroke) old_fill = id(canvas.fill) old_font = id(canvas.font) canvas.push_state() new_stroke = id(canvas.stroke) new_fill = id(canvas.fill) new_font = id(canvas.font) self.assertNotEqual(old_stroke, new_stroke) self.assertNotEqual(old_fill, new_fill) self.assertNotEqual(old_font, new_font)
def test_should_restore_previous_state_after_rendering_table(self): canvas = Canvas() set_state(canvas, STATE_1) Table(canvas, self.get_header(), row_height=10) assert_same(self, STATE_1, get_state(canvas))
def test_should_not_vertical_align_when_height_not_given(self, mock_obj): with self.assertRaises(AssertionError): Canvas().text(self.TEXT, 10, 10, width=170, halign=HAlign.CENTER, valign=VAlign.MIDDLE)
def test_should_render_blank_page_when_odd_number_of_chunks( self, mock_front, mock_rear, mock_blank_page, mock_config): mock_config.return_value.get = mock.Mock(return_value='false') template = Template(Canvas()) template.render(mock.Mock(), {mock.Mock(): mock.Mock()}) mock_blank_page.return_value.render.assert_called_once()
def test_cell_should_use_top_left_alignment_with_padding_by_default(self): table = Table(Canvas(), self.get_header(), row_height=10) with mock.patch( 'reportlab.pdfgen.canvas.Canvas.beginText') as mock_obj: table.cell(2, 5, 'lorem ipsum') mock_obj.assert_called_once_with(242.3622047244094, 643.7858267716535)
def test_should_align_single_line_of_text(self, mock_obj): canvas = Canvas() canvas.font.family = FontFamily.SERIF canvas.font.weight = FontWeight.NORMAL canvas.font.style = FontStyle.ITALIC canvas.font.size_pts = 10 canvas.font.render_mode = FontRenderMode.FILL canvas.font.char_space_pts = 1.5 canvas.font.word_space_pts = 2.5 canvas.font.leading = 1.5 x = canvas.text('lorem ipsum', 10, 10, 170, 40, HAlign.CENTER, VAlign.MIDDLE) self.assertAlmostEqual(106.45, x, places=2) mock_obj.return_value.setTextOrigin.assert_called_with( 236.8413385826772, mock.ANY)
def test_cell_should_use_custom_alignment(self): table = Table(Canvas(), self.get_header(), row_height=10) with mock.patch( 'reportlab.pdfgen.canvas.Canvas.beginText') as mock_obj: table.cell(2, 5, 'lorem ipsum', HAlign.CENTER, VAlign.MIDDLE) mock_obj.assert_called_once_with(242.3622047244094, 637.0299212598425)
def test_cell_should_use_custom_padding(self): table = Table(Canvas(), self.get_header(), row_height=10) with mock.patch( 'reportlab.pdfgen.canvas.Canvas.beginText') as mock_obj: table.cell(2, 5, 'lorem ipsum', padding=0) mock_obj.assert_called_once_with(240.94488188976374, 645.2031496062991)
def test_should_not_render_blank_page_in_rear_page_mode( self, mock_front, mock_rear, mock_blank_page, mock_config): mock_config.return_value.get = mock.Mock(return_value='true') template = Template(Canvas()) template.render(mock.Mock(), {mock.Mock(): mock.Mock()}) mock_blank_page.return_value.render.assert_not_called()
def test_should_draw_grid_with_flipped_y_axis_in_millimeters( self, mock_grid): Canvas().grid([10, 20, 30], [10, 20, 30, 40, 50]) self.assertEqual( mock.call( [28.34645669291339, 56.69291338582678, 85.03937007874016], [ 813.543307086614, 785.1968503937007, 756.8503937007873, 728.5039370078739, 700.1574803149606 ]), mock_grid.call_args)
def test_should_render_table(self, mock_canvas): mock_canvas.return_value.stringWidth.return_value = 120 FrontSide(Canvas(), mock.Mock()).render(self.mock_debtor, Chunk(1, 1, {}), 0) # body mock_canvas.return_value.assert_has_calls([ mock.call.setLineCap(2), mock.call.setLineWidth(0.2834645669291339), mock.call.grid([ 49.60629921259835, 162.9921259842519, 233.85826771653538, 318.89763779527556, 389.763779527559, 474.8031496062992, 545.6692913385826 ], [ 758.2677165354331, 727.0866141732284, 695.9055118110236, 664.7244094488188, 633.5433070866142, 602.3622047244095, 571.1811023622047, 540.0, 508.81889763779526, 477.6377952755905, 446.4566929133858, 415.27559055118104, 384.09448818897636, 352.9133858267716, 321.7322834645669, 290.55118110236214, 259.37007874015745, 228.1889763779527, 197.00787401574797, 165.82677165354323, 134.64566929133852, 103.46456692913371, 72.28346456692898, 41.10236220472425 ]) ]) # header mock_canvas.return_value.assert_has_calls([ mock.call.setLineWidth(0.8503937007874016), mock.call.grid([ 49.60629921259835, 162.9921259842519, 233.85826771653538, 318.89763779527556, 389.763779527559, 474.8031496062992, 545.6692913385826 ], [800.7874015748032, 758.2677165354331]), ]) # column titles mock_canvas.return_value.beginText.return_value.textLine.assert_has_calls( [ mock.call('Bank'), mock.call('Data'), mock.call('z\u0142o\u017cenia'), mock.call('zapytania'), mock.call('Podpis'), mock.call('sk\u0142adaj\u0105cego'), mock.call('zapytanie'), mock.call('Data'), mock.call('odbioru'), mock.call('odpowiedzi'), mock.call('Podpis'), mock.call('odbieraj\u0105cego'), mock.call('odpowied\u017a'), mock.call('Rodzaj'), mock.call('odpowiedzi') ])