Debian Codegraph @

sql_test.go

raw symbols SCIP
1 // Copyright (C) 2013-2018 by Maxim Bublis <[email protected]>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 package uuid
23
24 import (
25 "encoding/json"
26 "fmt"
27 "testing"
28 )
29
30 func TestSQL(t *testing.T) {
31 t.Run("Value", testSQLValue)
32 t.Run("Scan", func(t *testing.T) {
33 t.Run("Binary", testSQLScanBinary)
34 t.Run("String", testSQLScanString)
35 t.Run("Text", testSQLScanText)
36 t.Run("Unsupported", testSQLScanUnsupported)
37 t.Run("Nil", testSQLScanNil)
38 })
39 }
40
41 func testSQLValue(t *testing.T) {
42 v, err := codecTestUUID.Value()
43 if err != nil {
44 t.Fatal(err)
45 }
46 got, ok := v.(string)
47 if !ok {
48 t.Fatalf("Value() returned %T, want string", v)
49 }
50 if want := codecTestUUID.String(); got != want {
51 t.Errorf("Value() == %q, want %q", got, want)
52 }
53 }
54
55 func testSQLScanBinary(t *testing.T) {
56 got := UUID{}
57 err := got.Scan(codecTestData)
58 if err != nil {
59 t.Fatal(err)
60 }
61 if got != codecTestUUID {
62 t.Errorf("Scan(%x): got %v, want %v", codecTestData, got, codecTestUUID)
63 }
64 }
65
66 func testSQLScanString(t *testing.T) {
67 s := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
68 got := UUID{}
69 err := got.Scan(s)
70 if err != nil {
71 t.Fatal(err)
72 }
73 if got != codecTestUUID {
74 t.Errorf("Scan(%q): got %v, want %v", s, got, codecTestUUID)
75 }
76 }
77
78 func testSQLScanText(t *testing.T) {
79 text := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
80 got := UUID{}
81 err := got.Scan(text)
82 if err != nil {
83 t.Fatal(err)
84 }
85 if got != codecTestUUID {
86 t.Errorf("Scan(%q): got %v, want %v", text, got, codecTestUUID)
87 }
88 }
89
90 func testSQLScanUnsupported(t *testing.T) {
91 unsupported := []any{
92 true,
93 42,
94 }
95 for _, v := range unsupported {
96 got := UUID{}
97 err := got.Scan(v)
98 if err == nil {
99 t.Errorf("Scan(%T) succeeded, got %v", v, got)
100 }
101 }
102 }
103
104 func testSQLScanNil(t *testing.T) {
105 got := UUID{}
106 err := got.Scan(nil)
107 if err == nil {
108 t.Errorf("Scan(nil) succeeded, got %v", got)
109 }
110 }
111
112 func TestNullUUID(t *testing.T) {
113 t.Run("Value", func(t *testing.T) {
114 t.Run("Nil", testNullUUIDValueNil)
115 t.Run("Valid", testNullUUIDValueValid)
116 })
117
118 t.Run("Scan", func(t *testing.T) {
119 t.Run("Nil", testNullUUIDScanNil)
120 t.Run("Valid", testNullUUIDScanValid)
121 t.Run("UUID", testNullUUIDScanUUID)
122 })
123
124 t.Run("MarshalJSON", func(t *testing.T) {
125 t.Run("Nil", testNullUUIDMarshalJSONNil)
126 t.Run("Null", testNullUUIDMarshalJSONNull)
127 t.Run("Valid", testNullUUIDMarshalJSONValid)
128 })
129
130 t.Run("UnmarshalJSON", func(t *testing.T) {
131 t.Run("Nil", testNullUUIDUnmarshalJSONNil)
132 t.Run("Null", testNullUUIDUnmarshalJSONNull)
133 t.Run("Valid", testNullUUIDUnmarshalJSONValid)
134 t.Run("Malformed", testNullUUIDUnmarshalJSONMalformed)
135 })
136 }
137
138 func testNullUUIDValueNil(t *testing.T) {
139 nu := NullUUID{}
140 got, err := nu.Value()
141 if got != nil {
142 t.Errorf("null NullUUID.Value returned non-nil driver.Value")
143 }
144 if err != nil {
145 t.Errorf("null NullUUID.Value returned non-nil error")
146 }
147 }
148
149 func testNullUUIDValueValid(t *testing.T) {
150 nu := NullUUID{
151 Valid: true,
152 UUID: codecTestUUID,
153 }
154 got, err := nu.Value()
155 if err != nil {
156 t.Fatal(err)
157 }
158 s, ok := got.(string)
159 if !ok {
160 t.Errorf("Value() returned %T, want string", got)
161 }
162 want := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
163 if s != want {
164 t.Errorf("%v.Value() == %s, want %s", nu, s, want)
165 }
166 }
167
168 func testNullUUIDScanNil(t *testing.T) {
169 u := NullUUID{}
170 err := u.Scan(nil)
171 if err != nil {
172 t.Fatal(err)
173 }
174 if u.Valid {
175 t.Error("NullUUID is valid after Scan(nil)")
176 }
177 if u.UUID != Nil {
178 t.Errorf("NullUUID.UUID is %v after Scan(nil) want Nil", u.UUID)
179 }
180 }
181
182 func testNullUUIDScanValid(t *testing.T) {
183 s := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
184 u := NullUUID{}
185 err := u.Scan(s)
186 if err != nil {
187 t.Fatal(err)
188 }
189 if !u.Valid {
190 t.Errorf("Valid == false after Scan(%q)", s)
191 }
192 if u.UUID != codecTestUUID {
193 t.Errorf("UUID == %v after Scan(%q), want %v", u.UUID, s, codecTestUUID)
194 }
195 }
196
197 func testNullUUIDScanUUID(t *testing.T) {
198 u := NullUUID{}
199 err := u.Scan(codecTestUUID)
200 if err != nil {
201 t.Fatal(err)
202 }
203 if !u.Valid {
204 t.Errorf("Valid == false after scan(%v)", codecTestUUID)
205 }
206 if u.UUID != codecTestUUID {
207 t.Errorf("UUID == %v after Scan(%v), want %v", u.UUID, codecTestUUID, codecTestUUID)
208 }
209 }
210
211 func testNullUUIDMarshalJSONNil(t *testing.T) {
212 u := NullUUID{Valid: true}
213
214 data, err := u.MarshalJSON()
215 if err != nil {
216 t.Fatalf("(%#v).MarshalJSON err want: <nil>, got: %v", u, err)
217 }
218
219 dataStr := string(data)
220
221 if dataStr != fmt.Sprintf("%q", Nil) {
222 t.Fatalf("(%#v).MarshalJSON value want: %s, got: %s", u, Nil, dataStr)
223 }
224 }
225
226 func testNullUUIDMarshalJSONValid(t *testing.T) {
227 u := NullUUID{
228 Valid: true,
229 UUID: codecTestUUID,
230 }
231
232 data, err := u.MarshalJSON()
233 if err != nil {
234 t.Fatalf("(%#v).MarshalJSON err want: <nil>, got: %v", u, err)
235 }
236
237 dataStr := string(data)
238
239 if dataStr != fmt.Sprintf("%q", codecTestUUID) {
240 t.Fatalf("(%#v).MarshalJSON value want: %s, got: %s", u, codecTestUUID, dataStr)
241 }
242 }
243
244 func testNullUUIDMarshalJSONNull(t *testing.T) {
245 u := NullUUID{}
246
247 data, err := u.MarshalJSON()
248 if err != nil {
249 t.Fatalf("(%#v).MarshalJSON err want: <nil>, got: %v", u, err)
250 }
251
252 dataStr := string(data)
253
254 if dataStr != "null" {
255 t.Fatalf("(%#v).MarshalJSON value want: %s, got: %s", u, "null", dataStr)
256 }
257 }
258
259 func testNullUUIDUnmarshalJSONNil(t *testing.T) {
260 var u NullUUID
261
262 data := []byte(`"00000000-0000-0000-0000-000000000000"`)
263
264 if err := json.Unmarshal(data, &u); err != nil {
265 t.Fatalf("json.Unmarshal err = %v, want <nil>", err)
266 }
267
268 if !u.Valid {
269 t.Fatalf("u.Valid = false, want true")
270 }
271
272 if u.UUID != Nil {
273 t.Fatalf("u.UUID = %v, want %v", u.UUID, Nil)
274 }
275 }
276
277 func testNullUUIDUnmarshalJSONNull(t *testing.T) {
278 var u NullUUID
279
280 data := []byte(`null`)
281
282 if err := json.Unmarshal(data, &u); err != nil {
283 t.Fatalf("json.Unmarshal err = %v, want <nil>", err)
284 }
285
286 if u.Valid {
287 t.Fatalf("u.Valid = true, want false")
288 }
289
290 if u.UUID != Nil {
291 t.Fatalf("u.UUID = %v, want %v", u.UUID, Nil)
292 }
293 }
294
295 func testNullUUIDUnmarshalJSONValid(t *testing.T) {
296 var u NullUUID
297
298 data := []byte(`"6ba7b810-9dad-11d1-80b4-00c04fd430c8"`)
299
300 if err := json.Unmarshal(data, &u); err != nil {
301 t.Fatalf("json.Unmarshal err = %v, want <nil>", err)
302 }
303
304 if !u.Valid {
305 t.Fatalf("u.Valid = false, want true")
306 }
307
308 if u.UUID != codecTestUUID {
309 t.Fatalf("u.UUID = %v, want %v", u.UUID, Nil)
310 }
311 }
312
313 func testNullUUIDUnmarshalJSONMalformed(t *testing.T) {
314 var u NullUUID
315
316 data := []byte(`257`)
317
318 if err := json.Unmarshal(data, &u); err == nil {
319 t.Fatal("json.Unmarshal err = <nil>, want error")
320 }
321 }
322
323 func BenchmarkNullMarshalJSON(b *testing.B) {
324 b.Run("Valid", func(b *testing.B) {
325 u, err := FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
326 if err != nil {
327 b.Fatal(err)
328 }
329 n := NullUUID{UUID: u, Valid: true}
330 for i := 0; i < b.N; i++ {
331 _, _ = n.MarshalJSON()
332 }
333 })
334 b.Run("Invalid", func(b *testing.B) {
335 n := NullUUID{Valid: false}
336 for i := 0; i < b.N; i++ {
337 _, _ = n.MarshalJSON()
338 }
339 })
340 }
341
342 func BenchmarkNullUnmarshalJSON(b *testing.B) {
343 baseUUID, err := FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
344 if err != nil {
345 b.Fatal(err)
346 }
347 data, err := json.Marshal(&baseUUID)
348 if err != nil {
349 b.Fatal(err)
350 }
351
352 b.Run("Valid", func(b *testing.B) {
353 var u NullUUID
354 for i := 0; i < b.N; i++ {
355 _ = u.UnmarshalJSON(data)
356 }
357 })
358 b.Run("Invalid", func(b *testing.B) {
359 invalid := []byte("null")
360 var n NullUUID
361 for i := 0; i < b.N; i++ {
362 _ = n.UnmarshalJSON(invalid)
363 }
364 })
365 }