Skip to content

Commit 51c2277

Browse files
committed
Fix unittests for pygraphene@develop
1 parent 72989a8 commit 51c2277

12 files changed

+134
-172
lines changed

bitshares/storage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
SQLiteFile
99
)
1010

11-
12-
InRamConfigurationStore.setdefault("node", "wss://node.bitshares.eu")
13-
SqliteConfigurationStore.setdefault("node", "wss://node.bitshares.eu")
11+
url = "wss://node.bitshares.eu"
12+
InRamConfigurationStore.setdefault("node", url)
13+
SqliteConfigurationStore.setdefault("node", url)
1414

1515

1616
def get_default_config_store(*args, **kwargs):
@@ -19,7 +19,7 @@ def get_default_config_store(*args, **kwargs):
1919
return SqliteConfigurationStore(*args, **kwargs)
2020

2121

22-
def get_default_key_store(*args, config, **kwargs):
22+
def get_default_key_store(config, *args, **kwargs):
2323
if "appname" not in kwargs:
2424
kwargs["appname"] = "bitshares"
2525
return SqliteEncryptedKeyStore(

bitshares/wallet.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,38 @@ def setKeys(self, loadkeys):
9292
pub = format(PrivateKey(str(wif)).pubkey, self.prefix)
9393
self.store.add(str(wif), pub)
9494

95+
def is_encrypted(self):
96+
""" Is the key store encrypted?
97+
"""
98+
return self.store.is_encrypted()
99+
95100
def unlock(self, pwd):
96101
""" Unlock the wallet database
97102
"""
98-
return self.store.unlock(pwd)
103+
if self.store.is_encrypted():
104+
return self.store.unlock(pwd)
99105

100106
def lock(self):
101107
""" Lock the wallet database
102108
"""
103-
return self.store.lock()
109+
if self.store.is_encrypted():
110+
return self.store.lock()
111+
else:
112+
return False
104113

105114
def unlocked(self):
106115
""" Is the wallet database unlocked?
107116
"""
108-
return not self.store.locked()
117+
if self.store.is_encrypted():
118+
return not self.store.locked()
119+
else:
120+
return True
109121

110122
def locked(self):
111123
""" Is the wallet database locked?
112124
"""
113-
return self.store.locked()
125+
if self.store.is_encrypted():
126+
return self.store.locked()
114127

115128
def changePassphrase(self, new_pwd):
116129
""" Change the passphrase for the wallet database
@@ -143,18 +156,18 @@ def addPrivateKey(self, wif):
143156
pub = format(PrivateKey(str(wif)).pubkey, self.prefix)
144157
except:
145158
raise InvalidWifError("Invalid Key format!")
146-
if pub in self.store:
159+
if str(pub) in self.store:
147160
raise KeyAlreadyInStoreException("Key already in the store")
148-
self.store.add(str(wif), pub)
161+
self.store.add(str(wif), str(pub))
149162

150163
def getPrivateKeyForPublicKey(self, pub):
151164
""" Obtain the private key for a given public key
152165
153166
:param str pub: Public Key
154167
"""
155-
if pub not in self.store:
168+
if str(pub) not in self.store:
156169
raise KeyNotFound
157-
return self.store.getPrivateKeyForPublicKey(pub)
170+
return self.store.getPrivateKeyForPublicKey(str(pub))
158171

159172
def removePrivateKeyFromPublicKey(self, pub):
160173
""" Remove a key from the wallet database
@@ -208,7 +221,7 @@ def getAccountFromPrivateKey(self, wif):
208221
def getAccountsFromPublicKey(self, pub):
209222
""" Obtain all accounts associated with a public key
210223
"""
211-
names = self.rpc.get_key_references([pub])
224+
names = self.rpc.get_key_references([str(pub)])
212225
for name in names:
213226
for i in name:
214227
yield i
@@ -219,7 +232,7 @@ def getAccountFromPublicKey(self, pub):
219232
# FIXME, this only returns the first associated key.
220233
# If the key is used by multiple accounts, this
221234
# will surely lead to undesired behavior
222-
names = self.rpc.get_key_references([pub])[0]
235+
names = self.rpc.get_key_references([str(pub)])[0]
223236
if not names:
224237
return None
225238
else:
@@ -229,24 +242,24 @@ def getAllAccounts(self, pub):
229242
""" Get the account data for a public key (all accounts found for this
230243
public key)
231244
"""
232-
for id in self.getAccountsFromPublicKey(pub):
245+
for id in self.getAccountsFromPublicKey(str(pub)):
233246
try:
234247
account = Account(id, blockchain_instance=self.blockchain)
235248
except:
236249
continue
237250
yield {"name": account["name"],
238251
"account": account,
239-
"type": self.getKeyType(account, pub),
240-
"pubkey": pub}
252+
"type": self.getKeyType(account, str(pub)),
253+
"pubkey": str(pub)}
241254

242255
def getKeyType(self, account, pub):
243256
""" Get key type
244257
"""
245258
for authority in ["owner", "active"]:
246259
for key in account[authority]["key_auths"]:
247-
if pub == key[0]:
260+
if str(pub) == key[0]:
248261
return authority
249-
if pub == account["options"]["memo_key"]:
262+
if str(pub) == account["options"]["memo_key"]:
250263
return "memo"
251264
return None
252265

tests/fixtures.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,27 @@
99

1010
from bitsharesbase.operationids import operations
1111

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

16+
# default wif key for testing
1317
wif = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"
18+
19+
# bitshares instance
1420
bitshares = BitShares(
1521
keys=[wif],
1622
nobroadcast=True,
1723
num_retries=1,
18-
config_store=storage.InRamConfigurationStore(),
24+
config_store=config,
1925
key_store=storage.InRamPlainKeyStore()
2026
)
27+
28+
# Set defaults
2129
bitshares.set_default_account("init0")
2230
set_shared_blockchain_instance(bitshares)
31+
32+
# Ensure we are not going to transaction anythin on chain!
2333
assert bitshares.nobroadcast
2434

2535
# Setup custom Cache
@@ -37,7 +47,8 @@ def add_to_object_cache(objects, key="id"):
3747

3848

3949
def fixture_data():
40-
# Accounts.cache = dict()
50+
# Clear tx buffer
51+
bitshares.clear()
4152

4253
with open(os.path.join(
4354
os.path.dirname(__file__),

tests/test_amount.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def __init__(self, *args, **kwargs):
1313
self.asset = Asset("BTS")
1414
self.symbol = Asset("BTS")["symbol"]
1515
self.precision = Asset("BTS")["precision"]
16-
1716
self.asset2 = Asset("EUR")
1817

1918
def setUp(self):

tests/test_asset.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
from bitshares.asset import Asset
44
from bitshares.instance import set_shared_bitshares_instance
55
from bitshares.exceptions import AssetDoesNotExistsException
6+
from .fixtures import fixture_data
67

78

89
class Testcases(unittest.TestCase):
910

10-
def __init__(self, *args, **kwargs):
11-
super().__init__(*args, **kwargs)
12-
13-
self.bts = BitShares(
14-
nobroadcast=True,
15-
)
16-
set_shared_bitshares_instance(self.bts)
11+
def setUp(self):
12+
fixture_data()
1713

1814
def test_assert(self):
1915
with self.assertRaises(AssetDoesNotExistsException):
@@ -37,13 +33,3 @@ def test_properties(self):
3733
self.assertEqual(asset.permissions, asset["permissions"])
3834
self.assertIsInstance(asset.flags, dict)
3935
self.assertEqual(asset.flags, asset["flags"])
40-
41-
"""
42-
# Mocker comes from pytest-mock, providing an easy way to have patched objects
43-
# for the life of the test.
44-
def test_calls(mocker):
45-
asset = Asset("USD", lazy=True, bitshares_instance=BitShares(offline=True))
46-
method = mocker.patch.object(Asset, 'get_call_orders')
47-
asset.calls
48-
method.assert_called_with(10)
49-
"""

tests/test_base_objects.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,22 @@
33
from bitshares.instance import set_shared_bitshares_instance
44
from bitshares.account import Account
55
from bitshares.committee import Committee
6+
from .fixtures import fixture_data
67

78

89
class Testcases(unittest.TestCase):
910

10-
def __init__(self, *args, **kwargs):
11-
super().__init__(*args, **kwargs)
12-
13-
self.bts = BitShares(
14-
nobroadcast=True,
15-
)
16-
set_shared_bitshares_instance(self.bts)
11+
def setUp(self):
12+
fixture_data()
1713

1814
def test_Committee(self):
1915
with self.assertRaises(
2016
exceptions.AccountDoesNotExistsException
2117
):
2218
Committee("FOObarNonExisting")
2319

24-
c = Committee("init0")
25-
self.assertEqual(c["id"], "1.5.0")
20+
c = Committee("xeroc")
21+
self.assertEqual(c["id"], "1.5.27")
2622
self.assertIsInstance(c.account, Account)
2723

2824
with self.assertRaises(

tests/test_block.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,20 @@
44
from bitshares.block import Block, BlockHeader
55
from bitshares.instance import set_shared_bitshares_instance
66
from bitshares.utils import parse_time
7+
from .fixtures import fixture_data
78

89

910
class Testcases(unittest.TestCase):
1011

11-
def __init__(self, *args, **kwargs):
12-
super().__init__(*args, **kwargs)
13-
14-
self.bts = BitShares(
15-
"wss://node.testnet.bitshares.eu",
16-
nobroadcast=True,
17-
)
18-
self.bts.set_default_account("init0")
19-
set_shared_bitshares_instance(self.bts)
12+
def setUp(self):
13+
fixture_data()
2014

2115
def test_block(self):
2216
block = Block(1)
2317
self.assertEqual(block["previous"], "0000000000000000000000000000000000000000")
24-
self.assertEqual(block.time(), parse_time('2016-01-18T10:59:20'))
18+
self.assertEqual(block.time(), parse_time('2015-10-13T14:12:24'))
2519

2620
def test_blockheader(self):
2721
header = BlockHeader(1)
2822
self.assertEqual(header["previous"], "0000000000000000000000000000000000000000")
29-
self.assertEqual(header.time(), parse_time('2016-01-18T10:59:20'))
23+
self.assertEqual(header.time(), parse_time('2015-10-13T14:12:24'))

tests/test_blockchain.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@
55
from bitshares.block import Block
66
from bitshares.instance import set_shared_bitshares_instance
77
from bitshares.utils import parse_time
8+
from .fixtures import fixture_data
89

910

1011
class Testcases(unittest.TestCase):
1112

12-
def __init__(self, *args, **kwargs):
13-
super().__init__(*args, **kwargs)
14-
15-
self.bts = BitShares(
16-
"wss://node.testnet.bitshares.eu",
17-
nobroadcast=True,
18-
)
19-
self.bts.set_default_account("init0")
20-
set_shared_bitshares_instance(self.bts)
13+
def setUp(self):
14+
fixture_data()
2115
self.chain = Blockchain(mode="head")
2216

2317
def test_is_irv(self):

0 commit comments

Comments
 (0)