| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package awsdynamodb |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 26 |
dyn "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 27 |
dyn2Types "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 28 |
"github.com/google/go-cmp/cmp" |
| 29 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 30 |
"gocloud.dev/docstore/driver" |
| 31 |
"gocloud.dev/docstore/drivertest" |
| 32 |
) |
| 33 |
|
| 34 |
func TestPlanQuery(t *testing.T) { |
| 35 |
c := &collection{ |
| 36 |
table: "T", |
| 37 |
partitionKey: "tableP", |
| 38 |
description: &dyn2Types.TableDescription{}, |
| 39 |
opts: &Options{AllowScans: true, RevisionField: "rev"}, |
| 40 |
} |
| 41 |
|
| 42 |
// Build an ExpressionAttributeNames map with the given names. |
| 43 |
eans := func(names ...string) map[string]string { |
| 44 |
m := map[string]string{} |
| 45 |
for i, n := range names { |
| 46 |
m[fmt.Sprintf("#%d", i)] = n |
| 47 |
} |
| 48 |
return m |
| 49 |
} |
| 50 |
|
| 51 |
// Build an ExpressionAttributeValues map. Filter values are always the number 1 |
| 52 |
// and the keys are always :0, :1, ..., so we only need to know how many entries. |
| 53 |
eavs := func(n int) map[string]dyn2Types.AttributeValue { |
| 54 |
if n == 0 { |
| 55 |
return nil |
| 56 |
} |
| 57 |
one := &dyn2Types.AttributeValueMemberN{Value: "1"} |
| 58 |
m := map[string]dyn2Types.AttributeValue{} |
| 59 |
for i := range n { |
| 60 |
m[fmt.Sprintf(":%d", i)] = one |
| 61 |
} |
| 62 |
return m |
| 63 |
} |
| 64 |
|
| 65 |
// Ignores the ConsistentRead field from both QueryInput and ScanInput. |
| 66 |
opts := []cmp.Option{ |
| 67 |
cmpopts.IgnoreFields(dyn.ScanInput{}, "ConsistentRead"), |
| 68 |
cmpopts.IgnoreFields(dyn.QueryInput{}, "ConsistentRead"), |
| 69 |
cmpopts.IgnoreUnexported(dyn.ScanInput{}), |
| 70 |
cmpopts.IgnoreUnexported(dyn.QueryInput{}), |
| 71 |
compareIgnoreAttributeUnexported, |
| 72 |
} |
| 73 |
|
| 74 |
for _, test := range []struct { |
| 75 |
desc string |
| 76 |
// In all cases, the table has a partition key called "tableP". |
| 77 |
tableSortKey string // if non-empty, the table sort key |
| 78 |
localIndexSortKey string // if non-empty, there is a local index with this sort key |
| 79 |
localIndexFields []string // the fields projected into the local index |
| 80 |
globalIndexPartitionKey string // if non-empty, there is a global index with this partition key |
| 81 |
globalIndexSortKey string // if non-empty, the global index has this sort key |
| 82 |
globalIndexFields []string // the fields projected into the global index |
| 83 |
query *driver.Query |
| 84 |
want any // either a ScanInput or a QueryInput |
| 85 |
wantPlan string |
| 86 |
}{ |
| 87 |
{ |
| 88 |
desc: "empty query", |
| 89 |
// A query with no filters requires a scan. |
| 90 |
query: &driver.Query{}, |
| 91 |
want: &dyn.ScanInput{TableName: &c.table}, |
| 92 |
wantPlan: "Scan", |
| 93 |
}, |
| 94 |
{ |
| 95 |
desc: "equality filter on table partition field", |
| 96 |
// A filter that compares the table's partition key for equality is the minimum |
| 97 |
// requirement for querying the table. |
| 98 |
query: &driver.Query{Filters: []driver.Filter{{[]string{"tableP"}, "=", 1}}}, |
| 99 |
want: &dyn.QueryInput{ |
| 100 |
KeyConditionExpression: aws.String("#0 = :0"), |
| 101 |
ExpressionAttributeNames: eans("tableP"), |
| 102 |
ExpressionAttributeValues: eavs(1), |
| 103 |
}, |
| 104 |
wantPlan: "Table", |
| 105 |
}, |
| 106 |
{ |
| 107 |
desc: "equality filter on table partition field (sort key)", |
| 108 |
// Same as above, but the table has a sort key; shouldn't make a difference. |
| 109 |
tableSortKey: "tableS", |
| 110 |
query: &driver.Query{Filters: []driver.Filter{{[]string{"tableP"}, "=", 1}}}, |
| 111 |
want: &dyn.QueryInput{ |
| 112 |
KeyConditionExpression: aws.String("#0 = :0"), |
| 113 |
ExpressionAttributeNames: eans("tableP"), |
| 114 |
ExpressionAttributeValues: eavs(1), |
| 115 |
}, |
| 116 |
wantPlan: "Table", |
| 117 |
}, |
| 118 |
{ |
| 119 |
desc: "equality filter on other field", |
| 120 |
// This query has an equality filter, but not on the table's partition key. |
| 121 |
// Since there are no matching indexes, we must scan. |
| 122 |
query: &driver.Query{Filters: []driver.Filter{{[]string{"other"}, "=", 1}}}, |
| 123 |
want: &dyn.ScanInput{ |
| 124 |
FilterExpression: aws.String("#0 = :0"), |
| 125 |
ExpressionAttributeNames: eans("other"), |
| 126 |
ExpressionAttributeValues: eavs(1), |
| 127 |
}, |
| 128 |
wantPlan: "Scan", |
| 129 |
}, |
| 130 |
{ |
| 131 |
desc: "non-equality filter on table partition field", |
| 132 |
// If the query doesn't have an equality filter on the partition key, and there |
| 133 |
// are no indexes, we must scan. The filter becomes a FilterExpression, evaluated |
| 134 |
// on the backend. |
| 135 |
query: &driver.Query{Filters: []driver.Filter{{[]string{"tableP"}, ">", 1}}}, |
| 136 |
want: &dyn.ScanInput{ |
| 137 |
FilterExpression: aws.String("#0 > :0"), |
| 138 |
ExpressionAttributeNames: eans("tableP"), |
| 139 |
ExpressionAttributeValues: eavs(1), |
| 140 |
}, |
| 141 |
wantPlan: "Scan", |
| 142 |
}, |
| 143 |
{ |
| 144 |
desc: "equality filter on partition, filter on other", |
| 145 |
// The equality filter on the table's partition key lets us query the table. |
| 146 |
// The other filter is used in the filter expression. |
| 147 |
query: &driver.Query{Filters: []driver.Filter{ |
| 148 |
{[]string{"tableP"}, "=", 1}, |
| 149 |
{[]string{"other"}, "<=", 1}, |
| 150 |
}}, |
| 151 |
want: &dyn.QueryInput{ |
| 152 |
KeyConditionExpression: aws.String("#1 = :1"), |
| 153 |
FilterExpression: aws.String("#0 <= :0"), |
| 154 |
ExpressionAttributeNames: eans("other", "tableP"), |
| 155 |
ExpressionAttributeValues: eavs(2), |
| 156 |
}, |
| 157 |
wantPlan: "Table", |
| 158 |
}, |
| 159 |
{ |
| 160 |
desc: "equality filter on partition, filter on sort", |
| 161 |
// If the table has a sort key and the query has a filter on it as well |
| 162 |
// as an equality filter on the table's partition key, we can query the |
| 163 |
// table. |
| 164 |
tableSortKey: "tableS", |
| 165 |
query: &driver.Query{Filters: []driver.Filter{ |
| 166 |
{[]string{"tableP"}, "=", 1}, |
| 167 |
{[]string{"tableS"}, "<=", 1}, |
| 168 |
}}, |
| 169 |
want: &dyn.QueryInput{ |
| 170 |
KeyConditionExpression: aws.String("(#0 = :0) AND (#1 <= :1)"), |
| 171 |
ExpressionAttributeNames: eans("tableP", "tableS"), |
| 172 |
ExpressionAttributeValues: eavs(2), |
| 173 |
}, |
| 174 |
wantPlan: "Table", |
| 175 |
}, |
| 176 |
{ |
| 177 |
desc: "equality filter on table partition, filter on local index sort", |
| 178 |
// The equality filter on the table's partition key allows us to query |
| 179 |
// the table, but there is a better choice: a local index with a sort key |
| 180 |
// that is mentioned in the query. |
| 181 |
localIndexSortKey: "localS", |
| 182 |
query: &driver.Query{Filters: []driver.Filter{ |
| 183 |
{[]string{"tableP"}, "=", 1}, |
| 184 |
{[]string{"localS"}, "<=", 1}, |
| 185 |
}}, |
| 186 |
want: &dyn.QueryInput{ |
| 187 |
IndexName: aws.String("local"), |
| 188 |
KeyConditionExpression: aws.String("(#0 = :0) AND (#1 <= :1)"), |
| 189 |
ExpressionAttributeNames: eans("tableP", "localS"), |
| 190 |
}, |
| 191 |
wantPlan: `Index: "local"`, |
| 192 |
}, |
| 193 |
{ |
| 194 |
desc: "equality filter on table partition, filter on local index sort, bad projection", |
| 195 |
// The equality filter on the table's partition key allows us to query |
| 196 |
// the table. There seems to be a better choice: a local index with a sort key |
| 197 |
// that is mentioned in the query. But the query wants the entire document, |
| 198 |
// and the local index only has some fields. |
| 199 |
localIndexSortKey: "localS", |
| 200 |
localIndexFields: []string{}, // keys only |
| 201 |
query: &driver.Query{Filters: []driver.Filter{ |
| 202 |
{[]string{"tableP"}, "=", 1}, |
| 203 |
{[]string{"localS"}, "<=", 1}, |
| 204 |
}}, |
| 205 |
want: &dyn.QueryInput{ |
| 206 |
KeyConditionExpression: aws.String("#1 = :1"), |
| 207 |
FilterExpression: aws.String("#0 <= :0"), |
| 208 |
ExpressionAttributeNames: eans("localS", "tableP"), |
| 209 |
}, |
| 210 |
wantPlan: "Table", |
| 211 |
}, |
| 212 |
{ |
| 213 |
desc: "equality filter on table partition, filter on local index sort, good projection", |
| 214 |
// Same as above, but now the query no longer asks for all fields, so |
| 215 |
// we will only read the requested fields from the table. |
| 216 |
localIndexSortKey: "localS", |
| 217 |
localIndexFields: []string{}, // keys only |
| 218 |
query: &driver.Query{ |
| 219 |
FieldPaths: [][]string{{"tableP"}, {"localS"}}, |
| 220 |
Filters: []driver.Filter{ |
| 221 |
{[]string{"tableP"}, "=", 1}, |
| 222 |
{[]string{"localS"}, "<=", 1}, |
| 223 |
}, |
| 224 |
}, |
| 225 |
want: &dyn.QueryInput{ |
| 226 |
IndexName: aws.String("local"), |
| 227 |
KeyConditionExpression: aws.String("(#0 = :0) AND (#1 <= :1)"), |
| 228 |
ExpressionAttributeNames: eans("tableP", "localS"), |
| 229 |
ExpressionAttributeValues: eavs(2), |
| 230 |
ProjectionExpression: aws.String("#0, #1"), |
| 231 |
}, |
| 232 |
wantPlan: `Index: "local"`, |
| 233 |
}, |
| 234 |
{ |
| 235 |
desc: "equality filter on table partition, filters on local index and table sort", |
| 236 |
// Given the choice of querying the table or a local index, prefer the table. |
| 237 |
tableSortKey: "tableS", |
| 238 |
localIndexSortKey: "localS", |
| 239 |
query: &driver.Query{Filters: []driver.Filter{ |
| 240 |
{[]string{"tableP"}, "=", 1}, |
| 241 |
{[]string{"localS"}, "<=", 1}, |
| 242 |
{[]string{"tableS"}, ">", 1}, |
| 243 |
}}, |
| 244 |
want: &dyn.QueryInput{ |
| 245 |
IndexName: nil, |
| 246 |
KeyConditionExpression: aws.String("(#1 = :1) AND (#2 > :2)"), |
| 247 |
FilterExpression: aws.String("#0 <= :0"), |
| 248 |
ExpressionAttributeNames: eans("localS", "tableP", "tableS"), |
| 249 |
}, |
| 250 |
wantPlan: "Table", |
| 251 |
}, |
| 252 |
{ |
| 253 |
desc: "equality filter on other field with index", |
| 254 |
// The query is the same as in "equality filter on other field," but now there |
| 255 |
// is a global index with that field as partition key, so we can query it. |
| 256 |
globalIndexPartitionKey: "other", |
| 257 |
query: &driver.Query{Filters: []driver.Filter{{[]string{"other"}, "=", 1}}}, |
| 258 |
want: &dyn.QueryInput{ |
| 259 |
IndexName: aws.String("global"), |
| 260 |
KeyConditionExpression: aws.String("#0 = :0"), |
| 261 |
ExpressionAttributeNames: eans("other"), |
| 262 |
}, |
| 263 |
wantPlan: `Index: "global"`, |
| 264 |
}, |
| 265 |
{ |
| 266 |
desc: "equality filter on table partition, filter on global index sort", |
| 267 |
// The equality filter on the table's partition key allows us to query |
| 268 |
// the table, but there is a better choice: a global index with the same |
| 269 |
// partition key and a sort key that is mentioned in the query. |
| 270 |
// (In these tests, the global index has all the fields of the table by default.) |
| 271 |
globalIndexPartitionKey: "tableP", |
| 272 |
globalIndexSortKey: "globalS", |
| 273 |
query: &driver.Query{Filters: []driver.Filter{ |
| 274 |
{[]string{"tableP"}, "=", 1}, |
| 275 |
{[]string{"globalS"}, "<=", 1}, |
| 276 |
}}, |
| 277 |
want: &dyn.QueryInput{ |
| 278 |
IndexName: aws.String("global"), |
| 279 |
KeyConditionExpression: aws.String("(#0 = :0) AND (#1 <= :1)"), |
| 280 |
ExpressionAttributeNames: eans("tableP", "globalS"), |
| 281 |
}, |
| 282 |
wantPlan: `Index: "global"`, |
| 283 |
}, |
| 284 |
{ |
| 285 |
desc: "equality filter on table partition, filter on global index sort, bad projection", |
| 286 |
// Although there is a global index that matches the filters best, it doesn't |
| 287 |
// have the necessary fields. So we query against the table. |
| 288 |
// The query does not specify FilterPaths, so it retrieves the entire document. |
| 289 |
// globalIndexFields explicitly lists the fields that the global index has. |
| 290 |
// Since the global index does not have all the document fields, it can't be used. |
| 291 |
globalIndexPartitionKey: "tableP", |
| 292 |
globalIndexSortKey: "globalS", |
| 293 |
globalIndexFields: []string{"other"}, |
| 294 |
query: &driver.Query{Filters: []driver.Filter{ |
| 295 |
{[]string{"tableP"}, "=", 1}, |
| 296 |
{[]string{"globalS"}, "<=", 1}, |
| 297 |
}}, |
| 298 |
want: &dyn.QueryInput{ |
| 299 |
IndexName: nil, |
| 300 |
KeyConditionExpression: aws.String("#1 = :1"), |
| 301 |
FilterExpression: aws.String("#0 <= :0"), |
| 302 |
ExpressionAttributeNames: eans("globalS", "tableP"), |
| 303 |
}, |
| 304 |
wantPlan: "Table", |
| 305 |
}, |
| 306 |
{ |
| 307 |
desc: "equality filter on table partition, filter on global index sort, good projection", |
| 308 |
// The global index matches the filters best and has the necessary |
| 309 |
// fields. So we query against it. |
| 310 |
globalIndexPartitionKey: "tableP", |
| 311 |
globalIndexSortKey: "globalS", |
| 312 |
globalIndexFields: []string{"other", "rev"}, |
| 313 |
query: &driver.Query{ |
| 314 |
FieldPaths: [][]string{{"other"}}, |
| 315 |
Filters: []driver.Filter{ |
| 316 |
{[]string{"tableP"}, "=", 1}, |
| 317 |
{[]string{"globalS"}, "<=", 1}, |
| 318 |
}, |
| 319 |
}, |
| 320 |
want: &dyn.QueryInput{ |
| 321 |
IndexName: aws.String("global"), |
| 322 |
KeyConditionExpression: aws.String("(#0 = :0) AND (#1 <= :1)"), |
| 323 |
ProjectionExpression: aws.String("#2, #0"), |
| 324 |
ExpressionAttributeNames: eans("tableP", "globalS", "other"), |
| 325 |
ExpressionAttributeValues: eavs(2), |
| 326 |
}, |
| 327 |
wantPlan: `Index: "global"`, |
| 328 |
}, |
| 329 |
} { |
| 330 |
t.Run(test.desc, func(t *testing.T) { |
| 331 |
c.sortKey = test.tableSortKey |
| 332 |
if test.localIndexSortKey == "" { |
| 333 |
c.description.LocalSecondaryIndexes = nil |
| 334 |
} else { |
| 335 |
c.description.LocalSecondaryIndexes = []dyn2Types.LocalSecondaryIndexDescription{ |
| 336 |
{ |
| 337 |
IndexName: aws.String("local"), |
| 338 |
KeySchema: keySchema("tableP", test.localIndexSortKey), |
| 339 |
Projection: indexProjection(test.localIndexFields), |
| 340 |
}, |
| 341 |
} |
| 342 |
} |
| 343 |
if test.globalIndexPartitionKey == "" { |
| 344 |
c.description.GlobalSecondaryIndexes = nil |
| 345 |
} else { |
| 346 |
c.description.GlobalSecondaryIndexes = []dyn2Types.GlobalSecondaryIndexDescription{ |
| 347 |
{ |
| 348 |
IndexName: aws.String("global"), |
| 349 |
KeySchema: keySchema(test.globalIndexPartitionKey, test.globalIndexSortKey), |
| 350 |
Projection: indexProjection(test.globalIndexFields), |
| 351 |
}, |
| 352 |
} |
| 353 |
} |
| 354 |
gotRunner, err := c.planQuery(test.query) |
| 355 |
if err != nil { |
| 356 |
t.Fatal(err) |
| 357 |
} |
| 358 |
var got any |
| 359 |
switch tw := test.want.(type) { |
| 360 |
case *dyn.ScanInput: |
| 361 |
got = gotRunner.scanIn |
| 362 |
tw.TableName = &c.table |
| 363 |
if tw.ExpressionAttributeValues == nil { |
| 364 |
tw.ExpressionAttributeValues = eavs(len(tw.ExpressionAttributeNames)) |
| 365 |
} |
| 366 |
case *dyn.QueryInput: |
| 367 |
got = gotRunner.queryIn |
| 368 |
tw.TableName = &c.table |
| 369 |
if tw.ExpressionAttributeValues == nil { |
| 370 |
tw.ExpressionAttributeValues = eavs(len(tw.ExpressionAttributeNames)) |
| 371 |
} |
| 372 |
default: |
| 373 |
t.Fatalf("bad type for test.want: %T", test.want) |
| 374 |
} |
| 375 |
if diff := cmp.Diff(got, test.want, opts...); diff != "" { |
| 376 |
t.Error("input:\n", diff) |
| 377 |
} |
| 378 |
gotPlan := gotRunner.queryPlan() |
| 379 |
if diff := cmp.Diff(gotPlan, test.wantPlan); diff != "" { |
| 380 |
t.Error("plan:\n", diff) |
| 381 |
} |
| 382 |
}) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
func TestQueryNoScans(t *testing.T) { |
| 387 |
c := &collection{ |
| 388 |
table: "T", |
| 389 |
partitionKey: "tableP", |
| 390 |
description: &dyn2Types.TableDescription{}, |
| 391 |
opts: &Options{AllowScans: false}, |
| 392 |
} |
| 393 |
|
| 394 |
for _, test := range []struct { |
| 395 |
q *driver.Query |
| 396 |
wantErr bool |
| 397 |
}{ |
| 398 |
{&driver.Query{}, false}, |
| 399 |
{&driver.Query{Filters: []driver.Filter{{[]string{"other"}, "=", 1}}}, true}, |
| 400 |
} { |
| 401 |
qr, err := c.planQuery(test.q) |
| 402 |
if err != nil { |
| 403 |
t.Fatalf("%v: %v", test.q, err) |
| 404 |
} |
| 405 |
err = c.checkPlan(qr) |
| 406 |
if test.wantErr { |
| 407 |
if err == nil || !strings.Contains(err.Error(), "AllowScans") { |
| 408 |
t.Errorf("%v: got %v, want an error that mentions the AllowScans option", test.q, err) |
| 409 |
} |
| 410 |
} else if err != nil { |
| 411 |
t.Errorf("%v: got %v, want nil", test.q, err) |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
// Make a key schema from the names of the partition and sort keys. |
| 417 |
func keySchema(pkey, skey string) []dyn2Types.KeySchemaElement { |
| 418 |
return []dyn2Types.KeySchemaElement{ |
| 419 |
{AttributeName: &pkey, KeyType: dyn2Types.KeyTypeHash}, |
| 420 |
{AttributeName: &skey, KeyType: dyn2Types.KeyTypeRange}, |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
func indexProjection(fields []string) *dyn2Types.Projection { |
| 425 |
var ptype dyn2Types.ProjectionType |
| 426 |
switch { |
| 427 |
case fields == nil: |
| 428 |
ptype = dyn2Types.ProjectionTypeAll |
| 429 |
case len(fields) == 0: |
| 430 |
ptype = dyn2Types.ProjectionTypeKeysOnly |
| 431 |
default: |
| 432 |
ptype = dyn2Types.ProjectionTypeInclude |
| 433 |
} |
| 434 |
proj := &dyn2Types.Projection{ProjectionType: ptype} |
| 435 |
for _, f := range fields { |
| 436 |
proj.NonKeyAttributes = append(proj.NonKeyAttributes, f) |
| 437 |
} |
| 438 |
return proj |
| 439 |
} |
| 440 |
|
| 441 |
func TestGlobalFieldsIncluded(t *testing.T) { |
| 442 |
c := &collection{partitionKey: "tableP", sortKey: "tableS"} |
| 443 |
gi := dyn2Types.GlobalSecondaryIndexDescription{ |
| 444 |
KeySchema: keySchema("globalP", "globalS"), |
| 445 |
} |
| 446 |
for _, test := range []struct { |
| 447 |
desc string |
| 448 |
queryFields []string |
| 449 |
wantKeysOnly bool // when the projection includes only table and index keys |
| 450 |
wantInclude bool // when the projection includes fields "f" and "g". |
| 451 |
}{ |
| 452 |
{ |
| 453 |
desc: "all", |
| 454 |
queryFields: nil, |
| 455 |
wantKeysOnly: false, |
| 456 |
wantInclude: false, |
| 457 |
}, |
| 458 |
{ |
| 459 |
desc: "key fields", |
| 460 |
queryFields: []string{"tableS", "globalP"}, |
| 461 |
wantKeysOnly: true, |
| 462 |
wantInclude: true, |
| 463 |
}, |
| 464 |
{ |
| 465 |
desc: "included fields", |
| 466 |
queryFields: []string{"f", "g"}, |
| 467 |
wantKeysOnly: false, |
| 468 |
wantInclude: true, |
| 469 |
}, |
| 470 |
{ |
| 471 |
desc: "included and key fields", |
| 472 |
queryFields: []string{"f", "g", "tableP", "globalS"}, |
| 473 |
wantKeysOnly: false, |
| 474 |
wantInclude: true, |
| 475 |
}, |
| 476 |
{ |
| 477 |
desc: "not included field", |
| 478 |
queryFields: []string{"f", "g", "h"}, |
| 479 |
wantKeysOnly: false, |
| 480 |
wantInclude: false, |
| 481 |
}, |
| 482 |
} { |
| 483 |
t.Run(test.desc, func(t *testing.T) { |
| 484 |
var fps [][]string |
| 485 |
for _, qf := range test.queryFields { |
| 486 |
fps = append(fps, strings.Split(qf, ".")) |
| 487 |
} |
| 488 |
q := &driver.Query{FieldPaths: fps} |
| 489 |
for _, p := range []struct { |
| 490 |
name string |
| 491 |
proj *dyn2Types.Projection |
| 492 |
want bool |
| 493 |
}{ |
| 494 |
{"ALL", indexProjection(nil), true}, |
| 495 |
{"KEYS_ONLY", indexProjection([]string{}), test.wantKeysOnly}, |
| 496 |
{"INCLUDE", indexProjection([]string{"f", "g"}), test.wantInclude}, |
| 497 |
} { |
| 498 |
t.Run(p.name, func(t *testing.T) { |
| 499 |
gi.Projection = p.proj |
| 500 |
got := c.globalFieldsIncluded(q, gi) |
| 501 |
if got != p.want { |
| 502 |
t.Errorf("got %t, want %t", got, p.want) |
| 503 |
} |
| 504 |
}) |
| 505 |
} |
| 506 |
}) |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
func TestCompare(t *testing.T) { |
| 511 |
tm := time.Now() |
| 512 |
for _, test := range []struct { |
| 513 |
a, b any |
| 514 |
want int |
| 515 |
}{ |
| 516 |
{1, 1, 0}, |
| 517 |
{1, 2, -1}, |
| 518 |
{2, 1, 1}, |
| 519 |
{1.5, 2, -1}, |
| 520 |
{2.5, 2.1, 1}, |
| 521 |
{3.8, 3.8, 0}, |
| 522 |
{"x", "x", 0}, |
| 523 |
{"x", "xx", -1}, |
| 524 |
{"x", "a", 1}, |
| 525 |
{tm, tm, 0}, |
| 526 |
{tm, tm.Add(1), -1}, |
| 527 |
{tm, tm.Add(-1), 1}, |
| 528 |
{[]byte("x"), []byte("x"), 0}, |
| 529 |
{[]byte("x"), []byte("xx"), -1}, |
| 530 |
{[]byte("x"), []byte("a"), 1}, |
| 531 |
} { |
| 532 |
got := compare(test.a, test.b) |
| 533 |
if got != test.want { |
| 534 |
t.Errorf("compare(%v, %v) = %d, want %d", test.a, test.b, got, test.want) |
| 535 |
} |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
func TestCopyTopLevel(t *testing.T) { |
| 540 |
type E struct{ C int } |
| 541 |
type S struct { |
| 542 |
A int |
| 543 |
B int |
| 544 |
E |
| 545 |
} |
| 546 |
|
| 547 |
s := &S{A: 1, B: 2, E: E{C: 3}} |
| 548 |
m := map[string]any{"A": 1, "B": 2, "C": 3} |
| 549 |
for _, test := range []struct { |
| 550 |
dest, src any |
| 551 |
want any |
| 552 |
}{ |
| 553 |
{ |
| 554 |
dest: map[string]any{}, |
| 555 |
src: m, |
| 556 |
want: m, |
| 557 |
}, |
| 558 |
{ |
| 559 |
dest: &S{}, |
| 560 |
src: s, |
| 561 |
want: s, |
| 562 |
}, |
| 563 |
{ |
| 564 |
dest: map[string]any{}, |
| 565 |
src: s, |
| 566 |
want: m, |
| 567 |
}, |
| 568 |
{ |
| 569 |
dest: &S{}, |
| 570 |
src: m, |
| 571 |
want: s, |
| 572 |
}, |
| 573 |
} { |
| 574 |
dest := drivertest.MustDocument(test.dest) |
| 575 |
src := drivertest.MustDocument(test.src) |
| 576 |
if err := copyTopLevel(dest, src); err != nil { |
| 577 |
t.Fatalf("src=%+v: %v", test.src, err) |
| 578 |
} |
| 579 |
if !cmp.Equal(test.dest, test.want) { |
| 580 |
t.Errorf("src=%+v: got %v, want %v", test.src, test.dest, test.want) |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
func Test_documentIterator_Next(t *testing.T) { |
| 586 |
type fields struct { |
| 587 |
qr *queryRunner |
| 588 |
items []map[string]dyn2Types.AttributeValue |
| 589 |
curr int |
| 590 |
offset int |
| 591 |
limit int |
| 592 |
count int |
| 593 |
last map[string]dyn2Types.AttributeValue |
| 594 |
asFunc func(i any) bool |
| 595 |
} |
| 596 |
type args struct { |
| 597 |
ctx context.Context |
| 598 |
doc driver.Document |
| 599 |
} |
| 600 |
tests := []struct { |
| 601 |
name string |
| 602 |
fields fields |
| 603 |
args args |
| 604 |
wantErr bool |
| 605 |
}{ |
| 606 |
{ |
| 607 |
name: "nextWithNoDecodeError", |
| 608 |
fields: fields{ |
| 609 |
qr: &queryRunner{}, |
| 610 |
items: []map[string]dyn2Types.AttributeValue{ |
| 611 |
{"key": &dyn2Types.AttributeValueMemberM{Value: map[string]dyn2Types.AttributeValue{"key": &dyn2Types.AttributeValueMemberS{Value: "value"}}}}, |
| 612 |
}, |
| 613 |
curr: 0, |
| 614 |
offset: 0, |
| 615 |
limit: 0, |
| 616 |
count: 0, |
| 617 |
last: map[string]dyn2Types.AttributeValue{}, |
| 618 |
}, |
| 619 |
args: args{ |
| 620 |
ctx: context.Background(), |
| 621 |
doc: drivertest.MustDocument(map[string]any{}), |
| 622 |
}, |
| 623 |
wantErr: false, |
| 624 |
}, |
| 625 |
{ |
| 626 |
name: "nextWithDecodeError", |
| 627 |
fields: fields{ |
| 628 |
qr: &queryRunner{}, |
| 629 |
items: []map[string]dyn2Types.AttributeValue{ |
| 630 |
{"key": nil}, // set M to nil to trigger decode error |
| 631 |
}, |
| 632 |
curr: 0, |
| 633 |
offset: 0, |
| 634 |
limit: 0, |
| 635 |
count: 0, |
| 636 |
last: map[string]dyn2Types.AttributeValue{}, |
| 637 |
}, |
| 638 |
args: args{ |
| 639 |
ctx: context.Background(), |
| 640 |
doc: drivertest.MustDocument(map[string]any{}), |
| 641 |
}, |
| 642 |
wantErr: true, |
| 643 |
}, |
| 644 |
{ |
| 645 |
name: "nextWhereCurrIsGreaterThanOrEqualToItemsAndLastIsNotNil", |
| 646 |
fields: fields{ |
| 647 |
qr: &queryRunner{ |
| 648 |
scanIn: &dyn.ScanInput{}, |
| 649 |
// hack to return error from run |
| 650 |
beforeRun: func(asFunc func(i any) bool) error { return errors.New("invalid") }, |
| 651 |
}, |
| 652 |
items: []map[string]dyn2Types.AttributeValue{{"key": &dyn2Types.AttributeValueMemberM{Value: map[string]dyn2Types.AttributeValue{"key": &dyn2Types.AttributeValueMemberS{Value: "value"}}}}}, |
| 653 |
curr: 1, |
| 654 |
offset: 0, |
| 655 |
limit: 0, |
| 656 |
count: 0, |
| 657 |
last: map[string]dyn2Types.AttributeValue{"key": &dyn2Types.AttributeValueMemberS{Value: "value"}}, |
| 658 |
}, |
| 659 |
args: args{ |
| 660 |
ctx: context.Background(), |
| 661 |
doc: drivertest.MustDocument(map[string]any{}), |
| 662 |
}, |
| 663 |
wantErr: true, |
| 664 |
}, |
| 665 |
} |
| 666 |
for i, tt := range tests { |
| 667 |
t.Run(tt.name, func(t *testing.T) { |
| 668 |
it := &documentIterator{ |
| 669 |
qr: tt.fields.qr, |
| 670 |
items: tt.fields.items, |
| 671 |
curr: tt.fields.curr, |
| 672 |
offset: tt.fields.offset, |
| 673 |
limit: tt.fields.limit, |
| 674 |
count: tt.fields.count, |
| 675 |
last: tt.fields.last, |
| 676 |
asFunc: tt.fields.asFunc, |
| 677 |
} |
| 678 |
if err := it.Next(tt.args.ctx, tt.args.doc); (err != nil) != tt.wantErr { |
| 679 |
t.Errorf("documentIterator.Next() test=%v(%v) error = %v, wantErr %v", i+1, tt.name, err, tt.wantErr) |
| 680 |
} |
| 681 |
}) |
| 682 |
} |
| 683 |
} |