-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpgtest_test.go
99 lines (81 loc) · 2.91 KB
/
pgtest_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
94
95
96
97
98
99
package pgtest_test
import (
"context"
"github.com/ibrhmkoz/pgtest"
"github.com/stretchr/testify/require"
"testing"
)
const (
url pgtest.DesiredStateURL = "file://schema.sql"
)
func TestPGTest(t *testing.T) {
ctx := context.Background()
pool := pgtest.New(t, ctx, pgtest.WithDesiredState(url))
// Given
_, err := pool.Exec(ctx, `insert into clinics (id, name, email) values ($1, $2, $3)`, "test-clinic-uuid", "Test Clinic", "[email protected]")
require.NoError(t, err)
// When
var name string
var email string
err = pool.QueryRow(ctx, `select name, email from clinics`).Scan(&name, &email)
// Then
require.NoError(t, err)
require.Equal(t, "Test Clinic", name)
require.Equal(t, "[email protected]", email)
}
func TestWithReferentialIntegrityEnabled(t *testing.T) {
ctx := context.Background()
pool := pgtest.New(t, ctx, pgtest.WithDesiredState(url))
_, err := pool.Exec(ctx, `insert into doctors (id, clinic_id) values ($1, $2)`, "test-doctor-uuid", "non-existent-clinic-uuid")
require.Error(t, err)
}
func TestWithReferentialIntegrityDisabled(t *testing.T) {
ctx := context.Background()
pool := pgtest.New(t, ctx,
pgtest.WithReferentialIntegrityDisabled(),
pgtest.WithDesiredState(url),
)
_, err := pool.Exec(ctx, `insert into doctors (id, clinic_id) values ($1, $2)`, "test-doctor-uuid", "non-existent-clinic-uuid")
require.NoError(t, err)
}
func TestWithVersion(t *testing.T) {
ctx := context.Background()
pool := pgtest.New(t, ctx,
pgtest.WithVersion("14"),
pgtest.WithDesiredState(url),
)
// Given
_, err := pool.Exec(ctx, `insert into clinics (id, name, email) values ($1, $2, $3)`, "test-clinic-uuid", "Test Clinic", "[email protected]")
require.NoError(t, err)
// When
var name string
var email string
err = pool.QueryRow(ctx, `select name, email from clinics`).Scan(&name, &email)
// Then
require.NoError(t, err)
require.Equal(t, "Test Clinic", name)
require.Equal(t, "[email protected]", email)
}
func TestWithMigrator(t *testing.T) {
ctx := context.Background()
migrations := "file://migrations"
pool := pgtest.New(t, ctx,
pgtest.WithMigrator(pgtest.MigratorGoMigrator),
pgtest.WithDesiredState(migrations),
)
// Test that the tables were created
var tableCount int
err := pool.QueryRow(ctx, `select count(*) from information_schema.tables where table_schema = 'public'`).Scan(&tableCount)
require.NoError(t, err)
// Since go-migrate creates a table named schema_migrations, we should have 3 tables
require.Equal(t, 3, tableCount)
// Test inserting and querying data
_, err = pool.Exec(ctx, `insert into clinics (id, name, email) values ($1, $2, $3)`, "test-clinic-uuid", "Test Clinic", "[email protected]")
require.NoError(t, err)
var name string
var email string
err = pool.QueryRow(ctx, `select name, email from clinics where id = $1`, "test-clinic-uuid").Scan(&name, &email)
require.NoError(t, err)
require.Equal(t, "Test Clinic", name)
require.Equal(t, "[email protected]", email)
}