Completed
Push — master ( 907443...d1611c )
by Andreas
26s
created

TestBtcdeApi.test_showMyOrders()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
c 2
b 0
f 1
dl 0
loc 4
rs 10
1
from unittest import TestCase 
2
from mock import patch
3
import btcde
4
5
6
class TestBtcdeApi(TestCase):
7
    """Test Api Functions."""
8
    
9
    def setUp(self):
10
        self.patcher = patch('btcde.APIConnect')
11
        self.mock_APIConnect = self.patcher.start()
12
    
13
    def tearDown(self):
14
        self.patcher.stop()
15
    
16
    def assertArguments(self, expected_arguments, mock_APIConnect):
17
        for idx, expected in enumerate(expected_arguments):
18
            actual = mock_APIConnect.call_args[0][idx]
19
            self.assertEqual(actual, expected,
20
                             'Argument {} with value {} '
21
                             'does not match expected {}'.format(idx,
22
                                                                 actual,
23
                                                                 expected))
24
    
25
    def test_showOrderbook_buy_and_sell(self):
26
        methods = 'buy', 'sell'
27
        for method in methods:
28
            result = btcde.showOrderbook('mock', method)
29
            expected_arguments = ['mock', 'GET', {'type': method}, btcde.orderuri]
30
            self.assertArguments(expected_arguments, self.mock_APIConnect)
31
    
32
    def test_createOrder(self):
33
        OrderType = 'now'
34
        max_amount = 5
35
        price = 10
36
        result = btcde.createOrder('mock', OrderType, max_amount, price)
37
        params = {'type': OrderType, 'max_amount': max_amount, 'price': price}
38
        expected_arguments = ['mock', 'POST', params, btcde.orderuri]
39
        self.assertArguments(expected_arguments, self.mock_APIConnect)
40
41
    def test_deleteOrder(self):
42
        order_id = '42'
43
        result = btcde.deleteOrder('mock', order_id)
44
        params = {'order_id': order_id}
45
        expected_arguments = ['mock', 'DELETE', params, btcde.orderuri + "/" + order_id]
46
        self.assertArguments(expected_arguments, self.mock_APIConnect)
47
                                                             
48
    def test_showMyOrders(self):
49
        result = btcde.showMyOrders('mock')
50
        expected_arguments = ['mock', 'GET', {}, btcde.orderuri + '/my_own']
51
        self.assertArguments(expected_arguments, self.mock_APIConnect)
52
53
    def test_showMyOrderDetails(self):
54
        order_id = '42'
55
        result = btcde.showMyOrderDetails('mock', order_id)
56
        params = {'order_id': order_id}
57
        expected_arguments = ['mock', 'GET', params, btcde.orderuri + '/' + order_id]
58
        self.assertArguments(expected_arguments, self.mock_APIConnect)
59
                                                             
60
    def test_executeTrade(self):
61
        order_id = '42'
62
        OrderType = 'foobar'
63
        amount = '73'
64
        result = btcde.executeTrade('mock', order_id, OrderType, amount)
65
        params = {'order_id': order_id, 'type': OrderType, 'amount': amount}
66
        expected_arguments = ['mock', 'POST', params, btcde.tradeuri + '/' + order_id]
67
        self.assertArguments(expected_arguments, self.mock_APIConnect)
68
69
    def test_showMyTrades(self):
70
        result = btcde.showMyTrades('mock')
71
        expected_arguments = ['mock', 'GET', {}, btcde.tradeuri]
72
        self.assertArguments(expected_arguments, self.mock_APIConnect)
73
        
74
    def test_showMyTradeDetails(self):
75
        trade_id = '42'
76
        result = btcde.showMyTradeDetails('mock', trade_id)
77
        params = {'trade_id': trade_id}
78
        expected_arguments = ['mock', 'GET', params, btcde.tradeuri + '/' + trade_id]
79
        self.assertArguments(expected_arguments, self.mock_APIConnect)
80
81
    def test_showAccountInfo(self):
82
        result = btcde.showAccountInfo('mock')
83
        params = {}
84
        expected_arguments = ['mock', 'GET', params, btcde.accounturi]
85
        self.assertArguments(expected_arguments, self.mock_APIConnect)
86
87
    def test_showOrderbookCompact(self):
88
        result = btcde.showOrderbookCompact('mock')
89
        params = {}
90
        expected_arguments = ['mock', 'GET', params, btcde.orderuri + '/compact']
91
        self.assertArguments(expected_arguments, self.mock_APIConnect)
92
93
    def test_showPublicTradeHistory(self):
94
        result1 = btcde.showPublicTradeHistory('mock')
95
        params = {}
96
        expected_arguments = ['mock', 'GET', params, btcde.tradeuri + '/history']
97
        self.assertArguments(expected_arguments, self.mock_APIConnect)
98
        self.tearDown()
99
        self.setUp()
100
        since_tid = '3'
101
        params = {'since_tid': since_tid}
102
        result2 = btcde.showPublicTradeHistory('mock', since_tid)
103
        expected_arguments = ['mock', 'GET', params, btcde.tradeuri + '/history']
104
        self.assertArguments(expected_arguments, self.mock_APIConnect) 
105
106
    def test_showRates(self):
107
        result = btcde.showRates('mock')
108
        params = {}
109
        expected_arguments = ['mock', 'GET', params, btcde.apihost + '/' + btcde.apiversion + '/rates']
110
        self.assertArguments(expected_arguments, self.mock_APIConnect)
111
112
    def test_showAccountLedger(self):
113
        result = btcde.showAccountLedger('mock')
114
        expected_arguments = ['mock', 'GET', {}, btcde.accounturi + '/ledger']
115
        self.assertArguments(expected_arguments, self.mock_APIConnect)
116
117
class TestSimpleFunctions(TestCase):
118
    
119
    def test_params_url(self):
120
        sample_params = { 'foo': 'bar', 'bar': 'foo'}
121
        result = btcde.params_url(sample_params, 'https://foo.bar')
122
        expected_result = 'https://foo.bar?bar=foo&foo=bar'
123
        self.assertEquals(result, expected_result)
124
    
125
    def test_params_url_wo_params(self):
126
        result = btcde.params_url({}, 'https://foo.bar')
127
        expected_result = 'https://foo.bar'
128
        self.assertEquals(result, expected_result)
129
        
130
        
131
class TestApiComm(TestCase):
132
    
133
    def setUp(self):
134
        self.fake_url = 'https://foo.bar/apiv1/'
135
        btcde.nonce = 9
136
        btcde.conn = btcde.Connection('foobar', 'barfoo')
137
   
138
    def tearDown(self):
139
        pass
140
141
    def test_api_signing_wout_post(self):
142
        '''Test API signing without POST.'''
143
        btcde.method = ''
144
        self.header = btcde.set_header(self.fake_url)
145
        self.assertEqual(self.header.get('X-API-KEY'),
146
                                         'foobar')
147
        self.assertEqual(self.header.get('X-API-NONCE'),
148
                                         '10')
149
        self.ApiSign = '51e827c7b856cd243cbcac28aeedb34e0676e7f01d469109e3dbe1553aa40c92'
150
        self.assertEqual(self.header.get('X-API-SIGNATURE'), self.ApiSign)
151
        
152
    def test_api_signing_with_post(self):
153
        '''Test API signing with POST.'''
154
        btcde.method = 'POST'
155
        btcde.encoded_string = 'foo&bar'
156
        self.header = btcde.set_header(self.fake_url)
157
        self.assertEqual(self.header.get('X-API-KEY'),
158
                                         'foobar')
159
        self.assertEqual(self.header.get('X-API-NONCE'),
160
                                         '10')
161
        self.ApiSign = '165515ed7f21a6480adac31b69edadb9416922b4b411da93bcc45d3288e5a361'
162
        self.assertEqual(self.header.get('X-API-SIGNATURE'), self.ApiSign)
163
        
164
#    def test_api_errors(self):
165
#        '''Test for valid API errors.'''
166
#        # TODO random return
167
#        patch('requests.status_code', return_value=201)
168
#        self.assertTrue(btcde.HandleAPIErrors(requests))
169
            
170