1- from unittest import expectedFailure
2-
3- from django .test import TransactionTestCase , skipUnlessDBFeature
1+ from django .db import DatabaseError
2+ from django .test import TransactionTestCase , skipIfDBFeature , skipUnlessDBFeature
43
54from django_mongodb_backend import transaction
65
98
109@skipUnlessDBFeature ("_supports_transactions" )
1110class AtomicTests (TransactionTestCase ):
12- """
13- Tests for the atomic decorator and context manager.
14-
15- The tests make assertions on internal attributes because there isn't a
16- robust way to ask the database for its current transaction state.
17-
18- Since the decorator syntax is converted into a context manager (see the
19- implementation), there are only a few basic tests with the decorator
20- syntax and the bulk of the tests use the context manager syntax.
21- """
11+ """Largely copied from Django's test/transactions."""
2212
2313 available_apps = ["transactions_" ]
2414
@@ -76,15 +66,6 @@ def test_nested_commit_commit(self):
7666 reporter2 = Reporter .objects .create (first_name = "Archibald" , last_name = "Haddock" )
7767 self .assertSequenceEqual (Reporter .objects .all (), [reporter2 , reporter1 ])
7868
79- @expectedFailure
80- def test_nested_commit_rollback (self ):
81- with transaction .atomic ():
82- reporter = Reporter .objects .create (first_name = "Tintin" )
83- with self .assertRaisesMessage (Exception , "Oops" ), transaction .atomic ():
84- Reporter .objects .create (first_name = "Haddock" )
85- raise Exception ("Oops, that's his last name" )
86- self .assertSequenceEqual (Reporter .objects .all (), [reporter ])
87-
8869 def test_nested_rollback_commit (self ):
8970 with self .assertRaisesMessage (Exception , "Oops" ), transaction .atomic ():
9071 Reporter .objects .create (last_name = "Tintin" )
@@ -111,16 +92,6 @@ def test_reuse_commit_commit(self):
11192 reporter2 = Reporter .objects .create (first_name = "Archibald" , last_name = "Haddock" )
11293 self .assertSequenceEqual (Reporter .objects .all (), [reporter2 , reporter1 ])
11394
114- @expectedFailure
115- def test_reuse_commit_rollback (self ):
116- atomic = transaction .atomic ()
117- with atomic :
118- reporter = Reporter .objects .create (first_name = "Tintin" )
119- with self .assertRaisesMessage (Exception , "Oops" ), atomic :
120- Reporter .objects .create (first_name = "Haddock" )
121- raise Exception ("Oops, that's his last name" )
122- self .assertSequenceEqual (Reporter .objects .all (), [reporter ])
123-
12495 def test_reuse_rollback_commit (self ):
12596 atomic = transaction .atomic ()
12697 with self .assertRaisesMessage (Exception , "Oops" ), atomic :
@@ -141,16 +112,6 @@ def test_reuse_rollback_rollback(self):
141112 raise Exception ("Oops, that's his first name" )
142113 self .assertSequenceEqual (Reporter .objects .all (), [])
143114
144- # class AtomicInsideTransactionTests(AtomicTests):
145- # """All basic tests for atomic should also pass within an existing transaction."""
146-
147- # def setUp(self):
148- # self.atomic = transaction.atomic()
149- # self.atomic.__enter__()
150-
151- # def tearDown(self):
152- # self.atomic.__exit__(*sys.exc_info())
153-
154115 def test_wrap_callable_instance (self ):
155116 """Atomic can wrap callable instances."""
156117
@@ -160,3 +121,14 @@ def __call__(self):
160121
161122 # Must not raise an exception
162123 transaction .atomic (Callable ())
124+
125+
126+ @skipIfDBFeature ("_supports_transactions" )
127+ class AtomicNotSupportedTests (TransactionTestCase ):
128+ available_apps = ["transactions_" ]
129+
130+ def test_not_supported (self ):
131+ # MongoDB error:
132+ # "Transaction numbers are only allowed on a replica set member or mongos"
133+ with self .assertRaises (DatabaseError ), transaction .atomic ():
134+ Reporter .objects .create (first_name = "Haddock" )
0 commit comments