summaryrefslogtreecommitdiff
path: root/payments/taler/test_amount.py
blob: bd59c74ba14dfb6bd4bfa39f8e6ddb0df62e1b58 (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
#  This file is part of TALER
#  (C) 2017 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 2.1 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/copylib.git/
#  This code is "copylib", it is versioned under the Git repository
#  mentioned above, and it is meant to be manually copied into any project
#  which might need it.

from __future__ import unicode_literals
from .amount import Amount, BadAmount
from unittest import TestCase
import json
from mock import MagicMock

class TestAmount(TestCase):
    def setUp(self):
        self.amount = Amount('TESTKUDOS')

    def test_parse_and_cmp(self):
        a = self.amount.parse('TESTKUDOS:0.0')
        self.assertEqual(Amount.cmp(self.amount, a), 0)
        b = self.amount.parse('TESTKUDOS:0.1')
        self.assertEqual(Amount.cmp(Amount('TESTKUDOS', fraction=10000000), b), 0)
        c = self.amount.parse('TESTKUDOS:3.3')
        self.assertEqual(Amount.cmp(Amount('TESTKUDOS', 3, 30000000), c), 0)
        self.assertEqual(Amount.cmp(a, b), -1)
        self.assertEqual(Amount.cmp(c, b), 1)
        with self.assertRaises(BadAmount):
            Amount.parse(':3')

    def test_add_and_dump(self):
        mocky = MagicMock()
        self.amount.add(Amount('TESTKUDOS', 9, 10**8))
        mocky(**self.amount.dump())
        mocky.assert_called_with(currency='TESTKUDOS', value=10, fraction=0)

    def test_subtraction(self):
        with self.assertRaises(ValueError):
            self.amount.subtract(Amount('TESTKUDOS', fraction=1))
        a = Amount('TESTKUDOS', 2)
        a.subtract(Amount('TESTKUDOS', 1, 99999999))
        self.assertEqual(Amount.cmp(a, Amount('TESTKUDOS', fraction=1)), 0)

    def test_stringify(self):
        self.assertEqual(self.amount.stringify(3), 'TESTKUDOS:0.000')
        self.amount.add(Amount('TESTKUDOS', 2, 100))
        self.assertEqual(self.amount.stringify(6), 'TESTKUDOS:2.000001')