test_price_feed.py (2308B)
1 import json 2 import os 3 import pytest 4 import sys 5 6 sys.path.append(os.path.join(os.path.dirname(__file__), "..")) 7 from payments.price_feed import \ 8 CoinDeskPriceFeed, \ 9 CoinGeckoPriceFeed 10 11 testdir = os.path.dirname(os.path.realpath(__file__)) 12 13 14 def read_price_feed_data(filename: str) -> dict: 15 with open(os.path.join(testdir, filename), "r") as f: 16 json_data = f.read() 17 return json.loads(json_data) 18 19 20 @pytest.mark.parametrize( 21 "price_feed", 22 [ 23 { 24 "class": CoinDeskPriceFeed, 25 "data_file": "coindesk_price_data.json" 26 }, 27 { 28 "class": CoinGeckoPriceFeed, 29 "data_file": "coingecko_price_data.json" 30 } 31 ]) 32 def test_invalid_base_currency(price_feed: dict) -> None: 33 provider = price_feed["class"](price_feed_url=None) 34 provider.set_price_data(read_price_feed_data(price_feed["data_file"])) 35 with pytest.raises(RuntimeError): 36 provider.get_btc_value(1, "xxx") 37 38 39 @pytest.mark.parametrize( 40 "price_feed", 41 [ 42 {"class": CoinDeskPriceFeed}, 43 {"class": CoinGeckoPriceFeed} 44 ]) 45 def test_btc_btc_price(price_feed: dict) -> None: 46 provider = price_feed["class"](price_feed_url=None) 47 assert (provider.get_btc_value(1, "BTC") == 1) 48 assert (provider.get_btc_value(1000000, "sats") == 0.01) 49 50 51 @pytest.mark.parametrize( 52 "price_feed", 53 [ 54 { 55 "class": CoinDeskPriceFeed, 56 "data_file": "coindesk_price_data.json", 57 "conversions": [ 58 { 59 "base_value": 100, "base_currency": "USD", "btc_value": 0.00544072 60 } 61 ] 62 }, 63 { 64 "class": CoinGeckoPriceFeed, 65 "data_file": "coingecko_price_data.json", 66 "conversions": [ 67 { 68 "base_value": 100, "base_currency": "USD", "btc_value": 0.0052687 69 } 70 ] 71 } 72 ]) 73 def test_fiat_btc_price(price_feed: dict) -> None: 74 provider = price_feed["class"](price_feed_url=None) 75 provider.set_price_data(read_price_feed_data(price_feed["data_file"])) 76 for conv in price_feed["conversions"]: 77 assert ( 78 provider.get_btc_value(conv["base_value"], conv["base_currency"]) == 79 conv["btc_value"] 80 )