class TestMarkers: def setup_method(self, method): self.document = Document("letter", "portrait") def test_insert_page_break(self): self.document.draw_rectangle(0, 0, 200, 200, (0, 0, 0), (0, 0, 0), 1) self.document.insert_page_break() assert self.document._document[-1] == { "type": "page_break", }
def test_invalid_parameters(self, doc_size, doc_layout, match): with pytest.raises(KeyError, match=match): document = Document(doc_size, doc_layout)
def test_dimensions(self, doc_size, doc_layout, w, h): document = Document(doc_size, doc_layout) assert document.w == w assert document.h == h
def new_populated_document(self, size="letter"): document = Document(size, "portrait") document.draw_rectangle(100, 100, 400, 400, None, (50, 99, 154), 40) document.draw_rectangle(700, 100, 200, 400, "#181c1f", None, 0) document.draw_circle(160, 800, 150, None, (138, 41, 0), 20) document.draw_circle(160, 1200, 150, (172, 108, 19), None, 0) document.draw_circle(160, 1600, 150, "#0d754b", (150, 153, 155), 20) document.draw_line(document.w / 2, 0, document.w / 2, document.h, 30, (224, 224, 224)) document.draw_string("Left Align", document.w / 2, 800, "left", "OpenSans-Bold", 100, (0, 0, 0)) document.draw_string("Center Align", document.w / 2, 1000, "middle", "OpenSans-Bold", 100, (0, 0, 0)) document.draw_string("Right Align", document.w / 2, 1200, "right", "OpenSans-Bold", 100, (0, 0, 0)) document.draw_string("Page 1", document.w - 50, document.h - 50, "right", "OpenSans-Regular", 100, "#000") document.insert_page_break() document.draw_rectangle(100, 100, 400, 400, None, (50, 99, 154), 40) document.draw_rectangle(700, 100, 200, 400, (24, 28, 31), None, 0) document.draw_line(document.w / 2, 0, document.w / 2, document.h, 30, "#e0e0e0") document.draw_string("Left Align", document.w / 2, 800, "left", "OpenSans-Bold", 100, (0, 0, 0)) document.draw_string("Center Align", document.w / 2, 1000, "middle", "OpenSans-Bold", 100, (0, 0, 0)) document.draw_string("Right Align", document.w / 2, 1200, "right", "OpenSans-Bold", 100, (0, 0, 0)) document.draw_string("Page 2", document.w - 50, document.h - 50, "right", "OpenSans-Regular", 100, (0, 0, 0)) return document
def setup_method(self, method): self.document = Document("letter", "portrait")
class TestDrawing: def setup_method(self, method): self.document = Document("letter", "portrait") def test_draw_string(self): self.document.draw_string("Test", 0, 100, "left", "OpenSans-Regular", 12, (0, 0, 0)) assert self.document._document[-1] == { "type": "string", "string": "Test", "x": 0, "y": 100, "alignment": "left", "font": "OpenSans-Regular", "size": 12, "color": (0, 0, 0) } @pytest.mark.parametrize( "string, x, y, alignment, font, size, color", [ # string # x ("Test", -1, 0, "left", "OpenSans-Regular", 12, (0, 0, 0)), ("Test", 10000, 0, "left", "OpenSans-Regular", 12, (0, 0, 0)), ("Test", "a", 0, "left", "OpenSans-Regular", 12, (0, 0, 0)), ("Test", None, 0, "left", "OpenSans-Regular", 12, (0, 0, 0)), ("Test", "", 0, "left", "OpenSans-Regular", 12, (0, 0, 0)), # y ("Test", 0, -1, "left", "OpenSans-Regular", 12, (0, 0, 0)), ("Test", 0, 10000, "left", "OpenSans-Regular", 12, (0, 0, 0)), ("Test", 0, "a", "left", "OpenSans-Regular", 12, (0, 0, 0)), ("Test", 0, None, "left", "OpenSans-Regular", 12, (0, 0, 0)), ("Test", 0, "", "left", "OpenSans-Regular", 12, (0, 0, 0)), # alignment ("Test", 0, 0, "top", "OpenSans-Regular", 12, (0, 0, 0)), ("Test", 0, 0, -1, "OpenSans-Regular", 12, (0, 0, 0)), ("Test", 0, 0, None, "OpenSans-Regular", 12, (0, 0, 0)), # font ("Test", 0, 0, "left", "Verdana", 12, (0, 0, 0)), ("Test", 0, 0, "left", -1, 12, (0, 0, 0)), ("Test", 0, 0, "left", None, 12, (0, 0, 0)), # size ("Test", 0, 0, "left", "OpenSans-Regular", "a", (0, 0, 0)), ("Test", 0, 0, "left", "OpenSans-Regular", -1, (0, 0, 0)), # color ("Test", 0, 0, "left", "OpenSans-Regular", 12, ("r", "g", "b")), ("Test", 0, 0, "left", "OpenSans-Regular", 12, (-1, -1, -1)), ("Test", 0, 0, "left", "OpenSans-Regular", 12, (256, 256, 256)), ]) def test_draw_string_error(self, string, x, y, alignment, font, size, color): with pytest.raises(RuntimeError): self.document.draw_string(string, x, y, alignment, font, size, color) def test_draw_rectangle(self): self.document.draw_rectangle(0, 0, 200, 200, (0, 0, 0), (0, 0, 0), 1) assert self.document._document[-1] == { "type": "rectangle", "x": 0, "y": 0, "w": 200, "h": 200, "fill_color": (0, 0, 0), "border_color": (0, 0, 0), "border_width": 1 } @pytest.mark.parametrize( "x,y,w,h,fill_color,border_color,border_width", [ # blank rectangle (-1, 0, 200, 200, None, (0, 0, 0), 0), # x (-1, 0, 200, 200, (0, 0, 0), (0, 0, 0), 1), (5000, 0, 200, 200, (0, 0, 0), (0, 0, 0), 1), ("a", 0, 200, 200, (0, 0, 0), (0, 0, 0), 1), (None, 0, 200, 200, (0, 0, 0), (0, 0, 0), 1), ("", 0, 200, 200, (0, 0, 0), (0, 0, 0), 1), # y (0, -1, 200, 200, (0, 0, 0), (0, 0, 0), 1), (0, 5000, 200, 200, (0, 0, 0), (0, 0, 0), 1), (0, "a", 200, 200, (0, 0, 0), (0, 0, 0), 1), (0, None, 200, 200, (0, 0, 0), (0, 0, 0), 1), (0, "", 200, 200, (0, 0, 0), (0, 0, 0), 1), # w (0, 0, -1, 200, (0, 0, 0), (0, 0, 0), 1), (0, 0, 5000, 200, (0, 0, 0), (0, 0, 0), 1), (0, 0, "a", 200, (0, 0, 0), (0, 0, 0), 1), (0, 0, None, 200, (0, 0, 0), (0, 0, 0), 1), (0, 0, "", 200, (0, 0, 0), (0, 0, 0), 1), # h (0, 0, 200, -1, (0, 0, 0), (0, 0, 0), 1), (0, 0, 200, 5000, (0, 0, 0), (0, 0, 0), 1), (0, 0, 200, "a", (0, 0, 0), (0, 0, 0), 1), (0, 0, 200, None, (0, 0, 0), (0, 0, 0), 1), (0, 0, 200, "", (0, 0, 0), (0, 0, 0), 1), # fill_color (0, 0, 200, 200, (-1, -1, -1), (0, 0, 0), 1), (0, 0, 200, 200, (256, 256, 256), (0, 0, 0), 1), (0, 0, 200, 200, ("a", "b", "c"), (0, 0, 0), 1), # border_color (0, 0, 200, 200, (0, 0, 0), (-1, -1, -1), 1), (0, 0, 200, 200, (0, 0, 0), (256, 256, 256), 1), (0, 0, 200, 200, (0, 0, 0), ("a", "b", "c"), 1), # border_width (0, 0, 200, 200, (0, 0, 0), (0, 0, 0), -1), (0, 0, 200, 200, (0, 0, 0), (0, 0, 0), "a"), ]) def test_draw_rectangle_error(self, x, y, w, h, fill_color, border_color, border_width): with pytest.raises(RuntimeError): self.document.draw_rectangle(x, y, w, h, fill_color, border_color, border_width) def test_draw_line(self): self.document.draw_line(0, 0, 100, 100, 2, (0, 0, 0)) assert self.document._document[-1] == { "type": "line", "x": 0, "y": 0, "x1": 100, "y1": 100, "width": 2, "color": (0, 0, 0) } @pytest.mark.parametrize( "x,y,x1,y1,width,color", [ # x (-1, 0, 100, 100, 2, (0, 0, 0)), (10000, 0, 100, 100, 2, (0, 0, 0)), ("a", 0, 100, 100, 2, (0, 0, 0)), (None, 0, 100, 100, 2, (0, 0, 0)), ("", 0, 100, 100, 2, (0, 0, 0)), # y (0, -1, 100, 100, 2, (0, 0, 0)), (0, 10000, 100, 100, 2, (0, 0, 0)), (0, "a", 100, 100, 2, (0, 0, 0)), (0, None, 100, 100, 2, (0, 0, 0)), (0, "", 100, 100, 2, (0, 0, 0)), # x1 (0, 0, -1, 100, 2, (0, 0, 0)), (0, 0, 10000, 100, 2, (0, 0, 0)), (0, 0, "a", 100, 2, (0, 0, 0)), (0, 0, None, 100, 2, (0, 0, 0)), (0, 0, "", 100, 2, (0, 0, 0)), # y1 (0, 0, 100, -1, 2, (0, 0, 0)), (0, 0, 100, 10000, 2, (0, 0, 0)), (0, 0, 100, "a", 2, (0, 0, 0)), (0, 0, 100, None, 2, (0, 0, 0)), (0, 0, 100, "", 2, (0, 0, 0)), # width (0, 0, 100, 100, "a", (0, 0, 0)), (0, 0, 100, 100, -1, (0, 0, 0)), # color (0, 0, 100, 100, 2, ("r", "g", "b")), (0, 0, 100, 100, 2, (-1, -1, -1)), (0, 0, 100, 100, 2, (256, 256, 256)), ]) def test_draw_line_error(self, x, y, x1, y1, width, color): with pytest.raises(RuntimeError): self.document.draw_line(x, y, x1, y1, width, color) def test_draw_circle(self): self.document.draw_circle(0, 0, 200, (0, 0, 0), (0, 0, 0), 1) assert self.document._document[-1] == { "type": "circle", "x": 0, "y": 0, "radius": 200, "fill_color": (0, 0, 0), "border_color": (0, 0, 0), "border_width": 1 } @pytest.mark.parametrize( "x,y,radius,fill_color,border_color,border_width", [ # blank rectangle (-1, 0, 200, None, (0, 0, 0), 0), # x (-1, 0, 200, (0, 0, 0), (0, 0, 0), 1), (5000, 0, 200, (0, 0, 0), (0, 0, 0), 1), ("a", 0, 200, (0, 0, 0), (0, 0, 0), 1), (None, 0, 200, (0, 0, 0), (0, 0, 0), 1), ("", 0, 200, (0, 0, 0), (0, 0, 0), 1), # y (0, -1, 200, (0, 0, 0), (0, 0, 0), 1), (0, 5000, 200, (0, 0, 0), (0, 0, 0), 1), (0, "a", 200, (0, 0, 0), (0, 0, 0), 1), (0, None, 200, (0, 0, 0), (0, 0, 0), 1), (0, "", 200, (0, 0, 0), (0, 0, 0), 1), # radius (0, 0, -1, (0, 0, 0), (0, 0, 0), 1), (0, 0, "a", (0, 0, 0), (0, 0, 0), 1), (0, 0, None, (0, 0, 0), (0, 0, 0), 1), (0, 0, "", (0, 0, 0), (0, 0, 0), 1), # fill_color (0, 0, 200, (-1, -1, -1), (0, 0, 0), 1), (0, 0, 200, (256, 256, 256), (0, 0, 0), 1), (0, 0, 200, ("a", "b", "c"), (0, 0, 0), 1), # border_color (0, 0, 200, (0, 0, 0), (-1, -1, -1), 1), (0, 0, 200, (0, 0, 0), (256, 256, 256), 1), (0, 0, 200, (0, 0, 0), ("a", "b", "c"), 1), # border_width (0, 0, 200, (0, 0, 0), (0, 0, 0), -1), (0, 0, 200, (0, 0, 0), (0, 0, 0), "a"), ]) def test_draw_circle_error(self, x, y, radius, fill_color, border_color, border_width): with pytest.raises(RuntimeError): self.document.draw_circle(x, y, radius, fill_color, border_color, border_width)
def new_document(self): return Document("A4", "portrait")