Skip to content

Commit

Permalink
Release 0.24.17
Browse files Browse the repository at this point in the history
* Fixed a bug when using skip_account_check=True
* Refactor code in Account
* Add more unit tests
  • Loading branch information
holgern committed Oct 28, 2020
1 parent e2b1f33 commit 2c91a7e
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 58 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
96 changes: 44 additions & 52 deletions beem/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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():
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion beem/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""THIS FILE IS GENERATED FROM beem SETUP.PY."""
version = '0.24.16'
version = '0.24.17'
2 changes: 1 addition & 1 deletion beemapi/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""THIS FILE IS GENERATED FROM beem SETUP.PY."""
version = '0.24.16'
version = '0.24.17'
2 changes: 1 addition & 1 deletion beembase/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""THIS FILE IS GENERATED FROM beem SETUP.PY."""
version = '0.24.16'
version = '0.24.17'
2 changes: 1 addition & 1 deletion beemgraphenebase/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""THIS FILE IS GENERATED FROM beem SETUP.PY."""
version = '0.24.16'
version = '0.24.17'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down
51 changes: 50 additions & 1 deletion tests/beem/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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(""), "")
Loading

0 comments on commit 2c91a7e

Please sign in to comment.