forked from GetStream/stream-go2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity_test.go
82 lines (77 loc) · 2.59 KB
/
activity_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
package stream_test
import (
"encoding/json"
"testing"
"time"
"github.com/GetStream/stream-go2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestActivityMarshalUnmarshalJSON(t *testing.T) {
now := getTime(time.Now())
testCases := []struct {
activity stream.Activity
data []byte
}{
{
activity: stream.Activity{Actor: "actor", Verb: "verb", Object: "object"},
data: []byte(`{"actor":"actor","object":"object","verb":"verb"}`),
},
{
activity: stream.Activity{Actor: "actor", Verb: "verb", Object: "object", Time: now},
data: []byte(`{"actor":"actor","object":"object","time":"` + now.Format(stream.TimeLayout) + `","verb":"verb"}`),
},
{
activity: stream.Activity{Actor: "actor", Verb: "verb", Object: "object", Time: now, Extra: map[string]interface{}{"popularity": 42.0, "size": map[string]interface{}{"width": 800.0, "height": 600.0}}},
data: []byte(`{"actor":"actor","object":"object","popularity":42,"size":{"height":600,"width":800},"time":"` + now.Format(stream.TimeLayout) + `","verb":"verb"}`),
},
{
activity: stream.Activity{Actor: "actor", Verb: "verb", Object: "object", Time: now, Extra: map[string]interface{}{"popularity": 42.0, "size": map[string]interface{}{"width": 800.0, "height": 600.0}}},
data: []byte(`{"actor":"actor","object":"object","popularity":42,"size":{"height":600,"width":800},"time":"` + now.Format(stream.TimeLayout) + `","verb":"verb"}`),
},
{
activity: stream.Activity{To: []string{"abcd", "efgh"}},
data: []byte(`{"to":["abcd","efgh"]}`),
},
}
for _, tc := range testCases {
data, err := json.Marshal(tc.activity)
assert.NoError(t, err)
assert.Equal(t, tc.data, data)
var out stream.Activity
err = json.Unmarshal(tc.data, &out)
require.NoError(t, err)
assert.Equal(t, tc.activity, out)
}
}
func TestActivityMarshalUnmarshalJSON_toTargets(t *testing.T) {
testCases := []struct {
activity stream.Activity
data []byte
shouldError bool
}{
{
activity: stream.Activity{To: []string{"abcd", "efgh"}},
data: []byte(`{"to":["abcd","efgh"]}`),
},
{
activity: stream.Activity{To: []string{"abcd", "efgh"}},
data: []byte(`{"to":[["abcd", "foo"], ["efgh", "bar"]]}`),
},
{
activity: stream.Activity{To: []string{"abcd", "efgh"}},
data: []byte(`{"to":[[123]]}`),
shouldError: true,
},
}
for _, tc := range testCases {
var out stream.Activity
err := json.Unmarshal(tc.data, &out)
if tc.shouldError {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tc.activity, out)
}
}
}