Ticket #19645: ticket_19645.diff
File ticket_19645.diff, 3.7 KB (added by , 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 2 from django.contrib.auth.models import User 3 from django.db import models 4 from django.utils.encoding import python_2_unicode_compatible 5 6 7 @python_2_unicode_compatible 8 class 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 7 7 8 8 from django.conf import settings 9 9 from django.core import mail 10 from django.db import transaction 10 11 from django.http import HttpRequest 11 12 from django.http import HttpResponse, StreamingHttpResponse 12 13 from django.middleware.clickjacking import XFrameOptionsMiddleware 13 14 from django.middleware.common import CommonMiddleware 14 15 from django.middleware.http import ConditionalGetMiddleware 15 16 from django.middleware.gzip import GZipMiddleware 16 from django.test import TestCase, RequestFactory 17 from django.middleware.transaction import TransactionMiddleware 18 from django.test import TransactionTestCase, TestCase, RequestFactory 17 19 from django.test.utils import override_settings 18 20 from django.utils import six 19 21 from django.utils.six.moves import xrange 22 from django.utils.timezone import now 23 24 from .models import Band 20 25 21 26 22 27 class CommonMiddlewareTest(TestCase): … … class ETagGZipMiddlewareTest(TestCase): 627 632 nogzip_etag = response.get('ETag') 628 633 629 634 self.assertNotEqual(gzip_etag, nogzip_etag) 635 636 class 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())