commit 0b5a23780916ae3b0a8003719188737f00262296
parent 7c068c6cbc8584d19b90b3822e9874574a0c35a5
Author: Marcello Stanisci <marcello.stanisci@inria.fr>
Date: Tue, 21 Feb 2017 11:11:43 +0100
string_to_amount
Diffstat:
3 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/Python/lib/pytaler/__pycache__/amounts.cpython-35.pyc b/Python/lib/pytaler/__pycache__/amounts.cpython-35.pyc
Binary files differ.
diff --git a/Python/lib/pytaler/amounts.py b/Python/lib/pytaler/amounts.py
@@ -19,12 +19,18 @@ import re
FRACTION = 100000000
def amount_get_zero(currency):
- return {"value": 0, "fraction": 0, "currency": currency}
+ return dict(value=0, fraction=0, currency=currency)
def string_to_amount(fmt):
pattern = re.compile("^[0-9]+\.[0-9]+:[A-Z]+$")
assert(pattern.match(fmt))
- # TBD
+ split = fmt.split(":")
+ num = split[0]
+ currency = split[1]
+ split = num.split(".")
+ value = int(split[0])
+ fraction = int(split[1]) * FRACTION
+ return dict(value=value, fraction=fraction, currency=currency)
def amount_sum(a1, a2):
diff --git a/Python/lib/pytaler/tests.py b/Python/lib/pytaler/tests.py
@@ -2,13 +2,13 @@ import unittest
import amounts
from random import randint
-currency = "KUDOS"
+CURRENCY = "KUDOS"
class AmountsTest(unittest.TestCase):
- def test(self):
- a1 = amounts.amount_get_zero(currency)
- a2 = amounts.amount_get_zero(currency)
+ def test_sum_sub(self):
+ a1 = amounts.amount_get_zero(CURRENCY)
+ a2 = amounts.amount_get_zero(CURRENCY)
v1 = randint(1, 200)
v2 = randint(1, 200)
f1 = randint(10000000, 100000000)
@@ -22,5 +22,11 @@ class AmountsTest(unittest.TestCase):
self.assertTrue(amounts.same_amount(res2, a2))
+ def test_string_to_amount(self):
+ model = dict(value=1, fraction=0, currency=CURRENCY)
+ amount = amounts.string_to_amount("1.0:%s" % CURRENCY)
+ self.assertTrue(amounts.same_amount(model, amount))
+
+
if __name__ == '__main__':
unittest.main()