예제 #1
0
def test_nominal_csv(tmp_path):
    """Taken from docs/Templates.md"""
    tmpl = Template(format="A4", title="Sample Invoice")
    tmpl.parse_csv(HERE / "mycsvfile.csv", delimiter=";")
    tmpl.add_page()
    tmpl["company_name"] = "Sample Company"
    assert_pdf_equal(tmpl, HERE / "template_nominal_csv.pdf", tmp_path)
예제 #2
0
 def test_nominal_csv(self):
     "Taken from docs/Templates.md"
     tmpl = Template(format="A4", title="Sample Invoice")
     tmpl.parse_csv(relative_path_to("mycsvfile.csv"), delimiter=";")
     tmpl.add_page()
     tmpl["company_name"] = "Sample Company"
     assert_pdf_equal(self, tmpl, "test_nominal_csv.pdf")
예제 #3
0
def test_template_multipage(tmp_path):
    """Testing a Template() populating several pages."""
    tmpl = Template(format="A4", title="Sample Invoice")
    tmpl.parse_csv(HERE / "mycsvfile.csv", delimiter=";")
    tmpl.add_page()
    tmpl["name0"] = "Joe Doe"
    tmpl["title0"] = "Director"
    tmpl.add_page()
    tmpl["name0"] = "Jane Doe"
    tmpl["title0"] = "General Manager"
    tmpl.add_page()
    tmpl["name0"] = "Heinz Mustermann"
    tmpl["title0"] = "Worker"
    assert_pdf_equal(tmpl, HERE / "template_multipage.pdf", tmp_path)
예제 #4
0
def test_template_nominal_csv(tmp_path):
    """Same data as in docs/Templates.md
    The numeric_text tests for a regression."""
    tmpl = Template(format="A4", title="Sample Invoice")
    tmpl.parse_csv(HERE / "mycsvfile.csv", delimiter=";")
    tmpl.add_page()
    tmpl["empty_fields"] = "empty"
    assert_pdf_equal(tmpl, HERE / "template_nominal_csv.pdf", tmp_path)

    tmpl = Template(format="A4", title="Sample Invoice")
    tmpl.parse_csv(HERE / "mycsvfile.csv", delimiter=";", encoding="utf-8")
    tmpl.add_page()
    tmpl["empty_fields"] = "empty"
    assert_pdf_equal(tmpl, HERE / "template_nominal_csv.pdf", tmp_path)
예제 #5
0
    def test_RenderCSV(self):
        f = Template(paperformat="A4", title="testCSV")

        f.parse_csv("testTemplateFile.csv", delimiter=";")
        f.add_page("testTemplateFile.csv")

        pdf = f.getFPDF()
        set_doc_date_0(pdf)

        outfile = relative_path_to("testTemplateFile.pdf")
        pdf.output(outfile, 'F')

        test_hash = calculate_hash_of_file(outfile)
        # Assert versus a well know hash
        self.assertEqual(test_hash, "8d4e2060e5d8264d03ebca707a2ed1ca")

        os.unlink(outfile)
예제 #6
0
def test_template_badinput():
    """Testing Template() with non-conforming definitions."""
    for arg in (
            "format",
            "orientation",
            "unit",
            "title",
            "author",
            "subject",
            "creator",
            "keywords",
    ):
        with raises(TypeError):
            Template(**{arg: 7})  # numeric instead of str
    elements = [{}]
    with raises(KeyError):
        tmpl = Template(elements=elements)
    elements = [{"name": "n", "type": "X"}]
    with raises(KeyError):
        tmpl = Template(elements=elements)
        tmpl.render()
    elements = [  # missing mandatory x2
        {
            "name": "n",
            "type": "T",
            "x1": 0,
            "y1": 0,
            "y2": 0,
            "text": "Hello!",
        }
    ]
    with raises(KeyError):
        tmpl = Template(elements=elements)
        tmpl["n"] = "hello"
        tmpl.render()
    elements = [  # malformed y2
        {
            "name": "n",
            "type": "T",
            "x1": 0,
            "y1": 0,
            "x2": 0,
            "y2": "x",
            "text": "Hello!",
        }
    ]
    with raises(TypeError):
        tmpl = Template(elements=elements)
        tmpl["n"] = "hello"
        tmpl.render()
    tmpl = Template()
    with raises(FPDFException):
        tmpl.parse_csv(HERE / "mandmissing.csv", delimiter=";")
    with raises(ValueError):
        tmpl.parse_csv(HERE / "badint.csv", delimiter=";")
    with raises(ValueError):
        tmpl.parse_csv(HERE / "badfloat.csv", delimiter=";")
    with raises(KeyError):
        tmpl.parse_csv(HERE / "badtype.csv", delimiter=";")
        tmpl.render()
    with warns(DeprecationWarning):
        Template(infile="whatever")
    with raises(AttributeError):
        with warns(DeprecationWarning):
            tmpl = Template()
            tmpl.render(dest="whatever")