Skip to content

Commit

Permalink
Fixed bug when querying exchanges and added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Panos Sakkos committed Aug 28, 2018
1 parent c763151 commit 2e4fab3
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 14 deletions.
12 changes: 12 additions & 0 deletions .pytest_cache/v/cache/nodeids
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
[
"tests/test_api.py::TestWrapper::test_failed_get_coin_by_id",
"tests/test_api.py::TestWrapper::test_failed_get_coin_history_by_id",
"tests/test_api.py::TestWrapper::test_failed_get_coin_market_chart_by_id",
"tests/test_api.py::TestWrapper::test_failed_get_coins",
"tests/test_api.py::TestWrapper::test_failed_get_coins_list",
"tests/test_api.py::TestWrapper::test_failed_get_coins_markets",
"tests/test_api.py::TestWrapper::test_failed_get_exchange_rates",
"tests/test_api.py::TestWrapper::test_failed_get_exchanges_by_id",
"tests/test_api.py::TestWrapper::test_failed_get_exchanges_list",
"tests/test_api.py::TestWrapper::test_failed_get_global",
"tests/test_api.py::TestWrapper::test_failed_ping",
"tests/test_api.py::TestWrapper::test_get_coin_by_id",
"tests/test_api.py::TestWrapper::test_get_coin_history_by_id",
"tests/test_api.py::TestWrapper::test_get_coin_market_chart_by_id",
"tests/test_api.py::TestWrapper::test_get_coins",
"tests/test_api.py::TestWrapper::test_get_coins_list",
"tests/test_api.py::TestWrapper::test_get_coins_markets",
"tests/test_api.py::TestWrapper::test_get_exchange_rates",
"tests/test_api.py::TestWrapper::test_get_exchanges_by_id",
"tests/test_api.py::TestWrapper::test_get_exchanges_list",
"tests/test_api.py::TestWrapper::test_get_global",
"tests/test_api.py::TestWrapper::test_ping"
]
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ Python wrapper around the [CoinGecko](https://www.coingecko.com/) API (V3)
python3 setup.py install
```

### Test

Run unit tests with:

```
# after installing pytest using pip3
pytest tests
```

### Usage

```
Expand Down
15 changes: 7 additions & 8 deletions build/lib/pycoingecko/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def get_coins(self, **kwargs):
"""List all coins with data (name, price, market, developer, community, etc)"""

api_url = '{0}coins'.format(self.api_base_url)
#['order', 'per_page', 'page', 'localization']
#['order', 'per_page', 'page', 'localization']
api_url = self.__api_url_params(api_url, kwargs)

return self.__request(api_url)


Expand All @@ -70,7 +70,7 @@ def get_coins_markets(self, vs_currency, **kwargs):

def get_coin_by_id(self, id, **kwargs):
"""Get current data (name, price, market, ... including exchange tickers) for a coin"""

api_url = '{0}coins/{1}/'.format(self.api_base_url, id)
api_url = self.__api_url_params(api_url, kwargs)

Expand All @@ -79,9 +79,9 @@ def get_coin_by_id(self, id, **kwargs):

def get_coin_history_by_id(self, id, date, **kwargs):
"""Get historical data (name, price, market, stats) at a given date for a coin"""
kwargs['date'] = date

kwargs['date'] = date

api_url = '{0}coins/{1}/history'.format(self.api_base_url, id)
api_url = self.__api_url_params(api_url, kwargs)

Expand All @@ -100,7 +100,7 @@ def get_coin_market_chart_by_id(self, id, vs_currency, days):
def get_exchanges_list(self):
"""List all exchanges"""

api_url = '{0}exchanges/list'.format(self.api_base_url)
api_url = '{0}exchanges'.format(self.api_base_url)

return self.__request(api_url)

Expand Down Expand Up @@ -129,4 +129,3 @@ def get_global(self):
api_url = '{0}global'.format(self.api_base_url)

return self.__request(api_url)['data']

2 changes: 1 addition & 1 deletion pycoingecko/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_coin_market_chart_by_id(self, id, vs_currency, days):
def get_exchanges_list(self):
"""List all exchanges"""

api_url = '{0}exchanges/list'.format(self.api_base_url)
api_url = '{0}exchanges'.format(self.api_base_url)

return self.__request(api_url)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
long_description=open('README.md').read(),
author = 'Christoforou Manolis',
author_email = '[email protected]',
install_requires=['requests'],
install_requires=['requests', 'pytest', 'responses'],
url = 'https://github.com/man-c/pycoingecko',
)
Binary file modified tests/__pycache__/test_api.cpython-37-PYTEST.pyc
Binary file not shown.
165 changes: 161 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_failed_ping(self):
status = 404)
exception = HTTPError("HTTP Error")

# Act
# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().ping()

Expand All @@ -39,7 +39,7 @@ def test_failed_get_coins(self):
status = 404)
exception = HTTPError("HTTP Error")

# Act
# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().get_coins()

Expand Down Expand Up @@ -89,7 +89,7 @@ def test_failed_get_coins_markets(self):
status = 404)
exception = HTTPError("HTTP Error")

# Act
# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().get_coins_markets('usd')

Expand All @@ -114,7 +114,7 @@ def test_failed_get_coin_by_id(self):
status = 404)
exception = HTTPError("HTTP Error")

# Act
# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().get_coin_by_id('bitcoin')

Expand All @@ -132,3 +132,160 @@ def test_get_coin_by_id(self):

## Assert
assert response == bitcoin_json_sample

@responses.activate
def test_failed_get_coin_history_by_id(self):
# Arrange
responses.add(responses.GET, 'https://api.coingecko.com/api/v3/coins/bitcoin/history?date=27-08-2018',
status = 404)
exception = HTTPError("HTTP Error")

# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().get_coin_history_by_id('bitcoin', '27-08-2018')


@responses.activate
def test_get_coin_history_by_id(self):
# Arrange
history_json_sample = { "id": "bitcoin", "symbol": "btc", "name": "Bitcoin", "localization": { "en": "Bitcoin", "es": "Bitcoin", "de": "Bitcoin", "nl": "Bitcoin", "pt": "Bitcoin", "fr": "Bitcoin", "it": "Bitcoin", "hu": "Bitcoin", "ro": "Bitcoin", "sv": "Bitcoin", "pl": "Bitcoin", "id": "Bitcoin", "zh": "比特币", "zh-tw": "比特幣", "ja": "ビットコイン", "ko": "비트코인", "ru": "биткоина", "ar": "بيتكوين", "th": "บิตคอยน์", "vi": "Bitcoin", "tr": "Bitcoin" } }

responses.add(responses.GET, 'https://api.coingecko.com/api/v3/coins/bitcoin/history?date=27-08-2018',
json = history_json_sample, status = 200)

# Act
response = CoinGeckoAPI().get_coin_history_by_id('bitcoin', '27-08-2018')

## Assert
assert response == history_json_sample

@responses.activate
def test_failed_get_coin_market_chart_by_id(self):
# Arrange
responses.add(responses.GET, 'https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=1',
status = 404)
exception = HTTPError("HTTP Error")

# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().get_coin_market_chart_by_id('bitcoin', 'usd', 0)


@responses.activate
def test_get_coin_market_chart_by_id(self):
# Arrange
json_response = { "prices": [ [ 1535373899623, 6756.942910425894 ], [ 1535374183927, 6696.894541693875 ], [ 1535374496401, 6689.990513793263 ], [ 1535374779118, 6668.291007556478 ], [ 1535375102688, 6703.7499879964 ], [ 1535375384209, 6706.898948451269 ] ] }

responses.add(responses.GET, 'https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=1',
json = json_response, status = 200)

# Act
response = CoinGeckoAPI().get_coin_market_chart_by_id('bitcoin', 'usd', 0)

## Assert
assert response == json_response

@responses.activate
def test_failed_get_exchanges_list(self):
# Arrange
responses.add(responses.GET, 'https://api.coingecko.com/api/v3/exchanges',
status = 404)
exception = HTTPError("HTTP Error")

# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().get_exchanges_list()


@responses.activate
def test_get_exchanges_list(self):
# Arrange
json_response = [ { "id": "bitforex", "name": "Bitforex", "description": "", "url": "https://www.bitforex.com/", "image": "https://assets.coingecko.com/markets/images/214/small/bitforex.jpg?1533199114", "has_trading_incentive": "true", "trade_volume_24h_btc": 680266.637119918 }, { "id": "binance", "name": "Binance", "description": "Binance is a China-based cryptocurrency exchange that lists most of the Chinese coins. It is a popular exchange for its huge number of Initial Coin Offering (ICO) listings and low fees.", "url": "https://www.binance.com/", "image": "https://assets.coingecko.com/markets/images/52/small/binance.jpg?1519353250", "has_trading_incentive": "false", "trade_volume_24h_btc": 189744.350072168 } ]

responses.add(responses.GET, 'https://api.coingecko.com/api/v3/exchanges',
json = json_response, status = 200)

# Act
response = CoinGeckoAPI().get_exchanges_list()

## Assert
assert response == json_response

@responses.activate
def test_failed_get_exchanges_by_id(self):
# Arrange
responses.add(responses.GET, 'https://api.coingecko.com/api/v3/exchanges/bitforex',
status = 404)
exception = HTTPError("HTTP Error")

# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().get_exchanges_by_id('bitforex')


@responses.activate
def test_get_exchanges_by_id(self):
# Arrange
json_response = { "name": "Bitforex", "has_trading_incentive": "true", "trade_volume_24h_btc": 680266.637119918, "tickers": [ { "base": "BTC", "target": "USDT", "market": { "name": "Bitforex", "identifier": "bitforex", "has_trading_incentive": "true" }, "last": 7039.55, "converted_last": { "btc": "1.001711841446200081963480716", "eth": "24.4986463149997536428213651518458101194944", "usd": "7043.71831205846008527901735024184383795812" }, "volume": 447378.73, "converted_volume": { "btc": "448144.5713519911718500979009072226084", "eth": "10960173.27267390510353832059421689917189597190216256", "usd": "3151209752.222085727501972469271259554059845134991788" }, "timestamp": "2018-08-28T12:46:25.719Z", "is_anomaly": "false" } ] }

responses.add(responses.GET, 'https://api.coingecko.com/api/v3/exchanges/bitforex',
json = json_response, status = 200)

# Act
response = CoinGeckoAPI().get_exchanges_by_id('bitforex')

## Assert
assert response == json_response

@responses.activate
def test_failed_get_exchange_rates(self):
# Arrange
responses.add(responses.GET, 'https://api.coingecko.com/api/v3/exchange_rates',
status = 404)
exception = HTTPError("HTTP Error")

# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().get_exchange_rates()


@responses.activate
def test_get_exchange_rates(self):
# Arrange
json_response = { "rates": { "btc": { "name": "Bitcoin", "unit": "Ƀ", "value": 0, "type": "crypto" }, "eth": { "name": "Ether", "unit": "Ξ", "value": 24.451, "type": "crypto" }, "usd": { "name": "US Dollar", "unit": "$", "value": 7040.152, "type": "fiat" } } }

responses.add(responses.GET, 'https://api.coingecko.com/api/v3/exchange_rates',
json = json_response, status = 200)

# Act
response = CoinGeckoAPI().get_exchange_rates()

## Assert
assert response == json_response

@responses.activate
def test_failed_get_global(self):
# Arrange
responses.add(responses.GET, 'https://api.coingecko.com/api/v3/global',
status = 404)
exception = HTTPError("HTTP Error")

# Act Assert
with pytest.raises(HTTPError) as HE:
CoinGeckoAPI().get_global()


@responses.activate
def test_get_global(self):
# Arrange
json_response = { "data": { "active_cryptocurrencies": 2517, "upcoming_icos": 360, "ongoing_icos": 423, "ended_icos": 2037, "markets": 197 } }

responses.add(responses.GET, 'https://api.coingecko.com/api/v3/global',
json = json_response, status = 200)

# Act
response = CoinGeckoAPI().get_global()

## Assert
expected_response = { "active_cryptocurrencies": 2517, "upcoming_icos": 360, "ongoing_icos": 423, "ended_icos": 2037, "markets": 197 }
assert response == expected_response

0 comments on commit 2e4fab3

Please sign in to comment.