Skip to content

Commit

Permalink
chore: add populate amount and coingecko test
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jul 18, 2024
1 parent 50663d7 commit 50dd095
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 0 deletions.
200 changes: 200 additions & 0 deletions pkg/data_fetcher/populate_amounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package data_fetcher

import (
"main/assets"
aliasManagerPkg "main/pkg/alias_manager"
configPkg "main/pkg/config"
"main/pkg/config/types"
"main/pkg/fs"
loggerPkg "main/pkg/logger"
"main/pkg/metrics"
priceFetchers "main/pkg/price_fetchers"
amountPkg "main/pkg/types/amount"
"math/big"
"testing"

"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/require"
)

func TestPopulateAmountCachedNotConvertedNotFound(t *testing.T) {
t.Parallel()

config := &configPkg.AppConfig{
Chains: types.Chains{
{
Name: "chain",
ChainID: "chain-id",
Denoms: types.DenomInfos{},
},
},
Metrics: configPkg.MetricsConfig{Enabled: false},
}

filesystem := &fs.MockFs{}
logger := loggerPkg.GetNopLogger()
aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem)
metricsManager := metrics.NewManager(logger, config.Metrics)
dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager)

amount := &amountPkg.Amount{BaseDenom: "uatom", Value: big.NewFloat(1230000)}

dataFetcher.PopulateAmount(config.Chains[0].ChainID, amount)
require.Equal(t, "1230000", amount.Value.String())
require.Equal(t, "uatom", amount.BaseDenom.String())
require.Nil(t, amount.PriceUSD)
}

func TestPopulateAmountCachedNotConvertedWithoutPriceFetcher(t *testing.T) {
t.Parallel()

config := &configPkg.AppConfig{
Chains: types.Chains{
{
Name: "chain",
ChainID: "chain-id",
Denoms: types.DenomInfos{
{Denom: "uatom", DisplayDenom: "atom", DenomExponent: 6},
},
},
},
Metrics: configPkg.MetricsConfig{Enabled: false},
}

filesystem := &fs.MockFs{}
logger := loggerPkg.GetNopLogger()
aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem)
metricsManager := metrics.NewManager(logger, config.Metrics)
dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager)

amount := &amountPkg.Amount{BaseDenom: "uatom", Denom: "uatom", Value: big.NewFloat(1230000)}

dataFetcher.PopulateAmount(config.Chains[0].ChainID, amount)
require.Equal(t, "1.23", amount.Value.String())
require.Equal(t, "uatom", amount.BaseDenom.String())
require.Equal(t, "atom", amount.Denom.String())
require.Nil(t, amount.PriceUSD)
}

//nolint:paralleltest // disabled due to httpmock usage
func TestPopulateAmountCachedConvertedWithCachedPrice(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

config := &configPkg.AppConfig{
Chains: types.Chains{
{
Name: "chain",
ChainID: "chain-id",
Denoms: types.DenomInfos{
{Denom: "uatom", DisplayDenom: "atom", DenomExponent: 6, CoingeckoCurrency: "cosmos"},
},
},
},
Metrics: configPkg.MetricsConfig{Enabled: false},
}

filesystem := &fs.MockFs{}
logger := loggerPkg.GetNopLogger()
aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem)
metricsManager := metrics.NewManager(logger, config.Metrics)
dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager)

dataFetcher.Cache.Set("chain-id_price_uatom", float64(10))

amount := &amountPkg.Amount{BaseDenom: "uatom", Denom: "uatom", Value: big.NewFloat(1230000)}

dataFetcher.PopulateAmount(config.Chains[0].ChainID, amount)
require.Equal(t, "1.23", amount.Value.String())
require.Equal(t, "uatom", amount.BaseDenom.String())
require.Equal(t, "atom", amount.Denom.String())
require.NotNil(t, amount.PriceUSD)
require.Equal(t, "12.3", amount.PriceUSD.String())
}

//nolint:paralleltest // disabled due to httpmock usage
func TestPopulateAmountCachedNotConvertedWithCachedPriceInvalid(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

config := &configPkg.AppConfig{
Chains: types.Chains{
{
Name: "chain",
ChainID: "chain-id",
Denoms: types.DenomInfos{
{Denom: "uatom", DisplayDenom: "atom", DenomExponent: 6, CoingeckoCurrency: "cosmos"},
},
},
},
Metrics: configPkg.MetricsConfig{Enabled: false},
}

filesystem := &fs.MockFs{}
logger := loggerPkg.GetNopLogger()
aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem)
metricsManager := metrics.NewManager(logger, config.Metrics)
dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager)

dataFetcher.Cache.Set("chain-id_price_uatom", nil)

amount := &amountPkg.Amount{BaseDenom: "uatom", Denom: "uatom", Value: big.NewFloat(1230000)}

dataFetcher.PopulateAmount(config.Chains[0].ChainID, amount)
require.Equal(t, "1.23", amount.Value.String())
require.Equal(t, "uatom", amount.BaseDenom.String())
require.Equal(t, "atom", amount.Denom.String())
require.Nil(t, amount.PriceUSD)
}

//nolint:paralleltest // disabled due to httpmock usage
func TestPopulateAmountsFetched(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

config := &configPkg.AppConfig{
Chains: types.Chains{
{
Name: "chain",
ChainID: "chain-id",
Denoms: types.DenomInfos{
{Denom: "uatom", DisplayDenom: "atom", DenomExponent: 6, CoingeckoCurrency: "cosmos"},
},
},
},
Metrics: configPkg.MetricsConfig{Enabled: false},
}

filesystem := &fs.MockFs{}
logger := loggerPkg.GetDefaultLogger()
aliasManager := aliasManagerPkg.NewAliasManager(logger, config, filesystem)
metricsManager := metrics.NewManager(logger, config.Metrics)
dataFetcher := NewDataFetcher(logger, config, aliasManager, metricsManager)

dataFetcher.PriceFetchers[priceFetchers.MockPriceFetcherName] = &priceFetchers.MockPriceFetcher{}

httpmock.RegisterResponder(
"GET",
"https://api.coingecko.com/api/v3/simple/price?ids=cosmos,cosmos&vs_currencies=usd",
httpmock.NewBytesResponder(200, assets.GetBytesOrPanic("coingecko.json")),
)

amounts := amountPkg.Amounts{
{BaseDenom: "uatom", Denom: "uatom", Value: big.NewFloat(1230000)},
{BaseDenom: "uatom", Denom: "uatom", Value: big.NewFloat(4560000)},
}

dataFetcher.PopulateAmounts(config.Chains[0].ChainID, amounts)

require.Equal(t, "1.23", amounts[0].Value.String())
require.Equal(t, "uatom", amounts[0].BaseDenom.String())
require.Equal(t, "atom", amounts[0].Denom.String())
require.NotNil(t, amounts[0].PriceUSD)
require.Equal(t, "8.1057", amounts[0].PriceUSD.String())

require.Equal(t, "4.56", amounts[1].Value.String())
require.Equal(t, "uatom", amounts[1].BaseDenom.String())
require.Equal(t, "atom", amounts[1].Denom.String())
require.NotNil(t, amounts[1].PriceUSD)
require.Equal(t, "30.0504", amounts[1].PriceUSD.String())
}
1 change: 1 addition & 0 deletions pkg/price_fetchers/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package price_fetchers

const (
CoingeckoPriceFetcherName string = "coingecko"
MockPriceFetcherName string = "mock"
CoingeckoBaseCurrency string = "usd"
)
13 changes: 13 additions & 0 deletions pkg/price_fetchers/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package price_fetchers

import configTypes "main/pkg/config/types"

type MockPriceFetcher struct{}

func (f *MockPriceFetcher) GetPrices(denomInfos configTypes.DenomInfos) (map[*configTypes.DenomInfo]float64, error) {
return map[*configTypes.DenomInfo]float64{}, nil
}

func (c *MockPriceFetcher) Name() string {
return "mock"
}
19 changes: 19 additions & 0 deletions pkg/price_fetchers/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package price_fetchers

import (
"main/pkg/config/types"
"testing"

"github.com/stretchr/testify/require"
)

func TestMockPriceFetcher(t *testing.T) {
t.Parallel()

fetcher := MockPriceFetcher{}
require.Equal(t, "mock", fetcher.Name())

prices, err := fetcher.GetPrices(types.DenomInfos{})
require.NoError(t, err)
require.Empty(t, prices)
}

0 comments on commit 50dd095

Please sign in to comment.