@@ -519,8 +519,6 @@ def test_query_counter_ignores_particular_queries(self):
519519
520520 @requires_mongodb_gte_40
521521 def test_updating_a_document_within_a_transaction (self ):
522- connect ("mongoenginetest" )
523-
524522 class A (Document ):
525523 name = StringField ()
526524
@@ -538,8 +536,6 @@ class A(Document):
538536
539537 @requires_mongodb_gte_40
540538 def test_updating_a_document_within_a_transaction_that_fails (self ):
541- connect ("mongoenginetest" )
542-
543539 class A (Document ):
544540 name = StringField ()
545541
@@ -558,13 +554,16 @@ class A(Document):
558554
559555 @requires_mongodb_gte_40
560556 def test_creating_a_document_within_a_transaction (self ):
561- connect ("mongoenginetest" )
562557
563558 class A (Document ):
564559 name = StringField ()
565560
566561 A .drop_collection ()
567562
563+ # ensure collection is created (needed for transaction with MongoDB <= 4.2)
564+ A .objects .create (name = "test" )
565+ A .objects .delete ()
566+
568567 with run_in_transaction ():
569568 a_doc = A .objects .create (name = "a" )
570569 another_doc = A (name = "b" ).save ()
@@ -578,12 +577,14 @@ class A(Document):
578577
579578 @requires_mongodb_gte_40
580579 def test_creating_a_document_within_a_transaction_that_fails (self ):
581- connect ("mongoenginetest" )
582580
583581 class A (Document ):
584582 name = StringField ()
585583
586584 A .drop_collection ()
585+ # ensure collection is created (needed for transaction with MongoDB <= 4.2)
586+ A .objects .create (name = "test" )
587+ A .objects .delete ()
587588
588589 with pytest .raises (TestRollbackError ):
589590 with run_in_transaction ():
@@ -695,8 +696,6 @@ class B(Document):
695696
696697 @requires_mongodb_gte_40
697698 def test_exception_in_child_of_a_nested_transaction_rolls_parent_back (self ):
698- connect ("mongoenginetest" )
699-
700699 class A (Document ):
701700 name = StringField ()
702701
@@ -731,8 +730,6 @@ class B(Document):
731730 def test_exception_in_parent_of_nested_transaction_after_child_completed_only_rolls_parent_back (
732731 self ,
733732 ):
734- connect ("mongoenginetest" )
735-
736733 class A (Document ):
737734 name = StringField ()
738735
@@ -772,7 +769,6 @@ def run_tx():
772769
773770 @requires_mongodb_gte_40
774771 def test_nested_transactions_create_and_release_sessions_accordingly (self ):
775- connect ("mongoenginetest" )
776772 with run_in_transaction ():
777773 s1 = _get_session ()
778774 with run_in_transaction ():
@@ -808,7 +804,6 @@ def test_thread_safety_of_transactions(self):
808804
809805 0 + 10 + 2 + 30 + 4 + 50 + 6 + 70 + 8 + 90 = 270
810806 """
811- connect ("mongoenginetest" )
812807
813808 class A (Document ):
814809 i = IntField (unique = True )
0 commit comments