Ticket #24446: patch

File patch, 2.3 KB (added by Mathieu Pillard, 9 years ago)

Test demonstrating the issue

  • tests/handlers/tests.py

    diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py
    index a1fed74..871b65e 100644
    a b from __future__ import unicode_literals  
    44
    55from django.core.handlers.wsgi import WSGIHandler, WSGIRequest
    66from django.core.signals import request_finished, request_started
    7 from django.db import close_old_connections, connection
     7from django.db import close_old_connections, connection, transaction
    88from django.test import (
    99    RequestFactory, TestCase, TransactionTestCase, override_settings,
    1010)
    class TransactionsPerRequestTests(TransactionTestCase):  
    121121        old_atomic_requests = connection.settings_dict['ATOMIC_REQUESTS']
    122122        try:
    123123            connection.settings_dict['ATOMIC_REQUESTS'] = True
    124             response = self.client.get('/in_transaction/')
     124            with self.assertNumQueries(1):
     125                response = self.client.get('/in_transaction/')
    125126        finally:
    126127            connection.settings_dict['ATOMIC_REQUESTS'] = old_atomic_requests
    127128        self.assertContains(response, 'True')
    class TransactionsPerRequestTests(TransactionTestCase):  
    130131        old_atomic_requests = connection.settings_dict['ATOMIC_REQUESTS']
    131132        try:
    132133            connection.settings_dict['ATOMIC_REQUESTS'] = True
    133             response = self.client.get('/not_in_transaction/')
     134            with self.assertNumQueries(0):
     135                response = self.client.get('/not_in_transaction/')
    134136        finally:
    135137            connection.settings_dict['ATOMIC_REQUESTS'] = old_atomic_requests
    136138        self.assertContains(response, 'False')
    137139
     140    def test_within_existing_transaction(self):
     141        old_atomic_requests = connection.settings_dict['ATOMIC_REQUESTS']
     142        try:
     143            connection.settings_dict['ATOMIC_REQUESTS'] = True
     144            with transaction.atomic():
     145                # @atomic() opened a transaction, we should not see an extra
     146                # query.
     147                with self.assertNumQueries(0):
     148                    response = self.client.get('/in_transaction/')
     149        finally:
     150            connection.settings_dict['ATOMIC_REQUESTS'] = old_atomic_requests
     151        self.assertContains(response, 'True')
     152
    138153
    139154@override_settings(ROOT_URLCONF='handlers.urls')
    140155class SignalsTests(TestCase):
Back to Top