| Total Complexity | 57 |
| Total Lines | 517 |
| Duplicated Lines | 25.34 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like test_btcde_func often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | from unittest import TestCase |
||
| 2 | import hashlib |
||
| 3 | import hmac |
||
| 4 | import requests_mock |
||
| 5 | from mock import patch |
||
| 6 | import json |
||
| 7 | import btcde |
||
| 8 | from decimal import Decimal |
||
| 9 | from future.standard_library import install_aliases |
||
| 10 | install_aliases() |
||
| 11 | |||
| 12 | from urllib.parse import urlencode |
||
| 13 | |||
| 14 | @patch('btcde.log') |
||
| 15 | @requests_mock.Mocker() |
||
| 16 | class TestBtcdeAPIDocu(TestCase): |
||
| 17 | '''Tests are as in bitcoin.de API documentation. |
||
| 18 | https://www.bitcoin.de/de/api/tapi/v2/docu''' |
||
| 19 | |||
| 20 | def sampleData(self, file): |
||
| 21 | '''Retrieve sample data from json files.''' |
||
| 22 | filepath = 'tests/resources/{}.json'.format(file) |
||
| 23 | data = json.load(open(filepath)) |
||
| 24 | return data |
||
| 25 | |||
| 26 | def sortParams(self, url, params={}): |
||
| 27 | '''To sort params for url string.''' |
||
| 28 | self.encoded_string = '' |
||
| 29 | if len(params) > 0: |
||
| 30 | self.encoded_string = urlencode(params) |
||
| 31 | self.url = url + '?' + self.encoded_string |
||
| 32 | else: |
||
| 33 | self.url = url |
||
| 34 | return self.url, self.encoded_string |
||
| 35 | |||
| 36 | def verifySignature(self, url, method, nonce, params): |
||
| 37 | '''To verify API Signature.''' |
||
| 38 | self.XAPINONCE = nonce |
||
| 39 | self.url, self.encoded_string = self.sortParams(url, params) |
||
| 40 | if method == 'POST': |
||
| 41 | md5_encoded_query_string = hashlib.md5(self.encoded_string.encode() |
||
| 42 | ).hexdigest() |
||
| 43 | else: |
||
| 44 | md5_encoded_query_string = hashlib.md5(b'').hexdigest() |
||
| 45 | hmac_data = '{method}#{url}#{key}#{nonce}#{md5}'\ |
||
| 46 | .format(method=method, |
||
| 47 | url=self.url, |
||
| 48 | key=self.XAPIKEY, |
||
| 49 | nonce=str(self.XAPINONCE), |
||
| 50 | md5=md5_encoded_query_string) |
||
| 51 | hmac_signed = hmac.new(bytearray(self.XAPISECRET.encode()), |
||
| 52 | msg=hmac_data.encode(), |
||
| 53 | digestmod=hashlib.sha256).hexdigest() |
||
| 54 | return hmac_signed |
||
| 55 | |||
| 56 | def setUp(self): |
||
| 57 | self.XAPIKEY = 'f00b4r' |
||
| 58 | self.XAPISECRET = 'b4rf00' |
||
| 59 | self.conn = btcde.Connection(self.XAPIKEY, self.XAPISECRET) |
||
| 60 | self.XAPINONCE = self.conn.nonce |
||
| 61 | |||
| 62 | def tearDown(self): |
||
| 63 | del self.XAPIKEY |
||
| 64 | del self.XAPISECRET |
||
| 65 | del self.conn |
||
| 66 | |||
| 67 | def test_signature_post(self, mock_logger, m): |
||
| 68 | '''Test signature on a post request.''' |
||
| 69 | params = {'type': 'buy', |
||
| 70 | 'trading_pair': 'btceur', |
||
| 71 | 'max_amount': 10, |
||
| 72 | 'price': 1337} |
||
| 73 | response = self.sampleData('createOrder') |
||
| 74 | m.post(requests_mock.ANY, json=response, status_code=201) |
||
| 75 | self.conn.createOrder(params['type'], |
||
| 76 | params['trading_pair'], |
||
| 77 | params['max_amount'], |
||
| 78 | params['price']) |
||
| 79 | history = m.request_history |
||
| 80 | request_signature = history[0].headers.get('X-API-SIGNATURE') |
||
| 81 | verified_signature = self.verifySignature(self.conn.orderuri, |
||
| 82 | 'POST', self.conn.nonce, |
||
| 83 | params) |
||
| 84 | self.assertEqual(request_signature, verified_signature) |
||
| 85 | self.assertTrue(mock_logger.debug.called) |
||
| 86 | |||
| 87 | def test_signature_get(self, mock_logger, m): |
||
| 88 | '''Test signature on a get request.''' |
||
| 89 | params = {'type': 'buy', |
||
| 90 | 'trading_pair': 'btceur'} |
||
| 91 | response = self.sampleData('showOrderbook_buy') |
||
| 92 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 93 | self.conn.showOrderbook(params.get('type'), |
||
| 94 | params.get('trading_pair')) |
||
| 95 | history = m.request_history |
||
| 96 | request_signature = history[0].headers.get('X-API-SIGNATURE') |
||
| 97 | verified_signature = self.verifySignature(self.conn.orderuri, |
||
| 98 | 'GET', self.conn.nonce, |
||
| 99 | params) |
||
| 100 | self.assertEqual(request_signature, verified_signature) |
||
| 101 | self.assertTrue(mock_logger.debug.called) |
||
| 102 | |||
| 103 | def test_signature_delete(self, mock_logger, m): |
||
| 104 | '''Test signature on a delete request.''' |
||
| 105 | order_id = 'A1234BC' |
||
| 106 | trading_pair = 'btceur' |
||
| 107 | m.delete(requests_mock.ANY, json={}, status_code=200) |
||
| 108 | self.conn.deleteOrder(order_id, trading_pair) |
||
| 109 | history = m.request_history |
||
| 110 | request_signature = history[0].headers.get('X-API-SIGNATURE') |
||
| 111 | url = self.conn.orderuri + "/" + order_id + "/" + trading_pair |
||
| 112 | verified_signature = self.verifySignature(url, 'DELETE', self.conn.nonce, {}) |
||
| 113 | self.assertEqual(request_signature, verified_signature) |
||
| 114 | self.assertTrue(mock_logger.debug.called) |
||
| 115 | |||
| 116 | View Code Duplication | def test_show_orderbook(self, mock_logger, m): |
|
|
|
|||
| 117 | '''Test function showOrderbook.''' |
||
| 118 | params = {'type': 'buy', |
||
| 119 | 'trading_pair': 'btceur', |
||
| 120 | 'price': 1337} |
||
| 121 | base_url = 'https://api.bitcoin.de/v2/orders' |
||
| 122 | url_args = '?' + urlencode(params) |
||
| 123 | response = self.sampleData('showOrderbook_buy') |
||
| 124 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 125 | self.conn.showOrderbook(params.get('type'), |
||
| 126 | params.get('trading_pair'), |
||
| 127 | price=params.get('price')) |
||
| 128 | history = m.request_history |
||
| 129 | self.assertEqual(history[0].method, "GET") |
||
| 130 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 131 | self.assertTrue(mock_logger.debug.called) |
||
| 132 | |||
| 133 | View Code Duplication | def test_createOrder(self, mock_logger, m): |
|
| 134 | '''Test function createOrder.''' |
||
| 135 | params = {'type': 'buy', |
||
| 136 | 'trading_pair': 'btceur', |
||
| 137 | 'max_amount': '10', |
||
| 138 | 'price': '10'} |
||
| 139 | base_url = 'https://api.bitcoin.de/v2/orders' |
||
| 140 | url_args = '?' + urlencode(params) |
||
| 141 | response = self.sampleData('createOrder') |
||
| 142 | m.post(requests_mock.ANY, json=response, status_code=201) |
||
| 143 | self.conn.createOrder(params.get('type'), |
||
| 144 | params.get('trading_pair'), |
||
| 145 | params.get('max_amount'), |
||
| 146 | params.get('price')) |
||
| 147 | history = m.request_history |
||
| 148 | self.assertEqual(history[0].method, "POST") |
||
| 149 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 150 | self.assertTrue(mock_logger.debug.called) |
||
| 151 | |||
| 152 | View Code Duplication | def test_deleteOrder(self, mock_logger, m): |
|
| 153 | '''Test function deleteOrder.''' |
||
| 154 | params = {'trading_pair': 'btceur', |
||
| 155 | 'order_id': '1337'} |
||
| 156 | base_url = 'https://api.bitcoin.de/v2/orders' |
||
| 157 | url_args = '/{}/{}'.format(params.get('order_id'), |
||
| 158 | params.get('trading_pair')) |
||
| 159 | response = self.sampleData('deleteOrder') |
||
| 160 | m.delete(requests_mock.ANY, json=response, status_code=200) |
||
| 161 | self.conn.deleteOrder(params.get('order_id'), |
||
| 162 | params.get('trading_pair')) |
||
| 163 | history = m.request_history |
||
| 164 | self.assertEqual(history[0].method, "DELETE") |
||
| 165 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 166 | self.assertTrue(mock_logger.debug.called) |
||
| 167 | |||
| 168 | View Code Duplication | def test_showMyOrders(self, mock_logger, m): |
|
| 169 | '''Test function showMyOrders.''' |
||
| 170 | params = {'type': 'buy', |
||
| 171 | 'trading_pair': 'btceur'} |
||
| 172 | base_url = 'https://api.bitcoin.de/v2/orders/my_own' |
||
| 173 | url_args = '?' + urlencode(params) |
||
| 174 | response = self.sampleData('showMyOrders') |
||
| 175 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 176 | self.conn.showMyOrders(type=params.get('type'), |
||
| 177 | trading_pair=params.get('trading_pair')) |
||
| 178 | history = m.request_history |
||
| 179 | self.assertEqual(history[0].method, "GET") |
||
| 180 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 181 | self.assertTrue(mock_logger.debug.called) |
||
| 182 | |||
| 183 | def test_showMyOrderDetails(self, mock_logger, m): |
||
| 184 | '''Test function showMyOrderDetails.''' |
||
| 185 | params = {'order_id': '1337'} |
||
| 186 | base_url = 'https://api.bitcoin.de/v2/orders/{}'\ |
||
| 187 | .format(params.get('order_id')) |
||
| 188 | url_args = '' |
||
| 189 | response = self.sampleData('showMyOrderDetails') |
||
| 190 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 191 | self.conn.showMyOrderDetails(params.get('order_id')) |
||
| 192 | history = m.request_history |
||
| 193 | self.assertEqual(history[0].method, "GET") |
||
| 194 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 195 | self.assertTrue(mock_logger.debug.called) |
||
| 196 | |||
| 197 | def test_executeTrade(self, mock_logger, m): |
||
| 198 | '''Test function executeTrade.''' |
||
| 199 | params = {'order_id': '1337', |
||
| 200 | 'type': 'buy', |
||
| 201 | 'trading_pair': 'btceur', |
||
| 202 | 'amount': '10'} |
||
| 203 | base_url = 'https://api.bitcoin.de/v2/trades/{}'\ |
||
| 204 | .format(params.get('order_id')) |
||
| 205 | url_args = '?' + urlencode(params) |
||
| 206 | response = self.sampleData('executeTrade') |
||
| 207 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 208 | m.post(requests_mock.ANY, json=response, status_code=201) |
||
| 209 | self.conn.executeTrade(params.get('order_id'), |
||
| 210 | params.get('type'), |
||
| 211 | params.get('trading_pair'), |
||
| 212 | params.get('amount')) |
||
| 213 | history = m.request_history |
||
| 214 | self.assertEqual(history[0].method, "POST") |
||
| 215 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 216 | self.assertTrue(mock_logger.debug.called) |
||
| 217 | |||
| 218 | def test_showMyTrades(self, mock_logger, m): |
||
| 219 | '''Test function showMyTrades.''' |
||
| 220 | base_url = 'https://api.bitcoin.de/v2/trades' |
||
| 221 | url_args = '' |
||
| 222 | response = self.sampleData('showMyTrades') |
||
| 223 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 224 | self.conn.showMyTrades() |
||
| 225 | history = m.request_history |
||
| 226 | self.assertEqual(history[0].method, "GET") |
||
| 227 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 228 | self.assertTrue(mock_logger.debug.called) |
||
| 229 | |||
| 230 | View Code Duplication | def test_showMyTrades_with_params(self, mock_logger, m): |
|
| 231 | '''Test function showMyTrades with parameters.''' |
||
| 232 | params = {'type': 'buy', |
||
| 233 | 'trading_pair': 'btceur'} |
||
| 234 | base_url = 'https://api.bitcoin.de/v2/trades' |
||
| 235 | url_args = '?' + urlencode(params) |
||
| 236 | response = self.sampleData('showMyTrades') |
||
| 237 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 238 | self.conn.showMyTrades(type=params.get('type'), |
||
| 239 | trading_pair=params.get('trading_pair')) |
||
| 240 | history = m.request_history |
||
| 241 | self.assertEqual(history[0].method, "GET") |
||
| 242 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 243 | self.assertTrue(mock_logger.debug.called) |
||
| 244 | |||
| 245 | def test_showMyTradeDetails(self, mock_logger, m): |
||
| 246 | '''Test function showMyTradeDetails.''' |
||
| 247 | params = {'trade_id': '1337'} |
||
| 248 | base_url = 'https://api.bitcoin.de/v2/trades' |
||
| 249 | url_args = '/{}'.format(params.get('trade_id')) |
||
| 250 | response = self.sampleData('showMyTradeDetails') |
||
| 251 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 252 | self.conn.showMyTradeDetails(params.get('trade_id')) |
||
| 253 | history = m.request_history |
||
| 254 | self.assertEqual(history[0].method, "GET") |
||
| 255 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 256 | self.assertTrue(mock_logger.debug.called) |
||
| 257 | |||
| 258 | def test_showAccountInfo(self, mock_logger, m): |
||
| 259 | '''Test function showAccountInfo.''' |
||
| 260 | base_url = 'https://api.bitcoin.de/v2/account' |
||
| 261 | url_args = '' |
||
| 262 | response = self.sampleData('showAccountInfo') |
||
| 263 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 264 | self.conn.showAccountInfo() |
||
| 265 | history = m.request_history |
||
| 266 | self.assertEqual(history[0].method, "GET") |
||
| 267 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 268 | self.assertTrue(mock_logger.debug.called) |
||
| 269 | |||
| 270 | def test_showOrderbookCompact(self, mock_logger, m): |
||
| 271 | '''Test function showOrderbookCompact.''' |
||
| 272 | params = {'trading_pair': 'btceur'} |
||
| 273 | base_url = 'https://api.bitcoin.de/v2/orders/compact' |
||
| 274 | url_args = '?trading_pair={}'.format(params.get('trading_pair')) |
||
| 275 | response = self.sampleData('showOrderbookCompact') |
||
| 276 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 277 | self.conn.showOrderbookCompact(params.get('trading_pair')) |
||
| 278 | history = m.request_history |
||
| 279 | self.assertEqual(history[0].method, "GET") |
||
| 280 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 281 | self.assertTrue(mock_logger.debug.called) |
||
| 282 | |||
| 283 | def test_showPublicTradeHistory(self, mock_logger, m): |
||
| 284 | '''Test function showPublicTradeHistory.''' |
||
| 285 | params = {'trading_pair': 'btceur'} |
||
| 286 | base_url = 'https://api.bitcoin.de/v2/trades/history' |
||
| 287 | url_args = '?trading_pair={}'.format(params.get('trading_pair')) |
||
| 288 | response = self.sampleData('showPublicTradeHistory') |
||
| 289 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 290 | self.conn.showPublicTradeHistory(params.get('trading_pair')) |
||
| 291 | history = m.request_history |
||
| 292 | self.assertEqual(history[0].method, "GET") |
||
| 293 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 294 | self.assertTrue(mock_logger.debug.called) |
||
| 295 | |||
| 296 | def test_showPublicTradeHistory_since(self, mock_logger, m): |
||
| 297 | '''Test function showPublicTradeHistory with since_tid.''' |
||
| 298 | params = {'trading_pair': 'btceur', 'since_tid': '123'} |
||
| 299 | base_url = 'https://api.bitcoin.de/v2/trades/history' |
||
| 300 | url_args = '?' + urlencode(params) |
||
| 301 | response = self.sampleData('showPublicTradeHistory') |
||
| 302 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 303 | self.conn.showPublicTradeHistory(params.get('trading_pair'), |
||
| 304 | since_tid=params.get('since_tid')) |
||
| 305 | history = m.request_history |
||
| 306 | self.assertEqual(history[0].method, "GET") |
||
| 307 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 308 | self.assertTrue(mock_logger.debug.called) |
||
| 309 | |||
| 310 | View Code Duplication | def test_showRates(self, mock_logger, m): |
|
| 311 | '''Test function showRates.''' |
||
| 312 | params = {'trading_pair': 'btceur'} |
||
| 313 | base_url = 'https://api.bitcoin.de/v2/rates' |
||
| 314 | url_args = '?trading_pair={}'.format(params.get('trading_pair')) |
||
| 315 | response = self.sampleData('showRates') |
||
| 316 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 317 | self.conn.showRates(params.get('trading_pair')) |
||
| 318 | history = m.request_history |
||
| 319 | self.assertEqual(history[0].method, "GET") |
||
| 320 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 321 | self.assertTrue(mock_logger.debug.called) |
||
| 322 | |||
| 323 | View Code Duplication | def test_showAccountLedger(self, mock_logger, m): |
|
| 324 | '''Test function showAccountLedger.''' |
||
| 325 | params = {'currency': 'btc' } |
||
| 326 | base_url = 'https://api.bitcoin.de/v2/account/ledger' |
||
| 327 | url_args = '?currency={}'.format(params.get('currency')) |
||
| 328 | response = self.sampleData('showAccountLedger') |
||
| 329 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 330 | r = self.conn.showAccountLedger(params.get('currency')) |
||
| 331 | history = m.request_history |
||
| 332 | self.assertEqual(history[0].method, "GET") |
||
| 333 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 334 | self.assertTrue(mock_logger.debug.called) |
||
| 335 | |||
| 336 | def test_urlEncoding(self, mock_logger, m): |
||
| 337 | '''Test URL encoding on parameters.''' |
||
| 338 | params = {'currency': 'btc', 'date_start': '2018-01-01T01:00:00+01:00'} |
||
| 339 | base_url = 'https://api.bitcoin.de/v2/account/ledger' |
||
| 340 | url_args = '?' + urlencode(params) |
||
| 341 | response = self.sampleData('showAccountLedger') |
||
| 342 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 343 | r = self.conn.showAccountLedger(params['currency'], date_start="2018-01-01T01:00:00+01:00") |
||
| 344 | history = m.request_history |
||
| 345 | self.assertEqual(history[0].method, "GET") |
||
| 346 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 347 | self.assertTrue(mock_logger.debug.called) |
||
| 348 | |||
| 349 | View Code Duplication | def test_allowed_pairs(self, mock_logger, m): |
|
| 350 | '''Test the allowed trading pairs.''' |
||
| 351 | i = 0 |
||
| 352 | for pair in ['btceur', 'bcheur', 'etheur', 'btgeur', 'bsveur']: |
||
| 353 | params = {'trading_pair': pair} |
||
| 354 | base_url = 'https://api.bitcoin.de/v2/rates' |
||
| 355 | url_args = '?trading_pair={}'.format(params.get('trading_pair')) |
||
| 356 | response = self.sampleData('showRates') |
||
| 357 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 358 | self.conn.showRates(params.get('trading_pair')) |
||
| 359 | history = m.request_history |
||
| 360 | self.assertEqual(history[i].method, "GET") |
||
| 361 | self.assertEqual(history[i].url, base_url + url_args) |
||
| 362 | self.assertTrue(mock_logger.debug.called) |
||
| 363 | i += 1 |
||
| 364 | |||
| 365 | View Code Duplication | def test_allowed_currency(self, mock_logger, m): |
|
| 366 | '''Test the allowed currencies.''' |
||
| 367 | i = 0 |
||
| 368 | for curr in ['btc', 'bch', 'eth', 'btg', 'bsv']: |
||
| 369 | params = {'currency': curr} |
||
| 370 | base_url = 'https://api.bitcoin.de/v2/account/ledger' |
||
| 371 | url_args = '?currency={}'.format(params.get('currency')) |
||
| 372 | response = self.sampleData('showAccountLedger') |
||
| 373 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 374 | self.conn.showAccountLedger(params.get('currency')) |
||
| 375 | history = m.request_history |
||
| 376 | self.assertEqual(history[i].method, "GET") |
||
| 377 | self.assertEqual(history[i].url, base_url + url_args) |
||
| 378 | self.assertTrue(mock_logger.debug.called) |
||
| 379 | i += 1 |
||
| 380 | |||
| 381 | def test_decimal_parsing(self, mock_logger, m): |
||
| 382 | '''Test if the decimal parsing calculates correctly.''' |
||
| 383 | params = {'type': 'buy', |
||
| 384 | 'trading_pair': 'btceur', |
||
| 385 | 'max_amount': 10, |
||
| 386 | 'price': 1337} |
||
| 387 | response = self.sampleData('showOrderbook_buy') |
||
| 388 | m.get(requests_mock.ANY, json=response, status_code=200) |
||
| 389 | data = self.conn.showOrderbook(params.get('type'), |
||
| 390 | params.get('trading_pair'), |
||
| 391 | price=params.get('price')) |
||
| 392 | price = data.get('orders')[0].get('price') |
||
| 393 | self.assertIsInstance(price, Decimal) |
||
| 394 | self.assertEqual(price + Decimal('22.3'), Decimal('2232.2')) |
||
| 395 | self.assertNotEqual(float(price) + float('22.3'), float('2232.2')) |
||
| 396 | |||
| 397 | |||
| 398 | class TestBtcdeExceptions(TestCase): |
||
| 399 | '''Test for Exception Handling.''' |
||
| 400 | |||
| 401 | def sampleData(self, file): |
||
| 402 | '''Retrieve sample data from json files.''' |
||
| 403 | filepath = 'tests/resources/{}.json'.format(file) |
||
| 404 | data = json.load(open(filepath)) |
||
| 405 | return data |
||
| 406 | |||
| 407 | def setUp(self): |
||
| 408 | self.XAPIKEY = 'f00b4r' |
||
| 409 | self.XAPISECRET = 'b4rf00' |
||
| 410 | self.conn = btcde.Connection(self.XAPIKEY, self.XAPISECRET) |
||
| 411 | self.XAPINONCE = self.conn.nonce |
||
| 412 | |||
| 413 | def tearDown(self): |
||
| 414 | del self.XAPIKEY |
||
| 415 | del self.XAPISECRET |
||
| 416 | del self.conn |
||
| 417 | |||
| 418 | @requests_mock.Mocker() |
||
| 419 | def test_dont_fail_on_non_utf8(self, m): |
||
| 420 | '''Test if no exception raises with a non-utf8 response. |
||
| 421 | https://github.com/peshay/btcde/issues/12''' |
||
| 422 | filepath = 'tests/resources/NonUTF8' |
||
| 423 | with open(filepath, 'r') as f: |
||
| 424 | m.post(requests_mock.ANY, content=f.read().encode('utf-16', 'replace'), status_code=403) |
||
| 425 | try: |
||
| 426 | self.conn.executeTrade('foobar', 'buy', 'btceur', '0') |
||
| 427 | self.assertTrue(True) |
||
| 428 | except UnicodeDecodeError: |
||
| 429 | self.assertTrue(False) |
||
| 430 | |||
| 431 | @requests_mock.Mocker() |
||
| 432 | def test_APIException(self, m): |
||
| 433 | '''Test API Exception.''' |
||
| 434 | params = {'type': 'buy', |
||
| 435 | 'trading_pair': 'btceur', |
||
| 436 | 'max_amount': 10, |
||
| 437 | 'price': 13} |
||
| 438 | base_url = 'https://api.bitcoin.de/v2/orders' |
||
| 439 | url_args = '?' + urlencode(params) |
||
| 440 | response = self.sampleData('error') |
||
| 441 | m.post(requests_mock.ANY, json=response, status_code=400) |
||
| 442 | self.conn.createOrder(params.get('type'), |
||
| 443 | params.get('trading_pair'), params.get('max_amount'), |
||
| 444 | price=params.get('price')) |
||
| 445 | history = m.request_history |
||
| 446 | self.assertEqual(history[0].method, "POST") |
||
| 447 | self.assertEqual(history[0].url, base_url + url_args) |
||
| 448 | |||
| 449 | @patch('btcde.log') |
||
| 450 | def test_RequestException(self, mock_logger): |
||
| 451 | '''Test Requests Exception.''' |
||
| 452 | params = {'type': 'buy', |
||
| 453 | 'trading_pair': 'btceur', |
||
| 454 | 'max_amount': 10, |
||
| 455 | 'price': 13} |
||
| 456 | self.conn.orderuri = 'https://foo.bar' |
||
| 457 | self.conn.createOrder(params.get('type'), |
||
| 458 | params.get('trading_pair'), params.get('max_amount'), |
||
| 459 | price=params.get('price')) |
||
| 460 | self.assertTrue(mock_logger.warning.called) |
||
| 461 | |||
| 462 | def test_TradingPairValueException(self): |
||
| 463 | '''Test wrong traiding_pair Value Exception.''' |
||
| 464 | with self.assertRaises(ValueError) as context: |
||
| 465 | self.conn.deleteOrder('123', 'usdeur') |
||
| 466 | self.assertTrue('usdeur is not any of' in str(context.exception)) |
||
| 467 | |||
| 468 | def test_OrderTypeValueException(self): |
||
| 469 | '''Test wrong type Value Exception.''' |
||
| 470 | with self.assertRaises(ValueError) as context: |
||
| 471 | self.conn.createOrder('fail', 'btceur', '100', '100') |
||
| 472 | self.assertTrue('fail is not any of' in str(context.exception)) |
||
| 473 | |||
| 474 | def test_CurrencyValueException(self): |
||
| 475 | '''Test wrong currency Value Exception.''' |
||
| 476 | with self.assertRaises(ValueError) as context: |
||
| 477 | self.conn.showAccountLedger('usd') |
||
| 478 | self.assertTrue('usd is not any of' in str(context.exception)) |
||
| 479 | |||
| 480 | def test_BankSeatValueException(self): |
||
| 481 | '''Test wrong seat_of_bank Value Exception.''' |
||
| 482 | with self.assertRaises(ValueError) as context: |
||
| 483 | self.conn.showOrderbook('buy', 'btceur', seat_of_bank='SZ') |
||
| 484 | self.assertTrue('SZ is not any of' in str(context.exception)) |
||
| 485 | |||
| 486 | def test_TrustLevelValueException(self): |
||
| 487 | '''Test wrong trust_level Value Exception.''' |
||
| 488 | with self.assertRaises(ValueError) as context: |
||
| 489 | self.conn.createOrder('buy', 'btceur', '100', '100', |
||
| 490 | min_trust_level='foo') |
||
| 491 | self.assertTrue('foo is not any of' in str(context.exception)) |
||
| 492 | |||
| 493 | def test_PaymentOptionValueException(self): |
||
| 494 | '''Test wrong payment_option Value Exception.''' |
||
| 495 | with self.assertRaises(ValueError) as context: |
||
| 496 | self.conn.createOrder('buy', 'btceur', '100', '100', |
||
| 497 | payment_option=4) |
||
| 498 | self.assertTrue('4 is not any of' in str(context.exception)) |
||
| 499 | |||
| 500 | def test_OrderStateValueException(self): |
||
| 501 | '''Test wrong state Value Exception.''' |
||
| 502 | with self.assertRaises(ValueError) as context: |
||
| 503 | self.conn.showMyOrders(state=1) |
||
| 504 | self.assertTrue('1 is not any of' in str(context.exception)) |
||
| 505 | |||
| 506 | def test_TradeStateValueException(self): |
||
| 507 | '''Test wrong state Value Exception.''' |
||
| 508 | with self.assertRaises(ValueError) as context: |
||
| 509 | self.conn.showMyTrades(state=-2) |
||
| 510 | self.assertTrue('-2 is not any of' in str(context.exception)) |
||
| 511 | |||
| 512 | def test_UnknownKeyException(self): |
||
| 513 | '''Test wrong Key Exception.''' |
||
| 514 | with self.assertRaises(KeyError) as context: |
||
| 515 | self.conn.showMyOrders(foo=4) |
||
| 516 | self.assertTrue('foo is not any of' in str(context.exception)) |
||
| 517 |