summaryrefslogtreecommitdiff
path: root/tests/select_for_update
diff options
context:
space:
mode:
authorShai Berger <shai@platonix.com>2014-03-30 20:03:35 +0300
committerShai Berger <shai@platonix.com>2014-04-10 03:04:51 +0300
commitf095356ba2244f6ed48463bac0b46d3c3e671204 (patch)
treedc0157b441d70e296e8948fd84ea953901f5fe44 /tests/select_for_update
parent071c9337750b296d198cced56f3ffad0e176afb6 (diff)
downloaddjango-f095356ba2244f6ed48463bac0b46d3c3e671204.tar.gz
Fixed #22343 -- Disallowed select_for_update in autocommit mode
The ticket was originally about two failing tests, which are fixed by putting their queries in transactions. Thanks Tim Graham for the report, Aymeric Augustin for the fix, and Simon Charette, Tim Graham & Loïc Bistuer for review.
Diffstat (limited to 'tests/select_for_update')
-rw-r--r--tests/select_for_update/tests.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/tests/select_for_update/tests.py b/tests/select_for_update/tests.py
index a4136d7a16..54ba0d1d27 100644
--- a/tests/select_for_update/tests.py
+++ b/tests/select_for_update/tests.py
@@ -77,7 +77,8 @@ class SelectForUpdateTests(TransactionTestCase):
Test that the backend's FOR UPDATE variant appears in
generated SQL when select_for_update is invoked.
"""
- list(Person.objects.all().select_for_update())
+ with transaction.atomic():
+ list(Person.objects.all().select_for_update())
self.assertTrue(self.has_for_update_sql(connection))
@skipUnlessDBFeature('has_select_for_update_nowait')
@@ -86,7 +87,8 @@ class SelectForUpdateTests(TransactionTestCase):
Test that the backend's FOR UPDATE NOWAIT variant appears in
generated SQL when select_for_update is invoked.
"""
- list(Person.objects.all().select_for_update(nowait=True))
+ with transaction.atomic():
+ list(Person.objects.all().select_for_update(nowait=True))
self.assertTrue(self.has_for_update_sql(connection, nowait=True))
@requires_threading
@@ -125,6 +127,26 @@ class SelectForUpdateTests(TransactionTestCase):
Person.objects.all().select_for_update(nowait=True)
)
+ @skipUnlessDBFeature('has_select_for_update')
+ def test_for_update_requires_transaction(self):
+ """
+ Test that a TransactionManagementError is raised
+ when a select_for_update query is executed outside of a transaction.
+ """
+ with self.assertRaises(transaction.TransactionManagementError):
+ list(Person.objects.all().select_for_update())
+
+ @skipUnlessDBFeature('has_select_for_update')
+ def test_for_update_requires_transaction_only_in_execution(self):
+ """
+ Test that no TransactionManagementError is raised
+ when select_for_update is invoked outside of a transaction -
+ only when the query is executed.
+ """
+ people = Person.objects.all().select_for_update()
+ with self.assertRaises(transaction.TransactionManagementError):
+ list(people)
+
def run_select_for_update(self, status, nowait=False):
"""
Utility method that runs a SELECT FOR UPDATE against all