-
Notifications
You must be signed in to change notification settings - Fork 0
/
structscan_test.go
161 lines (130 loc) · 4.09 KB
/
structscan_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package pgxscan
import (
"context"
"os"
"testing"
"time"
pgx "github.com/jackc/pgx/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testEntity struct {
ID string `db:"id"`
CreatedAt time.Time `db:"created_at"`
SomeData string `db:"some_data"`
}
type testMissingField struct {
ID string `db:"id"`
CreatedAt time.Time `db:"created_at"`
}
func TestScanStruct(t *testing.T) {
connString := initDB(t)
conn, err := pgx.Connect(context.Background(), connString)
require.NoError(t, err)
defer func() {
err := conn.Close(context.Background())
assert.NoError(t, err)
}()
e1, e2 := prepareData(t, conn)
rows := selectRows(t, conn, e1.ID, e2.ID)
result := new(testEntity)
err = ScanStruct(rows, result)
require.NoError(t, err)
assert.Equal(t, e1.ID, result.ID)
assert.Equal(t, e1.SomeData, result.SomeData)
// compare unit timestamp to avoid milliseconds diff
assert.Equal(t, e1.CreatedAt.Unix(), result.CreatedAt.Unix())
// test some fail cases
rowsEmpty := selectRows(t, conn, "foo", "bar")
resultEmpty := new(testEntity)
err = ScanStruct(rowsEmpty, resultEmpty)
require.Error(t, err)
require.Equal(t, err, pgx.ErrNoRows)
}
func TestScanStructs(t *testing.T) {
connString := initDB(t)
conn, err := pgx.Connect(context.Background(), connString)
require.NoError(t, err)
defer func() {
err := conn.Close(context.Background())
assert.NoError(t, err)
}()
e1, e2 := prepareData(t, conn)
var result []*testEntity
rows := selectRows(t, conn, e1.ID, e2.ID)
err = ScanStructs(rows, &result)
require.NoError(t, err)
require.Len(t, result, 2)
rows.Close()
assert.Equal(t, e1.ID, result[0].ID)
assert.Equal(t, e1.SomeData, result[0].SomeData)
// compare unit timestamp to avoid milliseconds diff
assert.Equal(t, e1.CreatedAt.Unix(), result[0].CreatedAt.Unix())
assert.Equal(t, e2.ID, result[1].ID)
assert.Equal(t, e2.SomeData, result[1].SomeData)
// compare unit timestamp to avoid milliseconds diff
assert.Equal(t, e2.CreatedAt.Unix(), result[1].CreatedAt.Unix())
// test some fail cases
rowsFailStruct := selectRows(t, conn, e1.ID, e2.ID)
var invalidDest []*testEntity
err = ScanStructs(rowsFailStruct, invalidDest)
require.Error(t, err)
assert.Equal(t, "expected a pointer to a slice, got []*pgxscan.testEntity", err.Error())
rowsFailStruct.Close()
rowsFailMissing := selectRows(t, conn, e1.ID, e2.ID)
var missingDest []*testMissingField
err = ScanStructs(rowsFailMissing, &missingDest)
require.Error(t, err)
assert.Equal(t, `missing column "some_data" in dest *pgxscan.testMissingField`, err.Error())
rowsFailMissing.Close()
}
func prepareData(t *testing.T, conn *pgx.Conn) (testEntity, testEntity) {
t.Helper()
e1 := testEntity{
ID: "scan-structs-1-" + time.Now().String(),
CreatedAt: time.Now(),
SomeData: "foo bar baz",
}
e2 := testEntity{
ID: "scan-structs-2-" + time.Now().String(),
CreatedAt: time.Now().Add(time.Hour),
SomeData: "foo bar baz qux",
}
_, err := conn.Exec(
context.Background(),
"INSERT INTO structscan_test (id, some_data, created_at) VALUES ($1, $2, $3), ($4, $5, $6)",
e1.ID, e1.SomeData, e1.CreatedAt,
e2.ID, e2.SomeData, e2.CreatedAt,
)
require.NoError(t, err)
return e1, e2
}
func selectRows(t *testing.T, conn *pgx.Conn, id1, id2 string) pgx.Rows {
t.Helper()
rows, err := conn.Query(context.Background(), "SELECT * FROM structscan_test WHERE id IN ($1, $2) ORDER BY id ASC", id1, id2)
require.NoError(t, err)
return rows
}
func initDB(t *testing.T) string {
t.Helper()
connString := os.Getenv("TEST_POSTGRES_URI")
require.NotEmpty(t, connString)
conn, err := pgx.Connect(context.Background(), connString)
require.NoError(t, err)
defer func() {
err := conn.Close(context.Background())
assert.NoError(t, err)
}()
_, err = conn.Exec(context.Background(), `DROP TABLE IF EXISTS structscan_test`)
require.NoError(t, err)
_, err = conn.Exec(context.Background(), `
CREATE TABLE "structscan_test"
(
id text PRIMARY KEY,
some_data text not null,
created_at timestamptz not null
)
`)
require.NoError(t, err)
return connString
}