|
1
|
|
|
from unittest import TestCase |
|
2
|
|
|
from mock import patch |
|
3
|
|
|
import btcde |
|
4
|
|
|
|
|
5
|
|
|
class TestBtcdeApi(TestCase): |
|
6
|
|
|
"""Test Api Functions.""" |
|
7
|
|
|
|
|
8
|
|
|
def setUp(self): |
|
9
|
|
|
self.patcher = patch('btcde.APIConnect') |
|
10
|
|
|
self.mock_APIConnect = self.patcher.start() |
|
11
|
|
|
|
|
12
|
|
|
def tearDown(self): |
|
13
|
|
|
self.patcher.stop() |
|
14
|
|
|
|
|
15
|
|
|
def assertArguments(self, expected_arguments, mock_APIConnect): |
|
16
|
|
|
for idx, expected in enumerate(expected_arguments): |
|
17
|
|
|
actual = mock_APIConnect.call_args[0][idx] |
|
18
|
|
|
self.assertEqual(actual, expected, |
|
19
|
|
|
'Argument {} with value {} ' |
|
20
|
|
|
'does not match expected {}'.format(idx, |
|
21
|
|
|
actual, |
|
22
|
|
|
expected)) |
|
23
|
|
|
|
|
24
|
|
|
def test_showOrderbook_buy_and_sell(self): |
|
25
|
|
|
methods = 'buy', 'sell' |
|
26
|
|
|
trading_pairs = ['btceur', 'bcheur', 'etheur'] |
|
27
|
|
|
for trading_pair in trading_pairs: |
|
28
|
|
|
for method in methods: |
|
29
|
|
|
result = btcde.showOrderbook('mock', method, trading_pair) |
|
30
|
|
|
expected_arguments = ['mock', 'GET', {'type': method, 'trading_pair': trading_pair}, btcde.orderuri] |
|
31
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
32
|
|
|
|
|
33
|
|
|
def test_createOrder(self): |
|
34
|
|
|
OrderType = 'now' |
|
35
|
|
|
trading_pair = 'btceur' |
|
36
|
|
|
max_amount = 5 |
|
37
|
|
|
price = 10 |
|
38
|
|
|
result = btcde.createOrder('mock', OrderType, trading_pair, max_amount, price) |
|
39
|
|
|
params = {'type': OrderType, 'max_amount': max_amount, 'price': price, 'trading_pair': trading_pair} |
|
40
|
|
|
expected_arguments = ['mock', 'POST', params, btcde.orderuri] |
|
41
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
42
|
|
|
|
|
43
|
|
|
def test_deleteOrder(self): |
|
44
|
|
|
order_id = '42' |
|
45
|
|
|
trading_pair = 'btceur' |
|
46
|
|
|
result = btcde.deleteOrder('mock', order_id, trading_pair) |
|
47
|
|
|
params = {'order_id': order_id, 'trading_pair': trading_pair} |
|
48
|
|
|
expected_arguments = ['mock', 'DELETE', params, btcde.orderuri + "/" + order_id + "/" + trading_pair] |
|
49
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
50
|
|
|
|
|
51
|
|
|
def test_showMyOrders(self): |
|
52
|
|
|
result = btcde.showMyOrders('mock') |
|
53
|
|
|
expected_arguments = ['mock', 'GET', {}, btcde.orderuri + '/my_own'] |
|
54
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
55
|
|
|
|
|
56
|
|
|
def test_showMyOrderDetails(self): |
|
57
|
|
|
order_id = '42' |
|
58
|
|
|
result = btcde.showMyOrderDetails('mock', order_id) |
|
59
|
|
|
params = {'order_id': order_id} |
|
60
|
|
|
expected_arguments = ['mock', 'GET', params, btcde.orderuri + '/' + order_id] |
|
61
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
62
|
|
|
|
|
63
|
|
|
def test_executeTrade(self): |
|
64
|
|
|
order_id = '42' |
|
65
|
|
|
OrderType = 'foobar' |
|
66
|
|
|
amount = '73' |
|
67
|
|
|
trading_pair = 'btceur' |
|
68
|
|
|
result = btcde.executeTrade('mock', order_id, OrderType, trading_pair, amount) |
|
69
|
|
|
params = {'order_id': order_id, 'type': OrderType, 'amount': amount, 'trading_pair': trading_pair} |
|
70
|
|
|
expected_arguments = ['mock', 'POST', params, btcde.tradeuri + '/' + order_id] |
|
71
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
72
|
|
|
|
|
73
|
|
|
def test_showMyTrades(self): |
|
74
|
|
|
result = btcde.showMyTrades('mock') |
|
75
|
|
|
expected_arguments = ['mock', 'GET', {}, btcde.tradeuri] |
|
76
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
77
|
|
|
|
|
78
|
|
|
def test_showMyTradeDetails(self): |
|
79
|
|
|
trade_id = '42' |
|
80
|
|
|
result = btcde.showMyTradeDetails('mock', trade_id) |
|
81
|
|
|
params = {'trade_id': trade_id} |
|
82
|
|
|
expected_arguments = ['mock', 'GET', params, btcde.tradeuri + '/' + trade_id] |
|
83
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
84
|
|
|
|
|
85
|
|
|
def test_showAccountInfo(self): |
|
86
|
|
|
result = btcde.showAccountInfo('mock') |
|
87
|
|
|
params = {} |
|
88
|
|
|
expected_arguments = ['mock', 'GET', params, btcde.accounturi] |
|
89
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
90
|
|
|
|
|
91
|
|
|
def test_showOrderbookCompact(self): |
|
92
|
|
|
trading_pair = 'btceur' |
|
93
|
|
|
result = btcde.showOrderbookCompact('mock', trading_pair) |
|
94
|
|
|
params = {'trading_pair': trading_pair} |
|
95
|
|
|
expected_arguments = ['mock', 'GET', params, btcde.orderuri + '/compact'] |
|
96
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
97
|
|
|
|
|
98
|
|
|
def test_showPublicTradeHistory(self): |
|
99
|
|
|
trading_pair = 'btceur' |
|
100
|
|
|
result1 = btcde.showPublicTradeHistory('mock', trading_pair) |
|
101
|
|
|
params = {'trading_pair': trading_pair} |
|
102
|
|
|
expected_arguments = ['mock', 'GET', params, btcde.tradeuri + '/history'] |
|
103
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
104
|
|
|
self.tearDown() |
|
105
|
|
|
self.setUp() |
|
106
|
|
|
since_tid = '3' |
|
107
|
|
|
params.update({'since_tid': since_tid}) |
|
108
|
|
|
result2 = btcde.showPublicTradeHistory('mock', trading_pair, since_tid) |
|
109
|
|
|
expected_arguments = ['mock', 'GET', params, btcde.tradeuri + '/history'] |
|
110
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
111
|
|
|
|
|
112
|
|
|
def test_showRates(self): |
|
113
|
|
|
trading_pair = 'btceur' |
|
114
|
|
|
result = btcde.showRates('mock',trading_pair ) |
|
115
|
|
|
params = {'trading_pair': trading_pair} |
|
116
|
|
|
expected_arguments = ['mock', 'GET', params, btcde.apihost + '/' + btcde.apiversion + '/rates'] |
|
117
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
118
|
|
|
|
|
119
|
|
|
def test_showAccountLedger(self): |
|
120
|
|
|
result = btcde.showAccountLedger('mock') |
|
121
|
|
|
expected_arguments = ['mock', 'GET', {}, btcde.accounturi + '/ledger'] |
|
122
|
|
|
self.assertArguments(expected_arguments, self.mock_APIConnect) |
|
123
|
|
|
|
|
124
|
|
|
class TestSimpleFunctions(TestCase): |
|
125
|
|
|
|
|
126
|
|
|
def test_params_url(self): |
|
127
|
|
|
sample_params = { 'foo': 'bar', 'bar': 'foo'} |
|
128
|
|
|
result = btcde.params_url(sample_params, 'https://foo.bar') |
|
129
|
|
|
expected_result = 'https://foo.bar?bar=foo&foo=bar' |
|
130
|
|
|
self.assertEquals(result[0], expected_result) |
|
131
|
|
|
|
|
132
|
|
|
def test_params_url_wo_params(self): |
|
133
|
|
|
result = btcde.params_url({}, 'https://foo.bar') |
|
134
|
|
|
expected_result = 'https://foo.bar' |
|
135
|
|
|
self.assertEquals(result[0], expected_result) |
|
136
|
|
|
|