From 1ebf48de09ef62d70b3a94832b5b24cb7557bd85 Mon Sep 17 00:00:00 2001 From: Carlton N Hanna Date: Wed, 18 Oct 2023 12:48:24 -0600 Subject: [PATCH] add more test cases --- app/upgrades.go | 2 +- app/upgrades_test.go | 63 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/app/upgrades.go b/app/upgrades.go index 09843fc663..ad4c324dd8 100644 --- a/app/upgrades.go +++ b/app/upgrades.go @@ -359,7 +359,7 @@ func addMarkerNavs(ctx sdk.Context, app *App) { if !hasNav { nav := markertypes.NewNetAssetValue(sdk.NewInt64Coin(markertypes.UsdDenom, int64(150)), 1) if err := app.MarkerKeeper.AddSetNetAssetValues(ctx, record, []markertypes.NetAssetValue{nav}, "upgrade_handler"); err != nil { - panic(fmt.Sprintf("unable to set net asset value %v: %v", nav, err)) + ctx.Logger().Error(fmt.Sprintf("unable to set net asset value %v: %v", nav, err)) } } return false diff --git a/app/upgrades_test.go b/app/upgrades_test.go index 1e12645e88..b895512186 100644 --- a/app/upgrades_test.go +++ b/app/upgrades_test.go @@ -854,14 +854,59 @@ func (s *UpgradeTestSuite) TestAddMarkerNavs() { []markertypes.AccessGrant{}) testcoin.Supply = sdk.OneInt() s.Require().NoError(s.app.MarkerKeeper.AddMarkerAccount(s.ctx, testcoin), "AddMarkerAccount() error") + + nosupplycoin := markertypes.NewEmptyMarkerAccount("nosupplycoin", + address1.String(), + []markertypes.AccessGrant{}) + s.Require().NoError(s.app.MarkerKeeper.AddMarkerAccount(s.ctx, nosupplycoin), "AddMarkerAccount() error") + + hasnavcoin := markertypes.NewEmptyMarkerAccount("hasnavcoin", + address1.String(), + []markertypes.AccessGrant{}) + hasnavcoin.Supply = sdk.NewInt(100) + s.Require().NoError(s.app.MarkerKeeper.AddMarkerAccount(s.ctx, hasnavcoin), "AddMarkerAccount() error") + presentnav := markertypes.NewNetAssetValue(sdk.NewInt64Coin(markertypes.UsdDenom, int64(55)), uint64(100)) + s.Require().NoError(s.app.MarkerKeeper.AddSetNetAssetValues(s.ctx, hasnavcoin, []markertypes.NetAssetValue{presentnav}, "test")) + addMarkerNavs(s.ctx, s.app) - netAssetValues := []markertypes.NetAssetValue{} - err := s.app.MarkerKeeper.IterateNetAssetValues(s.ctx, testcoin.GetAddress(), func(state markertypes.NetAssetValue) (stop bool) { - netAssetValues = append(netAssetValues, state) - return false - }) - s.Require().NoError(err, "IterateNetAssetValues err") - s.Assert().Len(netAssetValues, 1, "Should be 1 nav set for testcoin") - s.Assert().Equal(sdk.NewInt64Coin(markertypes.UsdDenom, int64(150)), netAssetValues[0].Price, "Net asset value price should equal default upgraded price") - s.Assert().Equal(uint64(1), netAssetValues[0].Volume, "Net asset value volume should equal 1") + + tests := []struct { + name string + markerAddr sdk.AccAddress + expNav *markertypes.NetAssetValue + }{ + { + name: "upgrade adds new default nav", + markerAddr: testcoin.GetAddress(), + expNav: &markertypes.NetAssetValue{Price: sdk.NewInt64Coin(markertypes.UsdDenom, int64(150)), Volume: uint64(1)}, + }, + { + name: "already has nav", + markerAddr: hasnavcoin.GetAddress(), + expNav: &markertypes.NetAssetValue{Price: sdk.NewInt64Coin(markertypes.UsdDenom, int64(55)), Volume: uint64(100)}, + }, + { + name: "nav add fails for coin", + markerAddr: nosupplycoin.GetAddress(), + expNav: nil, + }, + } + + for _, tc := range tests { + s.Run(tc.name, func() { + netAssetValues := []markertypes.NetAssetValue{} + err := s.app.MarkerKeeper.IterateNetAssetValues(s.ctx, tc.markerAddr, func(state markertypes.NetAssetValue) (stop bool) { + netAssetValues = append(netAssetValues, state) + return false + }) + s.Require().NoError(err, "IterateNetAssetValues err") + if tc.expNav != nil { + s.Assert().Len(netAssetValues, 1, "Should be 1 nav set for testcoin") + s.Assert().Equal(tc.expNav.Price, netAssetValues[0].Price, "Net asset value price should equal default upgraded price") + s.Assert().Equal(tc.expNav.Volume, netAssetValues[0].Volume, "Net asset value volume should equal 1") + } else { + s.Assert().Len(netAssetValues, 0, "Marker not expected to have nav") + } + }) + } }