forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
69 lines (55 loc) · 1.34 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package stream_chat // nolint: golint
import (
"encoding/json"
"math/rand"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func randomExtraData(in interface{}) {
v := reflect.ValueOf(in).Elem()
if v.Kind() != reflect.Struct {
return
}
f := v.FieldByName("ExtraData")
f.Set(reflect.ValueOf(map[string]interface{}{
"extra_data": map[string]interface{}{
"mystring": randomString(10),
"mybool": rand.Float64() < 0.5,
},
"data": "custom",
"custom_data": "really_custom",
"extra": map[string]interface{}{
randomString(10): randomString(10),
},
"stream": randomString(10),
"my_score": float64(rand.Intn(100)),
}))
}
func testInvariantJSON(t *testing.T, in, in2 interface{}) {
// put random
randomExtraData(in)
// marshal given
data, err := json.Marshal(in)
require.NoError(t, err)
// unmarshal again
require.NoError(t, json.Unmarshal(data, in2))
// ensure they are same
require.Equal(t, in, in2)
}
func TestJSON(t *testing.T) {
var c, c2 Channel
testInvariantJSON(t, &c, &c2)
var u, u2 User
testInvariantJSON(t, &u, &u2)
var e, e2 Event
testInvariantJSON(t, &e, &e2)
var m, m2 Message
testInvariantJSON(t, &m, &m2)
var mr, mr2 messageRequestMessage
testInvariantJSON(t, &mr, &mr2)
var a, a2 Attachment
testInvariantJSON(t, &a, &a2)
var r, r2 Reaction
testInvariantJSON(t, &r, &r2)
}