From 862a0e6fa8934844590de0ae2e7741e610e62eb7 Mon Sep 17 00:00:00 2001 From: s3lph Date: Fri, 20 Jul 2018 15:20:33 +0200 Subject: [PATCH] Implemented unit tests for currency formatting and parsing. --- .../webserver/test/test_currency_format.py | 133 ++++++++++++++++++ matemat/webserver/util.py | 8 +- 2 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 matemat/webserver/test/test_currency_format.py diff --git a/matemat/webserver/test/test_currency_format.py b/matemat/webserver/test/test_currency_format.py new file mode 100644 index 0000000..6385852 --- /dev/null +++ b/matemat/webserver/test/test_currency_format.py @@ -0,0 +1,133 @@ + +import unittest + +from matemat.webserver.util import format_chf, parse_chf + + +class TestCurrencyFormat(unittest.TestCase): + + def test_format_zero(self): + self.assertEqual('CHF 0.00', format_chf(0)) + self.assertEqual('0.00', format_chf(0, False)) + + def test_format_positive_full(self): + self.assertEqual('CHF 42.00', format_chf(4200)) + self.assertEqual('42.00', format_chf(4200, False)) + + def test_format_negative_full(self): + self.assertEqual('CHF -42.00', format_chf(-4200)) + self.assertEqual('-42.00', format_chf(-4200, False)) + + def test_format_positive_frac(self): + self.assertEqual('CHF 13.37', format_chf(1337)) + self.assertEqual('13.37', format_chf(1337, False)) + + def test_format_negative_frac(self): + self.assertEqual('CHF -13.37', format_chf(-1337)) + self.assertEqual('-13.37', format_chf(-1337, False)) + + def test_format_pad_left_positive(self): + self.assertEqual('CHF 0.01', format_chf(1)) + self.assertEqual('0.01', format_chf(1, False)) + + def test_format_pad_left_negative(self): + self.assertEqual('CHF -0.01', format_chf(-1)) + self.assertEqual('-0.01', format_chf(-1, False)) + + def test_format_pad_right_positive(self): + self.assertEqual('CHF 4.20', format_chf(420)) + self.assertEqual('4.20', format_chf(420, False)) + + def test_format_pad_right_negative(self): + self.assertEqual('CHF -4.20', format_chf(-420)) + self.assertEqual('-4.20', format_chf(-420, False)) + + def test_parse_empty(self): + with self.assertRaises(ValueError): + parse_chf('') + with self.assertRaises(ValueError): + parse_chf('CHF') + with self.assertRaises(ValueError): + parse_chf('CHF ') + + def test_parse_zero(self): + self.assertEqual(0, parse_chf('CHF0')) + self.assertEqual(0, parse_chf('CHF 0')) + self.assertEqual(0, parse_chf('CHF -0')) + self.assertEqual(0, parse_chf('CHF 0.')) + self.assertEqual(0, parse_chf('CHF 0.0')) + self.assertEqual(0, parse_chf('CHF 0.00')) + self.assertEqual(0, parse_chf('CHF -0.')) + self.assertEqual(0, parse_chf('CHF -0.0')) + self.assertEqual(0, parse_chf('CHF -0.00')) + self.assertEqual(0, parse_chf('0')) + self.assertEqual(0, parse_chf('0')) + self.assertEqual(0, parse_chf('-0')) + self.assertEqual(0, parse_chf('0.')) + self.assertEqual(0, parse_chf('0.0')) + self.assertEqual(0, parse_chf('0.00')) + self.assertEqual(0, parse_chf('-0.')) + self.assertEqual(0, parse_chf('-0.0')) + self.assertEqual(0, parse_chf('-0.00')) + + def test_parse_positive_full(self): + self.assertEqual(4200, parse_chf('CHF 42.00')) + self.assertEqual(4200, parse_chf('42.00')) + self.assertEqual(4200, parse_chf('CHF 42')) + self.assertEqual(4200, parse_chf('42')) + self.assertEqual(4200, parse_chf('CHF 42.')) + self.assertEqual(4200, parse_chf('42.')) + self.assertEqual(4200, parse_chf('CHF 42.0')) + self.assertEqual(4200, parse_chf('42.0')) + + def test_parse_negative_full(self): + self.assertEqual(-4200, parse_chf('CHF -42.00')) + self.assertEqual(-4200, parse_chf('-42.00')) + self.assertEqual(-4200, parse_chf('CHF -42')) + self.assertEqual(-4200, parse_chf('-42')) + self.assertEqual(-4200, parse_chf('CHF -42.')) + self.assertEqual(-4200, parse_chf('-42.')) + self.assertEqual(-4200, parse_chf('CHF -42.0')) + self.assertEqual(-4200, parse_chf('-42.0')) + + def test_parse_positive_frac(self): + self.assertEqual(1337, parse_chf('CHF 13.37')) + self.assertEqual(1337, parse_chf('13.37')) + + def test_parse_negative_frac(self): + self.assertEqual(-1337, parse_chf('CHF -13.37')) + self.assertEqual(-1337, parse_chf('-13.37')) + + def test_parse_pad_left_positive(self): + self.assertEqual(1, parse_chf('CHF 0.01')) + self.assertEqual(1, parse_chf('0.01')) + + def test_parse_pad_left_negative(self): + self.assertEqual(-1, parse_chf('CHF -0.01')) + self.assertEqual(-1, parse_chf('-0.01')) + + def test_parse_pad_right_positive(self): + self.assertEqual(420, parse_chf('CHF 4.20')) + self.assertEqual(420, parse_chf('4.20')) + self.assertEqual(420, parse_chf('CHF 4.2')) + self.assertEqual(420, parse_chf('4.2')) + + def test_parse_pad_right_negative(self): + self.assertEqual(-420, parse_chf('CHF -4.20')) + self.assertEqual(-420, parse_chf('-4.20')) + self.assertEqual(-420, parse_chf('CHF -4.2')) + self.assertEqual(-420, parse_chf('-4.2')) + + def test_parse_too_many_decimals(self): + with self.assertRaises(ValueError): + parse_chf('123.456') + with self.assertRaises(ValueError): + parse_chf('CHF 0.456') + with self.assertRaises(ValueError): + parse_chf('CHF 0.450') + + def test_parse_wrong_separator(self): + with self.assertRaises(ValueError): + parse_chf('13,37') + with self.assertRaises(ValueError): + parse_chf('CHF 13,37') diff --git a/matemat/webserver/util.py b/matemat/webserver/util.py index ddbb41e..74041d8 100644 --- a/matemat/webserver/util.py +++ b/matemat/webserver/util.py @@ -173,16 +173,16 @@ def parse_chf(value: str) -> int: value = value[3:] value = value.strip() if '.' not in value: - return int(value) * 100 + return int(value, 10) * 100 full, frac = value.split('.', 1) if len(frac) > 2: raise ValueError('Needs max. 2 digits after decimal point') elif len(frac) < 2: frac = frac + '0' * (2 - len(frac)) - ifrac: int = int(frac) - ifull: int = int(full) + ifrac: int = int(frac, 10) + ifull: int = int(full, 10) if ifrac < 0: raise ValueError('Fraction part must not be negative.') - if ifull < 0: + if full.startswith('-'): ifrac = -ifrac return ifull * 100 + ifrac