-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathtest_helper.py
58 lines (47 loc) · 2.12 KB
/
test_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Copyright 2022 Camptocamp SA
# @author: Simone Orsi <simahawk@gmail.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
import os
from lxml import etree
from odoo.tests.common import TransactionCase, TreeCase
from odoo.addons.pdf_helper.utils import PDFParser
def read_test_file(filename, mode="r"):
path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
with open(path, mode) as thefile:
return thefile.read()
# NOTE: this class could use a bare `unittest.TestCase` as base
# but w/out TreeCase Odoo won't load these tests.
class TestPDFHelperUtils(TreeCase):
def test_parse_xml(self):
pdf_content = read_test_file("pdf_with_xml_test.pdf", mode="rb")
res = PDFParser(pdf_content).get_xml_files()
fname, xml_root = tuple(res.items())[0]
self.assertEqual(fname, "factur-x.xml")
self.assertTrue(isinstance(xml_root, etree._Element))
class TestPDFHelper(TransactionCase):
def test_get_xml(self):
pdf_content = read_test_file("pdf_with_xml_test.pdf", mode="rb")
res = self.env["pdf.helper"].pdf_get_xml_files(pdf_content)
fname, xml_root = tuple(res.items())[0]
self.assertEqual(fname, "factur-x.xml")
self.assertTrue(isinstance(xml_root, etree._Element))
def test_get_xml_fail(self):
with self.assertLogs(
"odoo.addons.pdf_helper.models.helper", level="ERROR"
) as log_catcher:
self.env["pdf.helper"].pdf_get_xml_files(b"")
self.assertIn(
"PDF file parsing failed: Cannot read an empty file",
log_catcher.output[0],
)
def test_embed_xml(self):
pdf_content = read_test_file("pdf_with_xml_test.pdf", mode="rb")
filename = "test"
xml = b"<root>test</root>"
newpdf_content = self.env["pdf.helper"].pdf_embed_xml(
pdf_content, filename, xml
)
attachments = self.env["pdf.helper"].pdf_get_xml_files(newpdf_content)
self.assertTrue(filename in attachments)
etree_content = attachments[filename]
self.assertEqual(xml, etree.tostring(etree_content))