-
Notifications
You must be signed in to change notification settings - Fork 87
/
postgres_integration_common_test.go
59 lines (46 loc) · 1.62 KB
/
postgres_integration_common_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package postgres
import (
"context"
"testing"
test2 "github.com/sirupsen/logrus/hooks/test"
"github.com/algorand/go-algorand/data/bookkeeping"
"github.com/algorand/go-algorand/ledger"
"github.com/algorand/indexer/processor"
"github.com/algorand/indexer/processor/blockprocessor"
"github.com/algorand/indexer/util/test"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/stretchr/testify/require"
"github.com/algorand/indexer/idb"
pgtest "github.com/algorand/indexer/idb/postgres/internal/testing"
)
func setupIdbWithConnectionString(t *testing.T, connStr string, genesis bookkeeping.Genesis) *IndexerDb {
idb, _, err := OpenPostgres(connStr, idb.IndexerDbOptions{}, nil)
require.NoError(t, err)
err = idb.LoadGenesis(genesis)
require.NoError(t, err)
return idb
}
func setupIdb(t *testing.T, genesis bookkeeping.Genesis) (*IndexerDb, func(), processor.Processor, *ledger.Ledger) {
_, connStr, shutdownFunc := pgtest.SetupPostgres(t)
db := setupIdbWithConnectionString(t, connStr, genesis)
newShutdownFunc := func() {
db.Close()
shutdownFunc()
}
log, _ := test2.NewNullLogger()
l, err := test.MakeTestLedger(log)
require.NoError(t, err)
proc, err := blockprocessor.MakeProcessorWithLedger(l, db.AddBlock)
require.NoError(t, err, "failed to open ledger")
return db, newShutdownFunc, proc, l
}
// Helper to execute a query returning an integer, for example COUNT(*). Returns -1 on an error.
func queryInt(db *pgxpool.Pool, queryString string, args ...interface{}) int {
row := db.QueryRow(context.Background(), queryString, args...)
var count int
err := row.Scan(&count)
if err != nil {
return -1
}
return count
}