|
| 1 | +// Copyright (C) MongoDB, Inc. 2025-present. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 7 | +package integration |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "go.mongodb.org/mongo-driver/v2/bson" |
| 16 | + "go.mongodb.org/mongo-driver/v2/internal/integration/mtest" |
| 17 | + "go.mongodb.org/mongo-driver/v2/internal/require" |
| 18 | + "go.mongodb.org/mongo-driver/v2/mongo" |
| 19 | + "go.mongodb.org/mongo-driver/v2/mongo/options" |
| 20 | + "go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore" |
| 21 | +) |
| 22 | + |
| 23 | +func TestSearchIndex(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + |
| 26 | + const timeout = 5 * time.Minute |
| 27 | + |
| 28 | + uri := os.Getenv("SEARCH_INDEX_URI") |
| 29 | + if uri == "" { |
| 30 | + t.Skip("skipping") |
| 31 | + } |
| 32 | + |
| 33 | + opts := options.Client().ApplyURI(uri).SetTimeout(timeout) |
| 34 | + mt := mtest.New(t, mtest.NewOptions().ClientOptions(opts).MinServerVersion("7.0").Topologies(mtest.ReplicaSet)) |
| 35 | + |
| 36 | + mt.Run("search indexes with empty option", func(mt *mtest.T) { |
| 37 | + ctx := context.Background() |
| 38 | + |
| 39 | + expected := bson.RawArray(bsoncore.NewArrayBuilder().AppendDocument( |
| 40 | + bsoncore.NewDocumentBuilder(). |
| 41 | + AppendDocument("definition", bsoncore.NewDocumentBuilder(). |
| 42 | + AppendDocument("mappings", bsoncore.NewDocumentBuilder(). |
| 43 | + AppendBoolean("dynamic", true). |
| 44 | + Build()). |
| 45 | + Build()). |
| 46 | + Build()). |
| 47 | + Build()) |
| 48 | + |
| 49 | + _, err := mt.Coll.SearchIndexes().CreateOne(ctx, mongo.SearchIndexModel{ |
| 50 | + Definition: bson.D{ |
| 51 | + {"mappings", bson.D{ |
| 52 | + {"dynamic", true}, |
| 53 | + }}, |
| 54 | + }, |
| 55 | + }) |
| 56 | + require.NoError(mt, err, "failed to create index") |
| 57 | + evt := mt.GetStartedEvent() |
| 58 | + actual, ok := evt.Command.Lookup("indexes").ArrayOK() |
| 59 | + require.True(mt, ok, "expected command %v to contain an indexes array", evt.Command) |
| 60 | + require.Equal(mt, expected, actual, "expected indexes array %v, got %v", expected, actual) |
| 61 | + }) |
| 62 | +} |
0 commit comments