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
4
3
5
4
from django_mongodb_backend import transaction
6
5
9
8
10
9
@skipUnlessDBFeature ("_supports_transactions" )
11
10
class 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."""
22
12
23
13
available_apps = ["transactions_" ]
24
14
@@ -76,15 +66,6 @@ def test_nested_commit_commit(self):
76
66
reporter2 = Reporter .objects .create (first_name = "Archibald" , last_name = "Haddock" )
77
67
self .assertSequenceEqual (Reporter .objects .all (), [reporter2 , reporter1 ])
78
68
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
-
88
69
def test_nested_rollback_commit (self ):
89
70
with self .assertRaisesMessage (Exception , "Oops" ), transaction .atomic ():
90
71
Reporter .objects .create (last_name = "Tintin" )
@@ -111,16 +92,6 @@ def test_reuse_commit_commit(self):
111
92
reporter2 = Reporter .objects .create (first_name = "Archibald" , last_name = "Haddock" )
112
93
self .assertSequenceEqual (Reporter .objects .all (), [reporter2 , reporter1 ])
113
94
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
-
124
95
def test_reuse_rollback_commit (self ):
125
96
atomic = transaction .atomic ()
126
97
with self .assertRaisesMessage (Exception , "Oops" ), atomic :
@@ -141,16 +112,6 @@ def test_reuse_rollback_rollback(self):
141
112
raise Exception ("Oops, that's his first name" )
142
113
self .assertSequenceEqual (Reporter .objects .all (), [])
143
114
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
-
154
115
def test_wrap_callable_instance (self ):
155
116
"""Atomic can wrap callable instances."""
156
117
@@ -160,3 +121,14 @@ def __call__(self):
160
121
161
122
# Must not raise an exception
162
123
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