Skip to content

Commit e5b70ca

Browse files
author
Hong-Thai Nguyen
committed
Fix date parsing, ammount parsing
1 parent 5a25e3a commit e5b70ca

File tree

4 files changed

+46
-172
lines changed

4 files changed

+46
-172
lines changed

cfonb/tests/test_statement.py

Lines changed: 20 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,36 @@
11
# python import - http://docs.python.org/library/unittest.html
22
import unittest
3+
from datetime import date
34
from io import StringIO
45

5-
# import cfonb module
6-
from cfonb.parser import Row, statement as p
6+
from nose.tools import assert_is_not_none, assert_equal
77

8-
HEAD_LINE = '0130002 00447 0000888899H 160811 0000000132303H '
9-
10-
11-
HEAD_VALUES = [
12-
'01',
13-
'30002',
14-
' ',
15-
'00447',
16-
' ',
17-
' ',
18-
' ',
19-
'0000888899H',
20-
' ',
21-
'160811',
22-
' ',
23-
'0000000132303H',
24-
' ',
25-
]
26-
27-
28-
CONTENT_4_LINE = '0430002013400447EUR2E0000431040H21210811 210811VIREMENT COMBELL 0000000 0000000020000} '
29-
30-
CONTENT_4_VALUES = [
31-
'04',
32-
'30002',
33-
'0134',
34-
'00447',
35-
'EUR',
36-
'2',
37-
'E',
38-
'0000431040H',
39-
'21',
40-
'210811',
41-
' ',
42-
'210811',
43-
'VIREMENT COMBELL ',
44-
' ',
45-
'0000000',
46-
' ',
47-
' ',
48-
'0000000020000}',
49-
' ',
50-
]
51-
52-
53-
FOOT_LINE = '0730002 00447 0000888899H 280911 0000000118711D '
54-
55-
56-
FOOT_VALUES = [
57-
'07',
58-
'30002',
59-
' ',
60-
'00447',
61-
' ',
62-
' ',
63-
' ',
64-
'0000888899H',
65-
' ',
66-
'280911',
67-
' ',
68-
'0000000118711D',
69-
' ',
70-
]
8+
from cfonb import StatementReader
9+
from cfonb.writer.statement import Statement
7110

7211

7312
class TestStatement(unittest.TestCase):
7413

75-
def setUp(self):
76-
pass
77-
78-
def tearDown(self):
79-
pass
80-
81-
def test_parse_head(self):
82-
row = p.parse_head(HEAD_LINE)
83-
# little type check
84-
self.assertIsInstance(row, Row, 'invalid row type: %s' % type(row))
85-
# list copy for check
86-
list_1 = row.as_list()[:]
87-
list_2 = row.as_dict().values()[:]
88-
# sort list to be compared
89-
list_1.sort()
90-
list_2.sort()
91-
# list check
92-
self.assertEqual(list_1, list_2)
93-
# check dict and obj format
94-
for i in range(13):
95-
self.assertEqual(row.as_list()[i], getattr(row.as_obj(), p.HEAD_KEYS[i]))
96-
self.assertEqual(row.as_list()[i], HEAD_VALUES[i])
97-
98-
def test_parse_content_4(self):
99-
row = p.parse_content_4(CONTENT_4_LINE)
100-
# little type check
101-
self.assertIsInstance(row, Row, 'invalid row type: %s' % type(row))
102-
# list copy for check
103-
list_1 = row.as_list()[:]
104-
list_2 = row.as_dict().values()[:]
105-
# sort list to be compared
106-
list_1.sort()
107-
list_2.sort()
108-
# list check
109-
self.assertEqual(list_1, list_2)
110-
# check dict and obj format
111-
for i in range(19):
112-
self.assertEqual(row.as_list()[i], getattr(row.as_obj(), p.CONTENT_4_KEYS[i]))
113-
self.assertEqual(row.as_list()[i], CONTENT_4_VALUES[i])
114-
115-
def test_parse_footer(self):
116-
row = p.parse_footer(FOOT_LINE)
117-
# little type check
118-
self.assertIsInstance(row, Row, 'invalid row type: %s' % type(row))
119-
# list copy for check
120-
list_1 = row.as_list()[:]
121-
list_2 = row.as_dict().values()[:]
122-
# sort list to be compared
123-
list_1.sort()
124-
list_2.sort()
125-
# list check
126-
self.assertEqual(list_1, list_2)
127-
# check dict and obj format
128-
for i in range(13):
129-
self.assertEqual(row.as_list()[i], getattr(row.as_obj(), p.FOOT_KEYS[i]))
130-
self.assertEqual(row.as_list()[i], FOOT_VALUES[i])
131-
132-
def test_statement(self):
133-
# prepare file obj
134-
file_obj = StringIO()
135-
file_obj.write("%s\n" % HEAD_LINE)
136-
file_obj.write("%s\n" % CONTENT_4_LINE)
137-
file_obj.write("%s\n" % FOOT_LINE)
138-
file_obj.seek(0)
139-
# init statement
140-
statement = p.Statement()
141-
statement.parse(file_obj)
142-
# prepare values to compare
143-
header_line = p.parse_head(HEAD_LINE)
144-
content_line = p.parse_content_4(CONTENT_4_LINE)
145-
footer_line = p.parse_footer(FOOT_LINE)
146-
# some tests
147-
self.assertIsInstance(statement, p.Statement)
148-
self.assertIsInstance(statement.header, Row)
149-
self.assertIsInstance(statement.footer, Row)
150-
self.assertIsInstance(statement.lines, list)
151-
self.assertEqual(len(statement.lines), 1)
152-
self.assertIsInstance(statement.lines[0], Row)
153-
self.assertEqual(statement.header, header_line)
154-
self.assertEqual(statement.footer, footer_line)
155-
self.assertEqual(statement.lines[0], content_line)
14+
def test_render_parse_cfonb(self):
15+
content = Statement().header('20002', '90005', 'EUR', '01711467640', date(2011, 10, 14), 12345.67)\
16+
.add('20002', '1234567', '90005', 'EUR', '01711467640', date(2011, 10, 14), 'label 1', 1234.56, 'reference1')\
17+
.add('20002', '1234567', '90005', 'EUR', '01711467640', date(2011, 10, 13), 'label 2', 123.45, 'reference2')\
18+
.render()
19+
print('content: {}'.format(content))
20+
reader = StatementReader()
21+
result = reader.parse(StringIO(content))
22+
for account in result:
23+
assert_is_not_none(account.account_nb)
24+
assert_equal(account.header.get('account_nb'), '01711467640')
25+
assert_equal(account.header.get('currency_code'), 'EUR')
26+
assert_equal(account.header.get('nb_of_dec'), '2')
27+
assert_equal(account.header.get('bank_code'), '20002')
28+
print('{}'.format(account.header))
15629

15730

15831
def suite():
15932
suite = unittest.TestSuite()
160-
suite.addTest(TestStatement('test_parse_head'))
161-
suite.addTest(TestStatement('test_parse_content_4'))
162-
suite.addTest(TestStatement('test_parse_footer'))
163-
suite.addTest(TestStatement('test_statement'))
33+
suite.addTest(TestStatement('test_parse_cfonb'))
16434
return suite
16535

16636

cfonb/writer/common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
BR_LINE = '\r\n'
2+
13

24
def write(input_, length, rpad=False, fill_char=' '):
35
input_ = str(input_)
@@ -19,4 +21,4 @@ def save(header, body, footer, filename=None):
1921

2022

2123
def date_format(date):
22-
return date.strftime(format='%d%m') + date.strftime(format='%y')[1:]
24+
return date.strftime(format='%d%m') + date.strftime(format='%y')

cfonb/writer/statement.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
- ligne total (facultative)
2424
2525
"""
26-
from cfonb.writer.common import write, date_format, save
26+
from cfonb.writer.common import write, date_format, save, BR_LINE
2727

2828

2929
class Statement(object):
30-
_header = {}
31-
_content = []
32-
_footer = {}
30+
def __init__(self):
31+
self._header = {}
32+
self._content = []
33+
self._footer = {}
3334

3435
def header(self, bank_code, agency_code, currency, account_number, date, amount):
3536
self._header['bank_code'] = bank_code
@@ -59,7 +60,7 @@ def add(self, bank_code, operation_code, agency_code, currency,
5960
line += write('2', 1) # number of decimal
6061
line += write(' ', 1) # SIT code
6162
line += write(account_number, 11)
62-
line += write(' ', 2) # interbank code
63+
line += write('08', 2) # interbank code
6364
line += write(date_format(date), 6)
6465
line += write(' ', 2) # rejected code
6566
line += write(date_format(date), 6)
@@ -68,45 +69,46 @@ def add(self, bank_code, operation_code, agency_code, currency,
6869
line += write(' ', 7) # entry writing code
6970
line += write(' ', 1) # exoneration code
7071
line += write(' ', 1) # reserve zone
71-
line += write(amount, 14)
72+
line += write(str(amount).replace('.', '') + 'A', 14, rpad=True, fill_char='0')
7273
line += write(reference, 16)
7374
self._content.append(line)
7475
return self
7576

7677
def render(self, filename=None):
77-
return save(self._header(), '\r\n'.join(self._content), self._footer(), filename)
78+
return save(self._render_header(), BR_LINE.join(self._content) + BR_LINE, self._render_footer(), filename)
7879

79-
def _header(self):
80+
def _render_header(self):
8081
if self._header:
8182
line = write('01', 2)
8283
line += write(self._header['bank_code'], 5)
8384
line += write(' ', 4) # reserved zone
84-
line += write(self._header['agency_code', 5])
85-
line += write(self._header['currency', 3])
85+
line += write(self._header['agency_code'], 5)
86+
line += write(self._header['currency'], 3)
8687
line += write('2', 1) # number of decimal
8788
line += write(' ', 1) # reserved zone
8889
line += write(self._header['account_number'], 11)
8990
line += write(' ', 2) # reserved zone
9091
line += write(date_format(self._header['date']), 6)
9192
line += write(' ', 50) # reserved zone
92-
line += write(self._header['amount'], 14)
93+
line += write(str(self._header['amount']).replace('.', '') + 'A', 14, rpad=True, fill_char='0')
9394
line += write(' ', 16) # reserved zone
94-
return line
95+
return line + BR_LINE
9596
return ''
9697

97-
def _footer(self):
98+
def _render_footer(self):
9899
if self._footer:
99100
line = write('07', 2)
100101
line += write(self._footer['bank_code'], 5)
101102
line += write(' ', 4) # reserved zone
102-
line += write(self._footer['agency_code', 5])
103-
line += write(self._footer['currency', 3])
103+
line += write(self._footer['agency_code'], 5)
104+
line += write(self._footer['currency'], 3)
104105
line += write('2', 1) # number of decimal
105106
line += write(' ', 1) # reserved zone
106107
line += write(self._footer['account_number'], 11)
107108
line += write(' ', 2) # reserved zone
108109
line += write(date_format(self._footer['date']), 6)
109110
line += write(' ', 50) # reserved zone
110-
line += write(self._footer['amount'], 14)
111+
line += write(str(self._footer['amount']).replace('.', '') + 'A', 14, rpad=True, fill_char='0')
111112
line += write(' ', 16) # reserved zone
112-
return line
113+
return line + BR_LINE
114+
return ''

cfonb/writer/transfert.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
You should have received a copy of the GNU Lesser General Public License
1717
along with CFONB. If not, see <http://www.gnu.org/licenses/>.
1818
"""
19-
from cfonb.writer.common import write, save, date_format
19+
from cfonb.writer.common import write, save, date_format, BR_LINE
2020

2121
"""
2222
Coryright 2011, Stéphane Planquart <[email protected]>
@@ -114,7 +114,7 @@ def _header(self):
114114
content += write(self._emeteur['banque'], 5)
115115
#[zone G2] Espace reserve 6 caracteres
116116
content += write('', 6)
117-
content += "\r\n";
117+
content += BR_LINE
118118
return content
119119

120120
def _footer(self):
@@ -147,7 +147,7 @@ def _footer(self):
147147
content += write("", 5)
148148
#[zone G2]Réservée 6 caractères
149149
content += write("", 6)
150-
content += "\r\n";
150+
content += BR_LINE
151151
return content
152152

153153
def _add(self,reference, raisonsocial,domiciliation,
@@ -187,5 +187,5 @@ def _add(self,reference, raisonsocial,domiciliation,
187187
content += write(etablissement, 5)
188188
#[zone G2]Zone réservée de 6 caractères
189189
content += write("", 6)
190-
content += "\r\n"
190+
content += BR_LINE
191191
return content

0 commit comments

Comments
 (0)