Skip to content

Commit 14955fe

Browse files
CSHARP-2836: Handle absence of 'ns' field in index specifications returned from listIndexes.
1 parent 51c2f7f commit 14955fe

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Copyright 2020-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 FluentAssertions;
17+
using MongoDB.Bson;
18+
using MongoDB.Bson.TestHelpers.XunitExtensions;
19+
using MongoDB.Driver.Core.Misc;
20+
using MongoDB.Driver.Core.Operations;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Tests
24+
{
25+
public class MongoIndexManagerTests
26+
{
27+
[Theory]
28+
[ParameterAttributeData]
29+
public void List_should_return_expected_result(
30+
[Values("{ singleIndex : 1 }", "{ compoundIndex1 : 1, compoundIndex2 : 1 }")] string key,
31+
[Values(false, true)] bool unique,
32+
[Values(false, true)] bool async)
33+
{
34+
var indexKeyDocument = BsonDocument.Parse(key);
35+
var collection = GetEmptyCollection();
36+
var subject = collection.Indexes;
37+
38+
subject.CreateOne(new CreateIndexModel<BsonDocument>(indexKeyDocument, new CreateIndexOptions() { Unique = unique }));
39+
40+
var indexesCursor =
41+
async
42+
? subject.ListAsync().GetAwaiter().GetResult()
43+
: subject.List();
44+
var indexes = indexesCursor.ToList();
45+
46+
indexes.Count.Should().Be(2);
47+
AssertIndex(collection.CollectionNamespace, indexes[0], "_id_");
48+
var indexName = IndexNameHelper.GetIndexName(indexKeyDocument);
49+
AssertIndex(collection.CollectionNamespace, indexes[1], indexName, expectedUnique: unique);
50+
51+
void AssertIndex(CollectionNamespace collectionNamespace, BsonDocument index, string expectedName, bool expectedUnique = false)
52+
{
53+
index["name"].AsString.Should().Be(expectedName);
54+
55+
if (expectedUnique)
56+
{
57+
index["unique"].AsBoolean.Should().BeTrue();
58+
}
59+
else
60+
{
61+
index.Contains("unique").Should().BeFalse();
62+
}
63+
64+
if (CoreTestConfiguration.ServerVersion < new SemanticVersion(4, 3, 0))
65+
{
66+
index["ns"].AsString.Should().Be(collectionNamespace.ToString());
67+
}
68+
else
69+
{
70+
// the server doesn't return ns anymore
71+
index.Contains("ns").Should().BeFalse();
72+
}
73+
}
74+
}
75+
76+
// private methods
77+
private IMongoCollection<BsonDocument> GetEmptyCollection()
78+
{
79+
var collectionName = DriverTestConfiguration.CollectionNamespace.CollectionName;
80+
var client = DriverTestConfiguration.Client;
81+
var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
82+
database.DropCollection(collectionName);
83+
return database.GetCollection<BsonDocument>(collectionName);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)