undo xcatconf change, pull in old tests, create a older for unit tests

This commit is contained in:
James Prestwich 2017-09-12 22:52:21 -06:00
parent de8ec2df54
commit 917acd1efc
No known key found for this signature in database
GPG Key ID: 519E010A79028CCC
5 changed files with 173 additions and 103 deletions

View File

@ -215,14 +215,20 @@ def newtrade(tradeid, **kwargs):
protocol = Protocol() protocol = Protocol()
print("Creating new XCAT trade...") print("Creating new XCAT trade...")
utils.erase_trade() utils.erase_trade()
conf = kwargs['conf'] if 'conf' in kwargs else 'regtest'
network = kwargs['network'] if 'network' in kwargs else 'regtest'
tradeid, trade = protocol.initialize_trade( tradeid, trade = protocol.initialize_trade(
tradeid, tradeid,
conf=kwargs['conf'], conf=conf,
network=kwargs['network']) network=network)
print("New trade created: {0}".format(trade)) print("New trade created: {0}".format(trade))
trade = protocol.seller_init(tradeid, trade, network=kwargs['network'])
trade = protocol.seller_init(tradeid, trade, network=network)
print("\nUse 'xcat exporttrade [tradeid]' to export the trade and sent " print("\nUse 'xcat exporttrade [tradeid]' to export the trade and sent "
"to the buyer.\n") "to the buyer.\n")
save_state(trade, tradeid) save_state(trade, tradeid)
return trade return trade

View File

@ -1,111 +1,59 @@
import unittest import unittest
import unittest.mock as mock
import xcat.cli as cli import xcat.cli as cli
# from xcat.tests.utils import test_trade import xcat.db as db
# from xcat.trades import Trade from xcat.protocol import Protocol
from xcat.tests.utils import mktrade
from xcat.trades import Trade # , Contract
class TestCLI(unittest.TestCase): class SimpleTestCase(unittest.TestCase):
def setUp(self):
def test_save_state(self): self.trade = mktrade()
pass
def test_checkSellStatus(self):
pass
def test_buyer_check_status(self):
pass
def test_seller_check_status(self):
pass
def test_checkBuyStatus(self):
pass
def test_importtrade(self):
pass
def test_wormhole_importtrade(self):
pass
def test_exporttrade(self): def test_exporttrade(self):
self.__class__.hexstr = cli.exporttrade('test')
self.assertTrue(int(self.hexstr, 16))
def test_importtrade(self):
# trade = cli.importtrade('test', self.__class__.hexstr)
pass pass
class CliTest(SimpleTestCase):
def test_findtrade(self): def test_findtrade(self):
pass # trade = cli.findtrade('test')
@mock.patch('xcat.cli.Protocol')
def test_find_role_test(self, mock_protocol):
mock_protocol().is_myaddr = lambda k: k == 'me'
test_contract = mock.MagicMock()
test_contract.initiator = 'me'
test_contract.fulfiller = 'me'
res = cli.find_role(test_contract)
self.assertEqual(res, 'test')
@mock.patch('xcat.cli.Protocol')
def test_find_role_initiator(self, mock_protocol):
mock_protocol().is_myaddr = lambda k: k == 'me'
test_contract = mock.MagicMock()
test_contract.initiator = 'me'
test_contract.fulfiller = 'you'
res = cli.find_role(test_contract)
self.assertEqual(res, 'initiator')
@mock.patch('xcat.cli.Protocol')
def test_find_role_fulfiller(self, mock_protocol):
mock_protocol().is_myaddr = lambda k: k == 'me'
test_contract = mock.MagicMock()
test_contract.initiator = 'you'
test_contract.fulfiller = 'me'
res = cli.find_role(test_contract)
self.assertEqual(res, 'fulfiller')
@mock.patch('xcat.cli.Protocol')
def test_find_role_error(self, mock_protocol):
mock_protocol().is_myaddr = lambda k: k == 'me'
test_contract = mock.MagicMock()
test_contract.initiator = 'you'
test_contract.fulfiller = 'you'
with self.assertRaises(ValueError) as context:
cli.find_role(test_contract)
self.assertTrue(
'You are not a participant in this contract.'
in str(context.exception))
def test_checktrade(self):
pass pass
def test_newtrade(self): def test_newtrade(self):
pass trade = cli.newtrade('new', conf='regtest')
self.assertTrue(isinstance(trade, Trade))
def test_listtrades(self):
pass
def test_fundsell(self): def test_fundsell(self):
pass protocol = Protocol()
def test_fundbuy(self): trade = db.get('new')
pass status = cli.seller_check_status(trade)
print("Trade status: {0}\n".format(status))
self.assertEqual(status, 'init')
def test_seller_redeem(self): fund_tx = protocol.fund_sell_contract(trade)
pass print("Sent fund_tx", fund_tx)
def test_buyer_redeem(self):
pass
# def test_fundbuy(self):
# trade = db.get('new')
# status = cli.buyer_check_status(trade)
# self.assertEqual(status, 'sellerFunded')
# fund_tx = cli.fund_contract(trade.buy)
#
# def test_seller_redeem(self):
# trade = db.get('new')
# status = cli.seller_check_status(trade)
# self.assertEqual(status, 'buyerFunded')
#
# def test_buyer_redeem(self):
# trade = db.get('new')
# status = cli.buyer_check_status(trade)
# self.assertEqual(status, 'sellerFunded')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

111
xcat/tests/unit/test_cli.py Normal file
View File

@ -0,0 +1,111 @@
import unittest
import unittest.mock as mock
import xcat.cli as cli
# from xcat.tests.utils import test_trade
# from xcat.trades import Trade
class TestCLI(unittest.TestCase):
def test_save_state(self):
pass
def test_checkSellStatus(self):
pass
def test_buyer_check_status(self):
pass
def test_seller_check_status(self):
pass
def test_checkBuyStatus(self):
pass
def test_importtrade(self):
pass
def test_wormhole_importtrade(self):
pass
def test_exporttrade(self):
pass
def test_findtrade(self):
pass
@mock.patch('xcat.cli.Protocol')
def test_find_role_test(self, mock_protocol):
mock_protocol().is_myaddr = lambda k: k == 'me'
test_contract = mock.MagicMock()
test_contract.initiator = 'me'
test_contract.fulfiller = 'me'
res = cli.find_role(test_contract)
self.assertEqual(res, 'test')
@mock.patch('xcat.cli.Protocol')
def test_find_role_initiator(self, mock_protocol):
mock_protocol().is_myaddr = lambda k: k == 'me'
test_contract = mock.MagicMock()
test_contract.initiator = 'me'
test_contract.fulfiller = 'you'
res = cli.find_role(test_contract)
self.assertEqual(res, 'initiator')
@mock.patch('xcat.cli.Protocol')
def test_find_role_fulfiller(self, mock_protocol):
mock_protocol().is_myaddr = lambda k: k == 'me'
test_contract = mock.MagicMock()
test_contract.initiator = 'you'
test_contract.fulfiller = 'me'
res = cli.find_role(test_contract)
self.assertEqual(res, 'fulfiller')
@mock.patch('xcat.cli.Protocol')
def test_find_role_error(self, mock_protocol):
mock_protocol().is_myaddr = lambda k: k == 'me'
test_contract = mock.MagicMock()
test_contract.initiator = 'you'
test_contract.fulfiller = 'you'
with self.assertRaises(ValueError) as context:
cli.find_role(test_contract)
self.assertTrue(
'You are not a participant in this contract.'
in str(context.exception))
def test_checktrade(self):
pass
def test_newtrade(self):
pass
def test_listtrades(self):
pass
def test_fundsell(self):
pass
def test_fundbuy(self):
pass
def test_seller_redeem(self):
pass
def test_buyer_redeem(self):
pass
if __name__ == '__main__':
unittest.main()

View File

@ -1,3 +1,5 @@
from xcat import db
test_trade = { test_trade = {
"sell": { "sell": {
"amount": 3.5, "amount": 3.5,
@ -17,4 +19,9 @@ test_trade = {
"initiator": "tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ", "initiator": "tmFRXyju7ANM7A9mg75ZjyhFW1UJEhUPwfQ",
"p2sh": "t2HP59RpfR34nBCWH4VVD497tkc2ikzgniP", "p2sh": "t2HP59RpfR34nBCWH4VVD497tkc2ikzgniP",
"fulfiller": "tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY"}, "fulfiller": "tmTjZSg4pX2Us6V5HttiwFZwj464fD2ZgpY"},
"commitment": "03d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b6"} "commitment": "03d58daab37238604b3e57d4a8bdcffa401dc497a9c1aa4f08ffac81616c22b6"}
def mktrade():
db.create(test_trade, 'test')
trade = db.get('test')
return trade

View File

@ -2,12 +2,12 @@
ADDRS = { ADDRS = {
'regtest': { 'regtest': {
"initiator": { "initiator": {
"bitcoin": "moAccTjGt6nRCoLKhVLrDCAkqDt7fnsAgC", "bitcoin": "mvc56qCEVj6p57xZ5URNC3v7qbatudHQ9b",
"zcash": "tmJBCsE4ZBcgi2LykoUyei5PDT1cQPkFxpf" "zcash": "tmTF7LMLjvEsGdcepWPUsh4vgJNrKMWwEyc"
}, },
"fulfiller": { "fulfiller": {
"bitcoin": "mxdJ47MeEeqrBDjHj7SrSLFoDuSP3G37t5", "bitcoin": "moRt56gJQGDNK46Y6fYy2HbooKnQXrTGDN",
"zcash": "tmBbe7hWtexP94638H1QUD9Z92BM4ZiXXgA" "zcash": "tmK3rGzHDqa78MCwEicx9VcY9ZWX9gCF2nd"
}, },
"amounts": {'buy': {'currency': 'zcash', 'amount': 0.02}, 'sell': {'currency': 'bitcoin', 'amount': 0.01}} "amounts": {'buy': {'currency': 'zcash', 'amount': 0.02}, 'sell': {'currency': 'bitcoin', 'amount': 0.01}}
}, },
@ -17,11 +17,9 @@ ADDRS = {
"zcash": "tmTF7LMLjvEsGdcepWPUsh4vgJNrKMWwEyc" "zcash": "tmTF7LMLjvEsGdcepWPUsh4vgJNrKMWwEyc"
}, },
"fulfiller": { "fulfiller": {
"bitcoin": "mn2boR7rYq9DaAWWrVN5MazHKFyf7UhdyU", "bitcoin": "mm2smEJjRN4xoijEfpb5XvYd8e3EYWezom",
"zcash": "tmErB22A1G74aq32aAh5AoqgQSJsAAAdT2p" "zcash": "tmPwPdceaJAHQn7UiRCVnJ5tXBXHVqWMkis"
}, },
"amounts": {'buy': {'currency': 'zcash', 'amount': 0.02}, 'sell': {'currency': 'bitcoin', 'amount': 0.01}} "amounts": {'buy': {'currency': 'zcash', 'amount': 0.02}, 'sell': {'currency': 'bitcoin', 'amount': 0.01}}
} }
} }
NETWORK = 'testnet'