Skip to content

Commit d931626

Browse files
authored
fix(firestore): Move createIndexes calls (#9714)
1 parent d486481 commit d931626

1 file changed

Lines changed: 84 additions & 74 deletions

File tree

firestore/integration_test.go

Lines changed: 84 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ func initIntegrationTest() {
133133
iClient = c
134134
iColl = c.Collection(collectionIDs.New())
135135

136-
adminC, err := apiv1.NewFirestoreAdminClient(ctx, option.WithTokenSource(ts))
136+
adminCtx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
137+
defer cancel()
138+
adminC, err := apiv1.NewFirestoreAdminClient(adminCtx, option.WithTokenSource(ts))
137139
if err != nil {
138140
log.Fatalf("NewFirestoreAdminClient: %v", err)
139141
}
140142
iAdminClient = adminC
141143

142-
createIndexes(ctx, wantDBPath)
143-
144144
refDoc := iColl.NewDoc()
145145
integrationTestMap["ref"] = refDoc
146146
wantIntegrationTestMap["ref"] = refDoc
@@ -151,13 +151,7 @@ func initIntegrationTest() {
151151
// Indexes are required to run queries with composite filters on multiple fields.
152152
// Without indexes, FailedPrecondition rpc error is seen with
153153
// desc 'The query requires multiple indexes'.
154-
func createIndexes(ctx context.Context, dbPath string) {
155-
156-
indexFields := [][]string{
157-
{"updatedAt", "weight", "height"},
158-
{"weight", "height"},
159-
{"width", "depth"},
160-
{"width", "model"}}
154+
func createIndexes(ctx context.Context, dbPath string, indexFields [][]string) {
161155
indexNames = make([]string, len(indexFields))
162156
indexParent := fmt.Sprintf("%s/collectionGroups/%s", dbPath, iColl.ID)
163157

@@ -198,7 +192,7 @@ func handleCreateIndexResp(ctx context.Context, wg *sync.WaitGroup, i int, op *a
198192
defer wg.Done()
199193
createdIndex, waitErr := op.Wait(ctx)
200194
if waitErr != nil {
201-
log.Fatalf("Wait: %v", waitErr)
195+
log.Fatalf("CreateIndexes failed. Wait: %v", waitErr)
202196
}
203197
indexNames[i] = createdIndex.Name
204198
}
@@ -300,8 +294,11 @@ func deleteDocument(ctx context.Context, docRef *DocumentRef, bulkwriter *BulkWr
300294

301295
func cleanupIntegrationTest() {
302296
if iClient != nil {
297+
adminCtx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
298+
defer cancel()
299+
deleteIndexes(adminCtx)
300+
303301
ctx := context.Background()
304-
deleteIndexes(ctx)
305302
deleteCollection(ctx, iColl)
306303
iClient.Close()
307304
}
@@ -871,6 +868,14 @@ func TestIntegration_WriteBatch(t *testing.T) {
871868
func TestIntegration_QueryDocuments_WhereEntity(t *testing.T) {
872869
ctx := context.Background()
873870
coll := integrationColl(t)
871+
872+
indexFields := [][]string{
873+
{"updatedAt", "weight", "height"},
874+
{"weight", "height"}}
875+
adminCtx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
876+
defer cancel()
877+
createIndexes(adminCtx, wantDBPath, indexFields)
878+
874879
h := testHelper{t}
875880
nowTime := time.Now()
876881
todayTime := nowTime.Unix()
@@ -1096,8 +1101,6 @@ func TestIntegration_QueryDocuments(t *testing.T) {
10961101
continue
10971102
}
10981103

1099-
fmt.Printf("test.want: %+v\n", test.want)
1100-
11011104
docsEqual := true
11021105
docsNotEqualErr := ""
11031106
for j, g := range gotDocs {
@@ -2458,34 +2461,41 @@ func TestIntegration_AggregationQueries(t *testing.T) {
24582461
ctx := context.Background()
24592462
coll := integrationColl(t)
24602463
client := integrationClient(t)
2464+
2465+
indexFields := [][]string{
2466+
{"weight", "model"}}
2467+
adminCtx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
2468+
defer cancel()
2469+
createIndexes(adminCtx, wantDBPath, indexFields)
2470+
24612471
h := testHelper{t}
24622472
docs := []map[string]interface{}{
2463-
{"width": 1.5, "depth": 99, "model": "A"},
2464-
{"width": 2.6, "depth": 98, "model": "A"},
2465-
{"width": 3.7, "depth": 97, "model": "B"},
2466-
{"width": 4.8, "depth": 96, "model": "B"},
2467-
{"width": 5.9, "depth": 95, "model": "C"},
2468-
{"width": 6.0, "depth": 94, "model": "B"},
2469-
{"width": 7.1, "depth": 93, "model": "C"},
2470-
{"width": 8.2, "depth": 93, "model": "A"},
2473+
{"weight": 1.5, "height": 99, "model": "A"},
2474+
{"weight": 2.6, "height": 98, "model": "A"},
2475+
{"weight": 3.7, "height": 97, "model": "B"},
2476+
{"weight": 4.8, "height": 96, "model": "B"},
2477+
{"weight": 5.9, "height": 95, "model": "C"},
2478+
{"weight": 6.0, "height": 94, "model": "B"},
2479+
{"weight": 7.1, "height": 93, "model": "C"},
2480+
{"weight": 8.2, "height": 93, "model": "A"},
24712481
}
24722482
for _, doc := range docs {
24732483
newDoc := coll.NewDoc()
24742484
h.mustCreate(newDoc, doc)
24752485
}
24762486

2477-
query := coll.Where("width", ">=", 1)
2487+
query := coll.Where("weight", ">=", 1)
24782488

2479-
limitQuery := coll.Where("width", ">=", 1).Limit(4)
2480-
limitToLastQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).LimitToLast(4)
2489+
limitQuery := coll.Where("weight", ">=", 1).Limit(4)
2490+
limitToLastQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).LimitToLast(4)
24812491

2482-
startAtQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).StartAt(3.7)
2483-
startAfterQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).StartAfter(3.7)
2492+
startAtQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).StartAt(3.7)
2493+
startAfterQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).StartAfter(3.7)
24842494

2485-
endAtQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).EndAt(7.1)
2486-
endBeforeQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).EndBefore(7.1)
2495+
endAtQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).EndAt(7.1)
2496+
endBeforeQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).EndBefore(7.1)
24872497

2488-
emptyResultsQuery := coll.Where("width", "<", 1)
2498+
emptyResultsQuery := coll.Where("weight", "<", 1)
24892499
emptyResultsQueryPtr := &emptyResultsQuery
24902500

24912501
testcases := []struct {
@@ -2497,129 +2507,129 @@ func TestIntegration_AggregationQueries(t *testing.T) {
24972507
}{
24982508
{
24992509
desc: "Multiple aggregations",
2500-
aggregationQuery: query.NewAggregationQuery().WithCount("count1").WithAvg("width", "width_avg1").WithAvg("depth", "depth_avg1").WithSum("width", "width_sum1").WithSum("depth", "depth_sum1"),
2510+
aggregationQuery: query.NewAggregationQuery().WithCount("count1").WithAvg("weight", "weight_avg1").WithAvg("height", "height_avg1").WithSum("weight", "weight_sum1").WithSum("height", "height_sum1"),
25012511
wantErr: false,
25022512
result: map[string]interface{}{
2503-
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(8)}},
2504-
"width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}},
2505-
"depth_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(765)}},
2506-
"width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}},
2507-
"depth_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(95.625)}},
2513+
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(8)}},
2514+
"weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}},
2515+
"height_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(765)}},
2516+
"weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}},
2517+
"height_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(95.625)}},
25082518
},
25092519
},
25102520
{
25112521
desc: "Aggregations in transaction",
2512-
aggregationQuery: query.NewAggregationQuery().WithCount("count1").WithAvg("width", "width_avg1").WithAvg("depth", "depth_avg1").WithSum("width", "width_sum1").WithSum("depth", "depth_sum1"),
2522+
aggregationQuery: query.NewAggregationQuery().WithCount("count1").WithAvg("weight", "weight_avg1").WithAvg("height", "height_avg1").WithSum("weight", "weight_sum1").WithSum("height", "height_sum1"),
25132523
wantErr: false,
25142524
runInTransaction: true,
25152525
result: map[string]interface{}{
2516-
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(8)}},
2517-
"width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}},
2518-
"depth_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(765)}},
2519-
"width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}},
2520-
"depth_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(95.625)}},
2526+
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(8)}},
2527+
"weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}},
2528+
"height_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(765)}},
2529+
"weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}},
2530+
"height_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(95.625)}},
25212531
},
25222532
},
25232533
{
25242534
desc: "WithSum aggregation without alias",
2525-
aggregationQuery: query.NewAggregationQuery().WithSum("width", ""),
2535+
aggregationQuery: query.NewAggregationQuery().WithSum("weight", ""),
25262536
wantErr: false,
25272537
result: map[string]interface{}{
25282538
"field_1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}},
25292539
},
25302540
},
25312541
{
25322542
desc: "WithSumPath aggregation without alias",
2533-
aggregationQuery: query.NewAggregationQuery().WithSumPath([]string{"width"}, ""),
2543+
aggregationQuery: query.NewAggregationQuery().WithSumPath([]string{"weight"}, ""),
25342544
wantErr: false,
25352545
result: map[string]interface{}{
25362546
"field_1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}},
25372547
},
25382548
},
25392549
{
25402550
desc: "WithAvg aggregation without alias",
2541-
aggregationQuery: query.NewAggregationQuery().WithAvg("width", ""),
2551+
aggregationQuery: query.NewAggregationQuery().WithAvg("weight", ""),
25422552
wantErr: false,
25432553
result: map[string]interface{}{
25442554
"field_1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}},
25452555
},
25462556
},
25472557
{
25482558
desc: "WithAvgPath aggregation without alias",
2549-
aggregationQuery: query.NewAggregationQuery().WithAvgPath([]string{"width"}, ""),
2559+
aggregationQuery: query.NewAggregationQuery().WithAvgPath([]string{"weight"}, ""),
25502560
wantErr: false,
25512561
result: map[string]interface{}{
25522562
"field_1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}},
25532563
},
25542564
},
25552565
{
25562566
desc: "Aggregations with limit",
2557-
aggregationQuery: (&limitQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"),
2567+
aggregationQuery: (&limitQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"),
25582568
wantErr: false,
25592569
result: map[string]interface{}{
2560-
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(4)}},
2561-
"width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(12.6)}},
2562-
"width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(3.15)}},
2570+
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(4)}},
2571+
"weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(12.6)}},
2572+
"weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(3.15)}},
25632573
},
25642574
},
25652575
{
25662576
desc: "Aggregations with StartAt",
2567-
aggregationQuery: (&startAtQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"),
2577+
aggregationQuery: (&startAtQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"),
25682578
wantErr: false,
25692579
result: map[string]interface{}{
2570-
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(6)}},
2571-
"width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(35.7)}},
2572-
"width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(5.95)}},
2580+
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(6)}},
2581+
"weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(35.7)}},
2582+
"weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(5.95)}},
25732583
},
25742584
},
25752585
{
25762586
desc: "Aggregations with StartAfter",
2577-
aggregationQuery: (&startAfterQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"),
2587+
aggregationQuery: (&startAfterQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"),
25782588
wantErr: false,
25792589
result: map[string]interface{}{
2580-
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(5)}},
2581-
"width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(32)}},
2582-
"width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(6.4)}},
2590+
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(5)}},
2591+
"weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(32)}},
2592+
"weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(6.4)}},
25832593
},
25842594
},
25852595
{
25862596
desc: "Aggregations with EndAt",
2587-
aggregationQuery: (&endAtQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"),
2597+
aggregationQuery: (&endAtQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"),
25882598
wantErr: false,
25892599
result: map[string]interface{}{
2590-
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(6)}},
2591-
"width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(30.1)}},
2592-
"width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(5.016666666666667)}},
2600+
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(6)}},
2601+
"weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(30.1)}},
2602+
"weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(5.016666666666667)}},
25932603
},
25942604
},
25952605
{
25962606
desc: "Aggregations with EndBefore",
2597-
aggregationQuery: (&endBeforeQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"),
2607+
aggregationQuery: (&endBeforeQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"),
25982608
wantErr: false,
25992609
result: map[string]interface{}{
2600-
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(5)}},
2601-
"width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(23)}},
2602-
"width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.6)}},
2610+
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(5)}},
2611+
"weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(23)}},
2612+
"weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.6)}},
26032613
},
26042614
},
26052615
{
26062616
desc: "Aggregations with LimitToLast",
2607-
aggregationQuery: (&limitToLastQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"),
2617+
aggregationQuery: (&limitToLastQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"),
26082618
wantErr: false,
26092619
result: map[string]interface{}{
2610-
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(4)}},
2611-
"width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(27.2)}},
2612-
"width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(6.8)}},
2620+
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(4)}},
2621+
"weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(27.2)}},
2622+
"weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(6.8)}},
26132623
},
26142624
},
26152625
{
26162626
desc: "Aggregations on empty results",
2617-
aggregationQuery: emptyResultsQueryPtr.NewAggregationQuery().WithCount("count1").WithAvg("width", "width_avg1").WithSum("width", "width_sum1"),
2627+
aggregationQuery: emptyResultsQueryPtr.NewAggregationQuery().WithCount("count1").WithAvg("weight", "weight_avg1").WithSum("weight", "weight_sum1"),
26182628
wantErr: false,
26192629
result: map[string]interface{}{
2620-
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(0)}},
2621-
"width_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(0)}},
2622-
"width_avg1": &pb.Value{ValueType: &pb.Value_NullValue{NullValue: structpb.NullValue_NULL_VALUE}},
2630+
"count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(0)}},
2631+
"weight_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(0)}},
2632+
"weight_avg1": &pb.Value{ValueType: &pb.Value_NullValue{NullValue: structpb.NullValue_NULL_VALUE}},
26232633
},
26242634
},
26252635
{

0 commit comments

Comments
 (0)