aboutsummaryrefslogtreecommitdiff
path: root/tests/test_amount.py
blob: 559dd2c586adb1e254f3cd720b24ed49eaa5ce1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#  This file is part of TALER
#  (C) 2017, 2019 Taler Systems SA
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 3 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
#  @author Marcello Stanisci
#  @version 0.0
#  @repository https://git.taler.net/taler-util.git/

from taler.util.amount import (
    Amount,
    SignedAmount,
    AmountFormatError,
    AmountOverflowError,
    MAX_AMOUNT_VALUE,
)
from unittest import TestCase
import json


class TestAmount(TestCase):
    def test_very_big_number(self):
        with self.assertRaises(AmountOverflowError):
            self.Amount = Amount(
                "TESTKUDOS",
                value=99999999999999999999999999999999999999999999,
                fraction=0,
            )

    def test_add_overflow(self):
        a1 = Amount("TESTKUDOS", value=MAX_AMOUNT_VALUE, fraction=0)
        with self.assertRaises(AmountOverflowError):
            a2 = a1 + Amount.parse("TESTKUDOS:1")

    def test_sub_overflow(self):
        a1 = Amount("TESTKUDOS", value=MAX_AMOUNT_VALUE, fraction=0)
        s1 = SignedAmount(False, a1)
        with self.assertRaises(AmountOverflowError):
            s2 = s1 - SignedAmount.parse("TESTKUDOS:1")

    def test_parse_error(self):
        with self.assertRaises(AmountFormatError):
            Amount.parse("TESTKUDOS:0,5")
        with self.assertRaises(AmountFormatError):
            Amount.parse("+TESTKUDOS:0.5")
        with self.assertRaises(AmountFormatError):
            Amount.parse("0.5")
        with self.assertRaises(AmountFormatError):
            Amount.parse(":0.5")
        with self.assertRaises(AmountFormatError):
            Amount.parse("EUR::0.5")
        with self.assertRaises(AmountFormatError):
            Amount.parse("EUR:.5")

    def test_parse_and_cmp(self):
        self.assertTrue(Amount.parse("EUR:0.0") < Amount.parse("EUR:0.5"))

    def test_amount(self):
        self.assertEqual(Amount.parse("TESTKUDOS:0").stringify(3), "TESTKUDOS:0.000")

    def test_signed_amount(self):
        self.assertEqual(
            SignedAmount.parse("TESTKUDOS:1.5").stringify(3), "+TESTKUDOS:1.500"
        )

    def test_zero_crossing(self):
        p1 = SignedAmount.parse("EUR:1")
        p2 = SignedAmount.parse("EUR:2")
        p3 = SignedAmount.parse("EUR:3")
        p5 = SignedAmount.parse("EUR:5")
        p8 = SignedAmount.parse("EUR:8")

        self.assertEqual(p5 + p3, p8)
        self.assertEqual(p5 - p3, p2)
        self.assertEqual(p2 - p3, -p1)

        self.assertEqual((-p2) + p3, p1)