forked from cncf/devstats.archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_test.go
363 lines (325 loc) · 8.03 KB
/
pg_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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package gha2db
import (
"database/sql"
"testing"
"time"
lib "gha2db"
testlib "gha2db/test"
)
func TestCleanUTF8(t *testing.T) {
// Test cases
var testCases = []struct {
value string
expected string
}{
{value: "value", expected: "value"},
{value: "val\x00ue", expected: "value"},
{value: "val\u0000ue", expected: "value"},
{value: "v\x00a\U00000000l\u0000ue", expected: "value"},
{value: "平仮名, ひらがな", expected: "平仮名, ひらがな"},
{value: "\u0000平仮名\x00ひらがな\U00000000", expected: "平仮名ひらがな"},
}
// Execute test cases
for index, test := range testCases {
got := lib.CleanUTF8(test.value)
if got != test.expected {
t.Errorf("test number %d, expected %v, got %v", index+1, test.expected, got)
}
}
}
func TestTruncToBytes(t *testing.T) {
// Test cases
var testCases = []struct {
value string
n int
expectedStr string
expectedLen int
}{
{
value: "value",
n: 3,
expectedStr: "val",
expectedLen: 3,
},
{
value: "平仮名, ひらがな",
n: 6,
expectedStr: "平仮",
expectedLen: 6,
},
{
value: "平仮名, ひらがな",
n: 8,
expectedStr: "平仮",
expectedLen: 6,
},
{
value: "平仮名, ひらがな",
n: 9,
expectedStr: "平仮名",
expectedLen: 9,
},
{
value: "\u0000平仮名, \x00ひら\U00000000がな",
n: 9,
expectedStr: "平仮名",
expectedLen: 9,
},
}
// Execute test cases
for index, test := range testCases {
gotStr := lib.TruncToBytes(test.value, test.n)
if gotStr != test.expectedStr {
t.Errorf("test number %d, expected string %v, got %v", index+1, test.expectedStr, gotStr)
}
gotLen := len(gotStr)
if gotLen != test.expectedLen {
t.Errorf("test number %d, expected length %v, got %v", index+1, test.expectedLen, gotLen)
}
}
}
func TestTruncStringOrNil(t *testing.T) {
// Test cases
sValues := []string{"value", "平仮名, ひらがな", "\u0000平仮名, \x00ひら\U00000000がな"}
var testCases = []struct {
value *string
n int
expected interface{}
}{
{
value: nil,
n: 10,
expected: nil,
},
{
value: &sValues[0],
n: 3,
expected: "val",
},
{
value: &sValues[1],
n: 6,
expected: "平仮",
},
{
value: &sValues[2],
n: 9,
expected: "平仮名",
},
}
// Execute test cases
for index, test := range testCases {
got := lib.TruncStringOrNil(test.value, test.n)
if got != test.expected {
t.Errorf("test number %d, expected %v, got %v", index+1, test.expected, got)
}
}
}
func TestBoolOrNil(t *testing.T) {
result := lib.BoolOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
val := true
result = lib.BoolOrNil(&val)
if result != val {
t.Errorf("expected true, got %v", result)
}
}
func TestNegatedBoolOrNil(t *testing.T) {
result := lib.NegatedBoolOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
val := true
result = lib.NegatedBoolOrNil(&val)
expected := !val
if result != expected {
t.Errorf("expected %v, got %v", expected, result)
}
}
func TestTimeOrNil(t *testing.T) {
result := lib.TimeOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
val := time.Now()
result = lib.TimeOrNil(&val)
if result != val {
t.Errorf("expected %v, got %v", val, result)
}
}
func TestIntOrNil(t *testing.T) {
result := lib.IntOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
val := 2
result = lib.IntOrNil(&val)
if result != val {
t.Errorf("expected %v, got %v", val, result)
}
}
func TestFirstIntOrNil(t *testing.T) {
nn1 := 1
nn2 := 2
var testCases = []struct {
array []*int
expected interface{}
}{
{array: []*int{}, expected: nil},
{array: []*int{nil}, expected: nil},
{array: []*int{&nn1}, expected: nn1},
{array: []*int{nil, nil}, expected: nil},
{array: []*int{nil, &nn1}, expected: nn1},
{array: []*int{&nn1, nil}, expected: nn1},
{array: []*int{&nn1, &nn2}, expected: nn1},
{array: []*int{&nn2, &nn1}, expected: nn2},
{array: []*int{nil, &nn2, &nn1}, expected: nn2},
}
// Execute test cases
for index, test := range testCases {
got := lib.FirstIntOrNil(test.array)
if got != test.expected {
t.Errorf("test number %d, expected %v, got %v", index+1, test.expected, got)
}
}
}
func TestStringOrNil(t *testing.T) {
result := lib.StringOrNil(nil)
if result != nil {
t.Errorf("test nil case: expected <nil>, got %v", result)
}
val := "hello\x00 world"
expected := "hello world"
result = lib.StringOrNil(&val)
if result != expected {
t.Errorf("expected %v, got %v", val, result)
}
}
func TestPostgres(t *testing.T) {
// Environment context parse
var ctx lib.Ctx
ctx.Init()
// Do not allow to run tests in "gha" database
if ctx.PgDB == "gha" {
t.Errorf("tests cannot be run on \"gha\" database")
return
}
// Drop database if exists
lib.DropDatabaseIfExists(&ctx)
// Create database if needed
createdDatabase := lib.CreateDatabaseIfNeeded(&ctx)
if !createdDatabase {
t.Errorf("failed to create database \"%s\"", ctx.PgDB)
}
// Drop database after tests
defer func() {
// Drop database after tests
lib.DropDatabaseIfExists(&ctx)
}()
// Connect to Postgres DB
c := lib.PgConn(&ctx)
defer c.Close()
// Create example table
lib.ExecSQLWithErr(
c,
&ctx,
lib.CreateTable(
"test(an_int int, a_string text, a_dt {{ts}}, primary key(an_int))",
),
)
// Insert single row
lib.ExecSQLWithErr(
c,
&ctx,
"insert into test(an_int, a_string, a_dt) "+lib.NValues(3),
lib.AnyArray{1, "string", time.Now()}...,
)
// Get inserted int
i := 0
lib.QueryRowSQL(c, &ctx, "select an_int from test").Scan(&i)
if i != 1 {
t.Errorf("expected to insert 1, got %v", i)
}
// Insert another row
lib.ExecSQLWithErr(
c,
&ctx,
"insert into test(an_int, a_string, a_dt) "+lib.NValues(3),
lib.AnyArray{11, "another string", time.Now()}...,
)
// Get all ints from database
gotArr := getInts(c, &ctx)
expectedArr := []int{1, 11}
if !testlib.CompareIntSlices(gotArr, expectedArr) {
t.Errorf("expected %v after two inserts, got %v", expectedArr, gotArr)
}
// Start transaction
tx, err := c.Begin()
if err != nil {
t.Errorf(err.Error())
}
// Insert another row
lib.ExecSQLTxWithErr(
tx,
&ctx,
"insert into test(an_int, a_string, a_dt) "+lib.NValues(3),
lib.AnyArray{21, "this will be rolled back", time.Now()}...,
)
// Rollback transaction
tx.Rollback()
// Get all ints from database
gotArr = getInts(c, &ctx)
if !testlib.CompareIntSlices(gotArr, expectedArr) {
t.Errorf("expected %v after rollback, got %v", expectedArr, gotArr)
}
// Start transaction
tx, err = c.Begin()
if err != nil {
t.Errorf(err.Error())
}
// Insert another row
lib.ExecSQLTxWithErr(
tx,
&ctx,
"insert into test(an_int, a_string, a_dt) "+lib.NValues(3),
lib.AnyArray{31, "this will be committed", time.Now()}...,
)
// Commit transaction
tx.Commit()
// Get all ints from database
gotArr = getInts(c, &ctx)
expectedArr = []int{1, 11, 31}
if !testlib.CompareIntSlices(gotArr, expectedArr) {
t.Errorf("expected %v after commit, got %v", expectedArr, gotArr)
}
// Insert ignore row (that violetes primary key constraint)
lib.ExecSQLWithErr(
c,
&ctx,
lib.InsertIgnore("into test(an_int, a_string, a_dt) "+lib.NValues(3)),
lib.AnyArray{1, "conflicting key", time.Now()}...,
)
// Get all ints from database
gotArr = getInts(c, &ctx)
if !testlib.CompareIntSlices(gotArr, expectedArr) {
t.Errorf("expected %v after insert ignore, got %v", expectedArr, gotArr)
}
}
// getInts - gets all ints from database, sorted
func getInts(c *sql.DB, ctx *lib.Ctx) []int {
// Get inserted values
rows := lib.QuerySQLWithErr(c, ctx, "select an_int from test order by an_int asc")
defer rows.Close()
var (
i int
arr []int
)
for rows.Next() {
lib.FatalOnError(rows.Scan(&i))
arr = append(arr, i)
}
lib.FatalOnError(rows.Err())
return arr
}