forked from mrz1836/go-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
272 lines (224 loc) · 6.4 KB
/
utils_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
package datastore
import (
"database/sql"
"testing"
"time"
customtypes "github.com/mrz1836/go-datastore/custom_types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testTableName = "test_model"
)
type testModel struct {
Field string `json:"field"`
}
// GetModelName will return a model name
func (t *testModel) GetModelName() string {
return testModelName
}
// GetModelTableName will return a table name
func (t *testModel) GetModelTableName() string {
return testTableName
}
type badModel struct {
Field string `json:"field"`
}
// TestGetModelStringAttribute will test the method GetModelStringAttribute()
func TestGetModelStringAttribute(t *testing.T) {
t.Parallel()
type TestModel struct {
StringField string `json:"string_field"`
ID string `json:"id"`
}
t.Run("valid string attribute", func(t *testing.T) {
m := &TestModel{
StringField: "test",
ID: "12345678",
}
field1 := GetModelStringAttribute(m, "StringField")
id := GetModelStringAttribute(m, sqlIDFieldProper)
assert.Equal(t, "test", *field1)
assert.Equal(t, "12345678", *id)
})
t.Run("nil input", func(t *testing.T) {
id := GetModelStringAttribute(nil, sqlIDFieldProper)
assert.Nil(t, id)
})
t.Run("invalid type", func(t *testing.T) {
id := GetModelStringAttribute("invalid-type", sqlIDFieldProper)
assert.Nil(t, id)
})
}
// TestGetModelBoolAttribute will test the method GetModelBoolAttribute()
func TestGetModelBoolAttribute(t *testing.T) {
t.Parallel()
type TestModel struct {
BoolField bool `json:"bool_field"`
ID string `json:"id"`
}
t.Run("valid bool attribute", func(t *testing.T) {
m := &TestModel{
BoolField: true,
}
field1 := GetModelBoolAttribute(m, "BoolField")
assert.True(t, *field1)
})
t.Run("nil input", func(t *testing.T) {
val := GetModelBoolAttribute(nil, "BoolField")
assert.Nil(t, val)
})
t.Run("invalid type", func(t *testing.T) {
assert.Panics(t, func() {
val := GetModelBoolAttribute("invalid-type", "BoolField")
assert.Nil(t, val)
})
})
}
// TestGetModelUnset will test the method GetModelUnset()
func TestGetModelUnset(t *testing.T) {
t.Parallel()
type TestModel struct {
NullableTime customtypes.NullTime `json:"nullable_time" bson:"nullable_time"`
NullableString customtypes.NullString `json:"nullable_string" bson:"nullable_string"`
Internal string `json:"-" bson:"-"`
}
t.Run("basic test", func(t *testing.T) {
ty := make(map[string]bool)
m := &TestModel{
NullableTime: customtypes.NullTime{NullTime: sql.NullTime{
Time: time.Time{},
Valid: false,
}},
NullableString: customtypes.NullString{NullString: sql.NullString{
String: "",
Valid: false,
}},
Internal: "test",
}
un := GetModelUnset(m)
assert.IsType(t, ty, un)
assert.True(t, un["nullable_time"])
assert.True(t, un["nullable_string"])
assert.False(t, un["internal"])
})
}
// TestIsModelSlice will test the method IsModelSlice()
func TestIsModelSlice(t *testing.T) {
t.Parallel()
t.Run("valid slices", func(t *testing.T) {
s := []string{"test"}
assert.True(t, IsModelSlice(s))
i := []int{1}
assert.True(t, IsModelSlice(i))
in := []interface{}{"test"}
assert.True(t, IsModelSlice(in))
ptr := []string{"test"}
assert.True(t, IsModelSlice(&ptr))
})
t.Run("not a slice", func(t *testing.T) {
s := "string"
assert.False(t, IsModelSlice(s))
i := 1
assert.False(t, IsModelSlice(i))
})
}
// TestGetModelName will test the method GetModelName()
func TestGetModelName(t *testing.T) {
t.Parallel()
t.Run("model is nil", func(t *testing.T) {
name := GetModelName(nil)
require.Nil(t, name)
})
t.Run("model is set - pointer", func(t *testing.T) {
tm := &testModel{Field: testModelName}
name := GetModelName(tm)
assert.Equal(t, testModelName, *name)
})
t.Run("model is set - value", func(t *testing.T) {
tm := testModel{Field: testModelName}
name := GetModelName(tm)
assert.Equal(t, testModelName, *name)
})
t.Run("models are set - value", func(t *testing.T) {
tm := &[]testModel{{Field: testModelName}}
name := GetModelName(tm)
assert.Equal(t, testModelName, *name)
})
t.Run("model does not have method - pointer", func(t *testing.T) {
tm := &badModel{}
name := GetModelName(tm)
assert.Nil(t, name)
})
t.Run("model does not have method - value", func(t *testing.T) {
tm := badModel{}
name := GetModelName(tm)
assert.Nil(t, name)
})
}
// TestGetModelTableName will test the method GetModelTableName()
func TestGetModelTableName(t *testing.T) {
t.Parallel()
t.Run("model is nil", func(t *testing.T) {
name := GetModelTableName(nil)
require.Nil(t, name)
})
t.Run("model is set - pointer", func(t *testing.T) {
tm := &testModel{Field: testTableName}
name := GetModelTableName(tm)
assert.Equal(t, testTableName, *name)
})
t.Run("model is set - value", func(t *testing.T) {
tm := testModel{Field: testTableName}
name := GetModelTableName(tm)
assert.Equal(t, testTableName, *name)
})
t.Run("models are set - value", func(t *testing.T) {
tm := &[]testModel{{Field: testModelName}}
name := GetModelTableName(tm)
assert.Equal(t, testModelName, *name)
})
t.Run("model does not have method - pointer", func(t *testing.T) {
tm := &badModel{}
name := GetModelTableName(tm)
assert.Nil(t, name)
})
t.Run("model does not have method - value", func(t *testing.T) {
tm := badModel{}
name := GetModelTableName(tm)
assert.Nil(t, name)
})
}
// TestGetModelType will test the method GetModelType()
func TestGetModelType(t *testing.T) {
t.Parallel()
type modelExample struct {
Field string `json:"field"`
}
t.Run("panic - nil model", func(t *testing.T) {
assert.Panics(t, func() {
modelType := GetModelType(nil)
require.NotNil(t, modelType)
})
})
t.Run("default type", func(t *testing.T) {
m := new(modelExample)
modelType := GetModelType(m)
assert.NotNil(t, modelType)
})
}
// TestStringInSlice will test the method StringInSlice()
func TestStringInSlice(t *testing.T) {
t.Parallel()
t.Run("nil / empty", func(t *testing.T) {
assert.False(t, StringInSlice("test", []string{}))
assert.False(t, StringInSlice("test", nil))
})
t.Run("slices", func(t *testing.T) {
slice := []string{"test", "test1", "test2"}
assert.True(t, StringInSlice("test", slice))
assert.True(t, StringInSlice("test1", slice))
assert.True(t, StringInSlice("test2", slice))
assert.False(t, StringInSlice("test3", slice))
})
}