Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[18.0][MIG] account_statement_import_camt: Migration to 18.0 #737

Open
wants to merge 22 commits into
base: 18.0
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1a357c4
[MIG][14.0] account_statement_import_camt_oca
i-vyshnevska Jan 11, 2021
71c5d79
[14.0][FIX]camt import
luc-demeyer Jan 17, 2021
d958447
[FIX] Fix same eror in camt54 module
StefanRijnhart Feb 2, 2021
f91ba7c
account_statement_import_camt 14.0.1.1.0
OCA-git-bot Feb 3, 2021
7e80547
Translated using Weblate (Dutch)
bosd Apr 23, 2021
f40ca8d
Added translation using Weblate (Italian)
primes2h Nov 25, 2021
aec6f1c
Translated using Weblate (Italian)
primes2h Nov 25, 2021
d722392
[MIG] account_statement_import_camt: Migration to 15.0
yankinmax Jan 26, 2022
592745f
[IMP] account_statement_import_camt: look for currency under Ntry
StefanRijnhart Feb 1, 2022
ab70506
[IMP] account_bank_statement_import_camt: more infos in narration
TeoGoddet Jan 10, 2022
fab58a2
account_statement_import_camt - fix counterparty data
luc-demeyer Feb 13, 2023
c89b7c7
fix camt unit tests
luc-demeyer Feb 13, 2023
44c19bd
[16.0][MIG] account_statement_import_camt: Migrate to version 16.0
Dec 2, 2022
26f56a8
Translated using Weblate (Spanish)
Ivorra78 Jul 29, 2023
34f44cc
Translated using Weblate (Spanish)
Ivorra78 Oct 10, 2023
8b5c817
Translated using Weblate (Italian)
mymage Feb 7, 2024
d2a4539
[IMP] account_statement_import_camt: refactor tests for inheritability
SilvioC2C Mar 15, 2024
b161e77
[BOT] post-merge updates
OCA-git-bot Mar 27, 2024
02de637
[IMP] account_statement_import_camt: pre-commit auto fixes
hbrunn Apr 22, 2024
c8bbb4f
[MIG] account_statement_import_camt: Migration to 17.0
hbrunn Apr 22, 2024
b39bdad
[IMP] account_statement_import_camt: pre-commit auto fixes
xaviedoanhduy Nov 7, 2024
b686079
[MIG] account_statement_import_camt: Migration to 18.0
xaviedoanhduy Nov 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[IMP] account_statement_import_camt: refactor tests for inheritability
This refactoring will allow using `TestParser._do_parse_test()` method for subclasses defined in inheriting modules.

This way, if the new module defines new files in other paths than `account_statement_import_camt/test_files/`,
 subclasses of `TestParser` will still be able to use the `_do_parse_test()` method for imports and checks.

Moreover, a custom value can now be defined for the acceptable differing lines number between inputfile and goldenfile.

It will also maintain backward compatibility with the current usage of the method
 (ie: using only a filename with the assumption of finding it in `account_statement_import_camt/test_files/`).

NB: methods are now defined outside of `TestParser` to allow importing them directly, instead of inheriting from `TestParser`.
  • Loading branch information
SilvioC2C authored and xaviedoanhduy committed Dec 17, 2024
commit d2a4539dc319e4da50a0d69da17eeca2cb3b87c7
88 changes: 63 additions & 25 deletions account_statement_import_camt/tests/test_import_bank_statement.py
Original file line number Diff line number Diff line change
@@ -7,42 +7,80 @@
import pprint
import tempfile
from datetime import date
from pathlib import Path

from odoo.modules.module import get_module_resource
from odoo.tests.common import TransactionCase


class TestParser(TransactionCase):
"""Tests for the camt parser itself."""

class TestParserCommon(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.parser = cls.env["account.statement.import.camt.parser"]

def _do_parse_test(self, inputfile, goldenfile):
testfile = get_module_resource(
"account_statement_import_camt", "test_files", inputfile
def _do_parse_test(self, inputfile, goldenfile, max_diff_count=None):
"""Imports ``inputfile`` and confronts its output against ``goldenfile`` data

An AssertionError is raised if max_diff_count < 0

:param inputfile: file to import and test
:type inputfile: Path or str
:param goldenfile: file to use for comparison (the expected values)
:type goldenfile: Path or str
:param max_diff_count: maximum nr of lines that can differ (default: 2)
:type max_diff_count: int
"""
max_diff_count = max_diff_count or 2
assert max_diff_count >= 0
diff = self._get_files_diffs(*map(self._to_filepath, (inputfile, goldenfile)))
self.assertLessEqual(
len(diff),
max_diff_count,
f"Actual output doesn't match expected output:\n{''.join(diff)}",
)
with open(testfile, "rb") as data:
res = self.parser.parse(data.read())
with tempfile.NamedTemporaryFile(mode="w+", suffix=".pydata") as temp:
pprint.pprint(res, temp, width=160)
goldenfile_res = get_module_resource(
"account_statement_import_camt", "test_files", goldenfile
)
with open(goldenfile_res, "r") as golden:
temp.seek(0)
diff = list(
difflib.unified_diff(
golden.readlines(), temp.readlines(), golden.name, temp.name
)
)
if len(diff) > 2:
self.fail(
"actual output doesn't match expected "
+ "output:\n%s" % "".join(diff)
)

def _get_files_diffs(self, inputfile_path, goldenfile_path) -> list:
"""Creates diffs between ``inputfile_path`` and ``goldenfile_path`` data

:param inputfile_path: path for file to import and test
:type inputfile_path: Path
:param goldenfile_path: path for file to use for comparison (the expected values)
:type goldenfile_path: Path
"""

# Read the input file, store the actual imported values
with open(get_module_resource(*inputfile_path.parts), "rb") as inputf:
res = self.parser.parse(inputf.read())
# Read the output file, store the expected imported values
with open(get_module_resource(*goldenfile_path.parts), "r") as goldf:
gold_name, gold_lines = goldf.name, goldf.readlines()
# Save the imported values in a tmp file to compare them w/ the expected values
with tempfile.NamedTemporaryFile(mode="w+", suffix=".pydata") as tempf:
pprint.pprint(res, tempf, width=160)
tempf.seek(0)
out_name, out_lines = tempf.name, tempf.readlines()
# Return a list of diffs
return list(difflib.unified_diff(gold_lines, out_lines, gold_name, out_name))

def _to_filepath(self, file):
"""Converts ``obj`` to a ``pathlib.Path`` object

For backward compatibility: allows ``obj`` to be just a string representing only
a filename, and this method will convert it to filepath for files inside the
``test_files`` folder (this behavior was previously hardcoded in method
``TestParser._do_parse_test()``)

:param obj: the object to convert
:type obj: Path or str
"""
if isinstance(file, str) and len(Path(file).parts) == 1:
file = Path("account_statement_import_camt") / "test_files" / file
return Path(file)


class TestParser(TestParserCommon):
"""Tests for the camt parser itself."""

def test_parse(self):
self._do_parse_test("test-camt053", "golden-camt053.pydata")