gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-merchant-frontend-examples] 02/02: string_to_amount


From: gnunet
Subject: [GNUnet-SVN] [taler-merchant-frontend-examples] 02/02: string_to_amount
Date: Tue, 21 Feb 2017 11:12:01 +0100

This is an automated email from the git hooks/post-receive script.

marcello pushed a commit to branch master
in repository merchant-frontend-examples.

commit 0b5a23780916ae3b0a8003719188737f00262296
Author: Marcello Stanisci <address@hidden>
AuthorDate: Tue Feb 21 11:11:43 2017 +0100

    string_to_amount
---
 Python/lib/pytaler/__pycache__/amounts.cpython-35.pyc | Bin 1272 -> 1712 bytes
 Python/lib/pytaler/amounts.py                         |  10 ++++++++--
 Python/lib/pytaler/tests.py                           |  14 ++++++++++----
 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
index 24b724e..5f665fb 100644
Binary files a/Python/lib/pytaler/__pycache__/amounts.cpython-35.pyc and 
b/Python/lib/pytaler/__pycache__/amounts.cpython-35.pyc differ
diff --git a/Python/lib/pytaler/amounts.py b/Python/lib/pytaler/amounts.py
index c1821de..00f9ae4 100644
--- 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
index 120e968..ffc8cba 100644
--- 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()

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]