Ticket #8138: r9640based8138.diff
File r9640based8138.diff, 8.0 KB (added by , 16 years ago) |
---|
-
django/test/client.py
19 19 from django.utils.encoding import smart_str 20 20 from django.utils.http import urlencode 21 21 from django.utils.itercompat import is_iterable 22 from django.db import transaction 22 23 23 24 BOUNDARY = 'BoUnDaRyStRiNg' 24 25 MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY … … 61 62 62 63 signals.request_started.send(sender=self.__class__) 63 64 try: 65 transaction.enter_transaction_management() 66 transaction.managed(True) 64 67 request = WSGIRequest(environ) 65 68 response = self.get_response(request) 69 transaction.commit() 70 transaction.leave_transaction_management() 66 71 67 72 # Apply response middleware. 68 73 for middleware_method in self._response_middleware: … … 172 177 Obtains the current session variables. 173 178 """ 174 179 if 'django.contrib.sessions' in settings.INSTALLED_APPS: 180 # If a session db change hangs in a transaction, commit, 181 # just to be sure. 182 if transaction.is_dirty(): 183 transaction.commit() 175 184 engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) 176 185 cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None) 177 186 if cookie: -
django/test/testcases.py
8 8 from django.core.management import call_command 9 9 from django.core.urlresolvers import clear_url_caches 10 10 from django.db import transaction 11 from django.db.models.signals import post_save 11 12 from django.http import QueryDict 12 13 from django.test import _doctest as doctest 13 14 from django.test.client import Client … … 168 169 # Rollback, in case of database errors. Otherwise they'd have 169 170 # side effects on other tests. 170 171 transaction.rollback_unless_managed() 172 173 def run(self, test, compileflags=None, out=None, clear_globs=True): 174 """ 175 Wraps the parent run() and encloses it in a transaction. 176 """ 177 transaction.enter_transaction_management() 178 transaction.managed(True) 179 result = doctest.DocTestRunner.run(self, test, compileflags, out, clear_globs) 180 transaction.rollback() 181 transaction.leave_transaction_management() 182 return result 171 183 172 class T estCase(unittest.TestCase):184 class TransactionTestCase(unittest.TestCase): 173 185 def _pre_setup(self): 174 186 """Performs any pre-test setup. This includes: 175 187 … … 180 192 ROOT_URLCONF with it. 181 193 * Clearing the mail test outbox. 182 194 """ 195 self._fixture_setup() 196 self._urlconf_setup() 197 mail.outbox = [] 198 199 def _fixture_setup(self): 183 200 call_command('flush', verbosity=0, interactive=False) 184 201 if hasattr(self, 'fixtures'): 185 202 # We have to use this slightly awkward syntax due to the fact 186 203 # that we're using *args and **kwargs together. 187 204 call_command('loaddata', *self.fixtures, **{'verbosity': 0}) 205 206 def _urlconf_setup(self): 188 207 if hasattr(self, 'urls'): 189 208 self._old_root_urlconf = settings.ROOT_URLCONF 190 209 settings.ROOT_URLCONF = self.urls 191 210 clear_url_caches() 192 mail.outbox = []193 211 194 212 def __call__(self, result=None): 195 213 """ … … 206 224 import sys 207 225 result.addError(self, sys.exc_info()) 208 226 return 209 super(T estCase, self).__call__(result)227 super(TransactionTestCase, self).__call__(result) 210 228 try: 211 229 self._post_teardown() 212 230 except (KeyboardInterrupt, SystemExit): … … 221 239 222 240 * Putting back the original ROOT_URLCONF if it was changed. 223 241 """ 242 self._fixture_teardown() 243 self._urlconf_teardown() 244 245 def _fixture_teardown(self): 246 pass 247 248 def _urlconf_teardown(self): 224 249 if hasattr(self, '_old_root_urlconf'): 225 250 settings.ROOT_URLCONF = self._old_root_urlconf 226 251 clear_url_caches() … … 354 379 self.failIf(template_name in template_names, 355 380 (u"Template '%s' was used unexpectedly in rendering the" 356 381 u" response") % template_name) 382 383 class TestCase(TransactionTestCase): 384 """ 385 Does basically the same as TransactionTestCase, but surrounds every test 386 with a transaction. You have to use TransactionTestCase, if you need 387 transaction management inside a test. 388 """ 389 390 # has the db been changed during the test 391 db_was_changed = False 392 393 def _fixture_setup(self): 394 transaction.enter_transaction_management() 395 transaction.managed(True) 396 397 # whenever a save occured, the db must be dirty 398 post_save.connect(self._set_db_was_changed) 399 # this seems more elegant than patching ClientHandler 400 #request_started.connect(self._do_commit) 401 #request_finished.connect(self._do_commit) 402 403 if hasattr(self, 'fixtures'): 404 call_command('loaddata', *self.fixtures, **{ 405 'verbosity': 0, 406 'commit': False 407 }) 408 # TODO: find out, if loaddata does emit a post_save signal 409 self._set_db_was_changed() 410 411 def _fixture_teardown(self): 412 # If the transaction is not dirty, but the DB was changed, 413 # a commit must have happened, so flush instead of rollback. 414 # This currently doesn't catch the following case: 415 # Inside a test a commit happens and after that more data is changed. 416 if not transaction.is_dirty() and self.db_was_changed: 417 transaction.leave_transaction_management() 418 call_command('flush', verbosity=0, interactive=False) 419 else: 420 transaction.rollback() 421 transaction.leave_transaction_management() 422 423 def _set_db_was_changed(self, *args, **kwargs): 424 self.db_was_changed = True 425 426 def _do_commit(self, *args, **kwargs): 427 transaction.commit() -
django/test/__init__.py
3 3 """ 4 4 5 5 from django.test.client import Client 6 from django.test.testcases import TestCase 6 from django.test.testcases import TestCase, TransactionTestCase -
tests/regressiontests/admin_views/tests.py
1 1 # coding: utf-8 2 2 3 from django.test import TestCase 3 from django.test import TestCase, TransactionTestCase 4 4 from django.contrib.auth.models import User, Permission 5 5 from django.contrib.contenttypes.models import ContentType 6 6 from django.contrib.admin.models import LogEntry … … 173 173 ct = ContentType.objects.get_for_model(Model) 174 174 return Permission.objects.get(content_type=ct, codename=perm) 175 175 176 class AdminViewPermissionsTest(TestCase): 177 """Tests for Admin Views Permissions.""" 176 class AdminViewPermissionsTest(TransactionTestCase): 177 """ 178 Tests for Admin Views Permissions. 178 179 180 We need TransactionTestCase here, because some data is lodaed manually 181 via the ORM, not via fixtures and test.Client is used. 182 """ 183 179 184 fixtures = ['admin-views-users.xml'] 180 185 181 186 def setUp(self): … … 562 567 should_contain = """<h1>Change model with string primary key</h1>""" 563 568 self.assertContains(response, should_contain) 564 569 565 566 class SecureViewTest(T estCase):570 571 class SecureViewTest(TransactionTestCase): 567 572 fixtures = ['admin-views-users.xml'] 568 573 569 574 def setUp(self):