-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase_test.go
93 lines (81 loc) · 1.74 KB
/
database_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package go_test_pg
import (
"context"
"database/sql"
"fmt"
"regexp"
"testing"
"github.com/jackc/pgx/v5"
"github.com/pkg/errors"
)
func TestPgpool_WithStdEmpty(t *testing.T) {
x := Pgpool{}
db := x.WithStdEmpty(t)
err := db.Ping()
if err != nil {
t.Fatal(err)
}
dbName, err := queryDBName(db)
if err != nil {
t.Fatal(err)
}
match, err := regexp.MatchString(`template1_\d+`, dbName)
if err != nil {
t.Fatal(err)
}
if !match {
t.Fatalf("database name does not match RE: %v", dbName)
}
}
// test fail if unreleased connections exists
func TestPgpool_WithStdEmpty_InuseConnections(t *testing.T) {
x := Pgpool{}
db, cleanupFn := x.newStdDBWithCleanup(t)
if cleanupFn == nil {
t.Fatal("cleanupFn is nil")
}
dbName, err := queryDBName(db)
if err != nil {
t.Error(err)
if err := cleanupFn(); err != nil {
t.Error(err)
}
t.FailNow()
}
tx, err := db.Begin()
if err != nil {
t.Error(err)
if err := cleanupFn(); err != nil {
t.Log(err)
}
t.FailNow()
}
expectedErr := fmt.Sprintf(
"unreleased connections exists: 1, can't drop database %v", dbName)
err = cleanupFn()
if err == nil || err.Error() != expectedErr {
t.Error(err)
}
if err = tx.Rollback(); err != nil {
t.Error(err)
}
if err = cleanupFn(); err != nil {
t.Error(err)
}
}
func TestName(t *testing.T) {
var dbPool = Pgpool{
BaseName: "go_test_pg",
SchemaFile: "./testdata/schema1.sql",
}
db := dbPool.WithEmpty(t)
err := db.QueryRow(context.Background(), `SELECT id FROM table1`).Scan()
if err != pgx.ErrNoRows {
t.Fatalf("Wanot pgx.ErrNoRows error, got %v", err)
}
}
func queryDBName(db *sql.DB) (string, error) {
var dbName string
err := db.QueryRow(`SELECT current_database()`).Scan(&dbName)
return dbName, errors.WithStack(err)
}