Skip to content

Commit

Permalink
[1.4.x] Fixed #19645 -- Added tests for TransactionMiddleware
Browse files Browse the repository at this point in the history
Backpatch of f556df9. Backpatching
these tests so that it will be easier to backpatch the fix for #19707.
  • Loading branch information
akaariai committed Feb 10, 2013
1 parent 056b2b5 commit 498a5de
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
12 changes: 11 additions & 1 deletion tests/regressiontests/middleware/models.py
@@ -1 +1,11 @@
# models.py file for tests to run.
from django.db import models


class Band(models.Model):
name = models.CharField(max_length=100)

class Meta:
ordering = ('name',)

def __unicode__(self):
return self.name
49 changes: 48 additions & 1 deletion tests/regressiontests/middleware/tests.py
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, with_statement

import gzip
import re
Expand All @@ -7,15 +8,19 @@

from django.conf import settings
from django.core import mail
from django.db import transaction
from django.http import HttpRequest
from django.http import HttpResponse
from django.middleware.clickjacking import XFrameOptionsMiddleware
from django.middleware.common import CommonMiddleware
from django.middleware.http import ConditionalGetMiddleware
from django.middleware.gzip import GZipMiddleware
from django.test import TestCase, RequestFactory
from django.middleware.transaction import TransactionMiddleware
from django.test import TransactionTestCase, TestCase, RequestFactory
from django.test.utils import override_settings

from .models import Band

class CommonMiddlewareTest(TestCase):
def setUp(self):
self.append_slash = settings.APPEND_SLASH
Expand Down Expand Up @@ -613,3 +618,45 @@ def test_compress_response(self):
ETagGZipMiddlewareTest = override_settings(
USE_ETAGS=True,
)(ETagGZipMiddlewareTest)

class TransactionMiddlewareTest(TransactionTestCase):
"""
Test the transaction middleware.
"""
def setUp(self):
self.request = HttpRequest()
self.request.META = {
'SERVER_NAME': 'testserver',
'SERVER_PORT': 80,
}
self.request.path = self.request.path_info = "/"
self.response = HttpResponse()
self.response.status_code = 200

def test_request(self):
TransactionMiddleware().process_request(self.request)
self.assertTrue(transaction.is_managed())

def test_managed_response(self):
transaction.enter_transaction_management()
transaction.managed(True)
Band.objects.create(name='The Beatles')
self.assertTrue(transaction.is_dirty())
TransactionMiddleware().process_response(self.request, self.response)
self.assertFalse(transaction.is_dirty())
self.assertEqual(Band.objects.count(), 1)

def test_unmanaged_response(self):
transaction.managed(False)
TransactionMiddleware().process_response(self.request, self.response)
self.assertFalse(transaction.is_managed())
self.assertFalse(transaction.is_dirty())

def test_exception(self):
transaction.enter_transaction_management()
transaction.managed(True)
Band.objects.create(name='The Beatles')
self.assertTrue(transaction.is_dirty())
TransactionMiddleware().process_exception(self.request, None)
self.assertEqual(Band.objects.count(), 0)
self.assertFalse(transaction.is_dirty())

0 comments on commit 498a5de

Please sign in to comment.