mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-31 06:22:18 +00:00
Merge pull request #318 from jmaslek/crypto_test
Add docstring to crypto + add test
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
""" pycoingecko_api """
|
||||
__docformat__ = "numpy"
|
||||
|
||||
import argparse
|
||||
from typing import List
|
||||
import pandas as pd
|
||||
from pandas.plotting import register_matplotlib_converters
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -11,8 +15,15 @@ register_matplotlib_converters()
|
||||
# pylint: disable=inconsistent-return-statements
|
||||
|
||||
|
||||
def load(l_args):
|
||||
def load(other_args: List[str]):
|
||||
"""Load selected Cryptocurrency
|
||||
|
||||
Parameters
|
||||
----------
|
||||
other_args : List[str]
|
||||
argparse arguments
|
||||
|
||||
"""
|
||||
cg = CoinGeckoAPI()
|
||||
parser = argparse.ArgumentParser(
|
||||
add_help=False,
|
||||
@@ -40,11 +51,11 @@ def load(l_args):
|
||||
)
|
||||
|
||||
try:
|
||||
if l_args:
|
||||
if "-" not in l_args[0]:
|
||||
l_args.insert(0, "-c")
|
||||
if other_args:
|
||||
if "-" not in other_args[0]:
|
||||
other_args.insert(0, "-c")
|
||||
|
||||
ns_parser = parse_known_args_and_warn(parser, l_args)
|
||||
ns_parser = parse_known_args_and_warn(parser, other_args)
|
||||
|
||||
if not ns_parser:
|
||||
return
|
||||
@@ -57,7 +68,7 @@ def load(l_args):
|
||||
except KeyError:
|
||||
print(f"Could not find coin with the id: {ns_parser.coin}")
|
||||
print("")
|
||||
return [None, pd.DataFrame]
|
||||
return [None, pd.DataFrame()]
|
||||
|
||||
prices = cg.get_coin_market_chart_by_id(
|
||||
coin, vs_currency=ns_parser.vs, days=ns_parser.days
|
||||
@@ -81,8 +92,19 @@ def load(l_args):
|
||||
return [None, pd.DataFrame()]
|
||||
|
||||
|
||||
def view(coin, prices, l_args):
|
||||
def view(coin: str, prices: pd.DataFrame, other_args: List[str]):
|
||||
"""Plots loaded cryptocurrency
|
||||
|
||||
Parameters
|
||||
----------
|
||||
coin : str
|
||||
Cryptocurrency
|
||||
prices : pandas.DataFrame
|
||||
Dataframe containing prices and dates for selected coin
|
||||
other_args : List[str]
|
||||
argparse arguments
|
||||
|
||||
"""
|
||||
parser = argparse.ArgumentParser(
|
||||
add_help=False,
|
||||
prog="Crypto",
|
||||
@@ -91,7 +113,7 @@ def view(coin, prices, l_args):
|
||||
""",
|
||||
)
|
||||
try:
|
||||
ns_parser = parse_known_args_and_warn(parser, l_args)
|
||||
ns_parser = parse_known_args_and_warn(parser, other_args)
|
||||
|
||||
if not ns_parser:
|
||||
return
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
from pycoingecko import CoinGeckoAPI
|
||||
"""
|
||||
Helper script that generates a dictionary to convert between symbols and IDs.
|
||||
This allows the user to input (for example) "btc" instead of the expected "bitcoin"
|
||||
|
||||
"""
|
||||
from pycoingecko import CoinGeckoAPI
|
||||
|
||||
cg = CoinGeckoAPI()
|
||||
coins = cg.get_coins()
|
||||
|
||||
1
tests/data/btc_usd_test_data.json
Normal file
1
tests/data/btc_usd_test_data.json
Normal file
File diff suppressed because one or more lines are too long
35
tests/test_coingecko_crypto.py
Normal file
35
tests/test_coingecko_crypto.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from unittest import mock, TestCase
|
||||
import json
|
||||
from pycoingecko import CoinGeckoAPI
|
||||
from gamestonk_terminal.cryptocurrency.coin_api import load
|
||||
|
||||
# pylint: disable=unused-import
|
||||
|
||||
|
||||
class TestCoinGeckoAPI(TestCase):
|
||||
@mock.patch(
|
||||
"gamestonk_terminal.cryptocurrency.coin_api.CoinGeckoAPI.get_coin_market_chart_by_id"
|
||||
)
|
||||
def test_coin_api_load(self, mock_load):
|
||||
"""
|
||||
Mock load function through get_coin_market_chart_by_id.
|
||||
Mock returns a dict saved as .json
|
||||
|
||||
"""
|
||||
with open("tests/data/btc_usd_test_data.json") as f:
|
||||
sample_return = json.load(f)
|
||||
|
||||
mock_load.return_value = sample_return
|
||||
coin, mock_return = load(["-c", "bitcoin"])
|
||||
self.assertEqual(coin, "bitcoin")
|
||||
self.assertTrue(mock_load.called)
|
||||
self.assertTrue(mock_return.shape == (722, 2))
|
||||
|
||||
def test_coin(self):
|
||||
"""Test that pycoingecko retrieves the major coins"""
|
||||
|
||||
coins = CoinGeckoAPI().get_coins()
|
||||
coin_list = [coin["id"] for coin in coins]
|
||||
test_coins = ["bitcoin", "ethereum", "dogecoin"]
|
||||
for test in test_coins:
|
||||
self.assertIn(test, coin_list)
|
||||
Reference in New Issue
Block a user