-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestutils_test.go
More file actions
48 lines (38 loc) · 913 Bytes
/
testutils_test.go
File metadata and controls
48 lines (38 loc) · 913 Bytes
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
package models
import (
"context"
"os"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
)
func newTestDB(t *testing.T) *pgxpool.Pool {
dsn := "postgres://postgres:admin@localhost:5432/shrink_test?sslmode=disable"
dbPool, err := pgxpool.New(context.Background(), dsn)
if err != nil {
t.Fatal(err)
}
// Read and execute the setup SQL script
setupScript, err := os.ReadFile("./testdata/setup.sql")
if err != nil {
dbPool.Close()
t.Fatal(err)
}
_, err = dbPool.Exec(context.Background(), string(setupScript))
if err != nil {
dbPool.Close()
t.Fatal(err)
}
// Register a cleanup function to run the teardown script
t.Cleanup(func() {
defer dbPool.Close()
teardownScript, err := os.ReadFile("./testdata/teardown.sql")
if err != nil {
t.Fatal(err)
}
_, err = dbPool.Exec(context.Background(), string(teardownScript))
if err != nil {
t.Fatal(err)
}
})
return dbPool
}