Code

Ticket #19645: ticket_19645.diff

File ticket_19645.diff, 3.7 KB (added by slurms, 18 months ago)
Line 
1diff --git a/tests/regressiontests/middleware/models.py b/tests/regressiontests/middleware/models.py
2index 71abcc5..bd5a348 100644
3--- a/tests/regressiontests/middleware/models.py
4+++ b/tests/regressiontests/middleware/models.py
5@@ -1 +1,17 @@
6-# models.py file for tests to run.
7+# coding: utf-8
8+from django.contrib.auth.models import User
9+from django.db import models
10+from django.utils.encoding import python_2_unicode_compatible
11+
12+
13+@python_2_unicode_compatible
14+class Band(models.Model):
15+     name = models.CharField(max_length=100)
16+     bio = models.TextField()
17+     sign_date = models.DateField()
18+
19+     class Meta:
20+         ordering = ('name',)
21+
22+     def __str__(self):
23+         return self.name
24diff --git a/tests/regressiontests/middleware/tests.py b/tests/regressiontests/middleware/tests.py
25index e3d8350..c054a89 100644
26--- a/tests/regressiontests/middleware/tests.py
27+++ b/tests/regressiontests/middleware/tests.py
28@@ -7,16 +7,21 @@ from io import BytesIO
29 
30 from django.conf import settings
31 from django.core import mail
32+from django.db import transaction
33 from django.http import HttpRequest
34 from django.http import HttpResponse, StreamingHttpResponse
35 from django.middleware.clickjacking import XFrameOptionsMiddleware
36 from django.middleware.common import CommonMiddleware
37 from django.middleware.http import ConditionalGetMiddleware
38 from django.middleware.gzip import GZipMiddleware
39-from django.test import TestCase, RequestFactory
40+from django.middleware.transaction import TransactionMiddleware
41+from django.test import TransactionTestCase, TestCase, RequestFactory
42 from django.test.utils import override_settings
43 from django.utils import six
44 from django.utils.six.moves import xrange
45+from django.utils.timezone import now
46+
47+from .models import Band
48 
49 
50 class CommonMiddlewareTest(TestCase):
51@@ -627,3 +632,47 @@ class ETagGZipMiddlewareTest(TestCase):
52         nogzip_etag = response.get('ETag')
53 
54         self.assertNotEqual(gzip_etag, nogzip_etag)
55+
56+class TransactionMiddlewareTest(TransactionTestCase):
57+    """
58+    Test the transaction middleware.
59+    """
60+    def setUp(self):
61+        self.request = HttpRequest()
62+        self.request.META = {
63+            'SERVER_NAME': 'testserver',
64+            'SERVER_PORT': 80,
65+        }
66+        self.request.path = self.request.path_info = "/"
67+        self.response = HttpResponse()
68+        self.response.status_code = 200
69+
70+    def test_request(self):
71+        TransactionMiddleware().process_request(self.request)
72+        self.assertTrue(transaction.is_managed())
73+
74+    def test_managed_response(self):
75+        transaction.enter_transaction_management()
76+        transaction.managed(True)
77+        Band.objects.create(name='The Beatles', bio='Test bio',
78+                            sign_date=now().date())
79+        self.assertTrue(transaction.is_dirty())
80+        TransactionMiddleware().process_response(self.request, self.response)
81+        self.assertFalse(transaction.is_dirty())
82+        self.assertEqual(Band.objects.count(), 1)
83+
84+    def test_unmanaged_response(self):
85+        transaction.managed(False)
86+        TransactionMiddleware().process_response(self.request, self.response)
87+        self.assertFalse(transaction.is_managed())
88+        self.assertFalse(transaction.is_dirty())
89+
90+    def test_exception(self):
91+        transaction.enter_transaction_management()
92+        transaction.managed(True)
93+        Band.objects.create(name='The Beatles', bio='Test bio',
94+                            sign_date=now().date())
95+        self.assertTrue(transaction.is_dirty())
96+        TransactionMiddleware().process_exception(self.request, None)
97+        self.assertEqual(Band.objects.count(), 0)
98+        self.assertFalse(transaction.is_dirty())