Added test on get_historical_prices.

This commit is contained in:
AuroraTradingTeam
2017-11-08 11:10:31 +00:00
parent 8c9ee64485
commit 84409c29c3
4 changed files with 78 additions and 4 deletions
+1 -1
View File
@@ -100,7 +100,7 @@ Before you submit a pull request, check that it meets these guidelines:
1. The pull request should include tests.
2. If the pull request adds functionality, the docs should be updated. Put
your new functionality into a function with a docstring, and add the
feature to the list in README.rst.
feature to the list in README.md.
3. The pull request should work for Python 2.6, 2.7, 3.3, 3.4 and 3.5, and for PyPy. Check
https://travis-ci.org/gavincyi/libcryptomarket/pull_requests
and make sure that the tests pass for all supported Python versions.
+1 -1
View File
@@ -1,7 +1,7 @@
include CONTRIBUTING.rst
include HISTORY.rst
include LICENSE
include README.rst
include README.md
recursive-include tests *
recursive-exclude * __pycache__
+3 -1
View File
@@ -67,6 +67,8 @@ def get_historical_prices(source='cryptocompare', symbol=None, exchange=None,
data += func()['Data']
data = pd.DataFrame([CryptocompareHisto(**e).__dict__ for e in data])
return data.sort_values(['r_time'])
data = data.set_index(['r_time'])
data.index.name = 'datetime'
return data
else:
raise ValueError("No source is called {0}".format(source))
+73 -1
View File
@@ -1,13 +1,15 @@
import requests
from datetime import datetime
import pandas as pd
from pandas.util.testing import assert_frame_equal
from libcryptomarket.instrument import get_instruments
from libcryptomarket.historical import get_historical_prices
def test_get_instruments_cryptocompare(monkeypatch):
def mockreturn(url):
def mockreturn(url, *args, **kwargs):
# The result is from request.get(...).json()
class MockReturnClass:
@classmethod
@@ -91,3 +93,73 @@ def test_get_instruments_cryptocompare(monkeypatch):
'r_url': '/coins/bcn/overview'}])
assert_frame_equal(result.set_index(['r_id']).sort_index(),
expected_result.set_index(['r_id']).sort_index())
def test_get_historical_prices_cryptocompare(monkeypatch):
def mockreturn(url, *args, **kwargs):
# The result is from request.get(...).json()
class MockReturnClass:
@classmethod
def json(cls):
# Query all symbols
return {
'Aggregated': False,
'ConversionType': {
'conversionSymbol': '',
'type': 'force_direct'},
'Data': [
{
'close': 0.007707,
'high': 0.007716,
'low': 0.007701,
'open': 0.00771,
'time': 1510045800,
'volumefrom': 289.12,
'volumeto': 2.23
},
{
'close': 0.0077,
'high': 0.007716,
'low': 0.0077,
'open': 0.007707,
'time': 1510045860,
'volumefrom': 33.53,
'volumeto': 0.2586
}]
}
return MockReturnClass()
monkeypatch.setattr(requests, 'get', mockreturn)
# Test to get historical prices
result = get_historical_prices(source='cryptocompare',
period='minute',
exchange='Poloniex',
symbol='LTCBTC')
expected_result = pd.DataFrame([
{
'r_close': 0.007707,
'r_high': 0.007716,
'r_low': 0.007701,
'r_open': 0.00771,
'r_time': datetime(2017, 11, 7, 9, 10),
'r_volumefrom': 289.12,
'r_volumeto': 2.23
},
{
'r_close': 0.0077,
'r_high': 0.007716,
'r_low': 0.0077,
'r_open': 0.007707,
'r_time': datetime(2017, 11, 7, 9, 11),
'r_volumefrom': 33.53,
'r_volumeto': 0.2586
}]).set_index(['r_time'])
expected_result.index.name = 'datetime'
assert_frame_equal(result, expected_result)