-
Notifications
You must be signed in to change notification settings - Fork 2
/
int.go
70 lines (50 loc) · 1.48 KB
/
int.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
package testutil
import (
"context"
"database/sql"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/orlangure/gnomock"
"github.com/orlangure/gnomock/preset/postgres"
"github.com/stretchr/testify/assert"
"testing"
)
func SetupDB(t *testing.T) (*pgxpool.Pool, *sql.DB) {
t.Helper()
p := postgres.Preset(
postgres.WithUser("gnomock", "gnomick"),
postgres.WithDatabase("mydb"),
postgres.WithQueriesFile("../testutil/db/schema.sql"),
)
container, err := gnomock.Start(p)
assert.NoError(t, err)
t.Cleanup(func() { _ = gnomock.Stop(container) })
connStr := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
container.Host, container.DefaultPort(),
"gnomock", "gnomick", "mydb",
)
pgConfig, err := pgxpool.ParseConfig(connStr)
assert.NoError(t, err)
pool, err := pgxpool.NewWithConfig(context.Background(), pgConfig)
assert.NoError(t, err)
db, err := sql.Open("postgres", connStr)
assert.NoError(t, err)
return pool, db
}
func AssertSuccess(t *testing.T, pool *pgxpool.Pool, name string) {
t.Helper()
row := pool.QueryRow(context.TODO(), `select name from cats where name=$1`, name)
var n string
err := row.Scan(&n)
assert.NoError(t, err)
assert.Equal(t, name, n)
}
func AssertFailure(t *testing.T, pool *pgxpool.Pool, name string) {
t.Helper()
row := pool.QueryRow(context.TODO(), `select name from cats where name=$1`, name)
var n string
err := row.Scan(&n)
assert.ErrorIs(t, err, pgx.ErrNoRows)
}