From f0a831dfb946cbf7ba98cdcc8ad8fc54b3c5c5d5 Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:55:40 +0800 Subject: [PATCH] test: common method for building the app (#671) --- app/app_db_test.go | 22 ++++++++++++++++++++++ app/upgrade_test.go | 30 +++++++++++++++++------------- 2 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 app/app_db_test.go diff --git a/app/app_db_test.go b/app/app_db_test.go new file mode 100644 index 00000000..6b83014c --- /dev/null +++ b/app/app_db_test.go @@ -0,0 +1,22 @@ +package app_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/functionx/fx-core/v7/testutil/helpers" + fxtypes "github.com/functionx/fx-core/v7/types" +) + +func TestAppDB(t *testing.T) { + helpers.SkipTest(t, "Skipping local test:", t.Name()) + + myApp, chainId := buildApp(t, fxtypes.MainnetChainId) + require.NoError(t, myApp.LoadLatestVersion()) + ctx := newContext(t, myApp, chainId, false) + + _ = ctx + + // do something ... +} diff --git a/app/upgrade_test.go b/app/upgrade_test.go index f6d846df..1239f06e 100644 --- a/app/upgrade_test.go +++ b/app/upgrade_test.go @@ -30,19 +30,7 @@ import ( func Test_UpgradeAndMigrate(t *testing.T) { helpers.SkipTest(t, "Skipping local test:", t.Name()) - fxtypes.SetConfig(true) - - home := filepath.Join(os.Getenv("HOME"), "tmp") - chainId := fxtypes.MainnetChainId - fxtypes.SetChainId(chainId) - - db, err := dbm.NewDB("application", dbm.GoLevelDBBackend, filepath.Join(home, "data")) - require.NoError(t, err) - - makeEncodingConfig := app.MakeEncodingConfig() - myApp := app.New(log.NewFilter(log.NewTMLogger(os.Stdout), log.AllowAll()), - db, nil, false, map[int64]bool{}, home, 0, - makeEncodingConfig, app.EmptyAppOptions{}, baseapp.SetChainID(chainId)) + myApp, chainId := buildApp(t, fxtypes.MainnetChainId) myApp.SetStoreLoader(upgradetypes.UpgradeStoreLoader(myApp.LastBlockHeight()+1, nextversion.Upgrade.StoreUpgrades())) require.NoError(t, myApp.LoadLatestVersion()) @@ -84,6 +72,22 @@ func Test_UpgradeAndMigrate(t *testing.T) { checkProposalPassed(t, ctx, myApp, ingProposalIds) } +func buildApp(t *testing.T, chainId string) (*app.App, string) { + fxtypes.SetConfig(true) + + home := filepath.Join(os.Getenv("HOME"), "tmp") + fxtypes.SetChainId(chainId) + + db, err := dbm.NewDB("application", dbm.GoLevelDBBackend, filepath.Join(home, "data")) + require.NoError(t, err) + + makeEncodingConfig := app.MakeEncodingConfig() + myApp := app.New(log.NewFilter(log.NewTMLogger(os.Stdout), log.AllowAll()), + db, nil, false, map[int64]bool{}, home, 0, + makeEncodingConfig, app.EmptyAppOptions{}, baseapp.SetChainID(chainId)) + return myApp, chainId +} + func newContext(t *testing.T, myApp *app.App, chainId string, deliveState bool) sdk.Context { header := tmproto.Header{ ChainID: chainId,