|
1 | 1 | # python import - http://docs.python.org/library/unittest.html |
2 | 2 | import unittest |
| 3 | +from datetime import date |
3 | 4 | from io import StringIO |
4 | 5 |
|
5 | | -# import cfonb module |
6 | | -from cfonb.parser import Row, statement as p |
| 6 | +from nose.tools import assert_is_not_none, assert_equal |
7 | 7 |
|
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 |
71 | 10 |
|
72 | 11 |
|
73 | 12 | class TestStatement(unittest.TestCase): |
74 | 13 |
|
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)) |
156 | 29 |
|
157 | 30 |
|
158 | 31 | def suite(): |
159 | 32 | 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')) |
164 | 34 | return suite |
165 | 35 |
|
166 | 36 |
|
|
0 commit comments