More tests
authorMauro Scomparin <scompo@gmail.com>
Tue, 17 Jul 2018 19:20:43 +0000 (21:20 +0200)
committerMauro Scomparin <scompo@gmail.com>
Tue, 17 Jul 2018 19:20:43 +0000 (21:20 +0200)
money/money.py
money/tests/test_money.py

index 05f9974ec8f6111206fa9abbceef7f0cc52686a0..2982bcba9744462bcb04606e4d1bb518ca2bece6 100755 (executable)
@@ -8,7 +8,7 @@ import json
 import gettext
 
 loc_dir = resource_filename('money', 'translations')
-t = gettext.translation('money', localedir = loc_dir, fallback=True)
+t = gettext.translation('money', localedir=loc_dir, fallback=True)
 _ = t.gettext
 t.install()
 
@@ -23,25 +23,33 @@ DATE_FIELD_NAME = _('date')
 HOUR_FIELD_NAME = _('hour')
 DESCRIPTION_FIELD_NAME = _('description')
 
+
 def create_input_message(field_name, field_format, field_default_value):
     return field_name + " (" + field_format + ") [" + field_default_value + "]: "
 
+
 TYPE_MESSAGE = create_input_message(TYPE_FIELD_NAME, '+/-', '-')
 DATE_MESSAGE = create_input_message(DATE_FIELD_NAME, 'DD/MM/YYYY', _('today'))
 HOUR_MESSAGE = create_input_message(HOUR_FIELD_NAME, 'HH:MM', _('now'))
 VALUE_MESSAGE = create_input_message(VALUE_FIELD_NAME, '(#####.##)', '')
 DESCRIPTION_MESSAGE = create_input_message(DESCRIPTION_FIELD_NAME, '', '')
 
-def write_movement(path, m):
+
+def write_movement_to_file(path, m):
     with open(path, 'a') as f:
-        f.write(m[TYPE_FIELD_NAME] + m[VALUE_FIELD_NAME])
-        f.write(';')
-        f.write(m[DATE_FIELD_NAME])
-        f.write(';')
-        f.write(m[HOUR_FIELD_NAME])
-        f.write(';')
-        f.write(m[DESCRIPTION_FIELD_NAME])
-        f.write('\n')
+        write_movement_to_stream(f, m)
+    return
+
+
+def write_movement_to_stream(fileStream, movement):
+    fileStream.write(movement[TYPE_FIELD_NAME] + movement[VALUE_FIELD_NAME])
+    fileStream.write(';')
+    fileStream.write(movement[DATE_FIELD_NAME])
+    fileStream.write(';')
+    fileStream.write(movement[HOUR_FIELD_NAME])
+    fileStream.write(';')
+    fileStream.write(movement[DESCRIPTION_FIELD_NAME])
+    fileStream.write('\n')
     return
 
 
@@ -104,24 +112,30 @@ def get_data_file(config):
     return data_file
 
 
-def load_file(file):
+def load_movements_from_stream(data_file):
     movements = []
-    with open(file, "r") as data_file:
-        for line in data_file:
-            split_line = l.split(';')
-            movement = {
-                VALUE_FIELD_NAME: split_line[0],
-                DATE_FIELD_NAME: split_line[1],
-                HOUR_FIELD_NAME: split_line[2],
-                DESCRIPTION_FIELD_NAME: split_line[3]
-            }
-            movements.append(movement)
+    for line in data_file:
+        split_line = line.split(';')
+        movement = {
+            VALUE_FIELD_NAME: split_line[0],
+            DATE_FIELD_NAME: split_line[1],
+            HOUR_FIELD_NAME: split_line[2],
+            DESCRIPTION_FIELD_NAME: split_line[3].rstrip()
+        }
+        movements.append(movement)
     return movements
 
 
+def load_file(file):
+    res = []
+    with open(file, "r") as data_file:
+        res = load_movements_from_stream(data_file)
+    return res
+
+
 def insert_new_movement(data_file):
     movement = read_movement()
-    write_movement(data_file, movement)
+    write_movement_to_file(data_file, movement)
 
 
 def read_new_movement():
index b66f25baa5072847c30b08bf3874661edb0bc5d1..b404a32ec79eb751bfa6305535e8a4bf0d175e50 100644 (file)
@@ -4,6 +4,7 @@ import json
 from decimal import *
 from os.path import expanduser
 from money import money
+import io
 
 
 class MoneyTest(unittest.TestCase):
@@ -19,6 +20,31 @@ class MoneyTest(unittest.TestCase):
         self.assertEqual(money.last_movement_number_message(
             10), "latest 10 movements:")
 
+    def test_write_movement_to_stream(self):
+        test_str = io.StringIO()
+        m = {
+            money.TYPE_FIELD_NAME: '-',
+            money.VALUE_FIELD_NAME: '100',
+            money.DATE_FIELD_NAME: '18/04/2017',
+            money.HOUR_FIELD_NAME: '00:01',
+            money.DESCRIPTION_FIELD_NAME: 'testings',
+        }
+        money.write_movement_to_stream(test_str, m)
+        test_str.seek(0)
+        self.assertEqual('-100;18/04/2017;00:01;testings\n', test_str.readline(), 'wrong string read')
+
+    def test_load_movements_from_stream(self):
+        test_str = io.StringIO()
+        test_str.write('-100;18/04/2017;00:01;testings\n')
+        test_str.seek(0)
+        movements = money.load_movements_from_stream(test_str)
+        self.assertEqual(1, len(movements), 'should read only one movement')
+        m = movements[0]
+        self.assertEqual('-100', m[money.VALUE_FIELD_NAME])
+        self.assertEqual('18/04/2017', m[money.DATE_FIELD_NAME])
+        self.assertEqual('00:01', m[money.HOUR_FIELD_NAME])
+        self.assertEqual('testings', m[money.DESCRIPTION_FIELD_NAME])
+
     def test_value_message(self):
         self.assertEqual(money.value_message('blah: {}',
                                              Decimal(10.00)), "blah: 10")