forked from s3lph/matemat
Implemented unit tests for currency formatting and parsing.
This commit is contained in:
parent
103e06cf12
commit
862a0e6fa8
2 changed files with 137 additions and 4 deletions
133
matemat/webserver/test/test_currency_format.py
Normal file
133
matemat/webserver/test/test_currency_format.py
Normal file
|
@ -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')
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue