Add tests up to newtrade

This commit is contained in:
Jay Graber 2017-07-28 20:45:46 -07:00
parent 0cab1f7db3
commit 4bd5ac0bc9
9 changed files with 57 additions and 21 deletions

0
xcat.json Normal file
View File

View File

@ -1,9 +1,9 @@
import argparse, textwrap
from xcat.utils import *
import xcat.database as db
import xcat.db as db
import xcat.bitcoinRPC
import xcat.zcashRPC
import xcat.userInput
import xcat.userInput
from xcat.trades import *
from xcat.protocol import *
@ -51,17 +51,21 @@ def checkBuyStatus(trade):
print("XCAT trade complete!")
# Import a trade in hex, and save to db
def importtrade(hexstr):
def importtrade(hexstr, tradeid=None):
print('importing trade')
trade = x2s(hexstr)
trade = instantiate(trade)
save_state(trade)
save_state(trade, tradeid)
return trade
# Export a trade by its tradeid
def exporttrade(tradeid):
# trade = get_trade()
trade = db.get(tradeid)
hexstr = s2x(str(trade))
print(trade)
hexstr = s2x(trade.toJSON())
print(trade.toJSON())
print(hexstr)
return hexstr
def newtrade(tradeid):
erase_trade()
@ -79,8 +83,8 @@ def main():
== Trades ==
newtrade - create a new trade
checktrades - check for actions to be taken on existing trades
importtrade "hexstr" - import an existing trade from a hex string
exporttrade - export the data of an existing trade as a hex string. Takes the tradeid as an argument
importtrade "hexstr" "tradeid" - import an existing trade from a hex string and save by a unique tradid
exporttrade "tradeid" - export the data of an existing trade as a hex string. Takes the tradeid as an argument
findtrade - find a trade by the txid of the currency being traded out of
'''))
@ -89,13 +93,18 @@ def main():
# parser.add_argument("--daemon", "-d", action="store_true", help="Run as daemon process")
# TODO: function to view available trades
# TODO: function to tell if tradeid already exists for newtrade
# TODO: If no tradeid provided, save by funding txid
args = parser.parse_args()
# how to hold state of role
command = args.command
if command == 'importtrade':
hexstr = args.argument[0]
importtrade(hexstr)
try:
tradeid = args.argument[1]
except:
tradeid = None
importtrade(hexstr, tradeid)
elif command == 'exporttrade':
tradeid = args.argument[0]
exporttrade(tradeid)

View File

@ -6,9 +6,12 @@ import json
db = plyvel.DB('/tmp/testdb', create_if_missing=True)
# Takes object, saves json as bytes
# Takes dict or obj, saves json str as bytes
def create(trade, tradeid):
trade = trade.toJSON()
if type(trade) == dict:
trade = json.dumps(trade)
else:
trade = trade.toJSON()
db.put(b(tradeid), b(trade))
# Uses the funding txid as the key to save trade

View File

@ -7,7 +7,7 @@ import xcat.zcashRPC
import xcat.bitcoinRPC
from xcat.utils import *
from xcat.trades import Contract, Trade
import xcat.userInput
import xcat.userInput as userInput
def check_p2sh(currency, address):
if currency == 'bitcoin':

0
xcat/tests/__init__.py Normal file
View File

View File

@ -0,0 +1,21 @@
import unittest
import xcat.cli as cli
from xcat.tests.utils import mktrade
class SimpleTestCase(unittest.TestCase):
def setUp(self):
self.trade = mktrade()
self.hexstr = cli.exporttrade('test')
def test_exporttrade(self):
self.assertTrue(int(self.hexstr, 16))
class CliTest(SimpleTestCase):
def test_importtrade(self):
trade = cli.importtrade(self.hexstr, 'test')
def test_newtrade(self):
cli.newtrade('test2')
if __name__ == '__main__':
unittest.main()

View File

@ -1,17 +1,15 @@
import xcat.database as db
import unittest, json
import xcat.db as db
import xcat.trades as trades
from xcat.tests.utils import test_trade
class DatabaseTest(unittest.TestCase):
def setUp(self):
self.data = {"sell": {"amount": 3.5, "redeemScript": "63a82003d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b68876a9147788b4511a25fba1092e67b307a6dcdb6da125d967022a04b17576a914c7043e62a7391596116f54f6a64c8548e97d3fd96888ac", "redeemblocknum": 1066, "currency": "bitcoin", "initiator": "myfFr5twPYNwgeXyjCmGcrzXtCmfmWXKYp", "p2sh": "2MuYSQ1uQ4CJg5Y5QL2vMmVPHNJ2KT5aJ6f", "fulfiller": "mrQzUGU1dwsWRx5gsKKSDPNtrsP65vCA3Z", "fund_tx": "5c5e91a89a08b2d6698f50c9fd9bb2fa22da6c74e226c3dd63d59511566a2fdb"}, "buy": {"amount": 1.2, "redeemScript": "63a82003d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b68876a9143ea29256c9d2888ca23de42a8b8e69ca2ec235b167023f0db17576a914c5acca6ef39c843c7a9c3ad01b2da95fe2edf5ba6888ac", "redeemblocknum": 3391, "currency": "zcash", "locktime": 10, "initiator": "tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ", "p2sh": "t2HP59RpfR34nBCWH4VVD497tkc2ikzgniP", "fulfiller": "tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY"}, "commitment": "03d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b6"}
self.data = test_trade
self.sell = trades.Contract(self.data['sell'])
def test_create(self):
sell = trades.Contract(self.data['sell'])
buy = trades.Contract(self.data['buy'])
trade = trades.Trade(sell, buy, commitment=self.data['commitment'])
db.create(trade, 'test')
db.create(self.data, 'test')
def test_get(self):
trade = db.get('test')

8
xcat/tests/utils.py Normal file
View File

@ -0,0 +1,8 @@
import xcat.db as db
test_trade = {"sell": {"amount": 3.5, "redeemScript": "63a82003d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b68876a9147788b4511a25fba1092e67b307a6dcdb6da125d967022a04b17576a914c7043e62a7391596116f54f6a64c8548e97d3fd96888ac", "redeemblocknum": 1066, "currency": "bitcoin", "initiator": "myfFr5twPYNwgeXyjCmGcrzXtCmfmWXKYp", "p2sh": "2MuYSQ1uQ4CJg5Y5QL2vMmVPHNJ2KT5aJ6f", "fulfiller": "mrQzUGU1dwsWRx5gsKKSDPNtrsP65vCA3Z", "fund_tx": "5c5e91a89a08b2d6698f50c9fd9bb2fa22da6c74e226c3dd63d59511566a2fdb"}, "buy": {"amount": 1.2, "redeemScript": "63a82003d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b68876a9143ea29256c9d2888ca23de42a8b8e69ca2ec235b167023f0db17576a914c5acca6ef39c843c7a9c3ad01b2da95fe2edf5ba6888ac", "redeemblocknum": 3391, "currency": "zcash", "locktime": 10, "initiator": "tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ", "p2sh": "t2HP59RpfR34nBCWH4VVD497tkc2ikzgniP", "fulfiller": "tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY"}, "commitment": "03d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b6"}
def mktrade():
db.create(test_trade, 'test')
trade = db.get('test')
return trade

View File

@ -83,12 +83,9 @@ def save_secret(secret):
#############################################
def instantiate(tradedata):
print("tradedata", tradedata)
if type(tradedata) == dict:
string = str(tradedata)
print("STRING", string)
tradedata = json.loads(ast.literal_eval(tradedata))
print("@", tradedata)
elif type(tradedata) == hex:
hexstr = x2s(tradedata)
tradedata = ast.literal_eval(hexstr)