Skip to content

Commit f0cf3d6

Browse files
hugosantosredMiquelRForgeFlow
authored andcommitted
[FIX] l10n_es_aeat: Bytestring concatenation on file export to boe (Solves compatibility problems with python3) (#1)
* Adds tests for exporting files to boe with an export config (#2)
1 parent 4bd1938 commit f0cf3d6

3 files changed

Lines changed: 116 additions & 4 deletions

File tree

l10n_es_aeat/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
from . import test_l10n_es_aeat
44
from . import test_l10n_es_aeat_report
5+
from . import test_l10n_es_aeat_export_config
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# -*- coding: utf-8 -*-
2+
# © 2017 FactorLibre - Hugo Santos <hugo.santos@factorlibre.com>
3+
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0
4+
from odoo.tests import common
5+
6+
7+
class TestL10nEsAeatExportConfig(common.TransactionCase):
8+
9+
def setUp(self):
10+
super(TestL10nEsAeatExportConfig, self).setUp()
11+
12+
def test_onchange_export_config_line(self):
13+
export_config_line_env = self.env['aeat.model.export.config.line']
14+
with self.env.do_in_onchange():
15+
export_line_float = export_config_line_env.new()
16+
export_line_float.export_type = 'float'
17+
export_line_float.onchange_type()
18+
self.assertEqual(export_line_float.alignment, 'right',
19+
'Export type float must be aligned to the right')
20+
export_line_str = export_config_line_env.new()
21+
export_line_str.export_type = 'string'
22+
export_line_str.onchange_type()
23+
self.assertEqual(export_line_str.alignment, 'left',
24+
'Export type string must be aligned to the left')
25+
export_line_subtype = export_config_line_env.new()
26+
export_line_subtype.alignment = 'left'
27+
export_line_subtype.decimal_size = 10
28+
export_line_subtype.apply_sign = True
29+
export_line_subtype.subconfig_id = export_line_str.id
30+
export_line_subtype.onchange_subconfig()
31+
self.assertFalse(export_line_subtype.alignment,
32+
'Alignment must be False for a subtype line')
33+
self.assertEqual(export_line_subtype.decimal_size, 0)
34+
self.assertFalse(export_line_subtype.apply_sign,
35+
'Apply sign must be False for a subtype line')
36+
37+
def test_export_config_file(self):
38+
export_config = self.env['aeat.model.export.config'].create({
39+
'name': 'Test Export Config',
40+
'model_number': '000',
41+
'config_line_ids': [
42+
(0, 0, {
43+
'sequence': 1,
44+
'name': '<T',
45+
'fixed_value': '<T',
46+
'export_type': 'string',
47+
'size': 3,
48+
'alignment': 'left'
49+
}),
50+
(0, 0, {
51+
'sequence': 2,
52+
'name': 'Empty spaces',
53+
'fixed_value': '',
54+
'export_type': 'string',
55+
'size': 10,
56+
'alignment': 'left'
57+
}),
58+
(0, 0, {
59+
'sequence': 3,
60+
'name': 'Integer Value',
61+
'fixed_value': 1,
62+
'export_type': 'integer',
63+
'size': 3,
64+
'alignment': 'right'
65+
}),
66+
(0, 0, {
67+
'sequence': 4,
68+
'name': 'Float Value',
69+
'fixed_value': 15.0,
70+
'export_type': 'float',
71+
'size': 6,
72+
'decimal_size': 2,
73+
'alignment': 'right'
74+
}),
75+
(0, 0, {
76+
'sequence': 5,
77+
'name': 'Boolean True Value',
78+
'expression': True,
79+
'export_type': 'boolean',
80+
'size': 2,
81+
'alignment': 'left'
82+
}),
83+
(0, 0, {
84+
'sequence': 6,
85+
'name': 'Boolean False Value',
86+
'expression': False,
87+
'export_type': 'boolean',
88+
'size': 2,
89+
'alignment': 'left'
90+
}),
91+
(0, 0, {
92+
'sequence': 7,
93+
'name': '>',
94+
'fixed_value': '>',
95+
'size': 1,
96+
'alignment': 'left'
97+
})
98+
]
99+
})
100+
new_report = self.env['l10n.es.aeat.report'].new({
101+
'name': 'Test Report'
102+
})
103+
export_to_boe = self.env['l10n.es.aeat.report.export_to_boe'].create({
104+
'name': 'test_export_to_boe.txt'
105+
})
106+
export_file = export_to_boe._export_config(
107+
new_report, export_config)
108+
self.assertEqual(b'<T 001001500X >', export_file)

l10n_es_aeat/wizard/export_to_boe.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def action_get_file(self):
122122
if not active_id or not active_model:
123123
return False
124124
report = self.env[active_model].browse(active_id)
125-
contents = ''
125+
contents = b''
126126
if report.export_config_id:
127127
contents += self.action_get_file_from_config(report)
128128
else:
@@ -165,7 +165,7 @@ def action_get_file_from_config(self, report):
165165
@api.multi
166166
def _export_config(self, obj, export_config):
167167
self.ensure_one()
168-
contents = ''
168+
contents = b''
169169
for line in export_config.config_line_ids:
170170
contents += self._export_line_process(obj, line)
171171
return contents
@@ -188,7 +188,7 @@ def merge(match):
188188
result = merge_eval(exp)
189189
return result and tools.ustr(result) or ''
190190

191-
val = ''
191+
val = b''
192192
if line.conditional_expression:
193193
if not merge_eval(line.conditional_expression):
194194
return ''
@@ -204,7 +204,10 @@ def merge(match):
204204
field_val = EXPRESSION_PATTERN.sub(merge, line.expression)
205205
else:
206206
field_val = line.fixed_value
207-
val += self._export_simple_record(line, field_val)
207+
record = self._export_simple_record(line, field_val)
208+
if isinstance(record, str):
209+
record = record.encode('iso-8859-1')
210+
val += record
208211
return val
209212

210213
@api.multi

0 commit comments

Comments
 (0)