From a96898d6474136ee60d3e437477d0119088c424e Mon Sep 17 00:00:00 2001 From: Dmitry Date: Tue, 25 Jun 2024 17:02:10 +0200 Subject: [PATCH] Move sqlite-mem db to testutils --- zetaclient/chains/base/observer.go | 8 +++----- zetaclient/chains/base/observer_test.go | 3 ++- zetaclient/chains/bitcoin/observer/observer_test.go | 3 ++- zetaclient/chains/bitcoin/observer/outbound_test.go | 2 +- zetaclient/chains/bitcoin/rpc/rpc_live_test.go | 2 +- zetaclient/chains/evm/observer/outbound_test.go | 5 +---- zetaclient/testutils/constant.go | 3 +++ 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/zetaclient/chains/base/observer.go b/zetaclient/chains/base/observer.go index 2716637656..1f3fc2d0ca 100644 --- a/zetaclient/chains/base/observer.go +++ b/zetaclient/chains/base/observer.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "strconv" + "strings" "sync" "sync/atomic" @@ -33,9 +34,6 @@ const ( // DefaultHeaderCacheSize is the default number of headers that the observer will keep in cache for performance (without RPC calls) // Cached headers can be used to get header information DefaultHeaderCacheSize = 1000 - - // TempSQLiteDBPath is the temporary in-memory SQLite database used for testing - TempSQLiteDBPath = "file::memory:?cache=shared" ) // Observer is the base structure for chain observers, grouping the common logic for each chain observer client. @@ -307,8 +305,8 @@ func (ob *Observer) OpenDB(dbPath string, dbName string) error { path := fmt.Sprintf("%s/%s", dbPath, dbName) // use memory db if specified - if dbPath == TempSQLiteDBPath { - path = TempSQLiteDBPath + if strings.Contains(dbPath, ":memory:") { + path = dbPath } // open db diff --git a/zetaclient/chains/base/observer_test.go b/zetaclient/chains/base/observer_test.go index 065283d25d..a04a48fcc3 100644 --- a/zetaclient/chains/base/observer_test.go +++ b/zetaclient/chains/base/observer_test.go @@ -7,6 +7,7 @@ import ( lru "github.com/hashicorp/golang-lru" "github.com/rs/zerolog/log" "github.com/stretchr/testify/require" + "github.com/zeta-chain/zetacore/zetaclient/testutils" "github.com/zeta-chain/zetacore/pkg/chains" "github.com/zeta-chain/zetacore/testutil/sample" @@ -266,7 +267,7 @@ func TestOpenCloseDB(t *testing.T) { }) t.Run("should use memory db if specified", func(t *testing.T) { // open db with memory - err := ob.OpenDB(base.TempSQLiteDBPath, "") + err := ob.OpenDB(testutils.SQLiteMemory, "") require.NoError(t, err) // close db diff --git a/zetaclient/chains/bitcoin/observer/observer_test.go b/zetaclient/chains/bitcoin/observer/observer_test.go index ed9802567c..c79209e3fc 100644 --- a/zetaclient/chains/bitcoin/observer/observer_test.go +++ b/zetaclient/chains/bitcoin/observer/observer_test.go @@ -11,6 +11,7 @@ import ( "github.com/btcsuite/btcd/wire" lru "github.com/hashicorp/golang-lru" "github.com/stretchr/testify/require" + "github.com/zeta-chain/zetacore/zetaclient/testutils" "gorm.io/driver/sqlite" "gorm.io/gorm" @@ -35,7 +36,7 @@ var ( func setupDBTxResults(t *testing.T) (*gorm.DB, map[string]btcjson.GetTransactionResult) { submittedTx := map[string]btcjson.GetTransactionResult{} - db, err := gorm.Open(sqlite.Open(base.TempSQLiteDBPath), &gorm.Config{}) + db, err := gorm.Open(sqlite.Open(testutils.SQLiteMemory), &gorm.Config{}) require.NoError(t, err) err = db.AutoMigrate(&clienttypes.TransactionResultSQLType{}) diff --git a/zetaclient/chains/bitcoin/observer/outbound_test.go b/zetaclient/chains/bitcoin/observer/outbound_test.go index 910384ef9a..0aaeb7b600 100644 --- a/zetaclient/chains/bitcoin/observer/outbound_test.go +++ b/zetaclient/chains/bitcoin/observer/outbound_test.go @@ -27,7 +27,7 @@ func MockBTCObserverMainnet(t *testing.T) *Observer { tss := mocks.NewTSSMainnet() // create Bitcoin observer - ob, err := NewObserver(chain, btcClient, params, nil, nil, tss, base.TempSQLiteDBPath, base.Logger{}, nil) + ob, err := NewObserver(chain, btcClient, params, nil, nil, tss, testutils.SQLiteMemory, base.Logger{}, nil) require.NoError(t, err) return ob diff --git a/zetaclient/chains/bitcoin/rpc/rpc_live_test.go b/zetaclient/chains/bitcoin/rpc/rpc_live_test.go index 26355ad837..97a373f94d 100644 --- a/zetaclient/chains/bitcoin/rpc/rpc_live_test.go +++ b/zetaclient/chains/bitcoin/rpc/rpc_live_test.go @@ -55,7 +55,7 @@ func (suite *BitcoinObserverTestSuite) SetupTest() { btcClient := mocks.NewMockBTCRPCClient() // create observer - ob, err := observer.NewObserver(chain, btcClient, params, nil, nil, tss, base.TempSQLiteDBPath, + ob, err := observer.NewObserver(chain, btcClient, params, nil, nil, tss, testutils.SQLiteMemory, base.DefaultLogger(), nil) suite.Require().NoError(err) suite.Require().NotNil(ob) diff --git a/zetaclient/chains/evm/observer/outbound_test.go b/zetaclient/chains/evm/observer/outbound_test.go index df9a642a8e..72023d8f57 100644 --- a/zetaclient/chains/evm/observer/outbound_test.go +++ b/zetaclient/chains/evm/observer/outbound_test.go @@ -13,16 +13,13 @@ import ( "github.com/zeta-chain/zetacore/pkg/coin" "github.com/zeta-chain/zetacore/testutil/sample" observertypes "github.com/zeta-chain/zetacore/x/observer/types" - "github.com/zeta-chain/zetacore/zetaclient/chains/base" "github.com/zeta-chain/zetacore/zetaclient/chains/evm/observer" "github.com/zeta-chain/zetacore/zetaclient/config" "github.com/zeta-chain/zetacore/zetaclient/testutils" "github.com/zeta-chain/zetacore/zetaclient/testutils/mocks" ) -const ( - memDBPath = base.TempSQLiteDBPath -) +const memDBPath = testutils.SQLiteMemory // getContractsByChainID is a helper func to get contracts and addresses by chainID func getContractsByChainID( diff --git a/zetaclient/testutils/constant.go b/zetaclient/testutils/constant.go index ad8302577d..3d4f6e2a03 100644 --- a/zetaclient/testutils/constant.go +++ b/zetaclient/testutils/constant.go @@ -27,6 +27,9 @@ const ( EventZetaReverted = "ZetaReverted" EventERC20Deposit = "Deposited" EventERC20Withdraw = "Withdrawn" + + // SQLiteMemory is a SQLite in-memory database connection string. + SQLiteMemory = "file::memory:?cache=shared" ) // ConnectorAddresses contains constants ERC20 connector addresses for testing