Skip to content

Commit 7e5ff23

Browse files
committed
unit tests now with fixtures
1 parent 2cdaabd commit 7e5ff23

8 files changed

Lines changed: 392 additions & 184 deletions

File tree

tests/fixtures.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import os
2+
import yaml
3+
4+
from bitshares import BitShares, storage
5+
from bitshares.instance import set_shared_blockchain_instance
6+
from bitshares.blockchainobject import BlockchainObject, ObjectCache
7+
from bitshares.account import Account
8+
from bitshares.proposal import Proposals
9+
10+
from bitsharesbase.operationids import operations
11+
12+
13+
wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
14+
bitshares = BitShares(
15+
keys=[wif],
16+
nobroadcast=True,
17+
num_retries=1,
18+
config_store=storage.InRamConfigurationStore(),
19+
key_store=storage.InRamPlainKeyStore()
20+
)
21+
bitshares.set_default_account("init0")
22+
set_shared_blockchain_instance(bitshares)
23+
assert bitshares.nobroadcast
24+
25+
# Setup custom Cache
26+
BlockchainObject._cache = ObjectCache(
27+
default_expiration=60 * 60 * 1,
28+
no_overwrite=True
29+
)
30+
31+
32+
def add_to_object_cache(objects, key="id"):
33+
if objects:
34+
for i in objects:
35+
if key in i and i[key]:
36+
BlockchainObject._cache[i[key]] = i
37+
38+
39+
def fixture_data():
40+
# Accounts.cache = dict()
41+
42+
with open(os.path.join(
43+
os.path.dirname(__file__),
44+
"fixtures.yaml"
45+
)) as fid:
46+
data = yaml.safe_load(fid)
47+
for ob in data.keys():
48+
add_to_object_cache(data[ob])
49+
50+
add_to_object_cache(data.get("accounts", []), key="name")
51+
add_to_object_cache(data.get("assets", []), key="symbol")
52+
53+
for proposal in data.get("proposals", []):
54+
# id = proposal["required_active_approvals"][0]
55+
id = "1.2.1"
56+
ops = list()
57+
for _op in proposal["operations"]:
58+
for opName, op in _op.items():
59+
ops.append(
60+
[operations[opName], op]
61+
)
62+
# Proposal!
63+
proposal_id = proposal["proposal_id"]
64+
proposal_data = {'available_active_approvals': [],
65+
'available_key_approvals': [],
66+
'available_owner_approvals': [],
67+
'expiration_time': '2018-05-29T10:23:13',
68+
'id': proposal_id,
69+
'proposed_transaction': {'expiration': '2018-05-29T10:23:13',
70+
'extensions': [],
71+
'operations': ops,
72+
'ref_block_num': 0,
73+
'ref_block_prefix': 0},
74+
'proposer': '1.2.7',
75+
'required_active_approvals': ['1.2.1'],
76+
'required_owner_approvals': []}
77+
78+
if id not in Proposals.cache or not Proposals.cache[id]:
79+
Proposals.cache[id] = []
80+
Proposals.cache[id].append(proposal_data)
81+
# Also define the actual object in the Object Cache
82+
BlockchainObject._cache[proposal_id] = proposal_data

tests/fixtures.yaml

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
accounts:
2+
3+
- name: init0
4+
id: '1.2.100'
5+
registrar: '1.2.0'
6+
lifetime_referrer: '1.2.0'
7+
top_n_control_flags: 0
8+
blacklisting_accounts: []
9+
referrer: '1.2.0'
10+
whitelisted_accounts: []
11+
owner:
12+
address_auths: []
13+
key_auths:
14+
- - BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
15+
- 1
16+
account_auths: []
17+
weight_threshold: 1
18+
active:
19+
address_auths: []
20+
key_auths:
21+
- - BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
22+
- 1
23+
- - BTS5i8bEmtnN4fP4jAsBe17z9CCuQcHLkRyTuRZXYZeN2kVCL1sXa
24+
- 1
25+
account_auths: []
26+
weight_threshold: 1
27+
referrer_rewards_percentage: 0
28+
membership_expiration_date: 1969-12-31T23:59:59
29+
statistics: '2.6.1888'
30+
network_fee_percentage: 2000
31+
options:
32+
extensions: []
33+
voting_account: '1.2.5'
34+
votes: []
35+
num_committee: 0
36+
memo_key: BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
37+
num_witness: 0
38+
whitelisting_accounts: []
39+
lifetime_referrer_fee_percentage: 8000
40+
blacklisted_accounts: []
41+
owner_special_authority:
42+
- 0
43+
- {}
44+
active_special_authority:
45+
- 0
46+
- {}
47+
48+
- name: init1
49+
id: '1.2.101'
50+
registrar: '1.2.0'
51+
lifetime_referrer: '1.2.0'
52+
top_n_control_flags: 0
53+
blacklisting_accounts: []
54+
referrer: '1.2.0'
55+
whitelisted_accounts: []
56+
owner:
57+
address_auths: []
58+
key_auths:
59+
- - BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
60+
- 1
61+
account_auths: []
62+
weight_threshold: 1
63+
active:
64+
address_auths: []
65+
key_auths:
66+
- - BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
67+
- 1
68+
account_auths: []
69+
weight_threshold: 1
70+
referrer_rewards_percentage: 0
71+
membership_expiration_date: 1969-12-31T23:59:59
72+
statistics: '2.6.1888'
73+
network_fee_percentage: 2000
74+
options:
75+
extensions: []
76+
voting_account: '1.2.5'
77+
votes: []
78+
num_committee: 0
79+
memo_key: BTS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
80+
num_witness: 0
81+
whitelisting_accounts: []
82+
lifetime_referrer_fee_percentage: 8000
83+
blacklisted_accounts: []
84+
owner_special_authority:
85+
- 0
86+
- {}
87+
active_special_authority:
88+
- 0
89+
- {}
90+
91+
assets:
92+
- dynamic_asset_data_id: '2.3.121'
93+
precision: 4
94+
symbol: USD
95+
bitasset_data_id: '2.4.21'
96+
id: '1.3.121'
97+
options:
98+
issuer_permissions: 511
99+
flags: 129
100+
extensions: []
101+
core_exchange_rate:
102+
base:
103+
amount: 221
104+
asset_id: '1.3.121'
105+
quote:
106+
amount: 11564
107+
asset_id: '1.3.0'
108+
whitelist_authorities: []
109+
whitelist_markets: []
110+
max_market_fee: '1000000000000000'
111+
market_fee_percent: 10
112+
blacklist_markets: []
113+
blacklist_authorities: []
114+
max_supply: '1000000000000000'
115+
description: ! '{"main":"1 United States dollar","market":""}'
116+
issuer: '1.2.0'
117+
118+
- dynamic_asset_data_id: '2.3.120'
119+
precision: 4
120+
symbol: EUR
121+
bitasset_data_id: '2.4.20'
122+
id: '1.3.120'
123+
options:
124+
issuer_permissions: 511
125+
flags: 128
126+
extensions: []
127+
core_exchange_rate:
128+
base:
129+
amount: 1229
130+
asset_id: '1.3.120'
131+
quote:
132+
amount: 74364
133+
asset_id: '1.3.0'
134+
whitelist_authorities: []
135+
whitelist_markets: []
136+
max_market_fee: '1000000000000000'
137+
market_fee_percent: 0
138+
blacklist_markets: []
139+
blacklist_authorities: []
140+
max_supply: '1000000000000000'
141+
description: 1 euro
142+
issuer: '1.2.0'
143+

tests/test_account.py

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,21 @@
77
from bitshares.asset import Asset
88
from bitshares.instance import set_shared_bitshares_instance
99
from bitsharesbase.operationids import getOperationNameForId
10-
11-
wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
10+
from .fixtures import fixture_data, bitshares
1211

1312

1413
class Testcases(unittest.TestCase):
1514

16-
def __init__(self, *args, **kwargs):
17-
super().__init__(*args, **kwargs)
18-
19-
self.bts = BitShares(
20-
"wss://node.testnet.bitshares.eu",
21-
nobroadcast=True,
22-
# We want to bundle many operations into a single transaction
23-
bundle=True,
24-
# Overwrite wallet to use this list of wifs only
25-
wif={"active": wif}
26-
)
27-
self.bts.set_default_account("init0")
28-
set_shared_bitshares_instance(self.bts)
15+
def setUp(self):
16+
fixture_data()
2917

3018
def test_account(self):
31-
Account("witness-account")
19+
Account("init0")
3220
Account("1.2.3")
33-
asset = Asset("1.3.0")
34-
symbol = asset["symbol"]
35-
account = Account("witness-account", full=True)
36-
self.assertEqual(account.name, "witness-account")
21+
account = Account("init0", full=True)
22+
self.assertEqual(account.name, "init0")
3723
self.assertEqual(account["name"], account.name)
38-
self.assertEqual(account["id"], "1.2.1")
24+
self.assertEqual(account["id"], "1.2.100")
3925
self.assertIsInstance(account.balance("1.3.0"), Amount)
4026
# self.assertIsInstance(account.balance({"symbol": symbol}), Amount)
4127
self.assertIsInstance(account.balances, list)
@@ -48,39 +34,32 @@ def test_account(self):
4834
account.cached = False
4935
self.assertIn("id", account)
5036
account.cached = False
51-
self.assertEqual(account["id"], "1.2.1")
52-
self.assertEqual(str(account), "<Account 1.2.1>")
37+
self.assertEqual(account["id"], "1.2.90742")
38+
self.assertEqual(str(account), "<Account 1.2.90742>")
5339
self.assertIsInstance(Account(account), Account)
5440

5541
def test_account_upgrade(self):
56-
account = Account("witness-account")
42+
account = Account("init0")
43+
pprint(account)
5744
tx = account.upgrade()
5845
ops = tx["operations"]
5946
op = ops[0][1]
6047
self.assertEqual(len(ops), 1)
61-
self.assertEqual(
62-
getOperationNameForId(ops[0][0]),
63-
"account_upgrade"
64-
)
65-
self.assertTrue(
66-
op["upgrade_to_lifetime_member"]
67-
)
68-
self.assertEqual(
69-
op["account_to_upgrade"],
70-
"1.2.1",
71-
)
48+
self.assertEqual(getOperationNameForId(ops[0][0]), "account_upgrade")
49+
self.assertTrue(op["upgrade_to_lifetime_member"])
50+
self.assertEqual(op["account_to_upgrade"], "1.2.100")
7251

7352
def test_openorders(self):
74-
account = Account("witness-account")
53+
account = Account("init0")
7554
self.assertIsInstance(account.openorders, list)
7655

7756
def test_calls(self):
78-
account = Account("witness-account")
57+
account = Account("init0")
7958
self.assertIsInstance(account.callpositions, dict)
8059

8160
def test_whitelist(self):
8261
from bitsharesbase.operations import Account_whitelist
83-
account = Account("witness-account")
62+
account = Account("init0")
8463
tx = account.whitelist("committee-account")
8564
self.assertEqual(len(tx["operations"]), 1)
8665
self.assertEqual(tx["operations"][0][0], 7)
@@ -89,7 +68,7 @@ def test_whitelist(self):
8968

9069
def test_blacklist(self):
9170
from bitsharesbase.operations import Account_whitelist
92-
account = Account("witness-account")
71+
account = Account("init0")
9372
tx = account.blacklist("committee-account")
9473
self.assertEqual(len(tx["operations"]), 1)
9574
self.assertEqual(tx["operations"][0][0], 7)
@@ -98,7 +77,7 @@ def test_blacklist(self):
9877

9978
def test_unlist(self):
10079
from bitsharesbase.operations import Account_whitelist
101-
account = Account("witness-account")
80+
account = Account("init0")
10281
tx = account.nolist("committee-account")
10382
self.assertEqual(len(tx["operations"]), 1)
10483
self.assertEqual(tx["operations"][0][0], 7)
@@ -110,13 +89,13 @@ def test_accountupdate(self):
11089
t = {'id': '2.6.29',
11190
'lifetime_fees_paid': '44261516129',
11291
'most_recent_op': '2.9.0',
113-
'owner': '1.2.29',
92+
'owner': '1.2.100',
11493
'pending_fees': 0,
11594
'pending_vested_fees': 16310,
11695
'total_core_in_orders': '6788845277634',
11796
'total_ops': 0}
11897
update = AccountUpdate(t)
119-
self.assertEqual(update["owner"], "1.2.29")
98+
self.assertEqual(update["owner"], "1.2.100")
12099
self.assertIsInstance(update.account, Account)
121100
update.__repr__()
122101

tests/test_amount.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,28 @@
33
from bitshares.amount import Amount
44
from bitshares.asset import Asset
55
from bitshares.instance import set_shared_bitshares_instance, SharedInstance
6-
7-
8-
url = "wss://node.testnet.bitshares.eu"
6+
from .fixtures import fixture_data, bitshares
97

108

119
class Testcases(unittest.TestCase):
1210

1311
def __init__(self, *args, **kwargs):
1412
super().__init__(*args, **kwargs)
13+
self.asset = Asset("BTS")
14+
self.symbol = Asset("BTS")["symbol"]
15+
self.precision = Asset("BTS")["precision"]
1516

16-
self.bts = BitShares(url, nobroadcast=True)
17-
set_shared_bitshares_instance(self.bts)
18-
self.asset = Asset("1.3.0")
19-
self.symbol = self.asset["symbol"]
20-
self.precision = self.asset["precision"]
21-
self.asset2 = Asset("1.3.1")
17+
self.asset2 = Asset("EUR")
18+
19+
def setUp(self):
20+
fixture_data()
2221

2322
def dotest(self, ret, amount, symbol):
2423
self.assertEqual(float(ret), float(amount))
2524
self.assertEqual(ret["symbol"], symbol)
2625
self.assertIsInstance(ret["asset"], dict)
2726
self.assertIsInstance(ret["amount"], float)
2827

29-
def test_url(self):
30-
self.assertEqual(self.bts.rpc.url, url)
31-
3228
def test_init(self):
3329
# String init
3430
amount = Amount("1 {}".format(self.symbol))

0 commit comments

Comments
 (0)