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

Make transaction details optional #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/ofxstatement/plugins/mt940.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def parse_record(self, transaction: Transaction) -> StatementLine:
# Use str() to prevent rounding errors
bank_account_to = transaction.data.get('customer_reference')
amount = Decimal(str(transaction.data['amount'].amount))
memo = transaction.data['transaction_details']
memo = transaction.data['transaction_details'] if 'transaction_details' in transaction.data else transaction.data['purpose'] if 'purpose' in transaction.data else ''
memo = memo.replace("\n", '')
memo = memo.replace(transaction.data['customer_reference'], '', 1)
memo = memo.replace(transaction.data['extra_details'], '', 1).strip()
Expand Down
89 changes: 89 additions & 0 deletions tests/test_mt940.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
from ofxstatement.plugins.mt940 import Plugin, get_bank_id
from ofxstatement.exceptions import ValidationError

from mt940.models import Transaction
from mt940.models import Amount

from src.ofxstatement.plugins.mt940 import Parser


class ParserTest(TestCase):

# Integration tests:

def test_ASN(self):
# Create and configure parser:
here = os.path.dirname(__file__)
Expand Down Expand Up @@ -173,3 +180,85 @@ def test_end_date_derived_from_statements_not_set(self):

# And parse:
parser.parse().assert_valid()

# Unit tests:

def test_parse_record_parses_transaction_with_full_set_of_data(self):
transaction = Transaction(None)
transaction.update({
'customer_reference': '1234',
'extra_details': 'Jane Doe',
'amount': Amount('42', 'D'),
'transaction_details': 'Some details',
'date': datetime(2022, 2, 2).date(),
})

parser = Parser('', '', '')
statement_line = parser.parse_record(transaction)

self.assertEqual(statement_line.amount, Decimal('-42.00'))
self.assertEqual(statement_line.date, datetime.strptime("2022-02-02", parser.date_format).date())
self.assertEqual(statement_line.memo, 'Some details')
self.assertEqual(statement_line.payee, 'Jane Doe (1234)')

def test_parse_record_parses_transaction_with_only_mandatory_data(self):
transaction = Transaction(None)
transaction.update({
'customer_reference': '1234',
'extra_details': 'Jane Doe',
'amount': Amount('42', 'D'),
'date': datetime(2022, 2, 2).date(),
})

parser = Parser('', '', '')
statement_line = parser.parse_record(transaction)

self.assertEqual(statement_line.amount, Decimal('-42.00'))
self.assertEqual(statement_line.date, datetime.strptime("2022-02-02", parser.date_format).date())
self.assertEqual(statement_line.memo, 'UNKNOWN')
self.assertEqual(statement_line.payee, 'Jane Doe (1234)')

def test_parse_record_uses_purpose_if_given_and_transaction_details_are_missing(self):
transaction = Transaction(None)
transaction.update({
'customer_reference': '1234',
'extra_details': 'Jane Doe',
'amount': Amount('42', 'D'),
'purpose': 'Some purpose',
'date': datetime(2022, 2, 2).date(),
})

parser = Parser('', '', '')
statement_line = parser.parse_record(transaction)

self.assertEqual(statement_line.memo, 'Some purpose')

def test_parse_record_removes_payee_from_memo(self):
transaction = Transaction(None)
transaction.update({
'customer_reference': '1234',
'extra_details': 'Jane Doe',
'amount': Amount('42', 'D'),
'transaction_details': 'Some details Jane Doe 1234',
'date': datetime(2022, 2, 2).date(),
})

parser = Parser('', '', '')
statement_line = parser.parse_record(transaction)

self.assertEqual(statement_line.memo, 'Some details')

def test_parse_record_skips_zero_value_transactions(self):
transaction = Transaction(None)
transaction.update({
'customer_reference': '1234',
'extra_details': 'Jane Doe',
'amount': Amount('0', 'C'),
'transaction_details': 'Some details',
'date': datetime(2022, 2, 2).date(),
})

parser = Parser('', '', '')
statement_line = parser.parse_record(transaction)

self.assertIsNone(statement_line)