forked from jaapz/CryptsyPythonAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cryptsy.py
More file actions
114 lines (80 loc) · 3.41 KB
/
test_cryptsy.py
File metadata and controls
114 lines (80 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import pytest
import urllib2
from mock import Mock
from Cryptsy import Api
@pytest.fixture
def api():
return Api('KEY', 'SECRET')
@pytest.fixture
def mock_urlopen(monkeypatch):
def _mock_urlopen(request):
""" Mock urllib to return the url so we can check if the url is
constructed correctly. """
class MockRequest:
def read(self):
return '{"url": "%s"}' % request._Request__original
return MockRequest()
monkeypatch.setattr(urllib2, 'urlopen', _mock_urlopen)
def test_cryptsy_init():
""" The Cryptsy class constructor should accept the API Key and the Secret
in the constructor and set it on the instance. """
instance = Api('API Key', 'Secret')
assert instance.API_KEY == 'API Key'
assert instance.SECRET == 'Secret'
class TestPublicApiCall:
""" Some requests are done to the "public" API of cryptsy. """
def test_method_should_be_added_in_the_url(self, mock_urlopen, api):
rv = api._public_api_query('testmethod')
assert rv['url'] == 'http://pubapi.cryptsy.com/api.php?method=testmethod'
def test_marketid_should_be_added_as_get_parameter(self, mock_urlopen,
api):
rv = api._public_api_query('testmethod', marketid=10)
assert rv['url'] == 'http://pubapi.cryptsy.com/api.php?method=testmethod&marketid=10'
@pytest.fixture
def mock_create_order(api):
""" Mock the create order so we can check if the correct ordertype is
used. """
api._create_order = Mock()
def test_buy(mock_create_order, api):
""" The buy method should call the _create_order method with the ordertyp
as 'Buy'. """
api.buy(26, 10, 0.0000001)
api._create_order.assert_called_with(26, 'Buy', 10, 0.0000001)
def test_sell(mock_create_order, api):
""" The sell method should call the _create_order method with the ordertyp
as 'Sell'. """
rv = api.sell(26, 10, 0.0000001)
api._create_order.assert_called_with(26, 'Sell', 10, 0.0000001)
def test_generate_new_address_without_parameters(api):
""" generate_new_address should raise a ValueError when no parameters are
provided. """
with pytest.raises(ValueError):
api.generate_new_address()
@pytest.fixture
def api_query_mock(api):
api._api_query = Mock()
return api._api_query
@pytest.fixture
def public_api_query_mock(api):
api._public_api_query = Mock()
return api._public_api_query
def test_generate_new_address_currencycode(api, api_query_mock):
""" Should add currencycode as request data if provided. """
api.generate_new_address(currencycode=10)
api_query_mock.assert_called_with('generatenewaddress', request_data={
'currencycode': 10
})
def test_generate_new_address_currencyid(api, api_query_mock):
""" Should add currencyid as request data if provided. """
api.generate_new_address(currencyid=10)
api_query_mock.assert_called_with('generatenewaddress', request_data={
'currencyid': 10
})
def test_market_data_old(api, public_api_query_mock):
""" Should use the old marketdata method if v2 is not set to True. """
api.market_data()
public_api_query_mock.assert_called_with('marketdata')
def test_market_data_v2(api, public_api_query_mock):
""" Should use the old marketdata method if v2 is not set to True. """
api.market_data(v2=True)
public_api_query_mock.assert_called_with('marketdatav2')