Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Test query integration tests using a read-only session
Browse files Browse the repository at this point in the history
Setting the read-only mode on the session ensures we are not
writing to the database when executing queries.
  • Loading branch information
antekresic committed Apr 24, 2020
1 parent cb3e73a commit 4a276dd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
35 changes: 26 additions & 9 deletions pkg/internal/testhelpers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
"github.com/testcontainers/testcontainers-go/wait"
)

const (
defaultDB = "postgres"
connectTemplate = "postgres://postgres:password@%s:%d/%s"
)

var (
PromHost = "localhost"
PromPort nat.Port = "9090/tcp"
Expand All @@ -26,24 +31,35 @@ var (
pgPort nat.Port = "5432/tcp"
)

const defaultDB = "postgres"

func pgConnectURL(t *testing.T, dbName string) string {
template := "postgres://postgres:password@%s:%d/%s"
return fmt.Sprintf(template, pgHost, pgPort.Int(), dbName)
func pgConnectURL(dbName string) string {
return fmt.Sprintf(connectTemplate, pgHost, pgPort.Int(), dbName)
}

// WithDB establishes a database for testing and calls the callback
// WithDB establishes a database for testing and calls the callback.
func WithDB(t *testing.T, DBName string, f func(db *pgxpool.Pool, t *testing.T, connectString string)) {
db := dbSetup(t, DBName)
defer func() {
db.Close()
}()
f(db, t, pgConnectURL(t, DBName))
f(db, t, pgConnectURL(DBName))
}

func GetReadOnlyConnection(t testing.TB, DBName string) *pgxpool.Pool {
dbPool, err := pgxpool.Connect(context.Background(), pgConnectURL(DBName))
if err != nil {
t.Fatal(err)
}

_, err = dbPool.Exec(context.Background(), "SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY")
if err != nil {
t.Fatal(err)
}

return dbPool
}

func dbSetup(t *testing.T, DBName string) *pgxpool.Pool {
db, err := pgx.Connect(context.Background(), pgConnectURL(t, defaultDB))
db, err := pgx.Connect(context.Background(), pgConnectURL(defaultDB))
if err != nil {
t.Fatal(err)
}
Expand All @@ -57,12 +73,13 @@ func dbSetup(t *testing.T, DBName string) *pgxpool.Pool {
if err != nil {
t.Fatal(err)
}

err = db.Close(context.Background())
if err != nil {
t.Fatal(err)
}

dbPool, err := pgxpool.Connect(context.Background(), pgConnectURL(t, DBName))
dbPool, err := pgxpool.Connect(context.Background(), pgConnectURL(DBName))
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/testhelpers/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestPGConnection(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
db, err := pgx.Connect(context.Background(), pgConnectURL(t, defaultDB))
db, err := pgx.Connect(context.Background(), pgConnectURL(defaultDB))
if err != nil {
t.Fatal(err)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/pgmodel/query_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,11 @@ func TestSQLQuery(t *testing.T) {
withDB(t, *database, func(db *pgxpool.Pool, t *testing.T) {
// Ingest test dataset.
ingestQueryTestDataset(db, t, generateSmallTimeseries())
// Getting a read-only connection to ensure read path is idempotent.
readOnly := testhelpers.GetReadOnlyConnection(t, *database)
defer readOnly.Close()

r := NewPgxReader(db)
r := NewPgxReader(readOnly)
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
resp, err := r.Read(&c.readRequest)
Expand Down Expand Up @@ -879,8 +882,11 @@ func TestPromQL(t *testing.T) {
withDB(t, *database, func(db *pgxpool.Pool, t *testing.T) {
// Ingest test dataset.
ingestQueryTestDataset(db, t, generateLargeTimeseries())
// Getting a read-only connection to ensure read path is idempotent.
readOnly := testhelpers.GetReadOnlyConnection(t, *database)
defer readOnly.Close()

r := NewPgxReader(db)
r := NewPgxReader(readOnly)
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
connResp, connErr := r.Read(c.readRequest)
Expand Down

0 comments on commit 4a276dd

Please sign in to comment.