def test_cryptocompare_historical_data_price( data_dir, database, from_asset, to_asset, timestamp, price, ): """Test that the cryptocompare histohour data retrieval works and price is returned """ cc = Cryptocompare(data_directory=data_dir, database=database) # Get lots of historical prices from at least 1 query after the ts we need cc.query_and_store_historical_data( from_asset=from_asset, to_asset=to_asset, timestamp=timestamp + 2020 * 3600, ) # Query the ts we need directly from the cached data price_cache_entry = GlobalDBHandler().get_historical_price( from_asset=from_asset, to_asset=to_asset, timestamp=timestamp, max_seconds_distance=3600, source=HistoricalPriceOracle.CRYPTOCOMPARE, ) assert price_cache_entry.price == price
def test_cryptocompare_histohour_data_going_backward(data_dir, database, freezer): """Test that the cryptocompare histohour data retrieval works properly This test checks that doing an additional query in the past workd properly and that the cached data are properly appended to the cached result. In production this scenario should not happen often. Only way to happen if cryptocompare somehow adds older data than what was previously queried. """ globaldb = GlobalDBHandler() # first timestamp cryptocompare has histohour BTC/USD when queried from this test is btc_start_ts = 1279936800 # first timestamp cryptocompare has histohour BTC/USD is: 1279940400 now_ts = btc_start_ts + 3600 * 2000 + 122 # create a cache file for BTC_USD cache_data = [ HistoricalPrice( from_asset=A_BTC, to_asset=A_USD, source=HistoricalPriceOracle.CRYPTOCOMPARE, timestamp=Timestamp(1301536800), price=Price(FVal('0.298')), ), HistoricalPrice( from_asset=A_BTC, to_asset=A_USD, source=HistoricalPriceOracle.CRYPTOCOMPARE, timestamp=Timestamp(1301540400), price=Price(FVal('0.298')), ) ] globaldb.add_historical_prices(cache_data) freezer.move_to(datetime.fromtimestamp(now_ts)) cc = Cryptocompare(data_directory=data_dir, database=database) cc.query_and_store_historical_data( from_asset=A_BTC, to_asset=A_USD, timestamp=now_ts - 3600 * 2 - 55, ) result = get_globaldb_cache_entries(from_asset=A_BTC, to_asset=A_USD) assert len(result) == CRYPTOCOMPARE_HOURQUERYLIMIT * 3 + 2 check_cc_result(result, forward=False) data_range = globaldb.get_historical_price_range( A_BTC, A_USD, HistoricalPriceOracle.CRYPTOCOMPARE) # noqa: E501 assert data_range[0] == btc_start_ts assert data_range[ 1] == 1301540400 # that's the closest ts to now_ts cc returns
def test_empty_histohour(data_dir, database, freezer): """Histohour can be empty and can have also floating point zeros like in CHI/EUR This test makes sure that an empty list is returned at the very first all zeros result that also has floating point and querying stops. If cryptocompare actually fixes their zero historical price problem this test can go away """ now_ts = 1610365553 freezer.move_to(datetime.fromtimestamp(now_ts)) cc = Cryptocompare(data_directory=data_dir, database=database) cc.query_and_store_historical_data( from_asset=A_CHI, to_asset=A_EUR, timestamp=now_ts, ) result = get_globaldb_cache_entries(from_asset=A_CHI, to_asset=A_EUR) assert len(result) == 0
def test_cryptocompare_histohour_data_going_forward(data_dir, database, freezer): """Test that the cryptocompare histohour data retrieval works properly This test checks that doing an additional query in the future works properly and appends the cached data with the newly returned data """ # first timestamp cryptocompare has histohour BTC/USD when queried from this test is btc_start_ts = 1279940400 now_ts = btc_start_ts + 3600 * 2000 + 122 freezer.move_to(datetime.fromtimestamp(now_ts)) cc = Cryptocompare(data_directory=data_dir, database=database) cc.query_and_store_historical_data( from_asset=A_BTC, to_asset=A_USD, timestamp=now_ts - 3600 * 2 - 55, ) globaldb = GlobalDBHandler() result = get_globaldb_cache_entries(from_asset=A_BTC, to_asset=A_USD) assert len(result) == CRYPTOCOMPARE_HOURQUERYLIMIT + 1 assert all(x.price == Price(FVal(0.05454)) for x in result) data_range = globaldb.get_historical_price_range( A_BTC, A_USD, HistoricalPriceOracle.CRYPTOCOMPARE) # noqa: E501 assert data_range[0] == btc_start_ts assert data_range[ 1] == 1287140400 # that's the closest ts to now_ts cc returns # now let's move a bit to the future and query again to see the cache is appended to now_ts = now_ts + 3600 * 2000 * 2 + 4700 freezer.move_to(datetime.fromtimestamp(now_ts)) cc.query_and_store_historical_data( from_asset=A_BTC, to_asset=A_USD, timestamp=now_ts - 3600 * 4 - 55, ) result = get_globaldb_cache_entries(from_asset=A_BTC, to_asset=A_USD) assert len(result) == CRYPTOCOMPARE_HOURQUERYLIMIT * 3 + 2 check_cc_result(result, forward=True) data_range = globaldb.get_historical_price_range( A_BTC, A_USD, HistoricalPriceOracle.CRYPTOCOMPARE) # noqa: E501 assert data_range[0] == btc_start_ts assert data_range[ 1] == 1301544000 # that's the closest ts to now_ts cc returns