Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for GetIfFound #3145

Merged
merged 2 commits into from
Oct 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions osmoutils/store_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,98 @@ func (s *TestSuite) TestMustGet() {
}
}

// TestMustGet tests that GetIfFound returns a boolean indicating
// whether value exists for the given key and error
func (s *TestSuite) TestGetIfFound() {
tests := map[string]struct {
// keys and values to preset
preSetKeyValues map[string]proto.Message

// keys and values to attempt to get and validate
expectedGetKeyValues map[string]proto.Message

actualResultProto proto.Message

expectFound bool

expectErr bool
}{
"basic valid test": {
preSetKeyValues: map[string]proto.Message{
keyA: &sdk.DecProto{Dec: sdk.OneDec()},
keyB: &sdk.DecProto{Dec: sdk.OneDec().Add(sdk.OneDec())},
keyC: &sdk.DecProto{Dec: sdk.OneDec().Add(sdk.OneDec())},
},

expectedGetKeyValues: map[string]proto.Message{
keyA: &sdk.DecProto{Dec: sdk.OneDec()},
keyB: &sdk.DecProto{Dec: sdk.OneDec().Add(sdk.OneDec())},
keyC: &sdk.DecProto{Dec: sdk.OneDec().Add(sdk.OneDec())},
},

actualResultProto: &sdk.DecProto{},

expectFound: true,
},
"attempt to get non-existent key - not found & no err return": {
preSetKeyValues: map[string]proto.Message{
keyA: &sdk.DecProto{Dec: sdk.OneDec()},
keyC: &sdk.DecProto{Dec: sdk.OneDec().Add(sdk.OneDec())},
},

expectedGetKeyValues: map[string]proto.Message{
keyB: &sdk.DecProto{Dec: sdk.OneDec().Add(sdk.OneDec())},
},

actualResultProto: &sdk.DecProto{},

expectFound: false,

expectErr: false,
},
"invalid proto Dec vs TwapRecord - found but Unmarshal err": {
preSetKeyValues: map[string]proto.Message{
keyA: &sdk.DecProto{Dec: sdk.OneDec()},
},

expectedGetKeyValues: map[string]proto.Message{
keyA: &sdk.DecProto{Dec: sdk.OneDec()},
},

actualResultProto: &twaptypes.TwapRecord{},

expectFound: true,

expectErr: true,
},
}

for name, tc := range tests {
s.Run(name, func() {
s.SetupStoreWithBasePrefix()

// Setup
for key, value := range tc.preSetKeyValues {
osmoutils.MustSet(s.store, []byte(key), value)
}

for key, expectedValue := range tc.expectedGetKeyValues {
// System under test.
found, err := osmoutils.GetIfFound(s.store, []byte(key), tc.actualResultProto)
// Assertions.
s.Require().Equal(found, tc.expectFound)
if tc.expectErr {
s.Require().Error(err)
}
// make sure found by key & Unmarshal successfully
if !tc.expectErr && tc.expectFound {
s.Require().Equal(expectedValue.String(), tc.actualResultProto.String())
}
}
})
}
}

// TestMustSet tests that MustSet updates the store correctly
// and panics if an error is encountered.
func (s *TestSuite) TestMustSet() {
Expand Down