From 4ab6b24deac09260f0de9b84dd17b72e3e915479 Mon Sep 17 00:00:00 2001 From: Hugo Santos Date: Tue, 14 Nov 2017 09:15:53 +0100 Subject: [PATCH] [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) --- l10n_es_aeat/tests/__init__.py | 1 + .../tests/test_l10n_es_aeat_export_config.py | 108 ++++++++++++++++++ l10n_es_aeat/wizard/export_to_boe.py | 11 +- 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 l10n_es_aeat/tests/test_l10n_es_aeat_export_config.py diff --git a/l10n_es_aeat/tests/__init__.py b/l10n_es_aeat/tests/__init__.py index d29d2880c5e..855436978bc 100644 --- a/l10n_es_aeat/tests/__init__.py +++ b/l10n_es_aeat/tests/__init__.py @@ -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 diff --git a/l10n_es_aeat/tests/test_l10n_es_aeat_export_config.py b/l10n_es_aeat/tests/test_l10n_es_aeat_export_config.py new file mode 100644 index 00000000000..295afc282dd --- /dev/null +++ b/l10n_es_aeat/tests/test_l10n_es_aeat_export_config.py @@ -0,0 +1,108 @@ +# -*- coding: utf-8 -*- +# © 2017 FactorLibre - Hugo Santos +# 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': '', + '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'', export_file) diff --git a/l10n_es_aeat/wizard/export_to_boe.py b/l10n_es_aeat/wizard/export_to_boe.py index bef888247eb..2afb880966b 100644 --- a/l10n_es_aeat/wizard/export_to_boe.py +++ b/l10n_es_aeat/wizard/export_to_boe.py @@ -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: @@ -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 @@ -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 '' @@ -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