-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathenriched_activities_test.go
91 lines (86 loc) · 2.97 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
79
80
81
82
83
84
85
86
87
88
89
90
91
package stream_test
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
stream "github.com/GetStream/stream-go2/v8"
)
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]any{"popularity": 42.0, "size": map[string]any{"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]any{"popularity": 42.0, "size": map[string]any{"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"]}`),
},
{
activity: stream.EnrichedActivity{
ForeignID: "SA:123",
Extra: map[string]any{
"foreign_id_ref": map[string]any{
"id": "123",
"extra_prop": true,
},
},
},
data: []byte(`{"foreign_id":{"id":"123","extra_prop":true}}`),
},
}
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)
}
}
}