-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_test.go
More file actions
331 lines (287 loc) · 10.1 KB
/
Copy pathquery_test.go
File metadata and controls
331 lines (287 loc) · 10.1 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package electrodb
import (
"strings"
"testing"
)
func TestQueryWithWhereClause(t *testing.T) {
schema := &Schema{
Service: "TestService",
Entity: "User",
Table: "TestTable",
Attributes: map[string]*AttributeDefinition{
"userId": {Type: AttributeTypeString, Required: true},
"email": {Type: AttributeTypeString, Required: true},
"age": {Type: AttributeTypeNumber, Required: false},
"active": {Type: AttributeTypeBoolean, Required: false},
"tenantId": {Type: AttributeTypeString, Required: true},
},
Indexes: map[string]*IndexDefinition{
"primary": {
PK: FacetDefinition{Field: "pk", Facets: []string{"userId"}},
},
"byTenant": {
Index: stringPtr("gsi1pk-gsi1sk-index"),
PK: FacetDefinition{Field: "gsi1pk", Facets: []string{"tenantId"}},
SK: &FacetDefinition{Field: "gsi1sk", Facets: []string{"email"}},
},
},
}
entity, err := NewEntity(schema, nil)
if err != nil {
t.Fatalf("Failed to create entity: %v", err)
}
// Test query with filter expression
query := entity.Query("byTenant").Query("tenant1").Where(func(attrs map[string]*AttributeRef, ops *OperationBuilder) string {
return attrs["age"].Gt(21) + " AND " + attrs["active"].Eq(true)
})
// Get params to verify the filter expression is built correctly
params, err := query.Params()
if err != nil {
t.Fatalf("Failed to build params: %v", err)
}
// Verify filter expression exists
filterExpr, ok := params["FilterExpression"].(string)
if !ok || filterExpr == "" {
t.Fatal("Expected FilterExpression to be set")
}
// Verify filter expression contains expected operators
if !strings.Contains(filterExpr, ">") {
t.Errorf("Expected filter expression to contain '>', got: %s", filterExpr)
}
if !strings.Contains(filterExpr, "AND") {
t.Errorf("Expected filter expression to contain 'AND', got: %s", filterExpr)
}
// Verify expression attribute names exist
exprAttrNames, ok := params["ExpressionAttributeNames"].(map[string]string)
if !ok {
t.Fatal("Expected ExpressionAttributeNames to be set")
}
// Should have attribute names for 'age' and 'active'
if len(exprAttrNames) < 2 {
t.Errorf("Expected at least 2 expression attribute names, got %d", len(exprAttrNames))
}
// Verify expression attribute values exist
exprAttrValues := params["ExpressionAttributeValues"]
if exprAttrValues == nil {
t.Fatal("Expected ExpressionAttributeValues to be set")
}
}
func TestQueryWithComplexWhereClause(t *testing.T) {
schema := &Schema{
Service: "TestService",
Entity: "Product",
Table: "TestTable",
Attributes: map[string]*AttributeDefinition{
"productId": {Type: AttributeTypeString, Required: true},
"category": {Type: AttributeTypeString, Required: true},
"name": {Type: AttributeTypeString, Required: true},
"price": {Type: AttributeTypeNumber, Required: false},
"inStock": {Type: AttributeTypeBoolean, Required: false},
"description": {Type: AttributeTypeString, Required: false},
},
Indexes: map[string]*IndexDefinition{
"primary": {
PK: FacetDefinition{Field: "pk", Facets: []string{"productId"}},
},
"byCategory": {
Index: stringPtr("gsi1pk-gsi1sk-index"),
PK: FacetDefinition{Field: "gsi1pk", Facets: []string{"category"}},
SK: &FacetDefinition{Field: "gsi1sk", Facets: []string{"name"}},
},
},
}
entity, err := NewEntity(schema, nil)
if err != nil {
t.Fatalf("Failed to create entity: %v", err)
}
// Test complex filter with multiple conditions
query := entity.Query("byCategory").Query("electronics").Where(func(attrs map[string]*AttributeRef, ops *OperationBuilder) string {
priceCondition := attrs["price"].Between(100, 500)
stockCondition := attrs["inStock"].Eq(true)
descCondition := attrs["description"].Contains("premium")
return "(" + priceCondition + " AND " + stockCondition + ") AND " + descCondition
})
params, err := query.Params()
if err != nil {
t.Fatalf("Failed to build params: %v", err)
}
filterExpr, ok := params["FilterExpression"].(string)
if !ok || filterExpr == "" {
t.Fatal("Expected FilterExpression to be set")
}
// Verify BETWEEN operator
if !strings.Contains(filterExpr, "BETWEEN") {
t.Errorf("Expected filter expression to contain 'BETWEEN', got: %s", filterExpr)
}
// Verify contains function
if !strings.Contains(filterExpr, "contains") {
t.Errorf("Expected filter expression to contain 'contains', got: %s", filterExpr)
}
// Verify parentheses for grouping
if !strings.Contains(filterExpr, "(") || !strings.Contains(filterExpr, ")") {
t.Errorf("Expected filter expression to contain parentheses, got: %s", filterExpr)
}
}
func TestQueryWithFunctionBasedFilters(t *testing.T) {
schema := &Schema{
Service: "TestService",
Entity: "Record",
Table: "TestTable",
Attributes: map[string]*AttributeDefinition{
"recordId": {Type: AttributeTypeString, Required: true},
"email": {Type: AttributeTypeString, Required: false},
"metadata": {Type: AttributeTypeMap, Required: false},
},
Indexes: map[string]*IndexDefinition{
"primary": {
PK: FacetDefinition{Field: "pk", Facets: []string{"recordId"}},
},
},
}
entity, err := NewEntity(schema, nil)
if err != nil {
t.Fatalf("Failed to create entity: %v", err)
}
// Test with function-based filters (exists, not_exists, begins_with)
query := entity.Query("primary").Query("record1").Where(func(attrs map[string]*AttributeRef, ops *OperationBuilder) string {
hasMetadata := ops.Exists(attrs["metadata"])
emailFilter := attrs["email"].Begins("admin@")
return hasMetadata + " AND " + emailFilter
})
params, err := query.Params()
if err != nil {
t.Fatalf("Failed to build params: %v", err)
}
filterExpr, ok := params["FilterExpression"].(string)
if !ok || filterExpr == "" {
t.Fatal("Expected FilterExpression to be set")
}
// Verify attribute_exists function
if !strings.Contains(filterExpr, "attribute_exists") {
t.Errorf("Expected filter expression to contain 'attribute_exists', got: %s", filterExpr)
}
// Verify begins_with function
if !strings.Contains(filterExpr, "begins_with") {
t.Errorf("Expected filter expression to contain 'begins_with', got: %s", filterExpr)
}
}
func TestQueryParamsWithoutWhereClause(t *testing.T) {
schema := &Schema{
Service: "TestService",
Entity: "Item",
Table: "TestTable",
Attributes: map[string]*AttributeDefinition{
"itemId": {Type: AttributeTypeString, Required: true},
"name": {Type: AttributeTypeString, Required: true},
},
Indexes: map[string]*IndexDefinition{
"primary": {
PK: FacetDefinition{Field: "pk", Facets: []string{"itemId"}},
},
},
}
entity, err := NewEntity(schema, nil)
if err != nil {
t.Fatalf("Failed to create entity: %v", err)
}
// Test query without filter - should work as before
query := entity.Query("primary").Query("item1")
params, err := query.Params()
if err != nil {
t.Fatalf("Failed to build params: %v", err)
}
// Should not have FilterExpression
if _, ok := params["FilterExpression"]; ok {
t.Error("Expected no FilterExpression when Where is not called")
}
// Should still have required params
if params["TableName"] == nil {
t.Error("Expected TableName to be set")
}
if params["KeyConditionExpression"] == nil {
t.Error("Expected KeyConditionExpression to be set")
}
}
func TestQueryParamsWithProjectionAttributes(t *testing.T) {
schema := &Schema{
Service: "TestService",
Entity: "Course",
Table: "TestTable",
Attributes: map[string]*AttributeDefinition{
"courseId": {Type: AttributeTypeString, Required: true},
"tenantId": {Type: AttributeTypeString, Required: true},
"title": {Type: AttributeTypeString, Required: false},
},
Indexes: map[string]*IndexDefinition{
"primary": {
PK: FacetDefinition{Field: "pk", Facets: []string{"tenantId"}},
SK: &FacetDefinition{Field: "sk", Facets: []string{"courseId"}},
},
},
}
entity, err := NewEntity(schema, nil)
if err != nil {
t.Fatalf("Failed to create entity: %v", err)
}
// Query with Attributes set should produce a ProjectionExpression and the
// matching ExpressionAttributeNames placeholders.
query := entity.Query("primary").Query("tenant1").Options(&QueryOptions{
Attributes: []string{"courseId", "tenantId"},
})
params, err := query.Params()
if err != nil {
t.Fatalf("Failed to build params: %v", err)
}
projExpr, ok := params["ProjectionExpression"].(string)
if !ok || projExpr == "" {
t.Fatal("Expected ProjectionExpression to be set when Attributes are provided")
}
exprAttrNames, ok := params["ExpressionAttributeNames"].(map[string]string)
if !ok {
t.Fatal("Expected ExpressionAttributeNames to be set when Attributes are provided")
}
// Each requested attribute must be projected via a placeholder, and the
// placeholder must resolve back to the requested top-level name.
for _, want := range []string{"courseId", "tenantId"} {
found := false
for placeholder, name := range exprAttrNames {
if name == want {
found = true
if !strings.Contains(projExpr, placeholder) {
t.Errorf("Expected ProjectionExpression %q to contain placeholder %q for attribute %q", projExpr, placeholder, want)
}
}
}
if !found {
t.Errorf("Expected ExpressionAttributeNames to map a placeholder to attribute %q, got %v", want, exprAttrNames)
}
}
}
func TestQueryParamsRejectsInvalidProjectionAttribute(t *testing.T) {
schema := &Schema{
Service: "TestService",
Entity: "Course",
Table: "TestTable",
Attributes: map[string]*AttributeDefinition{
"courseId": {Type: AttributeTypeString, Required: true},
"tenantId": {Type: AttributeTypeString, Required: true},
},
Indexes: map[string]*IndexDefinition{
"primary": {
PK: FacetDefinition{Field: "pk", Facets: []string{"tenantId"}},
SK: &FacetDefinition{Field: "sk", Facets: []string{"courseId"}},
},
},
}
entity, err := NewEntity(schema, nil)
if err != nil {
t.Fatalf("Failed to create entity: %v", err)
}
// An attribute not defined in the schema must be rejected.
query := entity.Query("primary").Query("tenant1").Options(&QueryOptions{
Attributes: []string{"courseId", "notAField"},
})
if _, err := query.Params(); err == nil {
t.Fatal("Expected Params() to reject an unknown projection attribute")
}
}