faraday/fiat/coingecko_test.go
positiveblue c1e8cdc267
fiat: add support for multiple granularities to coingecko API
The coingecko API automatically changes its granularity depending on how
old is the pricing data you are asking for. For 90 days or less, it returns
hourly entries while for any date 91 days or older it returns daily
granularity.

To obtain the most exact results we should always make any reports in that
90 days window. That's why we currently do not support ranges that
include dates older than 90 days.

This commit extends the current API to adapt the range of our queries to
get the most accurate data. That means that if we use this to get the
fiat price of txs that happend between 100 and 1 day ago we will use
daily granularity for the ones older than 90 days and minute granularity
for the ones within the last 90 days.
2023-01-10 09:47:59 -08:00

74 lines
1.4 KiB
Go

package fiat
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
// TestCoinGeckoApiRanges tests that we can split up time spans into ranges with
// different granularity.
func TestCoinGeckoApiRanges(t *testing.T) {
// Freeze the current time.
now := time.Now()
tests := []struct {
name string
start time.Time
end time.Time
want []timeRange
}{
{
name: "range in the last 90 days",
start: now.AddDate(0, 0, -89),
end: now.Add(-time.Hour),
want: []timeRange{
{
start: now.AddDate(0, 0, -89),
end: now.Add(-time.Hour),
},
},
},
{
name: "range before the last 90 days",
start: now.AddDate(0, 0, -95),
end: now.AddDate(0, 0, -90),
want: []timeRange{
{
start: now.AddDate(0, 0, -95),
end: now.AddDate(0, 0, -90),
},
},
},
{
name: "range between 95 days and yesterday",
start: now.AddDate(0, 0, -95),
end: now.AddDate(0, 0, -1),
want: []timeRange{
{
start: now.AddDate(0, 0, -95),
end: now.AddDate(0, 0, -89),
},
{
start: now.AddDate(0, 0, -89),
end: now.AddDate(0, 0, -1),
},
},
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
c := &coinGeckoAPI{}
apiRanges := c.apiRanges(now, tc.start, tc.end)
require.Equal(t, tc.want, apiRanges)
})
}
}