Skip to content

Commit eda04f9

Browse files
committed
Added mocking using mock, added more tests, changed nonce to milliseconds.
1 parent 9e79421 commit eda04f9

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

Cryptsy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _public_api_query(self, method, marketid=None):
2424
def _api_query(self, method, request_data=None):
2525
""" Call to the "private" api and return the loaded json. """
2626
request_data['method'] = method
27-
request_data['nonce'] = int(time.time())
27+
request_data['nonce'] = int(round(time.time() * 1000))
2828
post_data = urllib.urlencode(request_data)
2929

3030
signed_data = hmac.new(self.SECRET, post_data, hashlib.sha512)\

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Version 0.2:
2727
* moved from camelCase names to python_style names
2828
* added new methods: `buy`, `sell`, `my_transfers`, `wallet_status`, `make_withdrawal`
2929
* started implementing tests
30+
* nonce is now in milliseconds instead of seconds, to support multiple calls
31+
per second.
3032

3133
Running the tests
3234
-----------------

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
argparse==1.2.1
22
distribute==0.7.3
3+
mock==1.0.1
34
py==1.4.20
45
pytest==2.5.2
56
wsgiref==0.1.2

test_cryptsy.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22
import urllib2
33

4+
from mock import Mock
5+
46
from Cryptsy import Api
57

68

@@ -45,24 +47,68 @@ def test_marketid_should_be_added_as_get_parameter(self, mock_urlopen,
4547

4648

4749
@pytest.fixture
48-
def mock_create_order(monkeypatch):
50+
def mock_create_order(api):
4951
""" Mock the create order so we can check if the correct ordertype is
5052
used. """
51-
def _mock_create_order(self, marketid, ordertype, quantity, price):
52-
return ordertype
53-
54-
monkeypatch.setattr(Api, '_create_order', _mock_create_order)
53+
api._create_order = Mock()
5554

5655

5756
def test_buy(mock_create_order, api):
5857
""" The buy method should call the _create_order method with the ordertyp
5958
as 'Buy'. """
60-
rv = api.buy(26, 10, 0.0000001)
61-
assert rv == 'Buy'
59+
api.buy(26, 10, 0.0000001)
60+
api._create_order.assert_called_with(26, 'Buy', 10, 0.0000001)
6261

6362

6463
def test_sell(mock_create_order, api):
6564
""" The sell method should call the _create_order method with the ordertyp
6665
as 'Sell'. """
6766
rv = api.sell(26, 10, 0.0000001)
68-
assert rv == 'Sell'
67+
api._create_order.assert_called_with(26, 'Sell', 10, 0.0000001)
68+
69+
70+
def test_generate_new_address_without_parameters(api):
71+
""" generate_new_address should raise a ValueError when no parameters are
72+
provided. """
73+
with pytest.raises(ValueError):
74+
api.generate_new_address()
75+
76+
77+
@pytest.fixture
78+
def api_query_mock(api):
79+
api._api_query = Mock()
80+
return api._api_query
81+
82+
83+
@pytest.fixture
84+
def public_api_query_mock(api):
85+
api._public_api_query = Mock()
86+
return api._public_api_query
87+
88+
89+
def test_generate_new_address_currencycode(api, api_query_mock):
90+
""" Should add currencycode as request data if provided. """
91+
api.generate_new_address(currencycode=10)
92+
api_query_mock.assert_called_with('generatenewaddress', request_data={
93+
'currencycode': 10
94+
})
95+
96+
97+
def test_generate_new_address_currencyid(api, api_query_mock):
98+
""" Should add currencyid as request data if provided. """
99+
api.generate_new_address(currencyid=10)
100+
api_query_mock.assert_called_with('generatenewaddress', request_data={
101+
'currencyid': 10
102+
})
103+
104+
105+
def test_market_data_old(api, public_api_query_mock):
106+
""" Should use the old marketdata method if v2 is not set to True. """
107+
api.market_data()
108+
public_api_query_mock.assert_called_with('marketdata')
109+
110+
111+
def test_market_data_v2(api, public_api_query_mock):
112+
""" Should use the old marketdata method if v2 is not set to True. """
113+
api.market_data(v2=True)
114+
public_api_query_mock.assert_called_with('marketdatav2')

0 commit comments

Comments
 (0)