From 8790fd59e6b1d1048ec53f0c5ec90cdd8465ab81 Mon Sep 17 00:00:00 2001 From: antonioburic Date: Wed, 23 Mar 2022 15:10:26 +0100 Subject: [PATCH] [IMP] l10n_nl_xaf_auditfile_export: include lines from inactive journals. [IMP] l10n_nl_xaf_auditfile_export: adding a simple test to include lines from inactive journals. --- .../models/xaf_auditfile_export.py | 7 +-- .../test_l10n_nl_xaf_auditfile_export.py | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py b/l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py index 6279ec113..8dedf3aaa 100644 --- a/l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py +++ b/l10n_nl_xaf_auditfile_export/models/xaf_auditfile_export.py @@ -287,9 +287,10 @@ def get_move_line_total_credit(self): @api.multi def get_journals(self): '''return journals''' - return self.env['account.journal'].search([ - ('company_id', '=', self.company_id.id), - ]) + return self.env['account.journal'].with_context( + active_test=False).search([ + ('company_id', '=', self.company_id.id), + ]) @api.multi def get_moves(self, journal): diff --git a/l10n_nl_xaf_auditfile_export/tests/test_l10n_nl_xaf_auditfile_export.py b/l10n_nl_xaf_auditfile_export/tests/test_l10n_nl_xaf_auditfile_export.py index 4d6afae4b..26a0f800b 100644 --- a/l10n_nl_xaf_auditfile_export/tests/test_l10n_nl_xaf_auditfile_export.py +++ b/l10n_nl_xaf_auditfile_export/tests/test_l10n_nl_xaf_auditfile_export.py @@ -2,6 +2,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import base64 +from lxml import etree from io import BytesIO import os from zipfile import ZipFile @@ -10,6 +11,31 @@ from odoo.tools import mute_logger +def get_transaction_line_count_from_xml(auditfile): + ''' Helper XML method to parse and return the transaction line count ''' + line_count = 0 + with ZipFile(BytesIO(base64.b64decode(auditfile)), 'r') as z: + contents = z.read(z.filelist[-1]).decode() + parser = etree.XMLParser( + ns_clean=True, + recover=True, + encoding="utf-8", + remove_blank_text=True + ) + root = etree.XML(bytes(contents, encoding='utf8'), parser=parser) + # xpath query to select all element nodes in namespace + # Source: https://stackoverflow.com/a/30233635 + query = "descendant-or-self::*[namespace-uri()!='']" + for element in root.xpath(query): + element.tag = etree.QName(element).localname + journals = root.xpath("/auditfile/company/transactions/journal") + for journal in journals: + transactions = journal.xpath("transaction/trLine") + for _ in transactions: + line_count += 1 + return line_count + + class TestXafAuditfileExport(TransactionCase): def test_01_default_values(self): @@ -97,3 +123,26 @@ def test_05_export_success(self): record.name += '%s01' % os.sep record.button_generate() self.assertTrue(record) + + def test_06_include_moves_from_inactive_journals(self): + ''' Include moves off of inactive journals ''' + record = self.env['xaf.auditfile.export'].create({}) + record.button_generate() + self.assertTrue(record) + + line_count = record.get_move_line_count() + parsed_line_count = get_transaction_line_count_from_xml(record.auditfile) + self.assertTrue(parsed_line_count == line_count) + + # archive all journals + all_journals = record.get_journals() + all_journals.write({'active': False}) + self.assertTrue(all(not j.active for j in all_journals)) + + record_after = self.env['xaf.auditfile.export'].create({}) + record_after.button_generate() + self.assertTrue(record_after) + + line_count_after = record_after.get_move_line_count() + parsed_count_after = get_transaction_line_count_from_xml(record_after.auditfile) + self.assertTrue(parsed_line_count == parsed_count_after == line_count_after)