-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes-bool_test.go
206 lines (201 loc) · 5.39 KB
/
types-bool_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
package sqljson_test
import (
"database/sql"
"reflect"
"testing"
"github.com/rhaseven7h/sqljson"
. "github.com/smartystreets/goconvey/convey"
)
func TestNullBoolValidateValuer(t *testing.T) {
Convey("Given a non-null sqljson.NullBool true value", t, func() {
ov := sqljson.NullBool{
NullBool: sql.NullBool{
Bool: true,
Valid: true,
},
}
ivIn := reflect.ValueOf(ov)
Convey("When evaluated", func() {
ivOut := sqljson.NullBoolValidateValuer(ivIn)
Convey("Then we should get the value", func() {
nv, ok := ivOut.(bool)
So(ok, ShouldBeTrue)
So(nv, ShouldBeTrue)
})
})
})
Convey("Given a non-null sqljson.NullBool false value", t, func() {
ov := sqljson.NullBool{
NullBool: sql.NullBool{
Bool: false,
Valid: true,
},
}
ivIn := reflect.ValueOf(ov)
Convey("When evaluated", func() {
ivOut := sqljson.NullBoolValidateValuer(ivIn)
Convey("Then we should get the value", func() {
nv, ok := ivOut.(bool)
So(ok, ShouldBeTrue)
So(nv, ShouldBeFalse)
})
})
})
Convey("Given a null sqljson.NullBool value", t, func() {
ov := sqljson.NullBool{
NullBool: sql.NullBool{
Bool: false,
Valid: false,
},
}
ivIn := reflect.ValueOf(ov)
Convey("When evaluated", func() {
ivOut := sqljson.NullBoolValidateValuer(ivIn)
Convey("Then we should get the value", func() {
So(ivOut, ShouldBeNil)
})
})
})
}
func TestBoolPtrOrNil(t *testing.T) {
Convey("Given a sqljson.NullBool non-null true value", t, func() {
in := sqljson.NullBool{
NullBool: sql.NullBool{
Bool: true,
Valid: true,
},
}
Convey("When I call BoolPtrOrNil", func() {
bptr := in.BoolPtrOrNil()
Convey("Then I should get a true bool value pointer", func() {
So(bptr, ShouldNotBeNil)
So(*bptr, ShouldBeTrue)
})
})
})
Convey("Given a sqljson.NullBool non-null false value", t, func() {
in := sqljson.NullBool{
NullBool: sql.NullBool{
Bool: false,
Valid: true,
},
}
Convey("When I call BoolPtrOrNil", func() {
bptr := in.BoolPtrOrNil()
Convey("Then I should get a false bool value pointer", func() {
So(bptr, ShouldNotBeNil)
So(*bptr, ShouldBeFalse)
})
})
})
Convey("Given a sqljson.NullBool null value", t, func() {
in := sqljson.NullBool{
NullBool: sql.NullBool{
Bool: false,
Valid: false,
},
}
Convey("When I call BoolPtrOrNil", func() {
bptr := in.BoolPtrOrNil()
Convey("Then I should get a nil pointer", func() {
So(bptr, ShouldBeNil)
})
})
})
}
func TestBoolMarshalJSON(t *testing.T) {
Convey("Given a non-null sqljson.NullBool", t, func() {
in := sqljson.NullBool{
NullBool: sql.NullBool{
Bool: true,
Valid: true,
},
}
Convey("When marshalled to JSON", func() {
b, err := in.MarshalJSON()
Convey("Then I should get a valid value", func() {
So(err, ShouldBeNil)
So(string(b), ShouldEqual, "true")
})
})
})
Convey("Given a null sqljson.NullBool", t, func() {
in := sqljson.NullBool{
NullBool: sql.NullBool{
Bool: true,
Valid: false,
},
}
Convey("When marshalled to JSON", func() {
b, err := in.MarshalJSON()
Convey("Then I should get a null value", func() {
So(err, ShouldBeNil)
So(string(b), ShouldEqual, "null")
})
})
})
}
func TestBoolUnmarshalJSON(t *testing.T) {
Convey("Given a true JSON string", t, func() {
obj := sqljson.NullBool{}
jsonIn := `true`
Convey("When unmarshalled", func() {
err := obj.UnmarshalJSON([]byte(jsonIn))
Convey("Then I should get the corresponding value back", func() {
So(err, ShouldBeNil)
So(obj.Valid, ShouldBeTrue)
So(obj.Bool, ShouldBeTrue)
})
})
})
Convey("Given a false JSON string", t, func() {
obj := sqljson.NullBool{}
jsonIn := `false`
Convey("When unmarshalled", func() {
err := obj.UnmarshalJSON([]byte(jsonIn))
Convey("Then I should get the corresponding value back", func() {
So(err, ShouldBeNil)
So(obj.Valid, ShouldBeTrue)
So(obj.Bool, ShouldBeFalse)
})
})
})
Convey("Given a null JSON string", t, func() {
obj := sqljson.NullBool{}
jsonIn := `null`
Convey("When unmarshalled", func() {
err := obj.UnmarshalJSON([]byte(jsonIn))
Convey("Then I should get the corresponding value back", func() {
So(err, ShouldBeNil)
So(obj.Valid, ShouldBeFalse)
})
})
})
type errorCaseDef struct {
Label string
InputJSON string
TestString string
}
errorCases := []errorCaseDef{
errorCaseDef{"an invalid", `invalid value`, "invalid character"},
errorCaseDef{"an empty", ``, "unexpected end of JSON input"},
errorCaseDef{"an integer", `7`, "cannot unmarshal number into Go value of type bool"},
errorCaseDef{"a float", `123.45`, "cannot unmarshal number into Go value of type bool"},
errorCaseDef{"a string", `"Hello!"`, "cannot unmarshal string into Go value of type bool"},
errorCaseDef{"an object", `{"status":"ok"}`, "cannot unmarshal object into Go value of type bool"},
errorCaseDef{"an array", `[1,2,3]`, "cannot unmarshal array into Go value of type bool"},
}
for _, errorCase := range errorCases {
Convey("Given "+errorCase.Label+" JSON string", t, func() {
obj := sqljson.NullBool{}
jsonIn := errorCase.InputJSON
Convey("When unmarshalled", func() {
err := obj.UnmarshalJSON([]byte(jsonIn))
Convey("Then I should get an error", func() {
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, errorCase.TestString)
})
})
})
}
}