-
Notifications
You must be signed in to change notification settings - Fork 7
/
struct_test.go
222 lines (214 loc) · 5.48 KB
/
struct_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
package goqux
import (
"testing"
"time"
"github.com/doug-martin/goqu/v9"
"github.com/doug-martin/goqu/v9/exp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEncodeValues(t *testing.T) {
tableTests := []struct {
name string
model interface{}
values map[string]SQLValuer
skipFlag string
skipZeroValues bool
}{
{
name: "encode_insert",
model: struct {
IntField int
unexported bool
FieldToSkip int `goqux:"skip_insert"`
}{IntField: 1},
values: map[string]SQLValuer{"int_field": {1}},
skipFlag: skipInsert,
skipZeroValues: false,
},
{
name: "encode_array",
model: struct {
IntField []int
}{IntField: []int{1, 2}},
values: map[string]SQLValuer{"int_field": {[]int{1, 2}}},
skipFlag: skipInsert,
skipZeroValues: false,
},
{
name: "encode_array_with_zero_values",
model: struct {
IntField []int
}{IntField: []int{1, 0}},
values: map[string]SQLValuer{"int_field": {[]int{1, 0}}},
skipFlag: skipInsert,
skipZeroValues: false,
},
{
name: "encode_zero_values",
model: struct {
IntField int
FloatField float64
unexported bool
FieldToSkip int `goqux:"skip_insert"`
}{IntField: 0},
values: map[string]SQLValuer{},
skipFlag: skipInsert,
skipZeroValues: true,
},
{
name: "encode_map_values",
model: struct {
MapValue map[string]any `goqux:"skip_compare"`
}{MapValue: map[string]any{"type": "map"}},
values: map[string]SQLValuer{"map_value": {map[string]any{"type": "map"}}},
skipFlag: skipInsert,
skipZeroValues: false,
},
{
name: "encode_empty_struct",
model: struct {
IntField int
FloatField float64
unexported bool
EmptyStruct struct {
Type string
Value string
}
}{},
values: map[string]SQLValuer{},
skipFlag: skipInsert,
skipZeroValues: true,
},
{
name: "encode_omitempty_nonempty",
model: struct {
IntField int `db:"int_field,omitempty"`
}{IntField: 3},
values: map[string]SQLValuer{"int_field": {3}},
skipFlag: skipInsert,
skipZeroValues: false,
},
{
name: "encode_omitempty_empty",
model: struct {
IntField int `db:"int_field,omitempty"`
}{IntField: 0},
values: map[string]SQLValuer{},
skipFlag: skipInsert,
skipZeroValues: false,
},
}
for _, tt := range tableTests {
t.Run(tt.name, func(t *testing.T) {
values := encodeValues(tt.model, tt.skipFlag, tt.skipZeroValues)
assert.Equal(t, tt.values, values)
})
}
}
func TestEncodeTimeValue(t *testing.T) {
values := encodeValues(struct {
TimeField *time.Time `goqux:"now"`
unexported bool
FieldToSkip int `goqux:"skip_insert"`
}{
FieldToSkip: 5,
}, skipInsert, true)
if tf, ok := values["time_field"]; ok {
require.NotNil(t, tf)
return
}
}
func TestGetColumnsFromStruct(t *testing.T) {
tableTests := []struct {
name string
model interface{}
expected []exp.IdentifierExpression
}{
{
name: "get_columns_from_struct",
model: struct {
IntField int
unexported bool
FieldToSkip int `goqux:"skip_select"`
DbOsField int `db:"db_field"`
}{},
expected: []exp.IdentifierExpression{goqu.T("table").Col("int_field"), goqu.T("table").Col("db_field")},
},
{
name: "get_columns_from_struct_with_omitempty",
model: struct {
IntField int `db:"int_field,omitempty"` // should not be skipped
StringField string
}{},
expected: []exp.IdentifierExpression{goqu.T("table").Col("int_field"), goqu.T("table").Col("string_field")},
},
}
for _, tt := range tableTests {
t.Run(tt.name, func(t *testing.T) {
columns := getColumnsFromStruct(goqu.T("table"), tt.model, skipSelect)
assert.Equal(t, tt.expected, columns)
})
}
}
type table1 struct {
IntField int
}
type table2 struct {
IntField int
StringField string `db:"cool_field"`
IgnoreField string `goqux:"skip_select"`
privateField string
}
func Test_getSelectionFieldsFromSelectionStruct(t *testing.T) {
tableTests := []struct {
name string
model interface{}
expected []exp.AliasedExpression
}{
{
name: "get_selection_fields_from_struct",
model: struct {
Table1 table1
Table2 table2 `db:"table_2"`
}{},
expected: []exp.AliasedExpression{
goqu.T("table_1").Col("int_field").As(goqu.C("table_1.int_field")),
goqu.T("table_2").Col("int_field").As(goqu.C("table_2.int_field")),
goqu.T("table_2").Col("cool_field").As(goqu.C("table_2.cool_field"))},
},
{
name: "get_selection_fields_from_struct_same_table",
model: struct {
Table1 table1
Table1Alias table1 `db:"table_alias"`
}{},
expected: []exp.AliasedExpression{
goqu.T("table_1").Col("int_field").As(goqu.C("table_1.int_field")),
goqu.T("table_alias").Col("int_field").As(goqu.C("table_alias.int_field")),
},
},
{
name: "get_selection_fields_from_struct_unexported_column",
model: struct {
t1Private table1
}{},
expected: []exp.AliasedExpression{},
},
{
name: "get_selection_fields_from_non_top_level_struct",
model: struct {
IntField int
FieldToSkip int `goqux:"skip_select"`
DbOsField int `db:"db_field"`
}{},
expected: []exp.AliasedExpression{},
},
}
for _, tt := range tableTests {
t.Run(tt.name, func(t *testing.T) {
columns := getSelectionFieldsFromSelectionStruct(tt.model)
assert.Equal(t, tt.expected, columns)
})
}
}