Skip to content

Commit

Permalink
Fix codecov: Backport the psql indexer into v0.34.x (#6906)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnasu committed Jan 27, 2022
1 parent f93ab9c commit 1d42c3f
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import (
"github.com/line/ostracon/types"
tmtime "github.com/line/ostracon/types/time"
"github.com/line/ostracon/version"

_ "github.com/lib/pq" // Register the Postgres database driver.
)

//------------------------------------------------------------------------------
Expand Down
56 changes: 56 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"testing"
"time"

"github.com/ory/dockertest"
"github.com/ory/dockertest/docker"

"github.com/line/tm-db/v2/memdb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -465,6 +468,59 @@ func TestNodeNewNodeTxIndexIndexer(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, n)
}
{
// Change to psql for test
config.TxIndex.Indexer = "psql"
n, err := doTest(DefaultDBProvider)
require.Error(t, err)
require.Equal(t, "no psql-conn is set for the \"psql\" indexer", err.Error())
require.Nil(t, n)

//config.TxIndex.PsqlConn = "cannot test with no-import postgres driver"
//n, err = doTest(DefaultDBProvider)
//require.Error(t, err)
//require.Equal(t, "creating psql indexer: sql: unknown driver \"postgres\" (forgotten import?)", err.Error())
//require.Nil(t, n)

config.TxIndex.PsqlConn = makeTestPsqlConn(t)
n, err = doTest(DefaultDBProvider)
require.NoError(t, err)
require.NotNil(t, n)
}
}

func makeTestPsqlConn(t *testing.T) string {
user := "postgres"
password := "secret"
port := "5432"
dsn := "postgres://%s:%s@localhost:%s/%s?sslmode=disable"
dbName := "postgres"

pool, err := dockertest.NewPool(os.Getenv("DOCKER_URL"))
if err != nil {
require.NoError(t, err)
}
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "postgres",
Tag: "13",
Env: []string{
"POSTGRES_USER=" + user,
"POSTGRES_PASSWORD=" + password,
"POSTGRES_DB=" + dbName,
"listen_addresses = '*'",
},
ExposedPorts: []string{port},
}, func(config *docker.HostConfig) {
// set AutoRemove to true so that stopped container goes away by itself
config.AutoRemove = true
config.RestartPolicy = docker.RestartPolicy{
Name: "no",
}
})
if err != nil {
require.NoError(t, err)
}
return fmt.Sprintf(dsn, user, password, resource.GetPort(port+"/tcp"), dbName)
}

func state(nVals int, height int64) (sm.State, dbm.DB, []types.PrivValidator) {
Expand Down
71 changes: 71 additions & 0 deletions state/indexer/sink/psql/backport_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,82 @@
package psql

import (
"context"
"testing"

abci "github.com/line/ostracon/abci/types"
"github.com/line/ostracon/libs/pubsub/query"
"github.com/line/ostracon/state/indexer"
"github.com/line/ostracon/state/txindex"
"github.com/line/ostracon/types"
"github.com/stretchr/testify/require"
)

var (
_ indexer.BlockIndexer = BackportBlockIndexer{}
_ txindex.TxIndexer = BackportTxIndexer{}
)

func TestBackportTxIndexer_AddBatch(t *testing.T) {
indexer := &EventSink{store: testDB(), chainID: chainID}
txIndexer := indexer.TxIndexer()
err := txIndexer.AddBatch(&txindex.Batch{})
require.NoError(t, err)
}

func TestBackportTxIndexer_Index(t *testing.T) {
indexer := &EventSink{store: testDB(), chainID: chainID}
txIndexer := indexer.TxIndexer()
err := txIndexer.Index(&abci.TxResult{})
require.Error(t, err)
require.Contains(t, err.Error(), "finding block ID: ")

blockIndexer := indexer.BlockIndexer()
err = blockIndexer.Index(types.EventDataNewBlockHeader{})
require.NoError(t, err)
err = txIndexer.Index(&abci.TxResult{})
require.NoError(t, err)
}

func TestBackportTxIndexer_Get(t *testing.T) {
indexer := &EventSink{store: testDB(), chainID: chainID}
txIndexer := indexer.TxIndexer()
result, err := txIndexer.Get([]byte{1})
require.Error(t, err)
require.Equal(t, "the TxIndexer.Get method is not supported", err.Error())
require.Nil(t, result)
}

func TestBackportTxIndexer_Search(t *testing.T) {
indexer := &EventSink{store: testDB(), chainID: chainID}
txIndexer := indexer.TxIndexer()
result, err := txIndexer.Get([]byte{1})
require.Error(t, err)
require.Equal(t, "the TxIndexer.Get method is not supported", err.Error())
require.Nil(t, result)
}

func TestBackportBlockIndexer_Has(t *testing.T) {
indexer := &EventSink{store: testDB(), chainID: chainID}
blockIndexer := indexer.BlockIndexer()
result, err := blockIndexer.Has(0)
require.Error(t, err)
require.Equal(t, "the BlockIndexer.Has method is not supported", err.Error())
require.False(t, result)
}

func TestBackportBlockIndexer_Index(t *testing.T) {
indexer := &EventSink{store: testDB(), chainID: chainID}
blockIndexer := indexer.BlockIndexer()
err := blockIndexer.Index(types.EventDataNewBlockHeader{})
require.NoError(t, err)
}

func TestBackportBlockIndexer_Search(t *testing.T) {
indexer := &EventSink{store: testDB(), chainID: chainID}
blockIndexer := indexer.BlockIndexer()
result, err := blockIndexer.Search(context.Background(), &query.Query{})
require.Error(t, err)
require.Equal(t, "the BlockIndexer.Search method is not supported", err.Error())
require.Nil(t, result)
}

0 comments on commit 1d42c3f

Please sign in to comment.