@@ -983,44 +983,52 @@ class Book(Document):
983983
984984 def test_indexes_after_database_drop (self ):
985985 """
986- Test to ensure that indexes are re-created on a collection even
987- after the database has been dropped.
986+ Test to ensure that indexes are not re-created on a collection
987+ after the database has been dropped unless auto_create_index_on_save
988+ is enabled.
988989
989- Issue #812
990+ Issue #812 and #1446.
990991 """
991992 # Use a new connection and database since dropping the database could
992993 # cause concurrent tests to fail.
993- connection = connect (
994- db = "tempdatabase" , alias = "test_indexes_after_database_drop"
995- )
994+ tmp_alias = "test_indexes_after_database_drop"
995+ connection = connect ( db = "tempdatabase" , alias = tmp_alias )
996+ self . addCleanup ( connection . drop_database , "tempdatabase" )
996997
997998 class BlogPost (Document ):
998- title = StringField ()
999999 slug = StringField (unique = True )
1000+ meta = {"db_alias" : tmp_alias }
10001001
1001- meta = {"db_alias" : "test_indexes_after_database_drop" }
1002+ BlogPost .drop_collection ()
1003+ BlogPost (slug = "test" ).save ()
1004+ with pytest .raises (NotUniqueError ):
1005+ BlogPost (slug = "test" ).save ()
10021006
1003- try :
1004- BlogPost .drop_collection ()
1005-
1006- # Create Post #1
1007- post1 = BlogPost (title = "test1" , slug = "test" )
1008- post1 .save ()
1009-
1010- # Drop the Database
1011- connection .drop_database ("tempdatabase" )
1012-
1013- # Re-create Post #1
1014- post1 = BlogPost (title = "test1" , slug = "test" )
1015- post1 .save ()
1016-
1017- # Create Post #2
1018- post2 = BlogPost (title = "test2" , slug = "test" )
1019- with pytest .raises (NotUniqueError ):
1020- post2 .save ()
1021- finally :
1022- # Drop the temporary database at the end
1023- connection .drop_database ("tempdatabase" )
1007+ # Drop the Database
1008+ connection .drop_database ("tempdatabase" )
1009+ BlogPost (slug = "test" ).save ()
1010+ # No error because the index was not recreated after dropping the database.
1011+ BlogPost (slug = "test" ).save ()
1012+
1013+ # Repeat with auto_create_index_on_save: True.
1014+ class BlogPost2 (Document ):
1015+ slug = StringField (unique = True )
1016+ meta = {
1017+ "db_alias" : tmp_alias ,
1018+ "auto_create_index_on_save" : True ,
1019+ }
1020+
1021+ BlogPost2 .drop_collection ()
1022+ BlogPost2 (slug = "test" ).save ()
1023+ with pytest .raises (NotUniqueError ):
1024+ BlogPost2 (slug = "test" ).save ()
1025+
1026+ # Drop the Database
1027+ connection .drop_database ("tempdatabase" )
1028+ BlogPost2 (slug = "test" ).save ()
1029+ # Error because ensure_indexes is run on every save().
1030+ with pytest .raises (NotUniqueError ):
1031+ BlogPost2 (slug = "test" ).save ()
10241032
10251033 def test_index_dont_send_cls_option (self ):
10261034 """
0 commit comments