Skip to content

Commit

Permalink
fix unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marianososto committed Sep 24, 2024
1 parent 3bcb153 commit 23e97cb
Showing 1 changed file with 62 additions and 60 deletions.
122 changes: 62 additions & 60 deletions api/handlers/protocols/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,56 +93,15 @@ func TestService_GetProtocolsTotalValues_Allbridge(t *testing.T) {

}

func TestService_GetProtocolsTotalValues_Mayan(t *testing.T) {
const mayan = "mayan"
var errNil error
respStatsLatest := &mockQueryTableResult{}
respStatsLatest.On("Next").Return(true)
respStatsLatest.On("Err").Return(errNil)
respStatsLatest.On("Close").Return(errNil)
respStatsLatest.On("Record").Return(query.NewFluxRecord(1, map[string]interface{}{
"protocol": mayan,
"total_messages": uint64(7),
"volume": float64(10),
}))

respStatsLastDay := &mockQueryTableResult{}
respStatsLastDay.On("Next").Return(true)
respStatsLastDay.On("Err").Return(errNil)
respStatsLastDay.On("Close").Return(errNil)
respStatsLastDay.On("Record").Return(query.NewFluxRecord(1, map[string]interface{}{
"protocol": mayan,
"total_messages": uint64(4),
"volume": float64(5),
}))

ctx := context.Background()
queryAPI := &mockQueryAPI{}
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStatsNow, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, mayan)).Return(respStatsLatest, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStats24HrAgo, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, mayan)).Return(respStatsLastDay, nil)

repository := protocols.NewRepository(queryAPI, "bucketInfinite", "bucket30d", "bucket24hr", zap.NewNop())
service := protocols.NewService([]string{protocols.MAYAN}, repository, zap.NewNop(), cache.NewDummyCacheClient(), "WORMSCAN:PROTOCOLS", 0, metrics.NewNoOpMetrics(), &mockTvl{})

values := service.GetProtocolsTotalValues(ctx)
assert.Equal(t, 1, len(values))
assert.Equal(t, mayan, values[0].Protocol)
assert.Equal(t, uint64(7), values[0].TotalMessages)
assert.Equal(t, 10.00, values[0].TotalValueTransferred)
assert.Equal(t, uint64(3), values[0].LastDayMessages)
assert.Equal(t, "75.00%", values[0].LastDayDiffPercentage)
assert.Equal(t, float64(5), values[0].Last24HourVolume)

}

func TestService_GetProtocolsTotalValues_FailedFetchingActivity(t *testing.T) {
func TestService_GetProtocolsTotalValues_Allbridge_FailedFetchingActivity(t *testing.T) {
const allbridge = "allbridge"
var errNil error
respStatsLatest := &mockQueryTableResult{}
respStatsLatest.On("Next").Return(true)
respStatsLatest.On("Err").Return(errNil)
respStatsLatest.On("Close").Return(errNil)
respStatsLatest.On("Record").Return(query.NewFluxRecord(1, map[string]interface{}{
"protocol": "protocol1",
"protocol": allbridge,
"total_messages": uint64(7),
"total_value_locked": float64(5),
}))
Expand All @@ -152,36 +111,37 @@ func TestService_GetProtocolsTotalValues_FailedFetchingActivity(t *testing.T) {
respStatsLastDay.On("Err").Return(errNil)
respStatsLastDay.On("Close").Return(errNil)
respStatsLastDay.On("Record").Return(query.NewFluxRecord(1, map[string]interface{}{
"protocol": "protocol1",
"protocol": allbridge,
"total_messages": uint64(4),
"total_value_locked": float64(5),
}))

ctx := context.Background()
queryAPI := &mockQueryAPI{}
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStatsNow, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, "protocol1")).Return(respStatsLatest, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStats24HrAgo, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, "protocol1")).Return(respStatsLastDay, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolActivity, "bucketInfinite", "1970-01-01T00:00:00Z", dbconsts.ProtocolsActivityMeasurementDaily, "protocol1")).Return(&mockQueryTableResult{}, errors.New("mocked_error"))
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStatsNow, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, allbridge)).Return(respStatsLatest, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStats24HrAgo, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, allbridge)).Return(respStatsLastDay, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolActivity, "bucketInfinite", "1970-01-01T00:00:00Z", dbconsts.ProtocolsActivityMeasurementDaily, allbridge)).Return(&mockQueryTableResult{}, errors.New("mocked_error"))

repository := protocols.NewRepository(queryAPI, "bucketInfinite", "bucket30d", "bucket24hr", zap.NewNop())
service := protocols.NewService([]string{"protocol1"}, repository, zap.NewNop(), cache.NewDummyCacheClient(), "WORMSCAN:PROTOCOLS", 0, metrics.NewNoOpMetrics(), &mockTvl{})
service := protocols.NewService([]string{protocols.ALLBRIDGE}, repository, zap.NewNop(), cache.NewDummyCacheClient(), "WORMSCAN:PROTOCOLS", 0, metrics.NewNoOpMetrics(), &mockTvl{})

values := service.GetProtocolsTotalValues(ctx)
assert.Equal(t, 1, len(values))
assert.Equal(t, "protocol1", values[0].Protocol)
assert.Equal(t, allbridge, values[0].Protocol)
assert.NotNil(t, values[0].Error)
assert.Equal(t, "mocked_error", values[0].Error)
}

func TestService_GetProtocolsTotalValues_FailedFetchingStats(t *testing.T) {
const allbridge = "allbridge"
var errNil error

respStatsLastDay := &mockQueryTableResult{}
respStatsLastDay.On("Next").Return(true)
respStatsLastDay.On("Err").Return(errNil)
respStatsLastDay.On("Close").Return(errNil)
respStatsLastDay.On("Record").Return(query.NewFluxRecord(1, map[string]interface{}{
"protocol": "protocol1",
"protocol": allbridge,
"total_messages": uint64(4),
"total_value_locked": float64(5),
}))
Expand All @@ -192,7 +152,7 @@ func TestService_GetProtocolsTotalValues_FailedFetchingStats(t *testing.T) {
respActivityLast.On("Close").Return(errNil)
ts := time.Now().UTC()
respActivityLast.On("Record").Return(query.NewFluxRecord(1, map[string]interface{}{
"protocol": "protocol1",
"protocol": allbridge,
"total_messages": uint64(4),
"total_value_transferred": float64(7),
"total_volume_secure": float64(9),
Expand All @@ -204,30 +164,72 @@ func TestService_GetProtocolsTotalValues_FailedFetchingStats(t *testing.T) {
respActivity2.On("Err").Return(errNil)
respActivity2.On("Close").Return(errNil)
respActivity2.On("Record").Return(query.NewFluxRecord(1, map[string]interface{}{
"protocol": "protocol1",
"protocol": allbridge,
"total_messages": uint64(4),
"total_value_transferred": float64(7),
"total_value_secure": float64(9),
}))

ctx := context.Background()
queryAPI := &mockQueryAPI{}
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStatsNow, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, "protocol1")).Return(&mockQueryTableResult{}, errors.New("mocked_error"))
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStats24HrAgo, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, "protocol1")).Return(respStatsLastDay, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolActivity, "bucketInfinite", "1970-01-01T00:00:00Z", dbconsts.ProtocolsActivityMeasurementDaily, "protocol1")).Return(respActivityLast, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolActivity, "bucket30d", ts.Format(time.RFC3339), dbconsts.ProtocolsActivityMeasurementHourly, "protocol1")).Return(respActivity2, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryLast24HrActivity, "bucketInfinite", dbconsts.ProtocolsActivityMeasurementDaily, "protocol1")).Return(respActivity2, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStatsNow, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, allbridge)).Return(&mockQueryTableResult{}, errors.New("mocked_error"))
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStats24HrAgo, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, allbridge)).Return(respStatsLastDay, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolActivity, "bucketInfinite", "1970-01-01T00:00:00Z", dbconsts.ProtocolsActivityMeasurementDaily, allbridge)).Return(respActivityLast, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolActivity, "bucket30d", ts.Format(time.RFC3339), dbconsts.ProtocolsActivityMeasurementHourly, allbridge)).Return(respActivity2, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryLast24HrActivity, "bucketInfinite", dbconsts.ProtocolsActivityMeasurementDaily, allbridge)).Return(respActivity2, nil)

repository := protocols.NewRepository(queryAPI, "bucketInfinite", "bucket30d", "bucket24hr", zap.NewNop())
service := protocols.NewService([]string{"protocol1"}, repository, zap.NewNop(), cache.NewDummyCacheClient(), "WORMSCAN:PROTOCOLS", 0, metrics.NewNoOpMetrics(), &mockTvl{})
service := protocols.NewService([]string{protocols.ALLBRIDGE}, repository, zap.NewNop(), cache.NewDummyCacheClient(), "WORMSCAN:PROTOCOLS", 0, metrics.NewNoOpMetrics(), &mockTvl{})

values := service.GetProtocolsTotalValues(ctx)
assert.Equal(t, 1, len(values))
assert.Equal(t, "protocol1", values[0].Protocol)
assert.Equal(t, allbridge, values[0].Protocol)
assert.NotNil(t, values[0].Error)
assert.Equal(t, "mocked_error", values[0].Error)
}

func TestService_GetProtocolsTotalValues_Mayan(t *testing.T) {
const mayan = "mayan"
var errNil error
respStatsLatest := &mockQueryTableResult{}
respStatsLatest.On("Next").Return(true)
respStatsLatest.On("Err").Return(errNil)
respStatsLatest.On("Close").Return(errNil)
respStatsLatest.On("Record").Return(query.NewFluxRecord(1, map[string]interface{}{
"protocol": mayan,
"total_messages": uint64(7),
"volume": float64(10),
}))

respStatsLastDay := &mockQueryTableResult{}
respStatsLastDay.On("Next").Return(true)
respStatsLastDay.On("Err").Return(errNil)
respStatsLastDay.On("Close").Return(errNil)
respStatsLastDay.On("Record").Return(query.NewFluxRecord(1, map[string]interface{}{
"protocol": mayan,
"total_messages": uint64(4),
"volume": float64(5),
}))

ctx := context.Background()
queryAPI := &mockQueryAPI{}
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStatsNow, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, mayan)).Return(respStatsLatest, nil)
queryAPI.On("Query", ctx, fmt.Sprintf(protocols.QueryTemplateProtocolStats24HrAgo, "bucket30d", dbconsts.ProtocolsStatsMeasurementHourly, mayan)).Return(respStatsLastDay, nil)

repository := protocols.NewRepository(queryAPI, "bucketInfinite", "bucket30d", "bucket24hr", zap.NewNop())
service := protocols.NewService([]string{protocols.MAYAN}, repository, zap.NewNop(), cache.NewDummyCacheClient(), "WORMSCAN:PROTOCOLS", 0, metrics.NewNoOpMetrics(), &mockTvl{})

values := service.GetProtocolsTotalValues(ctx)
assert.Equal(t, 1, len(values))
assert.Equal(t, mayan, values[0].Protocol)
assert.Equal(t, uint64(7), values[0].TotalMessages)
assert.Equal(t, 10.00, values[0].TotalValueTransferred)
assert.Equal(t, uint64(3), values[0].LastDayMessages)
assert.Equal(t, "75.00%", values[0].LastDayDiffPercentage)
assert.Equal(t, float64(5), values[0].Last24HourVolume)

}

func TestService_GetProtocolsTotalValues_CacheHit(t *testing.T) {
ctx := context.Background()
mockCache := &cacheMock.CacheMock{}
Expand Down

0 comments on commit 23e97cb

Please sign in to comment.