Skip to content

Commit

Permalink
fixed tests to be independent of string representations
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Mar 7, 2020
1 parent 16ea702 commit 9157791
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 49 deletions.
17 changes: 13 additions & 4 deletions mt940/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@


class Model(object):
pass

def __repr__(self):
return '<%s>' % self.__class__.__name__


class FixedOffset(datetime.tzinfo):
Expand Down Expand Up @@ -166,10 +168,14 @@ def __init__(self, amount, status, currency=None, **kwargs):
if status == 'D':
self.amount = -self.amount

def __eq__(self, other):
return self.amount == other.amount and self.currency == other.currency

def __str__(self):
return '%s %s' % (self.amount, self.currency)

def __repr__(self):
return '<%s %s>' % (
self.amount,
self.currency, )
return '<%s>' % self


class SumAmount(Amount):
Expand Down Expand Up @@ -212,6 +218,9 @@ def __init__(self, status=None, amount=None, date=None, **kwargs):
self.amount = amount
self.date = date

def __eq__(self, other):
return self.amount == other.amount and self.status == other.status

def __repr__(self):
return '<%s>' % self

Expand Down
6 changes: 2 additions & 4 deletions mt940/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,15 @@ class StatementASNB(Statement):
From: https://www.sepaforcorporates.com/swift-for-corporates
Pattern: 6!n[4!n]2a[1!a]15d1!a3!c16x[//16x]
[34x]
[34x]
But ASN bank puts the IBAN in the customer reference, which is acording to
Wikipedia at most 34 characters.
So this is the new pattern:
Pattern: 6!n[4!n]2a[1!a]15d1!a3!c34x[//16x]
[34x]
[34x]
'''
pattern = r'''^
(?P<year>\d{2}) # 6!n Value Date (YYMMDD)
Expand Down
4 changes: 3 additions & 1 deletion mt940_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@


def pytest_configure(config):
# Note: enable DEBUG logging to debug the parsing. But this becomes very
# verbose very quickly
logging.basicConfig(
level=LOG_LEVELS.get(config.option.verbose, logging.DEBUG))
level=LOG_LEVELS.get(config.option.verbose, logging.INFO))

107 changes: 67 additions & 40 deletions mt940_tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import mt940
from mt940.tags import Tag, StatementASNB
from mt940 import tags
from mt940 import models
import pprint


Expand All @@ -10,7 +11,7 @@ def long_statement_number():
return fh.read()


class MyStatementNumber(Tag):
class MyStatementNumber(tags.Tag):

'''Statement number / sequence number
Expand Down Expand Up @@ -38,54 +39,80 @@ def ASNB_mt940_data():


def test_ASNB_tags(ASNB_mt940_data):
tag_parser = StatementASNB()
tag_parser = tags.StatementASNB()
trs = mt940.models.Transactions(tags={
tag_parser.id: tag_parser
})

trs.parse(ASNB_mt940_data)
trs_data = pprint.pformat(trs.data, sort_dicts=False)
assert trs_data == """{'transaction_reference': '0000000000',
'account_identification': 'NL81ASNB9999999999',
'statement_number': '31',
'sequence_number': '1',
'final_opening_balance': <<404.81 EUR> @ 2020-01-31>,
'final_closing_balance': <<501.23 EUR> @ 2020-01-31>}"""

assert trs.data == {
'account_identification': 'NL81ASNB9999999999',
'transaction_reference': '0000000000',
'statement_number': '31',
'sequence_number': '1',
'final_opening_balance': models.Balance(
status='C',
amount=models.Amount('404.81', 'C', 'EUR'),
date=models.Date(2020, 1, 31),
),
'final_closing_balance': models.Balance(
status='C',
amount=models.Amount('501.23', 'C', 'EUR'),
date=models.Date(2020, 1, 31),
),
}
assert len(trs) == 8
# test first entry
td = trs.transactions[0].data.pop('transaction_details')
assert pprint.pformat(trs.transactions[0].data, sort_dicts=False) == \
"""{'status': 'D',
'funds_code': None,
'amount': <-65.00 EUR>,
'id': 'NOVB',
'customer_reference': 'NL47INGB9999999999',
'bank_reference': None,
'extra_details': 'hr gjlm paulissen',
'currency': 'EUR',
'date': Date(2020, 1, 1),
'entry_date': Date(2020, 1, 1),
'guessed_entry_date': Date(2020, 1, 1)}"""

pprint.pprint(trs.data)
pprint.pprint(trs.data['final_opening_balance'])
pprint.pprint(type(trs.data['final_opening_balance']))
pprint.pprint(trs.data['final_opening_balance'].__dict__)

assert trs.transactions[0].data == {
'status': 'D',
'funds_code': None,
'amount': models.Amount('65.00', 'D', 'EUR'),
'id': 'NOVB',
'customer_reference': 'NL47INGB9999999999',
'bank_reference': None,
'extra_details': 'hr gjlm paulissen',
'currency': 'EUR',
'date': models.Date(2020, 1, 1),
'entry_date': models.Date(2020, 1, 1),
'guessed_entry_date': models.Date(2020, 1, 1),
}

assert td == 'NL47INGB9999999999 hr gjlm paulissen\nBetaling sieraden'
assert str(trs.transactions[1].data['amount']) == '<1000.00 EUR>'
assert str(trs.transactions[2].data['amount']) == '<-801.55 EUR>'
assert str(trs.transactions[3].data['amount']) == '<-1.65 EUR>'
assert str(trs.transactions[4].data['amount']) == '<828.72 EUR>'
assert str(trs.transactions[5].data['amount']) == '<-1000.00 EUR>'
assert str(trs.transactions[6].data['amount']) == '<1000.18 EUR>'
assert trs.transactions[1].data['amount'] == models.Amount(
'1000.00', 'C', 'EUR')
assert trs.transactions[2].data['amount'] == models.Amount(
'801.55', 'D', 'EUR')
assert trs.transactions[3].data['amount'] == models.Amount(
'1.65', 'D', 'EUR')
assert trs.transactions[4].data['amount'] == models.Amount(
'828.72', 'C', 'EUR')
assert trs.transactions[5].data['amount'] == models.Amount(
'1000.00', 'D', 'EUR')
assert trs.transactions[6].data['amount'] == models.Amount(
'1000.18', 'C', 'EUR')

td = trs.transactions[7].data.pop('transaction_details')
assert pprint.pformat(trs.transactions[7].data, sort_dicts=False) == \
"""{'status': 'D',
'funds_code': None,
'amount': <-903.76 EUR>,
'id': 'NIDB',
'customer_reference': 'NL08ABNA9999999999',
'bank_reference': None,
'extra_details': 'international card services',
'currency': 'EUR',
'date': Date(2020, 1, 31),
'entry_date': Date(2020, 1, 31),
'guessed_entry_date': Date(2020, 1, 31)}"""
assert trs.transactions[7].data == {
'status': 'D',
'funds_code': None,
'amount': models.Amount('903.76', 'D', 'EUR'),
'id': 'NIDB',
'customer_reference': 'NL08ABNA9999999999',
'bank_reference': None,
'extra_details': 'international card services',
'currency': 'EUR',
'date': models.Date(2020, 1, 31),
'entry_date': models.Date(2020, 1, 31),
'guessed_entry_date': models.Date(2020, 1, 31),
}
assert td[0:46] == 'NL08ABNA9999999999 international card services'
assert td[47:112] == \
'000000000000000000000000000000000 0000000000000000 Betaling aan I'
Expand Down

0 comments on commit 9157791

Please sign in to comment.