From d9316263ca4ad0667d4a0f886a4977b36585b572 Mon Sep 17 00:00:00 2001 From: Baha Aiman Date: Mon, 15 Apr 2024 17:01:39 -0700 Subject: [PATCH] fix(firestore): Move createIndexes calls (#9714) --- firestore/integration_test.go | 158 ++++++++++++++++++---------------- 1 file changed, 84 insertions(+), 74 deletions(-) diff --git a/firestore/integration_test.go b/firestore/integration_test.go index 3cce9eae61d7..476013ad5948 100644 --- a/firestore/integration_test.go +++ b/firestore/integration_test.go @@ -133,14 +133,14 @@ func initIntegrationTest() { iClient = c iColl = c.Collection(collectionIDs.New()) - adminC, err := apiv1.NewFirestoreAdminClient(ctx, option.WithTokenSource(ts)) + adminCtx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) + defer cancel() + adminC, err := apiv1.NewFirestoreAdminClient(adminCtx, option.WithTokenSource(ts)) if err != nil { log.Fatalf("NewFirestoreAdminClient: %v", err) } iAdminClient = adminC - createIndexes(ctx, wantDBPath) - refDoc := iColl.NewDoc() integrationTestMap["ref"] = refDoc wantIntegrationTestMap["ref"] = refDoc @@ -151,13 +151,7 @@ func initIntegrationTest() { // Indexes are required to run queries with composite filters on multiple fields. // Without indexes, FailedPrecondition rpc error is seen with // desc 'The query requires multiple indexes'. -func createIndexes(ctx context.Context, dbPath string) { - - indexFields := [][]string{ - {"updatedAt", "weight", "height"}, - {"weight", "height"}, - {"width", "depth"}, - {"width", "model"}} +func createIndexes(ctx context.Context, dbPath string, indexFields [][]string) { indexNames = make([]string, len(indexFields)) indexParent := fmt.Sprintf("%s/collectionGroups/%s", dbPath, iColl.ID) @@ -198,7 +192,7 @@ func handleCreateIndexResp(ctx context.Context, wg *sync.WaitGroup, i int, op *a defer wg.Done() createdIndex, waitErr := op.Wait(ctx) if waitErr != nil { - log.Fatalf("Wait: %v", waitErr) + log.Fatalf("CreateIndexes failed. Wait: %v", waitErr) } indexNames[i] = createdIndex.Name } @@ -300,8 +294,11 @@ func deleteDocument(ctx context.Context, docRef *DocumentRef, bulkwriter *BulkWr func cleanupIntegrationTest() { if iClient != nil { + adminCtx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) + defer cancel() + deleteIndexes(adminCtx) + ctx := context.Background() - deleteIndexes(ctx) deleteCollection(ctx, iColl) iClient.Close() } @@ -871,6 +868,14 @@ func TestIntegration_WriteBatch(t *testing.T) { func TestIntegration_QueryDocuments_WhereEntity(t *testing.T) { ctx := context.Background() coll := integrationColl(t) + + indexFields := [][]string{ + {"updatedAt", "weight", "height"}, + {"weight", "height"}} + adminCtx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) + defer cancel() + createIndexes(adminCtx, wantDBPath, indexFields) + h := testHelper{t} nowTime := time.Now() todayTime := nowTime.Unix() @@ -1096,8 +1101,6 @@ func TestIntegration_QueryDocuments(t *testing.T) { continue } - fmt.Printf("test.want: %+v\n", test.want) - docsEqual := true docsNotEqualErr := "" for j, g := range gotDocs { @@ -2458,34 +2461,41 @@ func TestIntegration_AggregationQueries(t *testing.T) { ctx := context.Background() coll := integrationColl(t) client := integrationClient(t) + + indexFields := [][]string{ + {"weight", "model"}} + adminCtx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) + defer cancel() + createIndexes(adminCtx, wantDBPath, indexFields) + h := testHelper{t} docs := []map[string]interface{}{ - {"width": 1.5, "depth": 99, "model": "A"}, - {"width": 2.6, "depth": 98, "model": "A"}, - {"width": 3.7, "depth": 97, "model": "B"}, - {"width": 4.8, "depth": 96, "model": "B"}, - {"width": 5.9, "depth": 95, "model": "C"}, - {"width": 6.0, "depth": 94, "model": "B"}, - {"width": 7.1, "depth": 93, "model": "C"}, - {"width": 8.2, "depth": 93, "model": "A"}, + {"weight": 1.5, "height": 99, "model": "A"}, + {"weight": 2.6, "height": 98, "model": "A"}, + {"weight": 3.7, "height": 97, "model": "B"}, + {"weight": 4.8, "height": 96, "model": "B"}, + {"weight": 5.9, "height": 95, "model": "C"}, + {"weight": 6.0, "height": 94, "model": "B"}, + {"weight": 7.1, "height": 93, "model": "C"}, + {"weight": 8.2, "height": 93, "model": "A"}, } for _, doc := range docs { newDoc := coll.NewDoc() h.mustCreate(newDoc, doc) } - query := coll.Where("width", ">=", 1) + query := coll.Where("weight", ">=", 1) - limitQuery := coll.Where("width", ">=", 1).Limit(4) - limitToLastQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).LimitToLast(4) + limitQuery := coll.Where("weight", ">=", 1).Limit(4) + limitToLastQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).LimitToLast(4) - startAtQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).StartAt(3.7) - startAfterQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).StartAfter(3.7) + startAtQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).StartAt(3.7) + startAfterQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).StartAfter(3.7) - endAtQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).EndAt(7.1) - endBeforeQuery := coll.Where("width", ">=", 2.6).OrderBy("width", Asc).EndBefore(7.1) + endAtQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).EndAt(7.1) + endBeforeQuery := coll.Where("weight", ">=", 2.6).OrderBy("weight", Asc).EndBefore(7.1) - emptyResultsQuery := coll.Where("width", "<", 1) + emptyResultsQuery := coll.Where("weight", "<", 1) emptyResultsQueryPtr := &emptyResultsQuery testcases := []struct { @@ -2497,32 +2507,32 @@ func TestIntegration_AggregationQueries(t *testing.T) { }{ { desc: "Multiple aggregations", - aggregationQuery: query.NewAggregationQuery().WithCount("count1").WithAvg("width", "width_avg1").WithAvg("depth", "depth_avg1").WithSum("width", "width_sum1").WithSum("depth", "depth_sum1"), + aggregationQuery: query.NewAggregationQuery().WithCount("count1").WithAvg("weight", "weight_avg1").WithAvg("height", "height_avg1").WithSum("weight", "weight_sum1").WithSum("height", "height_sum1"), wantErr: false, result: map[string]interface{}{ - "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(8)}}, - "width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}}, - "depth_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(765)}}, - "width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}}, - "depth_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(95.625)}}, + "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(8)}}, + "weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}}, + "height_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(765)}}, + "weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}}, + "height_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(95.625)}}, }, }, { desc: "Aggregations in transaction", - aggregationQuery: query.NewAggregationQuery().WithCount("count1").WithAvg("width", "width_avg1").WithAvg("depth", "depth_avg1").WithSum("width", "width_sum1").WithSum("depth", "depth_sum1"), + aggregationQuery: query.NewAggregationQuery().WithCount("count1").WithAvg("weight", "weight_avg1").WithAvg("height", "height_avg1").WithSum("weight", "weight_sum1").WithSum("height", "height_sum1"), wantErr: false, runInTransaction: true, result: map[string]interface{}{ - "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(8)}}, - "width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}}, - "depth_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(765)}}, - "width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}}, - "depth_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(95.625)}}, + "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(8)}}, + "weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}}, + "height_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(765)}}, + "weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}}, + "height_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(95.625)}}, }, }, { desc: "WithSum aggregation without alias", - aggregationQuery: query.NewAggregationQuery().WithSum("width", ""), + aggregationQuery: query.NewAggregationQuery().WithSum("weight", ""), wantErr: false, result: map[string]interface{}{ "field_1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}}, @@ -2530,7 +2540,7 @@ func TestIntegration_AggregationQueries(t *testing.T) { }, { desc: "WithSumPath aggregation without alias", - aggregationQuery: query.NewAggregationQuery().WithSumPath([]string{"width"}, ""), + aggregationQuery: query.NewAggregationQuery().WithSumPath([]string{"weight"}, ""), wantErr: false, result: map[string]interface{}{ "field_1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(39.8)}}, @@ -2538,7 +2548,7 @@ func TestIntegration_AggregationQueries(t *testing.T) { }, { desc: "WithAvg aggregation without alias", - aggregationQuery: query.NewAggregationQuery().WithAvg("width", ""), + aggregationQuery: query.NewAggregationQuery().WithAvg("weight", ""), wantErr: false, result: map[string]interface{}{ "field_1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}}, @@ -2546,7 +2556,7 @@ func TestIntegration_AggregationQueries(t *testing.T) { }, { desc: "WithAvgPath aggregation without alias", - aggregationQuery: query.NewAggregationQuery().WithAvgPath([]string{"width"}, ""), + aggregationQuery: query.NewAggregationQuery().WithAvgPath([]string{"weight"}, ""), wantErr: false, result: map[string]interface{}{ "field_1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.975)}}, @@ -2554,72 +2564,72 @@ func TestIntegration_AggregationQueries(t *testing.T) { }, { desc: "Aggregations with limit", - aggregationQuery: (&limitQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"), + aggregationQuery: (&limitQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"), wantErr: false, result: map[string]interface{}{ - "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(4)}}, - "width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(12.6)}}, - "width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(3.15)}}, + "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(4)}}, + "weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(12.6)}}, + "weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(3.15)}}, }, }, { desc: "Aggregations with StartAt", - aggregationQuery: (&startAtQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"), + aggregationQuery: (&startAtQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"), wantErr: false, result: map[string]interface{}{ - "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(6)}}, - "width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(35.7)}}, - "width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(5.95)}}, + "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(6)}}, + "weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(35.7)}}, + "weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(5.95)}}, }, }, { desc: "Aggregations with StartAfter", - aggregationQuery: (&startAfterQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"), + aggregationQuery: (&startAfterQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"), wantErr: false, result: map[string]interface{}{ - "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(5)}}, - "width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(32)}}, - "width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(6.4)}}, + "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(5)}}, + "weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(32)}}, + "weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(6.4)}}, }, }, { desc: "Aggregations with EndAt", - aggregationQuery: (&endAtQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"), + aggregationQuery: (&endAtQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"), wantErr: false, result: map[string]interface{}{ - "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(6)}}, - "width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(30.1)}}, - "width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(5.016666666666667)}}, + "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(6)}}, + "weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(30.1)}}, + "weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(5.016666666666667)}}, }, }, { desc: "Aggregations with EndBefore", - aggregationQuery: (&endBeforeQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"), + aggregationQuery: (&endBeforeQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"), wantErr: false, result: map[string]interface{}{ - "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(5)}}, - "width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(23)}}, - "width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.6)}}, + "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(5)}}, + "weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(23)}}, + "weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(4.6)}}, }, }, { desc: "Aggregations with LimitToLast", - aggregationQuery: (&limitToLastQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"width"}, "width_avg1").WithSumPath([]string{"width"}, "width_sum1"), + aggregationQuery: (&limitToLastQuery).NewAggregationQuery().WithCount("count1").WithAvgPath([]string{"weight"}, "weight_avg1").WithSumPath([]string{"weight"}, "weight_sum1"), wantErr: false, result: map[string]interface{}{ - "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(4)}}, - "width_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(27.2)}}, - "width_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(6.8)}}, + "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(4)}}, + "weight_sum1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(27.2)}}, + "weight_avg1": &pb.Value{ValueType: &pb.Value_DoubleValue{DoubleValue: float64(6.8)}}, }, }, { desc: "Aggregations on empty results", - aggregationQuery: emptyResultsQueryPtr.NewAggregationQuery().WithCount("count1").WithAvg("width", "width_avg1").WithSum("width", "width_sum1"), + aggregationQuery: emptyResultsQueryPtr.NewAggregationQuery().WithCount("count1").WithAvg("weight", "weight_avg1").WithSum("weight", "weight_sum1"), wantErr: false, result: map[string]interface{}{ - "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(0)}}, - "width_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(0)}}, - "width_avg1": &pb.Value{ValueType: &pb.Value_NullValue{NullValue: structpb.NullValue_NULL_VALUE}}, + "count1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(0)}}, + "weight_sum1": &pb.Value{ValueType: &pb.Value_IntegerValue{IntegerValue: int64(0)}}, + "weight_avg1": &pb.Value{ValueType: &pb.Value_NullValue{NullValue: structpb.NullValue_NULL_VALUE}}, }, }, {