1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using MongoDB . DeclarativeIndexes ;
5+ using NSubstitute ;
6+ using Xunit ;
7+ using Index = MongoDB . DeclarativeIndexes . Index ;
8+
9+ namespace mongo_declarative_indexes . Tests
10+ {
11+ public class IndexEnsurerShould
12+ {
13+ [ Fact ]
14+ public void Ensure_CreatesMissingIndexes ( )
15+ {
16+ var database = Substitute . For < IDatabase > ( ) ;
17+ var ensurer = new IndexEnsurer ( database ) ;
18+ var expectedIndexes = new [ ] { new Index ( keys : new Key ( "field" , IndexType . Ascending ) ) } ;
19+ ensurer . Ensure ( new CollectionIndexes ( "testCollection" , expectedIndexes ) ) ;
20+
21+ database . Received ( ) . CreateManyIndexes ( "testCollection" ,
22+ Arg . Is < Index [ ] > ( actualIndexes =>
23+ actualIndexes . SequenceEqual ( expectedIndexes ) ) ) ;
24+ }
25+
26+ [ Fact ]
27+ public void Ensure_DropsExtraIndexes ( )
28+ {
29+ var database = Substitute . For < IDatabase > ( ) ;
30+ var extraIndex = new Dictionary < string , object >
31+ {
32+ { "v" , 2 } ,
33+ { "key" , new Dictionary < string , object > { { "field" , 1 } } } ,
34+ { "name" , "field_1" } ,
35+ { "ns" , "test.collections" }
36+ } ;
37+ database . ListCollectionNames ( ) . Returns ( new [ ] { "collectionName" } ) ;
38+ database . ListIndexes ( "collectionName" ) . Returns ( new [ ] { extraIndex } ) ;
39+
40+ var ensurer = new IndexEnsurer ( database ) ;
41+ ensurer . Ensure ( Array . Empty < CollectionIndexes > ( ) ) ;
42+
43+ database . DidNotReceiveWithAnyArgs ( ) . CreateManyIndexes ( default , default ) ;
44+ database . Received ( ) . DropOneIndex ( "collectionName" , "field_1" ) ;
45+ }
46+
47+ [ Fact ]
48+ public void Ensure_DoesNothingWithIdIndex ( )
49+ {
50+ var database = Substitute . For < IDatabase > ( ) ;
51+ var idIndex = new Dictionary < string , object >
52+ {
53+ { "v" , 2 } ,
54+ { "key" , new Dictionary < string , object > { { "_id" , 1 } } } ,
55+ { "name" , "_id_" } ,
56+ { "ns" , "test.collections" }
57+ } ;
58+ database . ListCollectionNames ( ) . Returns ( new [ ] { "collectionName" } ) ;
59+ database . ListIndexes ( "collectionName" ) . Returns ( new [ ] { idIndex } ) ;
60+
61+ var ensurer = new IndexEnsurer ( database ) ;
62+ ensurer . Ensure ( Array . Empty < CollectionIndexes > ( ) ) ;
63+
64+ database . DidNotReceiveWithAnyArgs ( ) . CreateManyIndexes ( default , default ) ;
65+ database . DidNotReceiveWithAnyArgs ( ) . DropOneIndex ( default , default ) ;
66+ }
67+
68+ [ Fact ]
69+ public void Ensure_DropsExtraAndCreatesMissingIndexes ( )
70+ {
71+ var database = Substitute . For < IDatabase > ( ) ;
72+ var extraIndex = new Dictionary < string , object >
73+ {
74+ { "v" , 2 } ,
75+ { "key" , new Dictionary < string , object > { { "field" , 1 } } } ,
76+ { "name" , "field_1" } ,
77+ { "ns" , "test.collections" }
78+ } ;
79+ const string remainingFieldName = "remaining_field" ;
80+ var remainingDbIndex = new Dictionary < string , object >
81+ {
82+ { "v" , 2 } ,
83+ { "key" , new Dictionary < string , object > { { remainingFieldName , 1 } } } ,
84+ { "name" , $ "{ remainingFieldName } _1"} ,
85+ { "ns" , "test.collections" }
86+ } ;
87+ const string collectionName = "collectionName" ;
88+ database . ListCollectionNames ( ) . Returns ( new [ ] { collectionName } ) ;
89+ database . ListIndexes ( collectionName ) . Returns ( new [ ] { extraIndex , remainingDbIndex } ) ;
90+
91+ var expectedCreatedIndexes = new [ ] { new Index ( keys : new Key ( "yet_another_field" , IndexType . Descending ) ) } ;
92+ var ensurer = new IndexEnsurer ( database ) ;
93+ var remainingIndex = new Index ( keys : new Key ( remainingFieldName , IndexType . Ascending ) ) ;
94+ ensurer . Ensure ( new CollectionIndexes ( collectionName ,
95+ expectedCreatedIndexes . Append ( remainingIndex ) . ToArray ( ) ) ) ;
96+ database . Received ( ) . DropOneIndex ( collectionName , "field_1" ) ;
97+ database . Received ( ) . CreateManyIndexes ( collectionName ,
98+ Arg . Is < Index [ ] > ( actualIndexes =>
99+ actualIndexes
100+ . SequenceEqual ( expectedCreatedIndexes ) ) ) ;
101+ }
102+ }
103+ }
0 commit comments