@@ -32,55 +32,58 @@ public void List_should_return_expected_result(
32
32
[ Values ( false , true ) ] bool async )
33
33
{
34
34
var indexKeyDocument = BsonDocument . Parse ( key ) ;
35
- var collection = GetEmptyCollection ( ) ;
36
- var subject = collection . Indexes ;
35
+ var collectionName = DriverTestConfiguration . CollectionNamespace . CollectionName ;
36
+ var client = DriverTestConfiguration . Client ;
37
+ var database = client . GetDatabase ( DriverTestConfiguration . DatabaseNamespace . DatabaseName ) ;
38
+ database . DropCollection ( collectionName ) ;
39
+ var collection = database . GetCollection < BsonDocument > ( collectionName ) ;
37
40
38
- subject . CreateOne ( new CreateIndexModel < BsonDocument > ( indexKeyDocument , new CreateIndexOptions ( ) { Unique = unique } ) ) ;
41
+ var subject = collection . Indexes ;
39
42
40
- var indexesCursor =
41
- async
42
- ? subject . ListAsync ( ) . GetAwaiter( ) . GetResult( )
43
- : subject . List ( ) ;
44
- var indexes = indexesCursor. ToList( ) ;
43
+ try
44
+ {
45
+ subject . CreateOne ( new CreateIndexModel < BsonDocument > ( indexKeyDocument , new CreateIndexOptions ( ) { Unique = unique } ) ) ;
45
46
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 ) ;
47
+ var indexesCursor =
48
+ async
49
+ ? subject . ListAsync ( ) . GetAwaiter( ) . GetResult( )
50
+ : subject . List ( ) ;
51
+ var indexes = indexesCursor. ToList( ) ;
50
52
51
- void AssertIndex( CollectionNamespace collectionNamespace , BsonDocument index , string expectedName , bool expectedUnique = false)
52
- {
53
- index[ "name" ] . AsString . Should ( ) . Be ( expectedName ) ;
53
+ indexes . Count . Should ( ) . Be( 2 ) ;
54
+ AssertIndex( collection . CollectionNamespace , indexes [ 0 ] , "_id_ ") ;
55
+ var indexName = IndexNameHelper . GetIndexName ( indexKeyDocument ) ;
56
+ AssertIndex( collection . CollectionNamespace , indexes [ 1 ] , indexName , expectedUnique : unique ) ;
54
57
55
- if ( expectedUnique )
56
- {
57
- index[ "unique" ] . AsBoolean . Should ( ) . BeTrue ( ) ;
58
- }
59
- else
58
+ void AssertIndex( CollectionNamespace collectionNamespace , BsonDocument index , string expectedName , bool expectedUnique = false)
60
59
{
61
- index. Contains ( "unique" ) . Should ( ) . BeFalse ( ) ;
62
- }
60
+ index[ "name" ] . AsString . Should ( ) . Be ( expectedName ) ;
63
61
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 ( ) ;
62
+ if ( expectedUnique )
63
+ {
64
+ index[ "unique" ] . AsBoolean . Should ( ) . BeTrue ( ) ;
65
+ }
66
+ else
67
+ {
68
+ index. Contains ( "unique" ) . Should ( ) . BeFalse ( ) ;
69
+ }
70
+
71
+ if ( CoreTestConfiguration . ServerVersion < new SemanticVersion ( 4 , 3 , 0 ) )
72
+ {
73
+ index[ "ns" ] . AsString . Should ( ) . Be ( collectionNamespace . ToString ( ) ) ;
74
+ }
75
+ else
76
+ {
77
+ // the server doesn't return ns anymore
78
+ index. Contains ( "ns" ) . Should ( ) . BeFalse ( ) ;
79
+ }
72
80
}
73
81
}
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 ) ;
82
+ finally
83
+ {
84
+ // make sure that index has been removed
85
+ database. DropCollection ( collectionName ) ;
86
+ }
84
87
}
85
88
}
86
89
}
0 commit comments