-
Notifications
You must be signed in to change notification settings - Fork 57
/
sql_test.go
200 lines (176 loc) · 4.77 KB
/
sql_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
//go:build example
// +build example
package example
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSQLExtras(t *testing.T) {
assert.Equal(t, "ProjectStatus(22)", ProjectStatus(22).String(), "String value is not correct")
_, err := ParseProjectStatus(`NotAStatus`)
assert.Error(t, err, "Should have had an error parsing a non status")
var (
intVal int = 3
strVal string = "completed"
strIntVal string = "2"
nullInt *int
nullInt64 *int64
nullFloat64 *float64
nullUint *uint
nullUint64 *uint64
nullString *string
nullProjectStatus *ProjectStatus
)
tests := map[string]struct {
input interface{}
result NullProjectStatus
}{
"nil": {},
"val": {
input: ProjectStatusRejected,
result: NullProjectStatus{
ProjectStatus: ProjectStatusRejected,
Valid: true,
},
},
"ptr": {
input: ProjectStatusCompleted.Ptr(),
result: NullProjectStatus{
ProjectStatus: ProjectStatusCompleted,
Valid: true,
},
},
"string": {
input: strVal,
result: NullProjectStatus{
ProjectStatus: ProjectStatusCompleted,
Valid: true,
},
},
"*string": {
input: &strVal,
result: NullProjectStatus{
ProjectStatus: ProjectStatusCompleted,
Valid: true,
},
},
"*string as int": {
input: &strIntVal,
result: NullProjectStatus{
ProjectStatus: ProjectStatusCompleted,
Valid: true,
},
},
"invalid string": {
input: "random value",
},
"[]byte": {
input: []byte(ProjectStatusInWork.String()),
result: NullProjectStatus{
ProjectStatus: ProjectStatusInWork,
Valid: true,
},
},
"int": {
input: intVal,
result: NullProjectStatus{
ProjectStatus: ProjectStatusRejected,
Valid: true,
},
},
"*int": {
input: &intVal,
result: NullProjectStatus{
ProjectStatus: ProjectStatusRejected,
Valid: true,
},
},
"nullInt": {
input: nullInt,
},
"nullInt64": {
input: nullInt64,
},
"nullUint": {
input: nullUint,
},
"nullUint64": {
input: nullUint64,
},
"nullFloat64": {
input: nullFloat64,
},
"nullString": {
input: nullString,
},
"nullProjectStatus": {
input: nullProjectStatus,
},
"int as []byte": {
input: []byte("1"),
result: NullProjectStatus{
ProjectStatus: ProjectStatusInWork,
Valid: true,
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
status := NewNullProjectStatus(tc.input)
tc.result.Set = true
assert.Equal(t, tc.result, status)
})
}
}
type SQLMarshalType struct {
Status NullProjectStatus `json:"status"`
StatusStr NullProjectStatusStr `json:"status_str"`
Status2 *ProjectStatus `json:"status2,omitempty"`
}
func TestSQLMarshal(t *testing.T) {
var val SQLMarshalType
var val2 SQLMarshalType
result, err := json.Marshal(val)
require.NoError(t, err)
assert.Equal(t, `{"status":null,"status_str":null}`, string(result))
require.NoError(t, json.Unmarshal([]byte(`{}`), &val2))
assert.Equal(t, val, val2)
require.NoError(t, json.Unmarshal(result, &val2))
val.Status.Set = true
val.StatusStr.Set = true
assert.Equal(t, val, val2)
val.Status = NewNullProjectStatus(1)
val.StatusStr = NewNullProjectStatusStr(2)
result, err = json.Marshal(val)
require.NoError(t, err)
assert.Equal(t, `{"status":"inWork","status_str":"completed"}`, string(result))
require.NoError(t, json.Unmarshal(result, &val2))
assert.Equal(t, val, val2)
require.NoError(t, json.Unmarshal([]byte(`{"status":"inWork"}`), &val2))
assert.Equal(t, val, val2)
require.NoError(t, json.Unmarshal([]byte(`{"status":"2"}`), &val2))
val.Status = NewNullProjectStatus(2)
assert.Equal(t, val, val2)
require.NoError(t, json.Unmarshal([]byte(`{"status":3}`), &val2))
val.Status = NewNullProjectStatus(3)
assert.Equal(t, val, val2)
require.NoError(t, json.Unmarshal([]byte(`{"status":null}`), &val2))
val.Status = NullProjectStatus{Set: true}
assert.Equal(t, val, val2)
val2 = SQLMarshalType{} // reset it so that the `set` value is false.
require.NoError(t, json.Unmarshal([]byte(`{"status2":"rejected"}`), &val2))
val.Status = NullProjectStatus{}
val.StatusStr = NullProjectStatusStr{}
val.Status2 = ProjectStatusRejected.Ptr()
assert.Equal(t, val, val2)
require.Error(t, json.Unmarshal([]byte(`{"status2":"xyz"}`), &val2))
val2 = SQLMarshalType{} // reset it so that the `set` value is false.
require.NoError(t, json.Unmarshal([]byte(`{"status_str":"rejected"}`), &val2))
val.Status = NullProjectStatus{}
val.StatusStr = NewNullProjectStatusStr(3)
val.Status2 = nil
assert.Equal(t, val, val2)
require.Error(t, json.Unmarshal([]byte(`{"status2":"xyz"}`), &val2))
}