Skip to content

Commit

Permalink
[FIX] l10n_es_aeat: Bytestring concatenation on file export to boe (S…
Browse files Browse the repository at this point in the history
…olves compatibility problems with python3) (#1)

* Adds tests for exporting files to boe with an export config (#2)
  • Loading branch information
hugosantosred authored and pedrobaeza committed Mar 21, 2024
1 parent 21492b6 commit 002f258
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
1 change: 1 addition & 0 deletions l10n_es_aeat/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from . import test_l10n_es_aeat
from . import test_l10n_es_aeat_report
from . import test_l10n_es_aeat_export_config
108 changes: 108 additions & 0 deletions l10n_es_aeat/tests/test_l10n_es_aeat_export_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# -*- coding: utf-8 -*-
# © 2017 FactorLibre - Hugo Santos <[email protected]>
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0
from odoo.tests import common


class TestL10nEsAeatExportConfig(common.TransactionCase):

def setUp(self):
super(TestL10nEsAeatExportConfig, self).setUp()

def test_onchange_export_config_line(self):
export_config_line_env = self.env['aeat.model.export.config.line']
with self.env.do_in_onchange():
export_line_float = export_config_line_env.new()
export_line_float.export_type = 'float'
export_line_float.onchange_type()
self.assertEqual(export_line_float.alignment, 'right',
'Export type float must be aligned to the right')
export_line_str = export_config_line_env.new()
export_line_str.export_type = 'string'
export_line_str.onchange_type()
self.assertEqual(export_line_str.alignment, 'left',
'Export type string must be aligned to the left')
export_line_subtype = export_config_line_env.new()
export_line_subtype.alignment = 'left'
export_line_subtype.decimal_size = 10
export_line_subtype.apply_sign = True
export_line_subtype.subconfig_id = export_line_str.id
export_line_subtype.onchange_subconfig()
self.assertFalse(export_line_subtype.alignment,
'Alignment must be False for a subtype line')
self.assertEqual(export_line_subtype.decimal_size, 0)
self.assertFalse(export_line_subtype.apply_sign,
'Apply sign must be False for a subtype line')

def test_export_config_file(self):
export_config = self.env['aeat.model.export.config'].create({
'name': 'Test Export Config',
'model_number': '000',
'config_line_ids': [
(0, 0, {
'sequence': 1,
'name': '<T',
'fixed_value': '<T',
'export_type': 'string',
'size': 3,
'alignment': 'left'
}),
(0, 0, {
'sequence': 2,
'name': 'Empty spaces',
'fixed_value': '',
'export_type': 'string',
'size': 10,
'alignment': 'left'
}),
(0, 0, {
'sequence': 3,
'name': 'Integer Value',
'fixed_value': 1,
'export_type': 'integer',
'size': 3,
'alignment': 'right'
}),
(0, 0, {
'sequence': 4,
'name': 'Float Value',
'fixed_value': 15.0,
'export_type': 'float',
'size': 6,
'decimal_size': 2,
'alignment': 'right'
}),
(0, 0, {
'sequence': 5,
'name': 'Boolean True Value',
'expression': True,
'export_type': 'boolean',
'size': 2,
'alignment': 'left'
}),
(0, 0, {
'sequence': 6,
'name': 'Boolean False Value',
'expression': False,
'export_type': 'boolean',
'size': 2,
'alignment': 'left'
}),
(0, 0, {
'sequence': 7,
'name': '>',
'fixed_value': '>',
'size': 1,
'alignment': 'left'
})
]
})
new_report = self.env['l10n.es.aeat.report'].new({
'name': 'Test Report'
})
export_to_boe = self.env['l10n.es.aeat.report.export_to_boe'].create({
'name': 'test_export_to_boe.txt'
})
export_file = export_to_boe._export_config(
new_report, export_config)
self.assertEqual(b'<T 001001500X >', export_file)
11 changes: 7 additions & 4 deletions l10n_es_aeat/wizard/export_to_boe.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def action_get_file(self):
if not active_id or not active_model:
return False
report = self.env[active_model].browse(active_id)
contents = ''
contents = b''
if report.export_config_id:
contents += self.action_get_file_from_config(report)
else:
Expand Down Expand Up @@ -165,7 +165,7 @@ def action_get_file_from_config(self, report):
@api.multi
def _export_config(self, obj, export_config):
self.ensure_one()
contents = ''
contents = b''
for line in export_config.config_line_ids:
contents += self._export_line_process(obj, line)
return contents
Expand All @@ -188,7 +188,7 @@ def merge(match):
result = merge_eval(exp)
return result and tools.ustr(result) or ''

val = ''
val = b''
if line.conditional_expression:
if not merge_eval(line.conditional_expression):
return ''
Expand All @@ -204,7 +204,10 @@ def merge(match):
field_val = EXPRESSION_PATTERN.sub(merge, line.expression)
else:
field_val = line.fixed_value
val += self._export_simple_record(line, field_val)
record = self._export_simple_record(line, field_val)
if isinstance(record, str):
record = record.encode('iso-8859-1')
val += record
return val

@api.multi
Expand Down

0 comments on commit 002f258

Please sign in to comment.