-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils_test.go
116 lines (96 loc) · 2.16 KB
/
utils_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
import (
"context"
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func init() {
rand.Seed(time.Now().UnixNano())
if err := clearOldChannelTypes(); err != nil {
panic(err) // app has bad data from previous runs
}
}
func clearOldChannelTypes() error {
c, err := NewClientFromEnvVars()
if err != nil {
return err
}
c.BaseURL = DefaultBaseURL
ctx := context.Background()
resp, err := c.ListChannelTypes(ctx)
if err != nil {
return err
}
for _, ct := range resp.ChannelTypes {
if contains(defaultChannelTypes, ct.Name) {
continue
}
filter := map[string]interface{}{"type": ct.Name}
resp, _ := c.QueryChannels(ctx, &QueryOption{Filter: filter})
hasChannel := false
for _, ch := range resp.Channels {
if _, err := ch.Delete(ctx); err != nil {
hasChannel = true
break
}
}
if !hasChannel {
_, _ = c.DeleteChannelType(ctx, ct.Name)
}
}
return nil
}
func randomUser(t *testing.T, c *Client) *User {
t.Helper()
ctx := context.Background()
resp, err := c.UpsertUser(ctx, &User{ID: randomString(10)})
require.NoError(t, err)
t.Cleanup(func() {
_, _ = c.DeleteUsers(ctx, []string{resp.User.ID}, DeleteUserOptions{
User: HardDelete,
Messages: HardDelete,
Conversations: HardDelete,
})
})
return resp.User
}
func randomUsers(t *testing.T, c *Client, n int) []*User {
t.Helper()
users := make([]*User, 0, n)
for i := 0; i < n; i++ {
users = append(users, &User{ID: randomString(10)})
}
resp, err := c.UpsertUsers(context.Background(), users...)
require.NoError(t, err)
users = users[:0]
for _, user := range resp.Users {
users = append(users, user)
}
return users
}
func randomUsersID(t *testing.T, c *Client, n int) []string {
t.Helper()
users := randomUsers(t, c, n)
ids := make([]string, n)
for i, u := range users {
ids[i] = u.ID
}
return ids
}
func randomString(n int) string {
bytes := make([]byte, n)
for i := 0; i < n; i++ {
bytes[i] = byte(65 + rand.Intn(25)) // A=65 and Z = 65+25
}
return string(bytes)
}
func contains(ls []string, s string) bool {
for _, item := range ls {
if item == s {
return true
}
}
return false
}