-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial db tests making use of dockertest, docker spinup of postg…
…res, testify suite builder, and a few tests around db utils. Remove a number of unused functions around database ops
- Loading branch information
Showing
7 changed files
with
244 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package db | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
"time" | ||
|
||
"github.com/DefiantLabs/cosmos-indexer/db/models" | ||
"github.com/ory/dockertest/v3" | ||
"github.com/stretchr/testify/suite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// TODO: Optimize tests to use a single database instance, clean database after each test, and teardown database after all tests are done | ||
|
||
type DBTestSuite struct { | ||
suite.Suite | ||
db *gorm.DB | ||
clean func() | ||
} | ||
|
||
func (suite *DBTestSuite) SetupTest() { | ||
clean, db, err := SetupTestDatabase() | ||
suite.Require().NoError(err) | ||
|
||
suite.db = db | ||
suite.clean = clean | ||
} | ||
|
||
func (suite *DBTestSuite) TearDownTest() { | ||
if suite.clean != nil { | ||
suite.clean() | ||
} | ||
|
||
suite.db = nil | ||
suite.clean = nil | ||
} | ||
|
||
func (suite *DBTestSuite) TestMigrateModels() { | ||
err := MigrateModels(suite.db) | ||
suite.Require().NoError(err) | ||
} | ||
|
||
func (suite *DBTestSuite) TestGetDBChainID() { | ||
err := MigrateModels(suite.db) | ||
suite.Require().NoError(err) | ||
|
||
initChain := models.Chain{ | ||
ChainID: "testchain-1", | ||
} | ||
|
||
err = suite.db.Create(&initChain).Error | ||
suite.Require().NoError(err) | ||
|
||
chainID, err := GetDBChainID(suite.db, initChain) | ||
suite.Require().NoError(err) | ||
suite.Assert().NotZero(chainID) | ||
} | ||
|
||
func SetupTestDatabase() (func(), *gorm.DB, error) { | ||
// TODO: allow environment overrides to skip creating mock database | ||
pool, err := dockertest.NewPool("") | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
err = pool.Client.Ping() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
resource, err := pool.Run("postgres", "15-alpine", []string{"POSTGRES_USER=test", "POSTGRES_PASSWORD=test", "POSTGRES_DB=test"}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var db *gorm.DB | ||
if err := pool.Retry(func() error { | ||
var err error | ||
db, err = PostgresDbConnect(resource.GetBoundIP("5432/tcp"), resource.GetPort("5432/tcp"), "test", "test", "test", "debug") | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
clean := func() { | ||
if err := pool.Purge(resource); err != nil { | ||
log.Fatalf("Could not purge resource: %s", err) | ||
} | ||
} | ||
|
||
return clean, db, nil | ||
} | ||
|
||
func createMockBlock(mockDb *gorm.DB, chain models.Chain, address models.Address, height int64, txIndexed bool, eventIndexed bool) (models.Block, error) { | ||
block := models.Block{ | ||
Chain: chain, | ||
Height: height, | ||
TimeStamp: time.Now(), | ||
TxIndexed: txIndexed, | ||
BlockEventsIndexed: eventIndexed, | ||
ProposerConsAddress: address, | ||
} | ||
|
||
err := mockDb.Create(&block).Error | ||
return block, err | ||
} | ||
|
||
func (suite *DBTestSuite) TestGetHighestBlockFunctions() { | ||
err := MigrateModels(suite.db) | ||
suite.Require().NoError(err) | ||
|
||
initChain := models.Chain{ | ||
ChainID: "testchain-1", | ||
} | ||
|
||
err = suite.db.Create(&initChain).Error | ||
suite.Require().NoError(err) | ||
|
||
initConsAddress := models.Address{ | ||
Address: "testchainaddress", | ||
} | ||
|
||
err = suite.db.Create(&initConsAddress).Error | ||
suite.Require().NoError(err) | ||
|
||
block1, err := createMockBlock(suite.db, initChain, initConsAddress, 1, true, true) | ||
suite.Require().NoError(err) | ||
|
||
txBlock := GetHighestIndexedBlock(suite.db, initChain.ID) | ||
eventBlock, err := GetHighestEventIndexedBlock(suite.db, initChain.ID) | ||
suite.Require().NoError(err) | ||
|
||
suite.Assert().Equal(block1.Height, txBlock.Height) | ||
suite.Assert().Equal(block1.Height, eventBlock.Height) | ||
|
||
_, err = createMockBlock(suite.db, initChain, initConsAddress, 2, false, false) | ||
suite.Require().NoError(err) | ||
|
||
txBlock = GetHighestIndexedBlock(suite.db, initChain.ID) | ||
eventBlock, err = GetHighestEventIndexedBlock(suite.db, initChain.ID) | ||
suite.Require().NoError(err) | ||
|
||
suite.Assert().Equal(block1.Height, txBlock.Height) | ||
suite.Assert().Equal(block1.Height, eventBlock.Height) | ||
|
||
block3, err := createMockBlock(suite.db, initChain, initConsAddress, 3, true, true) | ||
suite.Require().NoError(err) | ||
|
||
txBlock = GetHighestIndexedBlock(suite.db, initChain.ID) | ||
eventBlock, err = GetHighestEventIndexedBlock(suite.db, initChain.ID) | ||
suite.Require().NoError(err) | ||
|
||
suite.Assert().Equal(block3.Height, txBlock.Height) | ||
suite.Assert().Equal(block3.Height, eventBlock.Height) | ||
} | ||
|
||
func TestDBSuite(t *testing.T) { | ||
suite.Run(t, new(DBTestSuite)) | ||
} |
Oops, something went wrong.