Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Dec 4, 2024
1 parent be8a06d commit 42d316e
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ run-unprocessed-events-replay-local:
generate-mock-interface:
cd internal/db && mockery --name=DBClient --output=../../tests/mocks --outpkg=mocks --filename=mock_db_client.go
cd internal/clients/ordinals && mockery --name=OrdinalsClientInterface --output=../../../tests/mocks --outpkg=mocks --filename=mock_ordinal_client.go
cd internal/clients/coinmarketcap && mockery --name=CoinMarketCapClientInterface --output=../../../tests/mocks --outpkg=mocks --filename=mock_coinmarketcap_client.go

test:
./bin/local-startup.sh;
Expand Down
2 changes: 1 addition & 1 deletion internal/services/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (s *Services) GetOverallStats(

// Only fetch BTC price if ExternalAPIs are configured
var btcPrice *float64
if s.cfg.ExternalAPIs != nil {
if s.cfg.ExternalAPIs != nil && s.cfg.ExternalAPIs.CoinMarketCap != nil {
price, err := s.GetLatestBtcPriceUsd(ctx)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("error while fetching latest btc price")
Expand Down
6 changes: 6 additions & 0 deletions tests/config/config-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ assets:
timeout: 100
terms_acceptance_logging:
enabled: true
external_apis:
coinmarketcap:
api_key: "c382e883-36c9-401b-85c3-ff4607f473a4"
base_url: "https://pro-api.coinmarketcap.com/v1"
timeout: 10s # http client timeout
cache_ttl: 9s # mongodb ttl
106 changes: 106 additions & 0 deletions tests/integration_test/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import (
"time"

"github.com/babylonlabs-io/staking-api-service/internal/api/handlers"
"github.com/babylonlabs-io/staking-api-service/internal/clients"
"github.com/babylonlabs-io/staking-api-service/internal/config"
"github.com/babylonlabs-io/staking-api-service/internal/db/model"
"github.com/babylonlabs-io/staking-api-service/internal/services"
"github.com/babylonlabs-io/staking-api-service/internal/types"
"github.com/babylonlabs-io/staking-api-service/tests/mocks"
"github.com/babylonlabs-io/staking-api-service/tests/testutils"
"github.com/babylonlabs-io/staking-queue-client/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -466,6 +470,108 @@ func FuzzTestTopStakersWithPaginationResponse(f *testing.F) {
})
}

func TestOverallStatsEndpointBTCPriceIntegration(t *testing.T) {
t.Run("should return BTC price when successfully fetched from CoinMarketCap", func(t *testing.T) {
// Setup mock
mockCMC := new(mocks.CoinMarketCapClientInterface)
mockCMC.On("GetLatestBtcPrice", mock.Anything).
Return(42000.50, nil)

mockedClients := &clients.Clients{
CoinMarketCap: mockCMC,
}

testServer := setupTestServer(t, &TestServerDependency{
MockedClients: mockedClients,
})
defer testServer.Close()

// Fetch stats and verify
stats := fetchOverallStatsEndpoint(t, testServer)
assert.NotNil(t, stats.BtcPriceUsd)
assert.Equal(t, 42000.50, *stats.BtcPriceUsd)
mockCMC.AssertExpectations(t)
})

t.Run("should return nil BTC price when CoinMarketCap fetch fails", func(t *testing.T) {
mockCMC := new(mocks.CoinMarketCapClientInterface)
mockCMC.On("GetLatestBtcPrice", mock.Anything).
Return(0.0, types.NewErrorWithMsg(
http.StatusInternalServerError,
types.InternalServiceError,
"failed to fetch BTC price",
))

mockedClients := &clients.Clients{
CoinMarketCap: mockCMC,
}

testServer := setupTestServer(t, &TestServerDependency{
MockedClients: mockedClients,
})
defer testServer.Close()

// Fetch stats and verify
stats := fetchOverallStatsEndpoint(t, testServer)
assert.Nil(t, stats.BtcPriceUsd)
mockCMC.AssertExpectations(t)
})

t.Run("should return nil BTC price when external APIs are disabled", func(t *testing.T) {
// Setup mock
mockCMC := new(mocks.CoinMarketCapClientInterface)
mockCMC.On("GetLatestBtcPrice", mock.Anything).
Return(42000.50, nil)

mockedClients := &clients.Clients{
CoinMarketCap: mockCMC,
}

testConfig, err := config.New("../config/config-test.yml")
if err != nil {
t.Fatalf("Failed to load test config: %v", err)
}
testConfig.ExternalAPIs = nil

testServer := setupTestServer(t, &TestServerDependency{
ConfigOverrides: testConfig,
MockedClients: mockedClients,
})
defer testServer.Close()

// Fetch stats and verify
stats := fetchOverallStatsEndpoint(t, testServer)
assert.Nil(t, stats.BtcPriceUsd)
})

t.Run("should return nil BTC price when CoinMarketCap is disabled", func(t *testing.T) {
// Setup mock
mockCMC := new(mocks.CoinMarketCapClientInterface)
mockCMC.On("GetLatestBtcPrice", mock.Anything).
Return(42000.50, nil)

mockedClients := &clients.Clients{
CoinMarketCap: mockCMC,
}

testConfig, err := config.New("../config/config-test.yml")
if err != nil {
t.Fatalf("Failed to load test config: %v", err)
}
testConfig.ExternalAPIs.CoinMarketCap = nil

testServer := setupTestServer(t, &TestServerDependency{
ConfigOverrides: testConfig,
MockedClients: mockedClients,
})
defer testServer.Close()

// Fetch stats and verify
stats := fetchOverallStatsEndpoint(t, testServer)
assert.Nil(t, stats.BtcPriceUsd)
})
}

func fetchFinalityEndpoint(t *testing.T, testServer *TestServer) []services.FpDetailsPublic {
url := testServer.Server.URL + finalityProvidersPath
// Make a GET request to the finality providers endpoint
Expand Down
59 changes: 59 additions & 0 deletions tests/mocks/mock_coinmarketcap_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 42d316e

Please sign in to comment.