-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathtest_base_wamas_ubl.py
173 lines (157 loc) · 7.33 KB
/
test_base_wamas_ubl.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Copyright 2023 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# Copyright 2023 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from ast import literal_eval
from base64 import b64decode, b64encode
from freezegun import freeze_time
from odoo.tests.common import TransactionCase
from odoo.tools import file_open
from odoo.addons.account.tests.common import AccountTestInvoicingCommon
# FIXME: all simple convertion tests should move to lib/wamas/tests and there
# should be a test that runs the lib tests
class TestBaseWamas(TransactionCase):
maxDiff = None
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.base_wamas_ubl = cls.env["base.wamas.ubl"]
cls.wamas_ubl_wiz_check = cls.env["wamas.ubl.wiz.check"]
cls.wamas_ubl_wiz_simulate = cls.env["wamas.ubl.wiz.simulate"]
cls.assertXmlTreeEqual = AccountTestInvoicingCommon.assertXmlTreeEqual
cls.get_xml_tree_from_string = (
AccountTestInvoicingCommon.get_xml_tree_from_string
)
cls._turn_node_as_dict_hierarchy = (
AccountTestInvoicingCommon._turn_node_as_dict_hierarchy
)
cls.partner_1 = cls.env.ref("base.res_partner_1")
cls.partner_2 = cls.env.ref("base.res_partner_2")
country_code_1 = cls.partner_1.commercial_partner_id.country_id.code or ""
country_code_2 = cls.partner_2.commercial_partner_id.country_id.code or ""
cls.extra_data = {
"DespatchSupplierParty": {
"CustomerAssignedAccountID": cls.partner_1.commercial_partner_id.ref
or "",
"PartyName": cls.partner_1.commercial_partner_id.name or "",
"StreetName": cls.partner_1.commercial_partner_id.street or "",
"CityName": cls.partner_1.commercial_partner_id.city or "",
"PostalZone": cls.partner_1.commercial_partner_id.zip or "",
"Country.IdentificationCode": country_code_1,
"CompanyID": cls.partner_1.commercial_partner_id.vat or "",
"TaxScheme.ID": "",
"TaxScheme.TaxTypeCode": "",
"Contact.Name": cls.partner_1.child_ids
and cls.partner_1.child_ids[0].name
or "",
"Contact.Telephone": cls.partner_1.child_ids
and cls.partner_1.child_ids[0].phone
or "",
"Contact.ElectronicMail": cls.partner_1.child_ids
and cls.partner_1.child_ids[0].email
or "",
},
"DeliveryCustomerParty": {
"PartyName": cls.partner_2.commercial_partner_id.name or "",
"StreetName": cls.partner_2.commercial_partner_id.street or "",
"CityName": cls.partner_2.commercial_partner_id.city or "",
"PostalZone": cls.partner_2.commercial_partner_id.zip or "",
"CountrySubentity": cls.partner_2.commercial_partner_id.state_id.name
or "",
"Country.IdentificationCode": country_code_2,
"CompanyID": cls.partner_2.commercial_partner_id.vat or "",
"TaxScheme.ID": "",
"TaxScheme.TaxTypeCode": "",
"Contact.Name": cls.partner_2.child_ids
and cls.partner_2.child_ids[0].name
or "",
"Contact.Telephone": cls.partner_2.child_ids
and cls.partner_2.child_ids[0].phone
or "",
"Contact.Telefax": "",
"Contact.ElectronicMail": cls.partner_2.child_ids
and cls.partner_2.child_ids[0].email
or "",
},
}
@freeze_time("2023-05-01")
def _wamas_ubl_wiz_check(self, input_filename, expected_output_filename):
with file_open(input_filename) as inputfile, file_open(
expected_output_filename
) as outputfile:
str_input = inputfile.read()
str_expected_output = outputfile.read().strip("\n")
wizard = self.wamas_ubl_wiz_check.create(
{
"wamas_file": b64encode(str_input.encode("iso-8859-1")),
}
)
wizard._onchange_wamas_filename()
self.assertFalse(wizard.output)
wizard.btn_check()
self.assertEqual(wizard.output, str_expected_output)
@freeze_time("2023-05-01 00:00:00")
def _wamas_ubl_wiz_simulate(
self, input_filename, expected_output_filename, state="success"
):
with file_open(input_filename) as inputfile, file_open(
expected_output_filename
) as outputfile:
str_input = inputfile.read()
expected_output = outputfile.read()
wizard = self.wamas_ubl_wiz_simulate.create(
{
"wamas_file": b64encode(str_input.encode("iso-8859-1")),
}
)
wizard._onchange_wamas_filename()
self.assertFalse(wizard.output_wamas_file)
self.assertFalse(wizard.output_wamas_filename)
self.assertFalse(wizard.output)
wizard.btn_simulate()
if state == "success":
output = b64decode(wizard.output_wamas_file).decode("iso-8859-1")
self.assertEqual(output, expected_output)
else:
self.assertEqual(wizard.output, expected_output)
def test_wamas_ubl_wiz_check(self):
# Success
self._wamas_ubl_wiz_check(
"base_wamas_ubl/tests/samples/CHECKWAMAS-SAMPLE_INPUT.wamas",
"base_wamas_ubl/tests/samples/CHECKWAMAS-SAMPLE_OUTPUT.txt",
)
def test_wamas_ubl_wiz_check_exception_1(self):
# Raise Exception
self._wamas_ubl_wiz_check(
"base_wamas_ubl/tests/samples/CHECKWAMAS-SAMPLE_INPUT-EXCEPTION.wamas",
"base_wamas_ubl/tests/samples/CHECKWAMAS-SAMPLE_OUTPUT-EXCEPTION.txt",
)
def test_wamas_ubl_wiz_check_exception_2(self):
self._wamas_ubl_wiz_check(
"base_wamas_ubl/tests/samples/CHECKWAMAS-SAMPLE_INPUT-EXCEPTION-2.wamas",
"base_wamas_ubl/tests/samples/CHECKWAMAS-SAMPLE_OUTPUT-EXCEPTION-2.txt",
)
def test_wamas_ubl_wiz_simulate(self):
# Success
self._wamas_ubl_wiz_simulate(
"base_wamas_ubl/tests/samples/SIMULATEWAMAS-SAMPLE_INPUT.wamas",
"base_wamas_ubl/tests/samples/SIMULATEWAMAS-SAMPLE_OUTPUT.wamas",
)
# Raise Exception
self._wamas_ubl_wiz_simulate(
"base_wamas_ubl/tests/samples/SIMULATEWAMAS-SAMPLE_INPUT-EXCEPTION.wamas",
"base_wamas_ubl/tests/samples/SIMULATEWAMAS-SAMPLE_OUTPUT-EXCEPTION.txt",
"fail",
)
@freeze_time("2023-12-21 04:12:51")
def test_get_wamas_type(self):
input_filename = "CHECKWAMAS-SAMPLE_INPUT.wamas"
expected_output_filename = "CHECKWAMAS-SAMPLE_OUTPUT.dict"
path = "base_wamas_ubl/tests/samples/"
with file_open(path + input_filename) as inputfile, file_open(
path + expected_output_filename
) as outputfile:
str_input = inputfile.read()
dict_expected_output = literal_eval(outputfile.read())
wamas_type = self.base_wamas_ubl.get_wamas_type(str_input)
# Wamas Type
self.assertEqual(wamas_type, dict_expected_output["wamas_type"])