forked from valkey-io/valkey-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor_test.go
181 lines (165 loc) · 5.22 KB
/
cursor_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
package om
import (
"context"
"strconv"
"testing"
"github.com/valkey-io/valkey-go"
)
type Book struct {
Key string `json:"key" valkey:",key"`
Loc string `json:"loc" valkey:",loc"`
Ver int64 `json:"ver" valkey:",ver"`
ID int64 `json:"id" valkey:",id"`
Count int64 `json:"count" valkey:",count"`
}
func TestAggregateCursor(t *testing.T) {
ctx := context.Background()
client := setup(t)
client.Do(ctx, client.B().Flushall().Build())
defer client.Close()
jsonRepo := NewJSONRepository("book", Book{}, client)
hashRepo := NewHashRepository("book", Book{}, client)
if err := jsonRepo.CreateIndex(ctx, func(schema FtCreateSchema) valkey.Completed {
return schema.
FieldName("$.id").As("id").Numeric().
FieldName("$.loc").As("loc").Tag().
FieldName("$.count").As("count").Numeric().Sortable().Build()
}); err != nil {
t.Fatal(err)
}
if err := hashRepo.CreateIndex(ctx, func(schema FtCreateSchema) valkey.Completed {
return schema.
FieldName("id").As("id").Numeric().
FieldName("loc").As("loc").Tag().
FieldName("count").As("count").Numeric().Sortable().Build()
}); err != nil {
t.Fatal(err)
}
t.Run("Hash", func(t *testing.T) {
testRepo(t, hashRepo)
})
t.Run("Json", func(t *testing.T) {
testRepo(t, jsonRepo)
})
}
func testRepo(t *testing.T, repo Repository[Book]) {
for i := 0; i < 10; i++ {
book := repo.NewEntity()
book.ID = int64(i / 2)
book.Count = int64(i)
book.Loc = "1"
if err := repo.Save(context.Background(), book); err != nil {
panic(err)
}
}
t.Run("Deadline exceed", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := repo.Aggregate(ctx, func(search FtAggregateIndex) valkey.Completed {
return search.Query("any").Build()
})
if err != context.Canceled {
t.Fatalf("unexpected err %v", err)
}
})
t.Run("Without cursor", func(t *testing.T) {
cursor, err := repo.Aggregate(context.Background(), func(search FtAggregateIndex) valkey.Completed {
return search.Query("@loc:{1}").
Groupby(1).Property("@id").Reduce("MIN").Nargs(1).Arg("@count").As("minCount").
Sortby(2).Property("@minCount").Asc().Build()
})
if err != nil {
t.Fatalf("unexpected err %v", err)
}
result, err := cursor.Read(context.Background())
if err != nil {
t.Fatalf("unexpected err %v", err)
}
if len(result) != 5 || int64(len(result)) != cursor.Total() {
t.Fatalf("unexpected total %v %v", len(result), cursor.Total())
}
if _, err = cursor.Read(context.Background()); err != EndOfCursor {
t.Fatalf("unexpected err %v", err)
}
if err = cursor.Del(context.Background()); err != nil {
t.Fatalf("unexpected err %v", err)
}
for i, record := range result {
if record["id"] != strconv.Itoa(i) {
t.Fatalf("unexpected value %v", record["id"])
}
if record["minCount"] != strconv.Itoa(i*2) {
t.Fatalf("unexpected value %v", record["minCount"])
}
}
})
t.Run("With cursor", func(t *testing.T) {
cursor, err := repo.Aggregate(context.Background(), func(search FtAggregateIndex) valkey.Completed {
return search.Query("@loc:{1}").
Groupby(1).Property("@id").Reduce("MIN").Nargs(1).Arg("@count").As("minCount").
Sortby(2).Property("@minCount").Asc().Withcursor().Count(2).Build()
})
if err != nil {
t.Fatalf("unexpected err %v", err)
}
if cursor.Total() != 5 {
t.Fatalf("unexpected total %v", cursor.Total())
}
var result []map[string]string
for {
partial, err := cursor.Read(context.Background())
if err == EndOfCursor {
break
}
if len(partial) > 2 {
t.Fatalf("unexpected partial len %v", len(partial))
}
result = append(result, partial...)
}
if err = cursor.Del(context.Background()); err != nil {
t.Fatalf("unexpected err %v", err)
}
if len(result) != 5 {
t.Fatalf("unexpected total %v", len(result))
}
for i, record := range result {
if record["id"] != strconv.Itoa(i) {
t.Fatalf("unexpected value %v", record["id"])
}
if record["minCount"] != strconv.Itoa(i*2) {
t.Fatalf("unexpected value %v", record["minCount"])
}
}
})
t.Run("Read deadline", func(t *testing.T) {
cursor, err := repo.Aggregate(context.Background(), func(search FtAggregateIndex) valkey.Completed {
return search.Query("@loc:{1}").
Groupby(1).Property("@id").Reduce("MIN").Nargs(1).Arg("@count").As("minCount").
Sortby(2).Property("@minCount").Asc().Withcursor().Count(2).Build()
})
if err != nil {
t.Fatalf("unexpected err %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err = cursor.Read(ctx); err != nil { // initial result is not affected by ctx
t.Fatalf("unexpected err %v", err)
}
if _, err = cursor.Read(ctx); err != context.Canceled {
t.Fatalf("unexpected err %v", err)
}
})
t.Run("Del cursor", func(t *testing.T) {
cursor, err := repo.Aggregate(context.Background(), func(search FtAggregateIndex) valkey.Completed {
return search.Query("@loc:{1}").
Groupby(1).Property("@id").Reduce("MIN").Nargs(1).Arg("@count").As("minCount").
Sortby(2).Property("@minCount").Asc().Withcursor().Count(2).Build()
})
if err != nil {
t.Fatalf("unexpected err %v", err)
}
if err := cursor.Del(context.Background()); err != nil {
t.Fatalf("unexpected err %v", err)
}
})
}