Skip to content

Commit

Permalink
Unittest Account
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Dec 11, 2018
1 parent ac42f83 commit 11fd5cb
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 11 deletions.
2 changes: 1 addition & 1 deletion graphenecommon/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def account(self):
use the ``account`` attribute.
"""
account = self.account_class(self["owner"], blockchain_instance=self.blockchain)
account.refresh()
# account.refresh()
return account

def __repr__(self):
Expand Down
10 changes: 0 additions & 10 deletions graphenecommon/blockchainobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ class BlockchainObject(dict):
_cache = ObjectCache()
__originalname__ = ""

@classmethod
def inject(slf, cls):
class NewClass(cls, slf):
blockchain_object_class = slf
__originalname__ = cls.__name__
def __init__(self, *args, **kwargs):
cls.__init__(self, *args, **kwargs)
slf.__init__(self, *args, **kwargs)
return NewClass

def __init__(self, data, klass=None, lazy=False, use_cache=True, *args, **kwargs):
assert self.type_id or self.type_ids
self.cached = False
Expand Down
74 changes: 74 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import mock
import yaml

# Graphene API
Expand Down Expand Up @@ -72,3 +73,76 @@
EncryptedKeyInterface,
)
from graphenestorage.sqlite import SQLiteStore


# Common stuff

from graphenecommon.instance import BlockchainInstance as GBlockchainInstance
from graphenecommon.amount import Amount as GAmount
from graphenecommon.account import (
Account as GAccount,
AccountUpdate as GAccountUpdate
)
from graphenecommon.asset import Asset as GAsset


class Chain:
def __init__(self, *args, **kwargs):
pass

@property
def rpc(self):
""" We are patching rpc similar to a regular RPC
connection. However, it will always return
an empty object!
"""
class RPC:
def __getattr__(self, name):
def fun(self, *args, **kwargs):
return [{}]
return fun
return RPC()

def upgrade_account(self, *args, **kwargs):
pass


class BlockchainInstance(GBlockchainInstance):
def get_instance_class(self):
return Chain


@BlockchainInstance.inject
class Asset(GAsset):
type_id = 3


@BlockchainInstance.inject
class Amount(GAmount):
asset_class = Asset


@BlockchainInstance.inject
class Account(GAccount):
type_id = 2
amount_class = Amount
operations = operations


@BlockchainInstance.inject
class AccountUpdate(GAccountUpdate):
account_class = Account


def fixture_data():
with open(os.path.join(os.path.dirname(__file__), "fixtures.yaml")) as fid:
data = yaml.safe_load(fid)

# Feed our objects into the caches!
for account in data.get("accounts"):
Account._cache[account["id"]] = account
Account._cache[account["name"]] = account

for asset in data.get("assets"):
Asset._cache[asset["symbol"]] = asset
Asset._cache[asset["id"]] = asset
39 changes: 39 additions & 0 deletions tests/test_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from .fixtures import fixture_data, Account, AccountUpdate, getOperationNameForId


class Testcases(unittest.TestCase):

def setUp(self):
fixture_data()

def test_account(self):
Account("init0")
Account("1.2.101")
account = Account("init0", full=True)
self.assertEqual(account.name, "init0")
self.assertEqual(account["name"], account.name)
self.assertEqual(account["id"], "1.2.100")
self.assertEqual(str(account), "<Account init0>")
account = Account("1.2.100")
self.assertEqual(str(account), "<Account 1.2.100>")
for h in account.history(limit=1):
pass
self.assertIsInstance(Account(account), Account)

def test_account_upgrade(self):
account = Account("init0")
account.upgrade()

def test_accountupdate(self):
t = {'id': '2.6.29',
'lifetime_fees_paid': '44261516129',
'most_recent_op': '2.9.0',
'owner': '1.2.100',
'pending_fees': 0,
'pending_vested_fees': 16310,
'total_core_in_orders': '6788845277634',
'total_ops': 0}
update = AccountUpdate(t)
self.assertEqual(update["owner"], "1.2.100")
self.assertIsInstance(update.account, Account)

0 comments on commit 11fd5cb

Please sign in to comment.