forked from gorse-io/gorse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
297 lines (267 loc) · 7.02 KB
/
client_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//go:build integrate_test
// Copyright 2022 gorse Project Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"context"
"encoding/base64"
"github.com/go-redis/redis/v9"
"github.com/stretchr/testify/suite"
"github.com/thoas/go-funk"
"testing"
"time"
)
const (
RedisEndpoint = "redis://127.0.0.1:6379/0"
GorseEndpoint = "http://127.0.0.1:8087"
GorseApiKey = ""
)
type GorseClientTestSuite struct {
suite.Suite
client *GorseClient
redis *redis.Client
}
func (suite *GorseClientTestSuite) SetupSuite() {
suite.client = NewGorseClient(GorseEndpoint, GorseApiKey)
options, err := redis.ParseURL(RedisEndpoint)
suite.NoError(err)
suite.redis = redis.NewClient(options)
}
func (suite *GorseClientTestSuite) TearDownSuite() {
err := suite.redis.Close()
suite.NoError(err)
}
func (suite *GorseClientTestSuite) TestFeedback() {
ctx := context.TODO()
timestamp := time.Unix(1660459054, 0).UTC().Format(time.RFC3339)
userId := "800"
insertFeedbackResp, err := suite.client.InsertFeedback(ctx, []Feedback{{
FeedbackType: "like",
UserId: userId,
Timestamp: timestamp,
ItemId: "200",
}})
suite.NoError(err)
suite.Equal(1, insertFeedbackResp.RowAffected)
insertFeedbacksResp, err := suite.client.InsertFeedback(ctx, []Feedback{{
FeedbackType: "read",
UserId: userId,
Timestamp: timestamp,
ItemId: "300",
}, {
FeedbackType: "read",
UserId: userId,
Timestamp: timestamp,
ItemId: "400",
}})
suite.NoError(err)
suite.Equal(2, insertFeedbacksResp.RowAffected)
feedbacks, err := suite.client.ListFeedbacks(ctx, "read", userId)
suite.NoError(err)
suite.ElementsMatch([]Feedback{
{
FeedbackType: "read",
UserId: userId,
Timestamp: timestamp,
ItemId: "300",
}, {
FeedbackType: "read",
UserId: userId,
Timestamp: timestamp,
ItemId: "400",
},
}, feedbacks)
}
func (suite *GorseClientTestSuite) TestRecommend() {
ctx := context.TODO()
suite.hSet("offline_recommend", "100", []Score{
{Id: "1", Score: 1},
{Id: "2", Score: 2},
{Id: "3", Score: 3},
})
resp, err := suite.client.GetRecommend(ctx, "100", "", 10, 0)
suite.NoError(err)
suite.Equal([]string{"3", "2", "1"}, resp)
}
func (suite *GorseClientTestSuite) TestSessionRecommend() {
ctx := context.Background()
suite.hSet("item_neighbors", "1", []Score{
{Id: "2", Score: 100000},
{Id: "9", Score: 1},
})
suite.hSet("item_neighbors", "2", []Score{
{Id: "3", Score: 100000},
{Id: "8", Score: 1},
{Id: "9", Score: 1},
})
suite.hSet("item_neighbors", "3", []Score{
{Id: "4", Score: 100000},
{Id: "7", Score: 1},
{Id: "8", Score: 1},
{Id: "9", Score: 1},
})
suite.hSet("item_neighbors", "4", []Score{
{Id: "1", Score: 100000},
{Id: "6", Score: 1},
{Id: "7", Score: 1},
{Id: "8", Score: 1},
{Id: "9", Score: 1},
})
feedbackType := "like"
userId := "0"
timestamp := time.Unix(1660459054, 0).UTC().Format(time.RFC3339)
resp, err := suite.client.SessionRecommend(ctx, []Feedback{
{
FeedbackType: feedbackType,
UserId: userId,
ItemId: "1",
Timestamp: timestamp,
},
{
FeedbackType: feedbackType,
UserId: userId,
ItemId: "2",
Timestamp: timestamp,
},
{
FeedbackType: feedbackType,
UserId: userId,
ItemId: "3",
Timestamp: timestamp,
},
{
FeedbackType: feedbackType,
UserId: userId,
ItemId: "4",
Timestamp: timestamp,
},
}, 3)
suite.NoError(err)
suite.Equal([]Score{
{
Id: "9",
Score: 4,
},
{
Id: "8",
Score: 3,
},
{
Id: "7",
Score: 2,
},
}, resp)
}
func (suite *GorseClientTestSuite) TestNeighbors() {
ctx := context.Background()
suite.hSet("item_neighbors", "100", []Score{
{Id: "1", Score: 1},
{Id: "2", Score: 2},
{Id: "3", Score: 3},
})
itemId := "100"
resp, err := suite.client.GetNeighbors(ctx, itemId, 3)
suite.NoError(err)
suite.Equal([]Score{
{
Id: "3",
Score: 3,
}, {
Id: "2",
Score: 2,
}, {
Id: "1",
Score: 1,
},
}, resp)
}
func (suite *GorseClientTestSuite) TestUsers() {
ctx := context.TODO()
user := User{
UserId: "100",
Labels: []string{"a", "b", "c"},
Subscribe: []string{"d", "e"},
Comment: "comment",
}
userPatch := UserPatch{
Comment: &user.Comment,
}
rowAffected, err := suite.client.InsertUser(ctx, user)
suite.NoError(err)
suite.Equal(1, rowAffected.RowAffected)
rowAffected, err = suite.client.UpdateUser(ctx, user.UserId, userPatch)
suite.NoError(err)
suite.Equal(1, rowAffected.RowAffected)
userResp, err := suite.client.GetUser(ctx, "100")
suite.NoError(err)
suite.Equal(user, userResp)
deleteAffect, err := suite.client.DeleteUser(ctx, "100")
suite.NoError(err)
suite.Equal(1, deleteAffect.RowAffected)
_, err = suite.client.GetUser(ctx, "100")
suite.Equal("100: user not found", err.Error())
}
func (suite *GorseClientTestSuite) TestItems() {
ctx := context.TODO()
timestamp := time.Unix(1660459054, 0).UTC().Format(time.RFC3339)
item := Item{
ItemId: "100",
IsHidden: true,
Labels: []string{"a", "b", "c"},
Categories: []string{"d", "e"},
Timestamp: timestamp,
Comment: "comment",
}
itemPatch := ItemPatch{
Comment: &item.Comment,
}
rowAffected, err := suite.client.InsertItem(ctx, item)
suite.NoError(err)
suite.Equal(1, rowAffected.RowAffected)
rowAffected, err = suite.client.UpdateItem(ctx, item.ItemId, itemPatch)
suite.NoError(err)
suite.Equal(1, rowAffected.RowAffected)
resp, err := suite.client.GetPopularItems(ctx, "", "", 10, 0)
suite.NoError(err)
funk.NotEmpty(resp)
category := "d"
resp, err = suite.client.GetPopularItems(ctx, "", category, 10, 0)
suite.NoError(err)
funk.NotEmpty(resp)
itemResp, err := suite.client.GetItem(ctx, "100")
suite.NoError(err)
suite.Equal(item, itemResp)
deleteAffect, err := suite.client.DeleteItem(ctx, "100")
suite.NoError(err)
suite.Equal(1, deleteAffect.RowAffected)
_, err = suite.client.GetItem(ctx, "100")
suite.Equal("100: item not found", err.Error())
}
func (suite *GorseClientTestSuite) hSet(collection, subset string, scores []Score) {
for _, score := range scores {
err := suite.redis.HSet(context.TODO(), "documents:"+collection+":"+subset+":"+score.Id,
"collection", collection,
"subset", subset,
"id", score.Id,
"score", score.Score,
"is_hidden", 0,
"categories", base64.RawStdEncoding.EncodeToString([]byte("_")),
"timestamp", time.Now().UnixMicro(),
).Err()
suite.NoError(err)
}
}
func TestGorseClientTestSuite(t *testing.T) {
suite.Run(t, new(GorseClientTestSuite))
}