-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes-int64_test.go
207 lines (202 loc) · 5.55 KB
/
types-int64_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
package sqljson_test
import (
"database/sql"
"reflect"
"testing"
"github.com/rhaseven7h/sqljson"
. "github.com/smartystreets/goconvey/convey"
)
func TestNullInt64ValidateValuer(t *testing.T) {
Convey("Given a non-null sqljson.NullInt64 non-zero value", t, func() {
ov := sqljson.NullInt64{
NullInt64: sql.NullInt64{
Int64: 100,
Valid: true,
},
}
ivIn := reflect.ValueOf(ov)
Convey("When evaluated", func() {
ivOut := sqljson.NullInt64ValidateValuer(ivIn)
Convey("Then we should get the value", func() {
nv, ok := ivOut.(int64)
So(ok, ShouldBeTrue)
So(nv, ShouldEqual, 100)
})
})
})
Convey("Given a non-null sqljson.NullInt64 zero value", t, func() {
ov := sqljson.NullInt64{
NullInt64: sql.NullInt64{
Int64: 0,
Valid: true,
},
}
ivIn := reflect.ValueOf(ov)
Convey("When evaluated", func() {
ivOut := sqljson.NullInt64ValidateValuer(ivIn)
Convey("Then we should get the value", func() {
nv, ok := ivOut.(int64)
So(ok, ShouldBeTrue)
So(nv, ShouldEqual, 0)
})
})
})
Convey("Given a null sqljson.NullInt64 value", t, func() {
ov := sqljson.NullInt64{
NullInt64: sql.NullInt64{
Int64: 0,
Valid: false,
},
}
ivIn := reflect.ValueOf(ov)
Convey("When evaluated", func() {
ivOut := sqljson.NullInt64ValidateValuer(ivIn)
Convey("Then we should get the value", func() {
So(ivOut, ShouldBeNil)
})
})
})
}
func TestInt64PtrOrNil(t *testing.T) {
Convey("Given a sqljson.NullInt64 non-null non-zero value", t, func() {
in := sqljson.NullInt64{
NullInt64: sql.NullInt64{
Int64: 100,
Valid: true,
},
}
Convey("When I call Int64PtrOrNil", func() {
bptr := in.Int64PtrOrNil()
Convey("Then I should get a true int64 value pointer", func() {
So(bptr, ShouldNotBeNil)
So(*bptr, ShouldEqual, 100)
})
})
})
Convey("Given a sqljson.NullInt64 non-null non-zero value", t, func() {
in := sqljson.NullInt64{
NullInt64: sql.NullInt64{
Int64: 100,
Valid: true,
},
}
Convey("When I call Int64PtrOrNil", func() {
bptr := in.Int64PtrOrNil()
Convey("Then I should get a false int64 value pointer", func() {
So(bptr, ShouldNotBeNil)
So(*bptr, ShouldEqual, 100)
})
})
})
Convey("Given a sqljson.NullInt64 null value", t, func() {
in := sqljson.NullInt64{
NullInt64: sql.NullInt64{
Int64: 0,
Valid: false,
},
}
Convey("When I call Int64PtrOrNil", func() {
bptr := in.Int64PtrOrNil()
Convey("Then I should get a nil pointer", func() {
So(bptr, ShouldBeNil)
})
})
})
}
func TestInt64MarshalJSON(t *testing.T) {
Convey("Given a non-null sqljson.NullInt64", t, func() {
in := sqljson.NullInt64{
NullInt64: sql.NullInt64{
Int64: 100,
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, "100")
})
})
})
Convey("Given a null sqljson.NullInt64", t, func() {
in := sqljson.NullInt64{
NullInt64: sql.NullInt64{
Int64: 0,
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 TestInt64UnmarshalJSON(t *testing.T) {
Convey("Given a non-zero JSON string", t, func() {
obj := sqljson.NullInt64{}
jsonIn := `123`
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.Int64, ShouldEqual, 123)
})
})
})
Convey("Given a zero JSON string", t, func() {
obj := sqljson.NullInt64{}
jsonIn := `0`
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.Int64, ShouldEqual, 0)
})
})
})
Convey("Given a null JSON string", t, func() {
obj := sqljson.NullInt64{}
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{"a bool true", `true`, "cannot unmarshal bool into Go value of type int64"},
errorCaseDef{"a bool false", `false`, "cannot unmarshal bool into Go value of type int64"},
errorCaseDef{"a float", `123.45`, "cannot unmarshal number 123.45 into Go value of type int64"},
errorCaseDef{"a string", `"Hello!"`, "cannot unmarshal string into Go value of type int64"},
errorCaseDef{"an object", `{"status":"ok"}`, "cannot unmarshal object into Go value of type int64"},
errorCaseDef{"an array", `[1,2,3]`, "cannot unmarshal array into Go value of type int64"},
}
for _, errorCase := range errorCases {
Convey("Given "+errorCase.Label+" JSON string", t, func() {
obj := sqljson.NullInt64{}
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)
})
})
})
}
}