From 944265a7e90ee686769ad5a8d04441c57e1b32f8 Mon Sep 17 00:00:00 2001 From: Chris Berkhout Date: Sat, 31 Jul 2021 12:01:49 +0200 Subject: [PATCH] Test format. --- tests/pricehist/test_format.py | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/pricehist/test_format.py diff --git a/tests/pricehist/test_format.py b/tests/pricehist/test_format.py new file mode 100644 index 0000000..2795037 --- /dev/null +++ b/tests/pricehist/test_format.py @@ -0,0 +1,57 @@ +from collections import namedtuple +from decimal import Decimal + +from pricehist.format import Format + + +def test_fromargs(): + arg_values = { + "formatquote": None, + "formattime": "23:59:59", + "formatdecimal": None, + "formatthousands": None, + "formatsymbol": None, + "formatdatesep": None, + "formatcsvdelim": None, + "formatbase": None, + } + args = namedtuple("args", arg_values.keys())(**arg_values) + fmt = Format.fromargs(args) + assert fmt.time == "23:59:59" + assert fmt.symbol == "rightspace" + + +def test_format_date(): + assert Format().format_date("2021-01-01") == "2021-01-01" + assert Format(datesep="/").format_date("2021-01-01") == "2021/01/01" + + +def test_format_quote_amount(): + assert ( + Format(decimal=",").format_quote_amount("USD", Decimal("1234.5678")) + == "1234,5678 USD" + ) + assert ( + Format(symbol="rightspace").format_quote_amount("USD", Decimal("1234.5678")) + == "1234.5678 USD" + ) + assert ( + Format(symbol="right").format_quote_amount("€", Decimal("1234.5678")) + == "1234.5678€" + ) + assert ( + Format(symbol="leftspace").format_quote_amount("£", Decimal("1234.5678")) + == "£ 1234.5678" + ) + assert ( + Format(symbol="left").format_quote_amount("$", Decimal("1234.5678")) + == "$1234.5678" + ) + + +def test_format_num(): + assert Format().format_num(Decimal("1234.5678")) == "1234.5678" + assert ( + Format(decimal=",", thousands=".").format_num(Decimal("1234.5678")) + == "1.234,5678" + )