@@ -66,6 +66,8 @@ class BlogPost(InheritFrom):
6666 for expected in expected_specs :
6767 assert expected ["fields" ] in info
6868
69+ assert BlogPost .compare_indexes () == {"missing" : [], "extra" : []}
70+
6971 def _index_test_inheritance (self , InheritFrom ):
7072 class BlogPost (InheritFrom ):
7173 date = DateTimeField (db_field = "addDate" , default = datetime .now )
@@ -949,6 +951,8 @@ class MyDoc(Document):
949951 ]["key" ]
950952 assert info ["provider_ids.foo_1_provider_ids.bar_1" ]["sparse" ]
951953
954+ assert MyDoc .compare_indexes () == {"missing" : [], "extra" : []}
955+
952956 def test_text_indexes (self ):
953957 class Book (Document ):
954958 title = DictField ()
@@ -968,6 +972,8 @@ class Book(Document):
968972 assert "ref_id_hashed" in indexes
969973 assert ("ref_id" , "hashed" ) in indexes ["ref_id_hashed" ]["key" ]
970974
975+ assert Book .compare_indexes () == {"missing" : [], "extra" : []}
976+
971977 def test_indexes_after_database_drop (self ):
972978 """
973979 Test to ensure that indexes are not re-created on a collection
@@ -1045,6 +1051,8 @@ class TestChildDoc(TestDoc):
10451051 TestDoc .ensure_indexes ()
10461052 TestChildDoc .ensure_indexes ()
10471053
1054+ assert TestDoc .compare_indexes () == {"missing" : [], "extra" : []}
1055+
10481056 index_info = TestDoc ._get_collection ().index_information ()
10491057 for key in index_info :
10501058 del index_info [key ][
@@ -1082,9 +1090,34 @@ class TestDoc(Document):
10821090 TestDoc .drop_collection ()
10831091 TestDoc .ensure_indexes ()
10841092
1093+ assert TestDoc .compare_indexes () == {"missing" : [], "extra" : []}
1094+
10851095 index_info = TestDoc ._get_collection ().index_information ()
10861096 assert "shard_1_1__cls_1_txt_1_1" in index_info
10871097
1098+ def test_compare_indexes_works_with_compound_text_indexes (self ):
1099+ """The order of the fields in case of text indexes don't matter
1100+ so it's important to ensure that the compare_indexes method works that way
1101+ https://github.com/MongoEngine/mongoengine/issues/2612
1102+ """
1103+
1104+ class Sample1 (Document ):
1105+ a = StringField ()
1106+ b = StringField ()
1107+
1108+ meta = {"indexes" : [{"fields" : ["$a" , "$b" ]}]}
1109+
1110+ class Sample2 (Document ):
1111+ a = StringField ()
1112+ b = StringField ()
1113+
1114+ meta = {"indexes" : [{"fields" : ["$b" , "$a" ]}]}
1115+
1116+ Sample1 .drop_collection ()
1117+ Sample2 .drop_collection ()
1118+ assert Sample1 .compare_indexes () == {"missing" : [], "extra" : []}
1119+ assert Sample2 .compare_indexes () == {"missing" : [], "extra" : []}
1120+
10881121
10891122if __name__ == "__main__" :
10901123 unittest .main ()
0 commit comments