-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinsert_test.go
81 lines (77 loc) · 2.57 KB
/
insert_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
package goqux_test
import (
"testing"
"github.com/roneli/goqux"
"github.com/stretchr/testify/assert"
)
type insertModel struct {
IntField int64 `db:"int_field"`
OtherValue string
SkipInsert bool `goqux:"skip_insert"`
DbTag string `db:"another_col_name"`
DbTagOmitEmpty string `db:"another_col_name_omit,omitempty"`
}
func TestBuildInsert(t *testing.T) {
testTables := []struct {
name string
values []any
opts []goqux.InsertOption
expectedQuery string
expectedArgs []interface{}
expectedError error
}{
{
name: "simple_insert",
values: []any{insertModel{IntField: 5, DbTagOmitEmpty: "test"}},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "another_col_name_omit", "int_field", "other_value") VALUES ($1, $2, $3, $4)`,
expectedArgs: []interface{}{"", "test", int64(5), ""},
expectedError: nil,
},
{
name: "simple_insert_ompitempty",
values: []any{insertModel{IntField: 5}},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ($1, $2, $3)`,
expectedArgs: []interface{}{"", int64(5), ""},
expectedError: nil,
},
{
name: "insert_multiple_values",
values: []any{
insertModel{IntField: 5},
insertModel{IntField: 6},
},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ($1, $2, $3), ($4, $5, $6)`,
expectedArgs: []interface{}{"", int64(5), "", "", int64(6), ""},
expectedError: nil,
},
{
name: "insert_with_returning_all",
values: []any{
insertModel{IntField: 5},
},
opts: []goqux.InsertOption{goqux.WithInsertReturningAll()},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ($1, $2, $3) RETURNING *`,
expectedArgs: []interface{}{"", int64(5), ""},
},
{
name: "insert_with_insert_not_prepared",
values: []any{
insertModel{IntField: 5},
insertModel{IntField: 6},
},
opts: []goqux.InsertOption{goqux.WithInsertNotPrepared()},
expectedQuery: `INSERT INTO "insert_models" ("another_col_name", "int_field", "other_value") VALUES ('', 5, ''), ('', 6, '')`,
expectedArgs: []interface{}{},
},
}
for _, tt := range testTables {
t.Run(tt.name, func(t *testing.T) {
query, args, err := goqux.BuildInsert("insert_models", tt.values, tt.opts...)
if tt.expectedError != nil {
assert.ErrorIs(t, tt.expectedError, err)
}
assert.Equal(t, tt.expectedQuery, query)
assert.Equal(t, tt.expectedArgs, args)
})
}
}