diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c025995468915b1ee83ccc6c03f2218853a02d13..157cde17a9e00d739b5ec1ced22c4f470472bf88 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,5 +1,11 @@ Changelog ========= +0.24.17 +------- +* Fixed a bug when using skip_account_check=True +* Refactor code in Account +* Add more unit tests + 0.24.16 ------- * Fix bug in bytes representation of an Amount which prevents sending certain amounts (e.g. 8.19 HIVE) diff --git a/beem/account.py b/beem/account.py index 049901d6148db49d5567ea4cec2a21ff08ecb49f..c78ec60efa7b5646f103599261273bd7afde0ccb 100644 --- a/beem/account.py +++ b/beem/account.py @@ -21,6 +21,17 @@ from beem.constants import STEEM_VOTE_REGENERATION_SECONDS, STEEM_1_PERCENT, STE log = logging.getLogger(__name__) +def extract_account_name(account): + if isinstance(account, str): + return account + elif isinstance(account, Account): + return account["name"] + elif isinstance(account, dict) and "name" in account: + return account["name"] + else: + return "" + + class Account(BlockchainObject): """ This class allows to easily access Account data @@ -748,8 +759,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): return None from beem.discussions import Discussions, Query @@ -843,8 +853,8 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) + if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -907,8 +917,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -939,6 +948,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] + account = extract_account_name(account) if not account: raise ValueError("You need to provide an account") if last_read is None: @@ -977,8 +987,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -994,8 +1003,7 @@ class Account(BlockchainObject): """ get_follow_count """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1125,8 +1133,7 @@ class Account(BlockchainObject): """Returns all subscriptions""" if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(True) @@ -1136,8 +1143,7 @@ class Account(BlockchainObject): """Returns account feed""" if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if observer is None: observer = account if not self.blockchain.is_connected(): @@ -1344,6 +1350,7 @@ class Account(BlockchainObject): """ get_account_bandwidth """ if account is None: account = self["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1437,8 +1444,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1469,8 +1475,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1504,8 +1509,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1539,8 +1543,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1572,8 +1575,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1606,8 +1608,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1639,8 +1640,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1672,8 +1672,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") if not isinstance(keys, list): @@ -1697,8 +1696,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1731,8 +1729,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1762,8 +1759,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] - elif isinstance(account, Account): - account = account["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") # self.blockchain.rpc.set_next_node_on_empty_reply(False) @@ -1847,22 +1843,22 @@ class Account(BlockchainObject): def _get_account_history(self, account=None, start=-1, limit=0): if account is None: - account = self - account = Account(account, blockchain_instance=self.blockchain) + account = self["name"] + account = extract_account_name(account) if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) if self.blockchain.rpc.get_use_appbase(): try: - ret = self.blockchain.rpc.get_account_history({'account': account["name"], 'start': start, 'limit': limit}, api="account_history") + ret = self.blockchain.rpc.get_account_history({'account': account, 'start': start, 'limit': limit}, api="account_history") if ret is not None: ret = ret["history"] except ApiNotSupported: - ret = self.blockchain.rpc.get_account_history(account["name"], start, limit, api="condenser") + ret = self.blockchain.rpc.get_account_history(account, start, limit, api="condenser") else: - ret = self.blockchain.rpc.get_account_history(account["name"], start, limit, api="database") + ret = self.blockchain.rpc.get_account_history(account, start, limit, api="database") if ret is None or (len(ret) == 0 and limit == 0): - ret = self.blockchain.rpc.get_account_history(account["name"], start, limit + 1, api="database") + ret = self.blockchain.rpc.get_account_history(account, start, limit + 1, api="database") return ret def estimate_virtual_op_num(self, blocktime, stop_diff=0, max_count=100): @@ -2481,6 +2477,7 @@ class Account(BlockchainObject): """ if account is None: account = self["name"] + account = extract_account_name(account) if not account: raise ValueError("You need to provide an account") if not other: @@ -2789,11 +2786,9 @@ class Account(BlockchainObject): amount = Amount(amount, asset, blockchain_instance=self.blockchain) if not skip_account_check: to = Account(to, blockchain_instance=self.blockchain) - to_name = to["name"] - account_name = account["name"] - else: - to_name = to - account_name = account + + to_name = extract_account_name(to) + account_name = extract_account_name(account) if memo and memo[0] == "#": from .memo import Memo memoObj = Memo( @@ -2835,11 +2830,8 @@ class Account(BlockchainObject): if not skip_account_check: to = Account(to, blockchain_instance=self.blockchain) - to_name = to["name"] - account_name = account["name"] - else: - to_name = to - account_name = account + to_name = extract_account_name(to) + account_name = extract_account_name(account) op = operations.Transfer_to_vesting(**{ "from": account_name, diff --git a/beem/version.py b/beem/version.py index a8aa402efc21da67cb35932b6f46ba075fe7abc1..5dfb386f7f256f73e8502a6c56bbee9b625e4d8c 100644 --- a/beem/version.py +++ b/beem/version.py @@ -1,2 +1,2 @@ """THIS FILE IS GENERATED FROM beem SETUP.PY.""" -version = '0.24.16' +version = '0.24.17' diff --git a/beemapi/version.py b/beemapi/version.py index a8aa402efc21da67cb35932b6f46ba075fe7abc1..5dfb386f7f256f73e8502a6c56bbee9b625e4d8c 100644 --- a/beemapi/version.py +++ b/beemapi/version.py @@ -1,2 +1,2 @@ """THIS FILE IS GENERATED FROM beem SETUP.PY.""" -version = '0.24.16' +version = '0.24.17' diff --git a/beembase/version.py b/beembase/version.py index a8aa402efc21da67cb35932b6f46ba075fe7abc1..5dfb386f7f256f73e8502a6c56bbee9b625e4d8c 100644 --- a/beembase/version.py +++ b/beembase/version.py @@ -1,2 +1,2 @@ """THIS FILE IS GENERATED FROM beem SETUP.PY.""" -version = '0.24.16' +version = '0.24.17' diff --git a/beemgraphenebase/version.py b/beemgraphenebase/version.py index a8aa402efc21da67cb35932b6f46ba075fe7abc1..5dfb386f7f256f73e8502a6c56bbee9b625e4d8c 100644 --- a/beemgraphenebase/version.py +++ b/beemgraphenebase/version.py @@ -1,2 +1,2 @@ """THIS FILE IS GENERATED FROM beem SETUP.PY.""" -version = '0.24.16' +version = '0.24.17' diff --git a/setup.py b/setup.py index 0783d10f1a1a1c45300467169bbc7548e86fbaa6..aa949be6c5a4d16b11aae77e1389b1ee78cd9feb 100755 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ except LookupError: ascii = codecs.lookup('ascii') codecs.register(lambda name, enc=ascii: {True: enc}.get(name == 'mbcs')) -VERSION = '0.24.16' +VERSION = '0.24.17' tests_require = ['mock >= 2.0.0', 'pytest', 'pytest-mock', 'parameterized'] diff --git a/tests/beem/test_account.py b/tests/beem/test_account.py index 3cddf8fdd483653dcfdfe547cc68c2d5398d6880..819d5b215647d498d2979653678adcacbce9783d 100644 --- a/tests/beem/test_account.py +++ b/tests/beem/test_account.py @@ -5,7 +5,7 @@ from datetime import datetime, timedelta from parameterized import parameterized from pprint import pprint from beem import Steem, exceptions -from beem.account import Account +from beem.account import Account, extract_account_name from beem.block import Block from beem.amount import Amount from beem.asset import Asset @@ -402,6 +402,47 @@ class Testcases(unittest.TestCase): "beembot", op["from"]) + w.blockchain.txbuffer.clear() + tx = w.transfer_to_vesting("1 HIVE", skip_account_check=True) + self.assertEqual( + (tx["operations"][0][0]), + "transfer_to_vesting" + ) + op = tx["operations"][0][1] + self.assertIn( + "beembot", + op["from"]) + + def test_transfer(self): + w = self.account + w.blockchain.txbuffer.clear() + tx = w.transfer("beembot", "1", "HIVE") + self.assertEqual( + (tx["operations"][0][0]), + "transfer" + ) + op = tx["operations"][0][1] + self.assertIn( + "beembot", + op["from"]) + self.assertIn( + "beembot", + op["to"]) + + w.blockchain.txbuffer.clear() + tx = w.transfer("beembot", "1", "HIVE", skip_account_check=True) + self.assertEqual( + (tx["operations"][0][0]), + "transfer" + ) + op = tx["operations"][0][1] + self.assertIn( + "beembot", + op["from"]) + self.assertIn( + "beembot", + op["to"]) + def test_json_export(self): account = Account("beembot", steem_instance=self.bts) if account.blockchain.rpc.get_use_appbase(): @@ -522,3 +563,11 @@ class Testcases(unittest.TestCase): stm = self.bts account = Account("gtg", steem_instance=stm) assert isinstance(account.get_notifications(), list) + + def test_extract_account_name(self): + stm = self.bts + account = Account("holger80", steem_instance=stm) + self.assertEqual(extract_account_name(account), "holger80") + self.assertEqual(extract_account_name("holger80"), "holger80") + self.assertEqual(extract_account_name({"name": "holger80"}), "holger80") + self.assertEqual(extract_account_name(""), "") diff --git a/tests/beembase/test_objects.py b/tests/beembase/test_objects.py index c3bd9b5ff795f4dd0870a69e8da7c4181d508628..54809d487008d6d0cd75c3d8dd1c224a9d8bb7a6 100644 --- a/tests/beembase/test_objects.py +++ b/tests/beembase/test_objects.py @@ -59,6 +59,16 @@ class Testcases(unittest.TestCase): self.assertEqual("8.190 STEEM", t.__str__()) self.assertEqual("8.190 STEEM", str(t)) + a = "0.0009 STEEM" + t = Amount(a) + self.assertEqual("0.000 STEEM", t.__str__()) + self.assertEqual("0.000 STEEM", str(t)) + + a = "100.0009 STEEM" + t = Amount(a) + self.assertEqual("100.000 STEEM", t.__str__()) + self.assertEqual("100.000 STEEM", str(t)) + def test_Operation(self): a = {"amount": '1000', "precision": 3, "nai": '@@000000013'} j = ["transfer", {'from': 'a', 'to': 'b', 'amount': a, 'memo': 'c'}]