Ticket #19645: ticket_19645.diff

File ticket_19645.diff, 3.7 KB (added by Nick Sandford, 12 years ago)
  • tests/regressiontests/middleware/models.py

    diff --git a/tests/regressiontests/middleware/models.py b/tests/regressiontests/middleware/models.py
    index 71abcc5..bd5a348 100644
    a b  
    1 # models.py file for tests to run.
     1# coding: utf-8
     2from django.contrib.auth.models import User
     3from django.db import models
     4from django.utils.encoding import python_2_unicode_compatible
     5
     6
     7@python_2_unicode_compatible
     8class Band(models.Model):
     9     name = models.CharField(max_length=100)
     10     bio = models.TextField()
     11     sign_date = models.DateField()
     12
     13     class Meta:
     14         ordering = ('name',)
     15
     16     def __str__(self):
     17         return self.name
  • tests/regressiontests/middleware/tests.py

    diff --git a/tests/regressiontests/middleware/tests.py b/tests/regressiontests/middleware/tests.py
    index e3d8350..c054a89 100644
    a b from io import BytesIO  
    77
    88from django.conf import settings
    99from django.core import mail
     10from django.db import transaction
    1011from django.http import HttpRequest
    1112from django.http import HttpResponse, StreamingHttpResponse
    1213from django.middleware.clickjacking import XFrameOptionsMiddleware
    1314from django.middleware.common import CommonMiddleware
    1415from django.middleware.http import ConditionalGetMiddleware
    1516from django.middleware.gzip import GZipMiddleware
    16 from django.test import TestCase, RequestFactory
     17from django.middleware.transaction import TransactionMiddleware
     18from django.test import TransactionTestCase, TestCase, RequestFactory
    1719from django.test.utils import override_settings
    1820from django.utils import six
    1921from django.utils.six.moves import xrange
     22from django.utils.timezone import now
     23
     24from .models import Band
    2025
    2126
    2227class CommonMiddlewareTest(TestCase):
    class ETagGZipMiddlewareTest(TestCase):  
    627632        nogzip_etag = response.get('ETag')
    628633
    629634        self.assertNotEqual(gzip_etag, nogzip_etag)
     635
     636class TransactionMiddlewareTest(TransactionTestCase):
     637    """
     638    Test the transaction middleware.
     639    """
     640    def setUp(self):
     641        self.request = HttpRequest()
     642        self.request.META = {
     643            'SERVER_NAME': 'testserver',
     644            'SERVER_PORT': 80,
     645        }
     646        self.request.path = self.request.path_info = "/"
     647        self.response = HttpResponse()
     648        self.response.status_code = 200
     649
     650    def test_request(self):
     651        TransactionMiddleware().process_request(self.request)
     652        self.assertTrue(transaction.is_managed())
     653
     654    def test_managed_response(self):
     655        transaction.enter_transaction_management()
     656        transaction.managed(True)
     657        Band.objects.create(name='The Beatles', bio='Test bio',
     658                            sign_date=now().date())
     659        self.assertTrue(transaction.is_dirty())
     660        TransactionMiddleware().process_response(self.request, self.response)
     661        self.assertFalse(transaction.is_dirty())
     662        self.assertEqual(Band.objects.count(), 1)
     663
     664    def test_unmanaged_response(self):
     665        transaction.managed(False)
     666        TransactionMiddleware().process_response(self.request, self.response)
     667        self.assertFalse(transaction.is_managed())
     668        self.assertFalse(transaction.is_dirty())
     669
     670    def test_exception(self):
     671        transaction.enter_transaction_management()
     672        transaction.managed(True)
     673        Band.objects.create(name='The Beatles', bio='Test bio',
     674                            sign_date=now().date())
     675        self.assertTrue(transaction.is_dirty())
     676        TransactionMiddleware().process_exception(self.request, None)
     677        self.assertEqual(Band.objects.count(), 0)
     678        self.assertFalse(transaction.is_dirty())
Back to Top