File size: 6,412 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package test

import (
	"fmt"
	"testing"

	"github.com/go-openapi/strfmt"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/weaviate/weaviate/test/helper"
	graphqlhelper "github.com/weaviate/weaviate/test/helper/graphql"
	"github.com/weaviate/weaviate/test/helper/sample-schema/multishard"
)

func getWithCursorSearch(t *testing.T) {
	t.Run("listing objects using cursor api", func(t *testing.T) {
		tests := []struct {
			name             string
			className        string
			after            string
			limit            int
			filter           string
			expectedIDs      []strfmt.UUID
			expectedErrorMsg string
		}{
			{
				name:      `cursor with after: "" limit: 2`,
				className: "CursorClass",
				after:     "",
				limit:     2,
				expectedIDs: []strfmt.UUID{
					cursorClassID1,
					cursorClassID2,
					cursorClassID3,
					cursorClassID4,
					cursorClassID5,
					cursorClassID6,
					cursorClassID7,
				},
			},
			{
				name:      fmt.Sprintf("cursor with after: \"%s\" limit: 1", cursorClassID4),
				className: "CursorClass",
				after:     cursorClassID4.String(),
				limit:     1,
				expectedIDs: []strfmt.UUID{
					cursorClassID5,
					cursorClassID6,
					cursorClassID7,
				},
			},
			{
				name:             "error with offset",
				className:        "CursorClass",
				filter:           `limit: 1 after: "" offset: 1`,
				expectedErrorMsg: "cursor api: invalid 'after' parameter: offset cannot be set with after and limit parameters",
			},
			{
				name:             "error with nearObject",
				className:        "CursorClass",
				filter:           fmt.Sprintf("limit: 1 after: \"\" nearObject:{id:\"%s\"}", cursorClassID1),
				expectedErrorMsg: "cursor api: invalid 'after' parameter: other params cannot be set with after and limit parameters",
			},
			{
				name:             "error with nearVector",
				className:        "CursorClass",
				filter:           `limit: 1 after: "" nearVector:{vector:[0.1, 0.2]}`,
				expectedErrorMsg: "cursor api: invalid 'after' parameter: other params cannot be set with after and limit parameters",
			},
			{
				name:             "error with hybrid",
				className:        "CursorClass",
				filter:           `limit: 1 after: "" hybrid:{query:"cursor api"}`,
				expectedErrorMsg: "cursor api: invalid 'after' parameter: other params cannot be set with after and limit parameters",
			},
			{
				name:             "error with bm25",
				className:        "CursorClass",
				filter:           `limit: 1 after: "" bm25:{query:"cursor api"}`,
				expectedErrorMsg: "cursor api: invalid 'after' parameter: other params cannot be set with after and limit parameters",
			},
			{
				name:             "error with sort",
				className:        "CursorClass",
				filter:           `limit: 1 after: "" sort:{path:"name"}`,
				expectedErrorMsg: "cursor api: invalid 'after' parameter: sort cannot be set with after and limit parameters",
			},
			{
				name:             "error with where",
				className:        "CursorClass",
				filter:           `limit: 1 after: "" where:{path:"id" operator:Like valueText:"*"}`,
				expectedErrorMsg: "cursor api: invalid 'after' parameter: where cannot be set with after and limit parameters",
			},
			{
				name:             "error with bm25, hybrid and offset",
				className:        "CursorClass",
				filter:           `limit: 1 after: "" bm25:{query:"cursor api"} hybrid:{query:"cursor api"} offset:1`,
				expectedErrorMsg: "cursor api: invalid 'after' parameter: other params cannot be set with after and limit parameters",
			},
			{
				name:             "error with no limit set",
				className:        "CursorClass",
				filter:           `after:"00000000-0000-0000-0000-000000000000"`,
				expectedErrorMsg: "cursor api: invalid 'after' parameter: limit parameter must be set",
			},
			// multi shard
			{
				name:      `multi shard cursor with after: "" limit: 1`,
				className: "MultiShard",
				after:     "",
				limit:     1,
				expectedIDs: []strfmt.UUID{
					multishard.MultiShardID1,
					multishard.MultiShardID2,
					multishard.MultiShardID3,
				},
			},
		}
		for _, tt := range tests {
			t.Run(tt.name, func(t *testing.T) {
				query := "{ Get { " + tt.className + " %s { _additional { id } } } }"
				if len(tt.expectedErrorMsg) > 0 {
					errQuery := fmt.Sprintf(query, fmt.Sprintf("(%s)", tt.filter))
					result := graphqlhelper.ErrorGraphQL(t, helper.RootAuth, errQuery)
					assert.Len(t, result, 1)

					errMsg := result[0].Message
					assert.Equal(t, tt.expectedErrorMsg, errMsg)
				} else {
					parseResults := func(t *testing.T, cities []interface{}) []strfmt.UUID {
						var ids []strfmt.UUID
						for _, city := range cities {
							id, ok := city.(map[string]interface{})["_additional"].(map[string]interface{})["id"]
							require.True(t, ok)

							idString, ok := id.(string)
							require.True(t, ok)

							ids = append(ids, strfmt.UUID(idString))
						}
						return ids
					}
					// use cursor api
					cursorSearch := func(t *testing.T, className, after string, limit int) []strfmt.UUID {
						cursor := fmt.Sprintf(`(limit: %v after: "%s")`, limit, after)
						result := graphqlhelper.AssertGraphQL(t, helper.RootAuth, fmt.Sprintf(query, cursor))
						cities := result.Get("Get", className).AsSlice()
						return parseResults(t, cities)
					}

					var cursorIDs []strfmt.UUID
					after, limit := tt.after, tt.limit
					for {
						result := cursorSearch(t, tt.className, after, limit)
						cursorIDs = append(cursorIDs, result...)
						if len(result) == 0 {
							break
						}
						after = result[len(result)-1].String()
					}

					assert.ElementsMatch(t, tt.expectedIDs, cursorIDs)
					require.Equal(t, len(tt.expectedIDs), len(cursorIDs))
					for i := range tt.expectedIDs {
						assert.Equal(t, tt.expectedIDs[i], cursorIDs[i])
					}
				}
			})
		}
	})
}