From 9a72a1c558952d456ee3f5d667e6a36f5f54a2c5 Mon Sep 17 00:00:00 2001 From: Holger Nahrstaedt <holger@nahrstaedt.de> Date: Sat, 7 Apr 2018 11:55:13 +0200 Subject: [PATCH] More Unit tests * More unit tests * Handling of Amount for Appbase improved * Fixed unit tests for Python 2.7 --- beembase/objects.py | 5 ++-- beemgraphenebase/types.py | 5 +++- tests/beembase/test_objects.py | 37 ++++++++++++++++++++++++++++++ tests/beembase/test_operations.py | 24 +++++++++++++++++-- tests/beemgraphene/test_objects.py | 31 +++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 tests/beembase/test_objects.py create mode 100644 tests/beemgraphene/test_objects.py diff --git a/beembase/objects.py b/beembase/objects.py index 1e3cdd93..3ba66dfc 100644 --- a/beembase/objects.py +++ b/beembase/objects.py @@ -48,12 +48,13 @@ class Amount(object): self.amount = d[0] self.asset = d[2] self.precision = d[1] - self.str_repr = d + a = Array([String(d[0]), d[1], d[2]]) + self.str_repr = str(a.__str__()) else: self.amount = d.amount self.asset = d.symbol self.precision = d.asset["precision"] - self.str_repr = d.json() + self.str_repr = json.dumps((d.json())) def __bytes__(self): # padding diff --git a/beemgraphenebase/types.py b/beemgraphenebase/types.py index 73f7976e..c88759ee 100644 --- a/beemgraphenebase/types.py +++ b/beemgraphenebase/types.py @@ -248,7 +248,10 @@ class Array(object): r = [] for a in self.data: try: - r.append(JsonObj(a)) + if isinstance(a, String): + r.append(str(a)) + else: + r.append(JsonObj(a)) except Exception: r.append(str(a)) return json.dumps(r) diff --git a/tests/beembase/test_objects.py b/tests/beembase/test_objects.py new file mode 100644 index 00000000..463631b5 --- /dev/null +++ b/tests/beembase/test_objects.py @@ -0,0 +1,37 @@ +from builtins import chr +from builtins import range +from builtins import str +import unittest +import hashlib +from binascii import hexlify, unhexlify +import os +import json +from pprint import pprint +from beembase.objects import Amount +from beembase.objects import Operation +from beemgraphenebase.types import ( + Uint8, Int16, Uint16, Uint32, Uint64, + Varint32, Int64, String, Bytes, Void, + Array, PointInTime, Signature, Bool, + Set, Fixed_array, Optional, Static_variant, + Map, Id +) + + +class Testcases(unittest.TestCase): + def test_Amount(self): + a = "1.000 STEEM" + t = Amount(a) + self.assertEqual(a, t.__str__()) + self.assertEqual(a, str(t)) + + a = ["3000", 3, "@@00000032"] + t = Amount(a) + # self.assertEqual(str(a), t.__str__()) + self.assertEqual(a, json.loads(str(t))) + + def test_Operation(self): + a = ['1000', 3, '@@000000013'] + j = ["transfer", {'from': 'a', 'to': 'b', 'amount': a, 'memo': 'c'}] + o = Operation(j) + self.assertEqual(o.json()[1], j[1]) diff --git a/tests/beembase/test_operations.py b/tests/beembase/test_operations.py index aabc1ddb..2b17784b 100644 --- a/tests/beembase/test_operations.py +++ b/tests/beembase/test_operations.py @@ -6,21 +6,41 @@ from __future__ import unicode_literals from builtins import bytes from builtins import chr from builtins import range +from builtins import str import unittest import hashlib from binascii import hexlify, unhexlify import os import json from pprint import pprint +from beem.amount import Amount from beembase.operations import Transfer +from beembase.objects import Operation +from beembase.signedtransactions import Signed_Transaction +from beembase.chains import known_chains +wif = "5J4KCbg1G3my9b9hCaQXnHSm6vrwW9xQTJS6ZciW2Kek7cCkCEk" -class Testcases(unittest.TestCase): +class Testcases(unittest.TestCase): def test_Transfer(self): - transferJson = {'from': 'test', 'to': 'test1', 'amount': '1.000 SBD', 'memo': 'foobar'} + transferJson = {'from': 'test', 'to': 'test1', 'amount': "1.000 STEEM", 'memo': 'foobar'} t = Transfer(transferJson) self.assertEqual(transferJson, json.loads(str(t))) self.assertEqual(transferJson, t.json()) self.assertEqual(transferJson, t.toJson()) self.assertEqual(transferJson, t.__json__()) + + transferJson = {'from': 'test', 'to': 'test1', 'amount': ['3000', 3, '@@00000032'], 'memo': 'foobar'} + t = Transfer(transferJson) + self.assertEqual(transferJson, json.loads(str(t))) + self.assertEqual(transferJson, t.json()) + self.assertEqual(transferJson, t.toJson()) + self.assertEqual(transferJson, t.__json__()) + + o = Operation(Transfer(transferJson)) + self.assertEqual(o.json()[1], transferJson) + tx = {'ref_block_num': 0, 'ref_block_prefix': 0, 'expiration': '2018-04-07T09:30:53', 'operations': [o], 'extensions': [], 'signatures': []} + s = Signed_Transaction(tx) + s.sign(wifkeys=[wif], chain="STEEMAPPBASE") + self.assertEqual(s.json()["operations"][0][1], transferJson) diff --git a/tests/beemgraphene/test_objects.py b/tests/beemgraphene/test_objects.py new file mode 100644 index 00000000..195038d5 --- /dev/null +++ b/tests/beemgraphene/test_objects.py @@ -0,0 +1,31 @@ +# This Python file uses the following encoding: utf-8 +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals +import unittest +import json +from beemgraphenebase import objects +from beemgraphenebase import types +from beem.amount import Amount +from beem import Steem + + +class Testcases(unittest.TestCase): + def test_GrapheneObject(self): + j = {"a": 2, "b": "abcde", "c": ["a", "b"]} + j2 = objects.GrapheneObject(j) + self.assertEqual(j, j2.data) + self.assertEqual(json.loads(j2.__str__()), j2.json()) + + a = objects.Array(['1000', 3, '@@000000013']) + j = {"a": a} + j2 = objects.GrapheneObject(j) + self.assertEqual(j, j2.data) + self.assertEqual(json.loads(j2.__str__()), j2.json()) + + a = types.Array(['1000', 3, '@@000000013']) + j = {"a": a} + j2 = objects.GrapheneObject(j) + self.assertEqual(j, j2.data) + self.assertEqual(json.loads(j2.__str__()), j2.json()) -- GitLab