Add tests to bpvf processor

This commit is contained in:
Gabriel Augendre 2020-01-17 16:34:26 +01:00
parent 588cfc56e7
commit 6bc18a968f
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
2 changed files with 64 additions and 11 deletions

View file

@ -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

View file

@ -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()