-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgrestest_test.go
53 lines (49 loc) · 1.39 KB
/
postgrestest_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
package postgrestest
import (
"database/sql"
"testing"
"github.com/stretchr/testify/require"
)
func TestNewPostgresTest(t *testing.T) {
t.Parallel()
testDB := NewPostgresTest(t)
db, err := sql.Open("pgx", testDB)
require.NoError(t, err)
defer db.Close()
rows, err := db.Query(`SELECT 42`)
require.NoError(t, err)
for rows.Next() {
var r int
err = rows.Scan(&r)
require.NoError(t, err)
require.Equal(t, 42, r)
}
defer rows.Close()
}
func TestAlterTableSequences(t *testing.T) {
t.Parallel()
testDB := NewPostgresTest(t)
db, err := sql.Open("pgx", testDB)
require.NoError(t, err)
defer db.Close()
_, err = db.Exec(`CREATE TABLE table_a (id serial PRIMARY KEY);`)
require.NoError(t, err)
_, err = db.Exec(`CREATE SEQUENCE seq_a INCREMENT 1 START 1;`)
require.NoError(t, err)
currentSequenceValue := func(t *testing.T, db *sql.DB, seqName string) int {
t.Helper()
rows, err := db.Query(`SELECT last_value FROM ` + seqName + `;`)
require.NoError(t, err)
defer rows.Close()
require.True(t, rows.Next())
var curVal int
err = rows.Scan(&curVal)
require.NoError(t, err)
return curVal
}
require.Equal(t, 1, currentSequenceValue(t, db, "table_a_id_seq"))
require.Equal(t, 1, currentSequenceValue(t, db, "seq_a"))
AlterTableSequences(t, db)
require.NotEqual(t, 1, currentSequenceValue(t, db, "table_a_id_seq"))
require.NotEqual(t, 1, currentSequenceValue(t, db, "seq_a"))
}