forked from codeGROOVE-dev/ds9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor_coverage_test.go
More file actions
138 lines (117 loc) · 3.42 KB
/
cursor_coverage_test.go
File metadata and controls
138 lines (117 loc) · 3.42 KB
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
package datastore_test
import (
"context"
"errors"
"testing"
"github.com/codeGROOVE-dev/ds9/pkg/datastore"
)
// TestCursorWithPagination tests the Cursor() method with actual cursor from query
func TestCursorWithPagination(t *testing.T) {
client, cleanup := datastore.NewMockClient(t)
defer cleanup()
ctx := context.Background()
// Create enough entities to trigger pagination
for i := range 3 {
key := datastore.IDKey("CursorTest", int64(i+1), nil)
entity := &testEntity{
Name: "test",
Count: int64(i),
}
if _, err := client.Put(ctx, key, entity); err != nil {
t.Fatalf("Put failed: %v", err)
}
}
// Query with limit to trigger cursor generation
q := datastore.NewQuery("CursorTest").Limit(2)
it := client.Run(ctx, q)
// Fetch first result
var entity testEntity
_, err := it.Next(&entity)
if err != nil {
t.Fatalf("First Next failed: %v", err)
}
// Now try to get cursor - should be available after fetching results
cursor, err := it.Cursor()
if err != nil {
t.Logf("Cursor() returned error (mock implementation): %v", err)
// Mock might not support cursors yet, that's OK
} else {
// If cursor is available, verify it's not empty
if cursor == "" {
t.Error("Expected non-empty cursor after fetching with limit")
} else {
t.Logf("Successfully got cursor: %s", cursor)
// Verify we can convert cursor to string
cursorStr := cursor.String()
if cursorStr == "" {
t.Error("Cursor.String() returned empty string")
}
}
}
}
// TestCursorBeforeFetch tests Cursor() before any results are fetched
func TestCursorBeforeFetch(t *testing.T) {
client, cleanup := datastore.NewMockClient(t)
defer cleanup()
ctx := context.Background()
// Create test entity
key := datastore.NameKey("CursorTest2", "test", nil)
entity := &testEntity{Name: "test", Count: 1}
if _, err := client.Put(ctx, key, entity); err != nil {
t.Fatalf("Put failed: %v", err)
}
// Create iterator but don't fetch any results
q := datastore.NewQuery("CursorTest2")
it := client.Run(ctx, q)
// Try to get cursor before fetching - should fail
cursor, err := it.Cursor()
if err == nil {
t.Error("Expected error when getting cursor before fetching results")
}
if cursor != "" {
t.Errorf("Expected empty cursor before fetching, got: %s", cursor)
}
}
// TestCursorWithLimitedResults tests cursor behavior with pagination
func TestCursorWithLimitedResults(t *testing.T) {
client, cleanup := datastore.NewMockClient(t)
defer cleanup()
ctx := context.Background()
// Create multiple entities
for i := range 3 {
key := datastore.IDKey("CursorPaginationTest", int64(i+1), nil)
entity := &testEntity{
Name: "test",
Count: int64(i),
}
if _, err := client.Put(ctx, key, entity); err != nil {
t.Fatalf("Put failed: %v", err)
}
}
// Query with limit smaller than total entities
q := datastore.NewQuery("CursorPaginationTest").Limit(2)
it := client.Run(ctx, q)
// Fetch all limited results
count := 0
for {
var entity testEntity
_, err := it.Next(&entity)
if errors.Is(err, datastore.Done) {
break
}
if err != nil {
t.Fatalf("Iterator Next failed: %v", err)
}
count++
// Try to get cursor after each fetch
cursor, err := it.Cursor()
if err != nil {
t.Logf("Cursor not available at position %d: %v", count, err)
} else if cursor != "" {
t.Logf("Got cursor at position %d: %s", count, cursor)
}
}
if count != 2 {
t.Errorf("Expected 2 results with limit, got %d", count)
}
}