Skip to content

Commit

Permalink
more db tests, equality operators for Trade and Contract
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Sep 15, 2017
1 parent 8dc7f77 commit 182d9ca
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ deps =

commands =
flake8 \
--ignore=E501,E266 \
--ignore=E501,E266,W503 \
--exclude test.py \
xcat
pytest \
Expand Down
4 changes: 2 additions & 2 deletions xcat/db.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import plyvel
import json
import xcat.utils as utils
from xcat.trades import Trade, Contract
from xcat.trades import Trade


class DB():
Expand Down Expand Up @@ -63,7 +63,7 @@ def dump(self):
results = []
with self.db.iterator() as it:
for k, v in it:
j = json.loads(utils.x2s(utils.b2x(v)))
j = json.loads(str(v, 'utf-8'))
results.append((str(k, 'utf-8'), j))
return results

Expand Down
19 changes: 16 additions & 3 deletions xcat/tests/unit/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,26 @@ def test_createByFundtx_with_error(self):
in str(context.exception))

def test_get(self):
pass
self.db.db.get.return_value = str.encode(utils.test_trade.toJSON())

trade = self.db.get('test')

self.assertEqual(trade, utils.test_trade)

def test_save_secret(self):
pass
self.db.save_secret('my life', 'I like black liquorice')

self.db.preimageDB.put.assert_called_with(
str.encode('my life'),
str.encode('I like black liquorice'))

def test_get_secret(self):
pass
self.db.preimageDB.get.return_value = str.encode(
'I like black liquorice')

secret = self.db.get_secret('my life')

self.assertEqual(secret, 'I like black liquorice')

def test_dump(self):
pass
Expand Down
26 changes: 22 additions & 4 deletions xcat/trades.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ def __repr__(self):
self.buy.currency,
self.buy.initiator)

def __eq__(self, other):
return (self.sell == other.sell
and self.buy == other.buy
and self.commitment == other.commitment)


class Contract():

allowed = ('fulfiller', 'initiator', 'currency', 'p2sh', 'amount',
'fund_tx', 'redeem_tx', 'secret', 'redeemScript',
'redeemblocknum', 'locktime')

def __init__(self, data):
allowed = ('fulfiller', 'initiator', 'currency', 'p2sh', 'amount',
'fund_tx', 'redeem_tx', 'secret', 'redeemScript',
'redeemblocknum', 'locktime')
for key in data:
if key in allowed:
if key in Contract.allowed:
setattr(self, key, data[key])

def get_status(self):
Expand All @@ -56,3 +63,14 @@ def get_status(self):
return 'funded'
else:
return 'empty'

def __eq__(self, other):
for key in Contract.allowed:
if key in self.__dict__:
if key not in other.__dict__:
return False
if self.__dict__[key] != other.__dict__[key]:
return False
if key in other.__dict__ and key not in self.__dict__:
return False
return True

0 comments on commit 182d9ca

Please sign in to comment.