-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_test.go
271 lines (203 loc) · 5.72 KB
/
prepare_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package pgxe_test
import (
"testing"
"github.com/anton7r/pgxe"
)
func TestPrepareSelect(t *testing.T) {
prep := pgxe.Prepare("SELECT * FROM employees WHERE ID <= $1")
emps := []Employee{}
err := prep.Select(db, &emps, 3)
if err != nil {
t.Fatal("Errored in preparation: " + err.Error())
}
if len(emps) != 3 {
t.Fatal("Expected length 3 but got " + string(rune(len(emps))))
}
}
func TestPrepareGet(t *testing.T) {
prep := pgxe.Prepare("SELECT * FROM employees WHERE ID = $1 LIMIT 1")
emp := Employee{}
err := prep.Get(db, &emp, 1)
if err != nil {
t.Fatal("Errored during retrieval: " + err.Error())
}
if emp.Name != "ABC" {
t.Error("Name wasnt as expected and got: " + emp.Name)
}
if emp.Surname != "123" {
t.Error("Surname wasnt as expected and got: " + emp.Surname)
}
if emp.PaymentAddress != "FooBar" {
t.Error("PaymentAddress wasnt as expected and got: " + emp.PaymentAddress)
}
if emp.ID != 1 {
t.Errorf("ID wasnt as expected and got: %o", emp.ID)
}
}
func TestPrepareQuery(t *testing.T) {
prep := pgxe.Prepare("SELECT * FROM employees WHERE ID <= $1")
rows, err := prep.Query(db, 3)
for rows.Next() {
emp := Employee{}
rows.Scan(&emp.Name, &emp.Surname, &emp.PaymentAddress, &emp.ID)
if err != nil {
t.Error("Test errored:" + err.Error())
t.FailNow()
}
if emp.Name == "" {
t.Error("Name is empty")
}
if emp.Surname == "" {
t.Error("Surname is empty")
}
if emp.PaymentAddress == "" {
t.Error("PaymentAddress is empty")
}
if emp.ID == 0 {
t.Errorf("ID was 0")
}
}
}
func TestPrepareExec(t *testing.T) {
prep := pgxe.Prepare("INSERT INTO employees (name, surname, \"paymentAddress\") VALUES ($1, $1, $1)")
c, err := prep.Exec(db, "Hi")
if err != nil {
t.Error("Insertion to the database failed: " + err.Error())
t.FailNow()
}
t.Log("got command tag: " + c.String())
}
func TestPrepareQueryRow(t *testing.T) {
prep := pgxe.Prepare("SELECT * FROM employees WHERE ID = $1 LIMIT 1")
emp := Employee{}
row, err := prep.QueryRow(db, 1)
if err != nil {
t.Fatal("Errored during retrieval: " + err.Error())
}
err = row.Scan(&emp.Name, &emp.Surname, &emp.PaymentAddress, &emp.ID)
if err != nil {
t.Fatal("Errored during scan: " + err.Error())
}
if emp.Name != "ABC" {
t.Error("Name wasnt as expected and got: " + emp.Name)
}
if emp.Surname != "123" {
t.Error("Surname wasnt as expected and got: " + emp.Surname)
}
if emp.PaymentAddress != "FooBar" {
t.Error("PaymentAddress wasnt as expected and got: " + emp.PaymentAddress)
}
if emp.ID != 1 {
t.Errorf("ID wasnt as expected and got: %o", emp.ID)
}
}
func TestPrepareNamedGet(t *testing.T) {
prep, err := pgxe.PrepareNamed("SELECT * FROM employees WHERE ID = :Id LIMIT 1")
if err != nil {
t.Fatal("Errored in preparation: " + err.Error())
}
emp := Employee{}
er2 := prep.Get(db, &emp, &tNamed{Id: 1})
if er2 != nil {
t.Error("Test errored: " + er2.Error())
}
if emp.Name != "ABC" {
t.Error("Name wasnt as expected and got: " + emp.Name)
}
if emp.Surname != "123" {
t.Error("Surname wasnt as expected and got: " + emp.Surname)
}
if emp.PaymentAddress != "FooBar" {
t.Error("PaymentAddress wasnt as expected and got: " + emp.PaymentAddress)
}
if emp.ID != 1 {
t.Errorf("ID wasnt as expected and got: %o", emp.ID)
}
}
func TestPrepareNamedSelect(t *testing.T) {
prep, err := pgxe.PrepareNamed("SELECT * FROM employees WHERE ID <= :Id")
if err != nil {
t.Fatal("Errored during preparation: " + err.Error())
}
emps := []Employee{}
err2 := prep.Select(db, &emps, &tNamed{Id: 3})
if err2 != nil {
t.Fatal("Errored during query: " + err.Error())
}
if len(emps) != 3 {
t.Fatal("Expected length 3 but got " + string(rune(len(emps))))
}
}
func TestPrepareNamedQuery(t *testing.T) {
prep, err := pgxe.PrepareNamed("SELECT * FROM employees WHERE ID <= :Id")
if err != nil {
t.Fatal("Errored in preparation: " + err.Error())
}
rows, er2 := prep.Query(db, &tNamed{Id: 3})
if er2 != nil {
t.Error("Test errored: " + er2.Error())
}
for rows.Next() {
emp := Employee{}
rows.Scan(&emp.Name, &emp.Surname, &emp.PaymentAddress, &emp.ID)
if err != nil {
t.Error("Test errored:" + err.Error())
t.FailNow()
}
if emp.Name == "" {
t.Error("Name is empty")
}
if emp.Surname == "" {
t.Error("Surname is empty")
}
if emp.PaymentAddress == "" {
t.Error("PaymentAddress is empty")
}
if emp.ID == 0 {
t.Errorf("ID was 0")
}
}
}
func TestPrepareNamedQueryRow(t *testing.T) {
prep, err := pgxe.PrepareNamed("SELECT * FROM employees WHERE ID = :Id LIMIT 1")
if err != nil {
t.Fatal("Errored in preparation: " + err.Error())
}
row, er2 := prep.QueryRow(db, &tNamed{Id: 1})
if er2 != nil {
t.Error("Test errored: " + er2.Error())
}
emp := Employee{}
er3 := row.Scan(&emp.Name, &emp.Surname, &emp.PaymentAddress, &emp.ID)
if er3 != nil {
t.Error("Test errored:" + err.Error())
t.FailNow()
}
if emp.Name != "ABC" {
t.Error("Name wasnt as expected and got: " + emp.Name)
}
if emp.Surname != "123" {
t.Error("Surname wasnt as expected and got: " + emp.Surname)
}
if emp.PaymentAddress != "FooBar" {
t.Error("PaymentAddress wasnt as expected and got: " + emp.PaymentAddress)
}
if emp.ID != 1 {
t.Errorf("ID wasnt as expected and got: %o", emp.ID)
}
}
type testingS struct {
Value string `json:"value,omitempty"`
}
func TestPrepareNamedExec(t *testing.T) {
prep, err := pgxe.PrepareNamed("INSERT INTO employees (name, surname, \"paymentAddress\") VALUES (:Value, :Value, :Value)")
if err != nil {
t.Fatal("Errored during preparation: " + err.Error())
}
c, err := prep.Exec(db, &testingS{"Hi"})
if err != nil {
t.Error("Insertion to the database failed: " + err.Error())
t.FailNow()
}
t.Log("got command tag: " + c.String())
}