forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_test.go
116 lines (93 loc) · 2.43 KB
/
query_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
package stream_chat // nolint: golint
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient_QueryUsers(t *testing.T) {
c := initClient(t)
const n = 4
ids := make([]string, n)
defer func() {
for _, id := range ids {
if id != "" {
_ = c.DeleteUser(id, nil)
}
}
}()
for i := n - 1; i > -1; i-- {
u := &User{ID: randomString(30), ExtraData: map[string]interface{}{"order": n - i - 1}}
_, err := c.UpsertUser(u)
require.NoError(t, err)
ids[i] = u.ID
time.Sleep(200 * time.Millisecond)
}
t.Parallel()
t.Run("Query all", func(tt *testing.T) {
results, err := c.QueryUsers(&QueryOption{
Filter: map[string]interface{}{
"id": map[string]interface{}{
"$in": ids,
},
},
})
require.NoError(tt, err)
require.Len(tt, results, len(ids))
})
t.Run("Query with offset/limit", func(tt *testing.T) {
offset := 1
results, err := c.QueryUsers(
&QueryOption{
Filter: map[string]interface{}{
"id": map[string]interface{}{
"$in": ids,
},
},
Offset: offset,
Limit: 2,
},
)
require.NoError(tt, err)
require.Len(tt, results, 2)
require.Equal(tt, results[0].ID, ids[offset])
require.Equal(tt, results[1].ID, ids[offset+1])
})
}
func TestClient_QueryChannels(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
_, err := ch.SendMessage(&Message{Text: "abc"}, "some")
require.NoError(t, err)
_, err = ch.SendMessage(&Message{Text: "abc"}, "some")
require.NoError(t, err)
messageLimit := 1
got, err := c.QueryChannels(&QueryOption{
Filter: map[string]interface{}{
"id": map[string]interface{}{
"$eq": ch.ID,
},
},
MessageLimit: &messageLimit,
})
require.NoError(t, err, "query channels error")
require.Equal(t, ch.ID, got[0].ID, "received channel ID")
require.Len(t, got[0].Messages, messageLimit)
}
func TestClient_Search(t *testing.T) {
c := initClient(t)
ch := initChannel(t, c)
user1, user2 := randomUser(), randomUser()
text := randomString(10)
_, err := ch.SendMessage(&Message{Text: text + " " + randomString(25)}, user1.ID)
require.NoError(t, err)
_, err = ch.SendMessage(&Message{Text: text + " " + randomString(25)}, user2.ID)
require.NoError(t, err)
got, err := c.Search(SearchRequest{Query: text, Filters: map[string]interface{}{
"members": map[string][]string{
"$in": {user1.ID, user2.ID},
},
}})
require.NoError(t, err)
assert.Len(t, got, 2)
}