-
Notifications
You must be signed in to change notification settings - Fork 0
/
nullable_json_test.go
50 lines (42 loc) · 1.48 KB
/
nullable_json_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
package gosql
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNullableJSON(t *testing.T) {
t.Run("json", func(t *testing.T) {
js, err := NewNullableJSON[map[string]string](map[string]any{
"name": "Yoda",
"number": "1",
})
if !assert.NoError(t, err) {
return
}
assert.Equal(t, "Yoda", (*js.Data)["name"])
assert.Equal(t, "1", (*js.Data)["number"])
assert.Error(t, js.SetValue(map[string]any{"invalid": 1}))
assert.Panics(t, func() {
MustNullableJSON[map[string]string]([]int{1, 2, 3})
})
})
t.Run("json_empty_parse", func(t *testing.T) {
var js NullableJSON[map[string]string]
assert.NoError(t, js.Scan([]byte{}), "scan empty byte")
assert.NoError(t, js.Scan([]byte(nil)), "scan nil byte")
assert.NoError(t, js.Scan([]byte(" ")), "scan empty string")
})
t.Run("data_or", func(t *testing.T) {
js, _ := NewNullableJSON[map[string]string](map[string]any{
"name": "Yoda",
"number": "1",
})
assert.Equal(t, "Yoda", js.DataOr(map[string]string{})["name"])
assert.Equal(t, "1", js.DataOr(map[string]string{})["number"])
assert.Equal(t, "Yoda", js.DataOr(map[string]string{"name": "Luke"})["name"])
assert.Equal(t, "1", js.DataOr(map[string]string{"number": "2"})["number"])
null, _ := NewNullableJSON[map[string]string](nil)
assert.Equal(t, "Luke", null.DataOr(map[string]string{"name": "Luke"})["name"])
assert.Equal(t, "2", null.DataOr(map[string]string{"number": "2"})["number"])
assert.Nil(t, null.DataOr(nil))
})
}