From 2cbfb9c6f528e24ec0552b431562a09aaba3e9b4 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 20 Jan 2023 02:10:36 +0100 Subject: [PATCH] use t.Run for test scenarios --- x/oracle/keeper/historic_avg_test.go | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 x/oracle/keeper/historic_avg_test.go diff --git a/x/oracle/keeper/historic_avg_test.go b/x/oracle/keeper/historic_avg_test.go new file mode 100644 index 0000000000..5ad0f1162a --- /dev/null +++ b/x/oracle/keeper/historic_avg_test.go @@ -0,0 +1,54 @@ +package keeper + +import ( + "testing" + "time" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/umee-network/umee/v4/x/oracle/types" + "gotest.tools/v3/assert" +) + +func TestAvgKeeper(t *testing.T) { + t.Parallel() + s := AvgKeeperSuite{} + + t.Run("new counters", s.testNewCounters) + // t.Run("another scenario, s.testScenario2") +} + +type AvgKeeperSuite struct { + cdc codec.BinaryCodec + store sdk.KVStore +} + +func (s AvgKeeperSuite) newAvgKeeper(period, shift time.Duration) AvgKeeper { + return AvgKeeper{cdc: s.cdc, store: s.store, period: period, shift: shift} +} + +func (s AvgKeeperSuite) testNewCounters(t *testing.T) { + allCounters := []types.AvgCounter{} + now := time.Now() + shift := time.Second * 10 + for i := time.Duration(0); i < 100; i++ { + allCounters = append(allCounters, types.AvgCounter{Start: now.Add(shift * i)}) + } + tcs := []struct { + name string + period time.Duration + shift time.Duration + expected []types.AvgCounter + }{ + {"period = shift", + shift, shift, allCounters[:1]}, + {"period = 2*shift", + shift * 2, shift, allCounters[:2]}, + {"period = 2.5*shift", + shift*2 + shift/2, shift, allCounters[:2]}, + } + for _, tc := range tcs { + k := s.newAvgKeeper(tc.period, tc.shift) + assert.DeepEqual(t, k.newCounters(now), tc.expected) + } +}