-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathdb_libpq_test.go
More file actions
60 lines (45 loc) · 1.59 KB
/
db_libpq_test.go
File metadata and controls
60 lines (45 loc) · 1.59 KB
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
60
package gue
import (
"database/sql"
"fmt"
"testing"
_ "github.com/lib/pq" // register postgres driver
"github.com/stretchr/testify/require"
)
// openTestPoolMaxConnsLibPQ opens connections pool used in testing
func openTestPoolMaxConnsLibPQ(t testing.TB, maxConnections int, gueSchema, secondSchema string) *sql.DB {
t.Helper()
if (gueSchema == "" && secondSchema != "") || (gueSchema != "" && secondSchema == "") {
require.Fail(t, "Both schemas should be either set or unset")
}
applyMigrations(gueSchema).Do(func() {
doApplyMigrations(t, gueSchema)
})
dsn := testConnDSN(t)
if gueSchema != "" && secondSchema != "" {
dsn += fmt.Sprintf("&search_path=%s,%s", secondSchema, gueSchema)
}
db, err := sql.Open("postgres", dsn)
require.NoError(t, err)
db.SetMaxOpenConns(maxConnections)
// guw schema will be created by migrations routine, we need to take care only on the second one
if secondSchema != "" {
_, err := db.Exec("CREATE SCHEMA IF NOT EXISTS " + secondSchema)
require.NoError(t, err)
}
t.Cleanup(func() {
truncateAndClose(t, db)
})
return db
}
// openTestPoolLibPQ opens connections pool used in testing
func openTestPoolLibPQ(t testing.TB) *sql.DB {
t.Helper()
return openTestPoolMaxConnsLibPQ(t, defaultPoolConns, "", "")
}
// openTestPoolLibPQCustomSchemas opens connections pool used in testing with gue table installed to own schema and
// search_path set to two different schemas
func openTestPoolLibPQCustomSchemas(t testing.TB, gueSchema, secondSchema string) *sql.DB {
t.Helper()
return openTestPoolMaxConnsLibPQ(t, defaultPoolConns, gueSchema, secondSchema)
}