diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c0259954..157cde17 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 049901d6..c78ec60e 100644 --- a/beem/account.py +++ b/beem/account.py @@ -21,6 +21,17 @@ 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 @@ def get_feed(self, start_entry_id=0, limit=100, raw_data=False, short_entries=Fa """ 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 @@ def get_blog(self, start_entry_id=0, limit=100, raw_data=False, short_entries=Fa """ 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 @@ def get_notifications(self, only_unread=True, limit=100, raw_data=False, account """ 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 @@ def mark_notifications_as_read(self, last_read=None, account=None): """ 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 @@ def get_blog_authors(self, account=None): """ 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 @@ def get_follow_count(self, account=None): """ 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 @@ def list_all_subscriptions(self, account=None): """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 @@ def get_account_posts(self, sort="feed", limit=20, account=None, observer=None, """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 @@ def get_account_bandwidth(self, bandwidth_type=1, account=None): """ 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 @@ def get_owner_history(self, account=None): """ 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 @@ def get_conversion_requests(self, account=None): """ 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 @@ def get_vesting_delegations(self, start_account="", limit=100, account=None): """ 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 @@ def get_withdraw_routes(self, account=None): """ 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 @@ def get_savings_withdrawals(self, direction="from", account=None): """ 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 @@ def get_recovery_request(self, account=None): """ 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 @@ def get_escrow(self, escrow_id=0, account=None): """ 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 @@ def verify_account_authority(self, keys, account=None): """ 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 @@ def get_tags_used_by_author(self, account=None): """ 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 @@ def get_expiring_vesting_delegations(self, after=None, limit=1000, account=None) """ 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 @@ def get_account_votes(self, account=None, start_author="", start_permlink="", li """ 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 @@ def virtual_op_count(self, until=None): 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 @@ def follow(self, other, what=["blog"], account=None): """ 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 @@ def transfer(self, to, amount, asset, memo="", skip_account_check=False, account 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 @@ def transfer_to_vesting(self, amount, to=None, account=None, skip_account_check= 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 a8aa402e..5dfb386f 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 a8aa402e..5dfb386f 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 a8aa402e..5dfb386f 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 a8aa402e..5dfb386f 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 0783d10f..aa949be6 100755 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ 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 3cddf8fd..819d5b21 100644 --- a/tests/beem/test_account.py +++ b/tests/beem/test_account.py @@ -5,7 +5,7 @@ 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 @@ def test_transfer_to_vesting(self): "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 @@ def test_notifications(self): 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 c3bd9b5f..54809d48 100644 --- a/tests/beembase/test_objects.py +++ b/tests/beembase/test_objects.py @@ -59,6 +59,16 @@ def test_Amount_overflow(self): 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'}]