|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 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 | +* http://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 | + |
| 16 | +using System; |
| 17 | +using System.Linq; |
| 18 | +using System.Threading; |
| 19 | +using System.Threading.Tasks; |
| 20 | +using FluentAssertions; |
| 21 | +using MongoDB.Bson; |
| 22 | +using MongoDB.Driver.Core.TestHelpers.Logging; |
| 23 | +using MongoDB.Driver.TestHelpers; |
| 24 | +using MongoDB.TestHelpers.XunitExtensions; |
| 25 | +using Xunit; |
| 26 | +using Xunit.Abstractions; |
| 27 | + |
| 28 | +namespace MongoDB.Driver.Tests.Search |
| 29 | +{ |
| 30 | + [Category("AtlasSearchIndexHelpers")] |
| 31 | + public class AtlasSearchIndexManagementTests : LoggableTestClass |
| 32 | + { |
| 33 | + private const int Timeout = 5 * 60 * 1000; |
| 34 | + private const int IndexesPollPeriod = 5000; |
| 35 | + |
| 36 | + private readonly IMongoCollection<BsonDocument> _collection; |
| 37 | + private readonly IMongoDatabase _database; |
| 38 | + private readonly DisposableMongoClient _disposableMongoClient; |
| 39 | + private readonly BsonDocument _indexDefinition = BsonDocument.Parse("{ mappings: { dynamic: false } }"); |
| 40 | + |
| 41 | + public AtlasSearchIndexManagementTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) |
| 42 | + { |
| 43 | + RequireEnvironment.Check().EnvironmentVariable("ATLAS_SEARCH_INDEX_HELPERS_TESTS_ENABLED"); |
| 44 | + |
| 45 | + var atlasSearchUri = CoreTestConfiguration.ConnectionString.ToString(); |
| 46 | + |
| 47 | + _disposableMongoClient = new(new MongoClient(atlasSearchUri), CreateLogger<DisposableMongoClient>()); |
| 48 | + _database = _disposableMongoClient.GetDatabase("dotnet-test"); |
| 49 | + var collectionName = GetRandomName(); |
| 50 | + |
| 51 | + _database.CreateCollection(collectionName); |
| 52 | + _collection = _database.GetCollection<BsonDocument>(collectionName); |
| 53 | + } |
| 54 | + |
| 55 | + protected override void DisposeInternal() |
| 56 | + { |
| 57 | + _collection.Database.DropCollection(_collection.CollectionNamespace.CollectionName); |
| 58 | + _disposableMongoClient.Dispose(); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact(Timeout = Timeout)] |
| 62 | + public Task Case1_driver_should_successfully_create_and_list_search_indexes() => |
| 63 | + CreateIndexAndValidate("test-search-index"); |
| 64 | + |
| 65 | + [Fact(Timeout = Timeout)] |
| 66 | + public async Task Case2_driver_should_successfully_create_multiple_indexes_in_batch() |
| 67 | + { |
| 68 | + var indexDefinition1 = new CreateSearchIndexModel("test-search-index-1", _indexDefinition); |
| 69 | + var indexDefinition2 = new CreateSearchIndexModel("test-search-index-2", _indexDefinition); |
| 70 | + |
| 71 | + var indexNamesActual = await _collection.SearchIndexes.CreateManyAsync(new[] { indexDefinition1, indexDefinition2 }); |
| 72 | + |
| 73 | + indexNamesActual.Should().BeEquivalentTo(indexDefinition1.Name, indexDefinition2.Name); |
| 74 | + |
| 75 | + var indexes = await GetIndexes(indexDefinition1.Name, indexDefinition2.Name); |
| 76 | + |
| 77 | + indexes[0]["latestDefinition"].AsBsonDocument.Should().Be(_indexDefinition); |
| 78 | + indexes[1]["latestDefinition"].AsBsonDocument.Should().Be(_indexDefinition); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact(Timeout = Timeout)] |
| 82 | + public async Task Case3_driver_can_successfully_drop_search_indexes() |
| 83 | + { |
| 84 | + const string indexName = "test-search-index"; |
| 85 | + |
| 86 | + await CreateIndexAndValidate(indexName); |
| 87 | + |
| 88 | + await _collection.SearchIndexes.DropOneAsync(indexName); |
| 89 | + |
| 90 | + while (true) |
| 91 | + { |
| 92 | + var cursor = await _collection.SearchIndexes.ListAsync(); |
| 93 | + var indexes = await cursor.ToListAsync(); |
| 94 | + if (indexes.Count == 0) |
| 95 | + { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + Thread.Sleep(IndexesPollPeriod); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Fact(Timeout = Timeout)] |
| 104 | + public async Task Case4_driver_can_update_a_search_index() |
| 105 | + { |
| 106 | + const string indexName = "test-search-index"; |
| 107 | + var indexNewDefinition = BsonDocument.Parse("{ mappings: { dynamic: true }}"); |
| 108 | + |
| 109 | + await CreateIndexAndValidate(indexName); |
| 110 | + |
| 111 | + await _collection.SearchIndexes.UpdateAsync(indexName, indexNewDefinition); |
| 112 | + |
| 113 | + var updatedIndex = await GetIndexes(indexName); |
| 114 | + updatedIndex[0]["latestDefinition"].AsBsonDocument.Should().Be(indexNewDefinition); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact(Timeout = Timeout)] |
| 118 | + public async Task Case5_dropSearchIndex_suppresses_namespace_not_found_errors() |
| 119 | + { |
| 120 | + var collection = _database.GetCollection<BsonDocument>("non_existent_collection"); |
| 121 | + await collection.SearchIndexes.DropOneAsync("non_existing_index"); |
| 122 | + } |
| 123 | + |
| 124 | + private async Task<BsonDocument> CreateIndexAndValidate(string indexName) |
| 125 | + { |
| 126 | + var indexNameActual = await _collection.SearchIndexes.CreateOneAsync(_indexDefinition, indexName); |
| 127 | + indexNameActual.Should().Be(indexName); |
| 128 | + |
| 129 | + var result = await GetIndexes(indexName); |
| 130 | + |
| 131 | + return result[0]; |
| 132 | + } |
| 133 | + |
| 134 | + private async Task<BsonDocument[]> GetIndexes(params string[] indexNames) |
| 135 | + { |
| 136 | + while (true) |
| 137 | + { |
| 138 | + var cursor = await _collection.SearchIndexes.ListAsync(); |
| 139 | + var indexes = await cursor.ToListAsync(); |
| 140 | + |
| 141 | + var indexesFiltered = indexes |
| 142 | + .Where(i => indexNames.Contains(TryGetValue<string>(i, "name")) && TryGetValue<bool>(i, "queryable")) |
| 143 | + .ToArray(); |
| 144 | + |
| 145 | + if (indexesFiltered.Length == indexNames.Length) |
| 146 | + { |
| 147 | + return indexesFiltered; |
| 148 | + } |
| 149 | + |
| 150 | + Thread.Sleep(IndexesPollPeriod); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static string GetRandomName() => $"test_{Guid.NewGuid():N}"; |
| 155 | + |
| 156 | + private static T TryGetValue<T>(BsonDocument document, string name) |
| 157 | + { |
| 158 | + if (!document.TryGetValue(name, out var value)) |
| 159 | + { |
| 160 | + return default; |
| 161 | + } |
| 162 | + |
| 163 | + var result = BsonTypeMapper.MapToDotNetValue(value); |
| 164 | + return (T)result; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments