Skip to content

Commit

Permalink
Finish up unittests using new caching system
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Dec 21, 2018
1 parent 63a4030 commit 74fa037
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 57 deletions.
48 changes: 15 additions & 33 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
import os
import yaml

from pprint import pprint

from bitshares import BitShares, storage
from bitshares.instance import set_shared_blockchain_instance
from bitshares.blockchainobject import BlockchainObject, ObjectCache
from bitshares.asset import Asset
from bitshares.account import Account
from bitshares.proposal import Proposals
from bitshares.proposal import Proposals, Proposal

from bitsharesbase.operationids import operations

# Configuration for unit tests
config = storage.InRamConfigurationStore()
config["node"] = "wss://node.bitshares.eu"

# default wifs key for testing
wifs = [
"5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3",
Expand All @@ -24,12 +22,9 @@

# bitshares instance
bitshares = BitShares(
keys=wifs,
nobroadcast=True,
num_retries=1,
config_store=config,
key_store=storage.InRamPlainKeyStore(),
"wss://node.bitshares.eu", keys=wifs, nobroadcast=True, num_retries=1
)
config = bitshares.config

# Set defaults
bitshares.set_default_account("init0")
Expand All @@ -38,37 +33,26 @@
# Ensure we are not going to transaction anythin on chain!
assert bitshares.nobroadcast

# Setup custom Cache
BlockchainObject._cache = ObjectCache(default_expiration=60 * 60 * 1, no_overwrite=True)


def add_to_object_cache(objects, key="id"):
if objects:
for i in objects:
if key in i and i[key]:
BlockchainObject._cache[i[key]] = i


def fixture_data():
# Clear tx buffer
bitshares.clear()

Account.clear_cache()

with open(os.path.join(os.path.dirname(__file__), "fixtures.yaml")) as fid:
data = yaml.safe_load(fid)
for ob in data.keys():
add_to_object_cache(data[ob])

for account in data.get("accounts"):
Account._cache[account["id"]] = account
Account._cache[account["name"]] = account
Account.cache_object(account, account["id"])
Account.cache_object(account, account["name"])

for asset in data.get("assets"):
Asset._cache[asset["symbol"]] = asset
Asset._cache[asset["id"]] = asset
Asset.cache_object(asset, asset["symbol"])
Asset.cache_object(asset, asset["id"])

proposals = []
for proposal in data.get("proposals", []):
# id = proposal["required_active_approvals"][0]
id = "1.2.1"
ops = list()
for _op in proposal["operations"]:
for opName, op in _op.items():
Expand All @@ -92,9 +76,7 @@ def fixture_data():
"required_active_approvals": ["1.2.1"],
"required_owner_approvals": [],
}
proposals.append(Proposal(proposal_data))

if id not in Proposals.cache or not Proposals.cache[id]:
Proposals.cache[id] = []
Proposals.cache[id].append(proposal_data)
# Also define the actual object in the Object Cache
BlockchainObject._cache[proposal_id] = proposal_data
Proposals.cache_objects(proposals, "1.2.1")
Proposals.cache_objects(proposals, "witness-account")
5 changes: 2 additions & 3 deletions tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def setUp(self):
fixture_data()

def test_account(self):
pprint(Account._cache)
Account("init0")
Account("1.2.3")
account = Account("init0", full=True)
Expand All @@ -35,8 +34,8 @@ def test_account(self):
account.cached = False
self.assertIn("id", account)
account.cached = False
self.assertEqual(account["id"], "1.2.90742")
self.assertEqual(str(account), "<Account 1.2.90742>")
self.assertEqual(account["id"], "1.2.100")
self.assertTrue(str(account).startswith("<Account "))
self.assertIsInstance(Account(account), Account)

def test_account_upgrade(self):
Expand Down
33 changes: 12 additions & 21 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@

# -*- coding: utf-8 -*-
import unittest
from bitshares import BitShares
from bitshares.asset import Asset
from bitshares.instance import set_shared_bitshares_instance, SharedInstance
from bitshares.blockchainobject import BlockchainObject

import logging

log = logging.getLogger()


class Testcases(unittest.TestCase):

def test_bts1bts2(self):
b1 = BitShares(
"wss://node.testnet.bitshares.eu",
nobroadcast=True,
)
b1 = BitShares("wss://node.testnet.bitshares.eu", nobroadcast=True)

b2 = BitShares(
"wss://node.bitshares.eu",
nobroadcast=True,
)
b2 = BitShares("wss://node.bitshares.eu", nobroadcast=True)

self.assertNotEqual(b1.rpc.url, b2.rpc.url)

def test_default_connection(self):
b1 = BitShares(
"wss://node.testnet.bitshares.eu",
nobroadcast=True,
)
b1 = BitShares("wss://node.testnet.bitshares.eu", nobroadcast=True)
set_shared_bitshares_instance(b1)
test = Asset("1.3.0")
test = Asset("1.3.0", blockchain_instance=b1)
# Needed to clear cache
test.refresh()

b2 = BitShares(
"wss://node.bitshares.eu",
nobroadcast=True,
)
b2 = BitShares("wss://node.bitshares.eu", nobroadcast=True)
set_shared_bitshares_instance(b2)

bts = Asset("1.3.0")
bts = Asset("1.3.0", blockchain_instance=b2)
# Needed to clear cache
bts.refresh()

self.assertEqual(test["symbol"], "TEST")
self.assertEqual(bts["symbol"], "BTS")

0 comments on commit 74fa037

Please sign in to comment.