changeset 32:d14f6f06f270

Do not fail on optional balances bug10
author Cédric Krier <ced@b2ck.com>
date Mon, 16 Dec 2019 14:55:28 +0100
parents 55d9a0aee0e3
children 793ad42f4372
files CHANGELOG mt940/MT940-optional.txt mt940/__init__.py mt940/test.py setup.py
diffstat 5 files changed, 43 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGELOG	Sat Sep 29 22:52:19 2018 +0200
+++ b/CHANGELOG	Mon Dec 16 14:55:28 2019 +0100
@@ -1,3 +1,5 @@
+* Do not fail on optional balances
+
 Version 0.4 - 2017-07-05
 * Allow to use file-like object
 * Allow to define file encoding
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mt940/MT940-optional.txt	Mon Dec 16 14:55:28 2019 +0100
@@ -0,0 +1,13 @@
+ABNANL2A
+940
+ABNANL2A
+:20:ABN AMRO BANK NV
+:25:123456789
+:28:13501/1
+:61:1205120514C500,01N654NONREF
+987654321
+:61:1412051205RC15,67IDXXREF
+DATA
+:61:171214C15,67TIDXTEST//REFERENCE
+:86:/SUM/
+-
--- a/mt940/__init__.py	Sat Sep 29 22:52:19 2018 +0200
+++ b/mt940/__init__.py	Mon Dec 16 14:55:28 2019 +0100
@@ -1,8 +1,8 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (c) 2013-2016, Cédric Krier
+# Copyright (c) 2013-2018, Cédric Krier
 # Copyright (c) 2014-2017, Nicolas Évrard
-# Copyright (c) 2013-2017, B2CK
+# Copyright (c) 2013-2018, B2CK
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -85,8 +85,6 @@
 
     def _parse(self, f):
         values = defaultdict(str)
-        # Set optional values
-        values['description']
         transactions = []
         for line in self._readline(f):
             for name, sections in SECTIONS.iteritems():
@@ -159,12 +157,15 @@
             institution_reference, additional_data, '')
 
     def _set_statement(self, values, transactions):
+        # Set optional values
+        values.setdefault('start_balance')
+        values.setdefault('end_balance')
+        values.setdefault('description')
         self.statements.append(
             Statement(
                 transactions=[Transaction(*t) for t in transactions],
                 **values))
         values.clear()
-        values['description']
         del transactions[:]
 
 
--- a/mt940/test.py	Sat Sep 29 22:52:19 2018 +0200
+++ b/mt940/test.py	Mon Dec 16 14:55:28 2019 +0100
@@ -119,6 +119,27 @@
                 os.path.join(here, 'MT940.txt'), encoding='ascii'))
 
 
+class TestMT940Optional(unittest.TestCase):
+
+    def setUp(self):
+        self.mt940 = MT940(os.path.join(here, 'MT940-optional.txt'))
+
+    def test_statement_start_balance(self):
+        "Test statement has not start balance"
+        start_balance = self.mt940.statements[0].start_balance
+        self.assertEqual(start_balance, None)
+
+    def test_statement_end_balance(self):
+        "Test statement has no end balance"
+        end_balance = self.mt940.statements[0].end_balance
+        self.assertEqual(end_balance, None)
+
+    def test_statement_description(self):
+        "Test statement has no description"
+        description = self.mt940.statements[0].description
+        self.assertEqual(description, None)
+
+
 class TestRaboDescription(unittest.TestCase):
 
     def test_one_tag(self):
--- a/setup.py	Sat Sep 29 22:52:19 2018 +0200
+++ b/setup.py	Mon Dec 16 14:55:28 2019 +0100
@@ -51,7 +51,7 @@
     long_description=read('README'),
     packages=find_packages(),
     package_data={
-        'mt940': ['MT940.txt'],
+        'mt940': ['MT940.txt', 'MT940-optional.txt'],
         },
     classifiers=[
         'Intended Audience :: Developers',