From efdabf72bdeec30dda125e4ffd021cacab4e9195 Mon Sep 17 00:00:00 2001 From: Mauro Scomparin Date: Tue, 17 Jul 2018 21:20:43 +0200 Subject: [PATCH] More tests --- money/money.py | 58 ++++++++++++++++++++++++--------------- money/tests/test_money.py | 26 ++++++++++++++++++ 2 files changed, 62 insertions(+), 22 deletions(-) diff --git a/money/money.py b/money/money.py index 05f9974..2982bcb 100755 --- a/money/money.py +++ b/money/money.py @@ -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(): diff --git a/money/tests/test_money.py b/money/tests/test_money.py index b66f25b..b404a32 100644 --- a/money/tests/test_money.py +++ b/money/tests/test_money.py @@ -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") -- 2.25.1