From 6bc18a968f86f7f575e1b077e811511ea7844268 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Fri, 17 Jan 2020 16:34:26 +0100 Subject: [PATCH] Add tests to bpvf processor --- ofx_processor/bpvf_processor/main.py | 33 ++++++++++++++-------- tests/test_bpvf_processor.py | 42 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 tests/test_bpvf_processor.py diff --git a/ofx_processor/bpvf_processor/main.py b/ofx_processor/bpvf_processor/main.py index 50d1bc3..e5567bb 100644 --- a/ofx_processor/bpvf_processor/main.py +++ b/ofx_processor/bpvf_processor/main.py @@ -8,6 +8,26 @@ from ofxtools.Parser import OFXTree from ofxtools.header import make_header +def _process_name_and_memo(name, memo): + if "CB****" in name: + conversion = re.compile(r"\d+,\d{2}[a-zA-Z]{3}") + match = conversion.search(memo) + if match: + res_name = memo[: match.start() - 1] + res_memo = name + memo[match.start() - 1 :] + else: + res_name = memo + res_memo = name + + return res_name, res_memo, True + + return name, memo, False + + +def process_name_and_memo(transaction): + return _process_name_and_memo(transaction.name, transaction.memo) + + @click.command() @click.argument("ofx_filename") def cli(ofx_filename): @@ -25,18 +45,9 @@ def cli(ofx_filename): sys.exit(1) for transaction in ofx.statements[0].transactions: - if "CB****" in transaction.name: - name = transaction.name - memo = transaction.memo - conversion = re.compile(r"\d+,\d{2}[a-zA-Z]{3}") - match = conversion.search(memo) - if match: - transaction.name = memo[: match.start() - 1] - transaction.memo = name + memo[match.start() - 1 :] - else: - transaction.name = memo - transaction.memo = name + transaction.name, transaction.memo, edited = process_name_and_memo(transaction) + if edited: click.secho( "Edited transaction {} ({})".format( transaction.checknum, transaction.name diff --git a/tests/test_bpvf_processor.py b/tests/test_bpvf_processor.py new file mode 100644 index 0000000..9dd8d53 --- /dev/null +++ b/tests/test_bpvf_processor.py @@ -0,0 +1,42 @@ +import unittest + +from ofx_processor.bpvf_processor.main import _process_name_and_memo + + +class MyTestCase(unittest.TestCase): + def test_process_name_and_memo_no_change(self): + name = "business" + memo = "2020-01-17" + + result_name, result_memo, edited = _process_name_and_memo(name, memo) + self.assertFalse(edited) + self.assertEqual(result_name, name) + self.assertEqual(result_memo, memo) + + def test_process_name_and_memo_change_required_with_conversion(self): + name = "150120 CB****5874" + memo = "GUY AND SONS FR LYON 0,90EUR 1 EURO = 1,000000" + + expected_name = "GUY AND SONS FR LYON" + expected_memo = "150120 CB****5874 0,90EUR 1 EURO = 1,000000" + + result_name, result_memo, edited = _process_name_and_memo(name, memo) + self.assertTrue(edited) + self.assertEqual(result_name, expected_name) + self.assertEqual(result_memo, expected_memo) + + def test_process_name_and_memo_change_required_no_conversion(self): + name = "150120 CB****5874" + memo = "Dott 75PARIS" + + expected_name = "Dott 75PARIS" + expected_memo = "150120 CB****5874" + + result_name, result_memo, edited = _process_name_and_memo(name, memo) + self.assertTrue(edited) + self.assertEqual(result_name, expected_name) + self.assertEqual(result_memo, expected_memo) + + +if __name__ == "__main__": + unittest.main()