-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtype_test.go
121 lines (104 loc) · 3.35 KB
/
dtype_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
package dtype
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDefaultStringWithNullableReturnRawString(t *testing.T) {
input := "test str"
want := "test str"
got, err := DefaultStringWithNullable(input)
assert.NotNil(t, err)
assert.Equal(t, want, got)
}
func TestDefaultStringWithNullableReturnNull(t *testing.T) {
input := ""
got, err := DefaultStringWithNullable(input)
assert.Nil(t, err)
assert.Empty(t, got)
}
func TestDefaultIntWithNullableReturnRawInt(t *testing.T) {
input := 214748364700000
want := 214748364700000
got, err := DefaultIntWithNullable(input)
assert.NotNil(t, err)
assert.Equal(t, want, got)
}
func TestDefaultIntWithNullableReturnNull(t *testing.T) {
input := 0
got, err := DefaultIntWithNullable(input)
assert.Nil(t, err)
assert.Empty(t, got)
}
func TestDefaultFloat64WithNullableReturnRawInt(t *testing.T) {
input := 22111100.202424
want := 22111100.202424
got, err := DefaultFloat64WithNullable(input)
assert.NotNil(t, err)
assert.Equal(t, want, got)
}
func TestDefaultFloat64WithNullableReturnNull(t *testing.T) {
input := 0.0
got, err := DefaultFloat64WithNullable(input)
assert.Nil(t, err)
assert.Empty(t, got)
}
func TestDefaultArrayStringWithNullableReturnRawArrayString(t *testing.T) {
input := []string{"a", "b", "c"}
want := []string{"a", "b", "c"}
got, err := DefaultArrayStringWithNullable(input)
assert.NotNil(t, err)
assert.Equal(t, want, got)
}
func TestDefaultArrayStringWithNullableReturnNull(t *testing.T) {
input := []string{}
got, err := DefaultArrayStringWithNullable(input)
assert.Nil(t, err)
assert.Empty(t, got)
}
func TestDefaultArrayIntWithNullableReturnRawArrayInt(t *testing.T) {
input := []int{1, 2, 3, 4}
want := []int{1, 2, 3, 4}
got, err := DefaultArrayIntWithNullable(input)
assert.NotNil(t, err)
assert.Equal(t, want, got)
}
func TestDefaultArrayIntWithNullableReturnNull(t *testing.T) {
input := []int{}
got, err := DefaultArrayIntWithNullable(input)
assert.Nil(t, err)
assert.Empty(t, got)
}
func TestDefaultBooleanWithNullableReturnRawBoolean(t *testing.T) {
input := bool(true)
want := bool(true)
got, err := DefaultBooleanWithNullable(input)
assert.NotNil(t, err)
assert.Equal(t, want, got)
}
func TestDefaultBooleanWithNullableReturnNull(t *testing.T) {
input := bool(false)
got, err := DefaultBooleanWithNullable(input)
assert.Nil(t, err)
assert.Empty(t, got)
}
func TestDefaultJSONUnstructuredWithNullableReturnRawJSON(t *testing.T) {
inputJSON := `{"name":{"first":"Janet","last":"Prichard"},"age":47, "country": "TH"}`
inputKey := "country"
want := `{"name":{"first":"Janet","last":"Prichard"},"age":47, "country": "TH"}`
got, err := DefaultJSONUnstructuredWithNullable(inputJSON, inputKey)
assert.NotNil(t, err)
assert.Equal(t, want, got)
}
func TestDefaultJSONUnstructuredWithNullableReturnNull(t *testing.T) {
inputJSON := `{"name":{"first":"Janet","last":"Prichard"},"age":47}`
inputKey := "country"
want := `{"name":{"first":"Janet","last":"Prichard"},"age":47, "country": null}`
got, err := DefaultJSONUnstructuredWithNullable(inputJSON, inputKey)
assert.Nil(t, err)
assert.JSONEq(t, want, got)
}
func TestDefaultJSONUnstructuredWithNullableErrorUnmarshal(t *testing.T) {
inputJSON := `{"name":{"first":"Janet","last":"Prichard","age":47}`
inputKey := "country"
assert.Panics(t, func() { DefaultJSONUnstructuredWithNullable(inputJSON, inputKey) })
}