forked from GetStream/stream-go2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enriched_activities_test.go
78 lines (74 loc) · 2.72 KB
/
enriched_activities_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
package stream_test
import (
"encoding/json"
"testing"
"time"
"github.com/GetStream/stream-go2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEnrichedActivityUnmarshalJSON(t *testing.T) {
now := getTime(time.Now())
testCases := []struct {
activity stream.EnrichedActivity
data []byte
}{
{
activity: stream.EnrichedActivity{Actor: stream.Data{ID: "actor"}, Verb: "verb", Object: stream.Data{ID: "object"}},
data: []byte(`{"actor":"actor","object":"object","verb":"verb"}`),
},
{
activity: stream.EnrichedActivity{Actor: stream.Data{ID: "actor"}, Verb: "verb", Object: stream.Data{ID: "object"}, Time: now},
data: []byte(`{"actor":"actor","object":"object","time":"` + now.Format(stream.TimeLayout) + `","verb":"verb"}`),
},
{
activity: stream.EnrichedActivity{Actor: stream.Data{ID: "actor"}, Verb: "verb", Object: stream.Data{ID: "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.EnrichedActivity{Actor: stream.Data{ID: "actor"}, Verb: "verb", Object: stream.Data{ID: "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.EnrichedActivity{To: []string{"abcd", "efgh"}},
data: []byte(`{"to":["abcd","efgh"]}`),
},
}
for _, tc := range testCases {
var out stream.EnrichedActivity
err := json.Unmarshal(tc.data, &out)
require.NoError(t, err)
assert.Equal(t, tc.activity, out)
}
}
func TestEnrichedActivityUnmarshalJSON_toTargets(t *testing.T) {
testCases := []struct {
activity stream.EnrichedActivity
data []byte
shouldError bool
}{
{
activity: stream.EnrichedActivity{To: []string{"abcd", "efgh"}},
data: []byte(`{"to":["abcd","efgh"]}`),
},
{
activity: stream.EnrichedActivity{To: []string{"abcd", "efgh"}},
data: []byte(`{"to":[["abcd", "foo"], ["efgh", "bar"]]}`),
},
{
activity: stream.EnrichedActivity{To: []string{"abcd", "efgh"}},
data: []byte(`{"to":[[123]]}`),
shouldError: true,
},
}
for _, tc := range testCases {
var out stream.EnrichedActivity
err := json.Unmarshal(tc.data, &out)
if tc.shouldError {
require.Error(t, err)
} else {
require.NoError(t, err)
assert.Equal(t, tc.activity, out)
}
}
}