Skip to content

Commit

Permalink
Move sqlite-mem db to testutils
Browse files Browse the repository at this point in the history
  • Loading branch information
swift1337 committed Jun 25, 2024
1 parent 44e99fa commit a96898d
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
8 changes: 3 additions & 5 deletions zetaclient/chains/base/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion zetaclient/chains/base/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion zetaclient/chains/bitcoin/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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{})
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/bitcoin/observer/outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/bitcoin/rpc/rpc_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions zetaclient/chains/evm/observer/outbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions zetaclient/testutils/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a96898d

Please sign in to comment.