-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_raw_message_test.go
168 lines (143 loc) · 4.93 KB
/
json_raw_message_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
package nulls
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
)
// TestNewJSONRawMessage tests NewJSONRawMessage.
func TestNewJSONRawMessage(t *testing.T) {
raw := json.RawMessage("Hello World!")
rm := NewJSONRawMessage(raw)
assert.True(t, rm.Valid, "should be valid")
assert.Equal(t, raw, rm.RawMessage, "should contain correct value")
}
// JSONRawMessageMarshalJSONSuite tests JSONRawMessage.MarshalJSON.
type JSONRawMessageMarshalJSONSuite struct {
suite.Suite
}
func (suite *JSONRawMessageMarshalJSONSuite) TestNull() {
rm := NewJSONRawMessage(json.RawMessage("null"))
raw, err := json.Marshal(rm)
suite.Require().NoError(err, "should not fail")
suite.Equal(jsonNull, raw, "should return correct value")
}
func (suite *JSONRawMessageMarshalJSONSuite) TestNotValid() {
rm := JSONRawMessage{RawMessage: json.RawMessage(`{"hello":"world"}`)}
raw, err := json.Marshal(rm)
suite.Require().NoError(err, "should not fail")
suite.Equal(jsonNull, raw, "should return correct value")
}
func (suite *JSONRawMessageMarshalJSONSuite) TestOK() {
v := json.RawMessage(`{"hello":"world"}`)
rm := NewJSONRawMessage(v)
raw, err := json.Marshal(rm)
suite.Require().NoError(err, "should not fail")
suite.Equal(marshalMust(v), raw, "should return correct value")
}
func TestJSONRawMessage_MarshalJSON(t *testing.T) {
suite.Run(t, new(JSONRawMessageMarshalJSONSuite))
}
// JSONRawMessageUnmarshalJSONSuite tests JSONRawMessage.UnmarshalJSON.
type JSONRawMessageUnmarshalJSONSuite struct {
suite.Suite
}
func (suite *JSONRawMessageUnmarshalJSONSuite) TestUndefinedAsField() {
var s struct {
RM JSONRawMessage `json:"rm"`
}
err := json.Unmarshal([]byte(`{}`), &s)
suite.Require().NoError(err, "should not fail")
suite.False(s.RM.Valid, "should not be valid")
}
func (suite *JSONRawMessageUnmarshalJSONSuite) TestDirectly() {
var rm JSONRawMessage
err := rm.UnmarshalJSON(nil)
suite.Require().NoError(err, "should not fail")
suite.False(rm.Valid, "should not be valid")
}
func (suite *JSONRawMessageUnmarshalJSONSuite) TestNull() {
var s struct {
RM JSONRawMessage `json:"rm"`
}
err := json.Unmarshal([]byte(`{"rm": null}`), &s)
suite.Require().NoError(err, "should not fail")
suite.False(s.RM.Valid, "should not be valid")
}
func (suite *JSONRawMessageUnmarshalJSONSuite) TestOK() {
v := json.RawMessage(`{"hello":"world"}`)
var RM JSONRawMessage
err := json.Unmarshal(marshalMust(v), &RM)
suite.Require().NoError(err, "should not fail")
suite.True(RM.Valid, "should be valid")
suite.Equal(v, RM.RawMessage, "should unmarshal correct value")
}
func TestJSONRawMessage_UnmarshalJSON(t *testing.T) {
suite.Run(t, new(JSONRawMessageUnmarshalJSONSuite))
}
// JSONRawMessageScanSuite tests JSONRawMessage.Scan.
type JSONRawMessageScanSuite struct {
suite.Suite
}
func (suite *JSONRawMessageScanSuite) TestNull() {
var rm JSONRawMessage
err := rm.Scan(nil)
suite.Require().NoError(err, "should not fail")
suite.False(rm.Valid, "should not be valid")
}
func (suite *JSONRawMessageScanSuite) TestJSONNull() {
var rm JSONRawMessage
err := rm.Scan(jsonNull)
suite.Require().NoError(err, "should not fail")
suite.True(rm.Valid, "should not be valid")
}
func (suite *JSONRawMessageScanSuite) TestUnexpectedValue() {
var rm JSONRawMessage
err := rm.Scan(1234)
suite.Error(err, "should fail")
}
func (suite *JSONRawMessageScanSuite) TestOKByteSlice() {
v := json.RawMessage(`{"meow":"woof"}`)
var rm JSONRawMessage
err := rm.Scan([]byte(v))
suite.Require().NoError(err, "should not fail")
suite.True(rm.Valid, "should be valid")
suite.Equal(v, rm.RawMessage, "should scan correct value")
}
func (suite *JSONRawMessageScanSuite) TestOKString() {
v := json.RawMessage(`{"meow":"woof"}`)
var rm JSONRawMessage
err := rm.Scan(string(v))
suite.Require().NoError(err, "should not fail")
suite.True(rm.Valid, "should be valid")
suite.Equal(v, rm.RawMessage, "should scan correct value")
}
func TestJSONRawMessage_Scan(t *testing.T) {
suite.Run(t, new(JSONRawMessageScanSuite))
}
// JSONRawMessageValueSuite tests JSONRawMessage.Value.
type JSONRawMessageValueSuite struct {
suite.Suite
}
func (suite *JSONRawMessageValueSuite) TestUndefined() {
rm := JSONRawMessage{RawMessage: json.RawMessage(`{"hello": "world"}`)}
raw, err := rm.Value()
suite.Require().NoError(err, "should not fail")
suite.Nil(raw, "should return correct value")
}
func (suite *JSONRawMessageValueSuite) TestNull() {
rm := NewJSONRawMessage(json.RawMessage("null"))
raw, err := rm.Value()
suite.Require().NoError(err, "should not fail")
suite.Equal([]byte("null"), raw, "should return correct value")
}
func (suite *JSONRawMessageValueSuite) TestOK() {
v := json.RawMessage(`{"hello":"world"}`)
rm := NewJSONRawMessage(v)
raw, err := rm.Value()
suite.Require().NoError(err, "should not fail")
suite.Equal([]byte(v), raw, "should return correct value")
}
func TestJSONRawMessage_Value(t *testing.T) {
suite.Run(t, new(JSONRawMessageValueSuite))
}