diff -r ed4d2e6277b2 django/contrib/auth/tests/views.py
|
a
|
b
|
|
| 1 | 1 | |
| 2 | 2 | import re |
| 3 | 3 | from django.contrib.auth.models import User |
| 4 | | from django.test import TestCase |
| | 4 | from django.test import TransactionTestCase |
| 5 | 5 | from django.core import mail |
| 6 | 6 | |
| 7 | | class PasswordResetTest(TestCase): |
| | 7 | class PasswordResetTest(TransactionTestCase): |
| 8 | 8 | fixtures = ['authtestdata.json'] |
| 9 | 9 | urls = 'django.contrib.auth.urls' |
| 10 | 10 | |
diff -r ed4d2e6277b2 django/core/management/commands/loaddata.py
|
a
|
b
|
|
| 28 | 28 | |
| 29 | 29 | verbosity = int(options.get('verbosity', 1)) |
| 30 | 30 | show_traceback = options.get('traceback', False) |
| | 31 | no_commit = options.get('no_commit', False) |
| 31 | 32 | |
| 32 | 33 | # Keep a count of the installed objects and fixtures |
| 33 | 34 | fixture_count = 0 |
| … |
… |
|
| 44 | 45 | |
| 45 | 46 | # Start transaction management. All fixtures are installed in a |
| 46 | 47 | # single transaction to ensure that all references are resolved. |
| 47 | | transaction.commit_unless_managed() |
| 48 | | transaction.enter_transaction_management() |
| 49 | | transaction.managed(True) |
| | 48 | if not no_commit: |
| | 49 | transaction.commit_unless_managed() |
| | 50 | transaction.enter_transaction_management() |
| | 51 | transaction.managed(True) |
| 50 | 52 | |
| 51 | 53 | app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()] |
| 52 | 54 | for fixture_label in fixture_labels: |
| … |
… |
|
| 133 | 135 | (format, fixture_name, humanize(fixture_dir)) |
| 134 | 136 | |
| 135 | 137 | |
| 136 | | # If any of the fixtures we loaded contain 0 objects, assume that an |
| | 138 | # If any of the fixtures we loaded contain 0 objects, assume that an |
| 137 | 139 | # error was encountered during fixture loading. |
| 138 | 140 | if 0 in objects_per_fixture: |
| 139 | 141 | sys.stderr.write( |
| … |
… |
|
| 142 | 144 | transaction.rollback() |
| 143 | 145 | transaction.leave_transaction_management() |
| 144 | 146 | return |
| 145 | | |
| 146 | | # If we found even one object in a fixture, we need to reset the |
| | 147 | |
| | 148 | # If we found even one object in a fixture, we need to reset the |
| 147 | 149 | # database sequences. |
| 148 | 150 | if object_count > 0: |
| 149 | 151 | sequence_sql = connection.ops.sequence_reset_sql(self.style, models) |
| … |
… |
|
| 152 | 154 | print "Resetting sequences" |
| 153 | 155 | for line in sequence_sql: |
| 154 | 156 | cursor.execute(line) |
| 155 | | |
| 156 | | transaction.commit() |
| 157 | | transaction.leave_transaction_management() |
| | 157 | |
| | 158 | if not no_commit: |
| | 159 | transaction.commit() |
| | 160 | transaction.leave_transaction_management() |
| 158 | 161 | |
| 159 | 162 | if object_count == 0: |
| 160 | 163 | if verbosity > 1: |
| … |
… |
|
| 162 | 165 | else: |
| 163 | 166 | if verbosity > 0: |
| 164 | 167 | print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count) |
| 165 | | |
| | 168 | |
| 166 | 169 | # Close the DB connection. This is required as a workaround for an |
| 167 | 170 | # edge case in MySQL: if the same connection is used to |
| 168 | 171 | # create tables, load data, and query, the query can return |
| 169 | 172 | # incorrect results. See Django #7572, MySQL #37735. |
| 170 | | connection.close() |
| | 173 | if not no_commit: |
| | 174 | connection.close() |
diff -r ed4d2e6277b2 django/test/__init__.py
|
a
|
b
|
|
| 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 |
diff -r ed4d2e6277b2 django/test/testcases.py
|
a
|
b
|
|
| 55 | 55 | """Tries to do a 'xml-comparision' of want and got. Plain string |
| 56 | 56 | comparision doesn't always work because, for example, attribute |
| 57 | 57 | ordering should not be important. |
| 58 | | |
| | 58 | |
| 59 | 59 | Based on http://codespeak.net/svn/lxml/trunk/src/lxml/doctestcompare.py |
| 60 | 60 | """ |
| 61 | 61 | _norm_whitespace_re = re.compile(r'[ \t\n][ \t\n]+') |
| … |
… |
|
| 102 | 102 | wrapper = '<root>%s</root>' |
| 103 | 103 | want = wrapper % want |
| 104 | 104 | got = wrapper % got |
| 105 | | |
| | 105 | |
| 106 | 106 | # Parse the want and got strings, and compare the parsings. |
| 107 | 107 | try: |
| 108 | 108 | want_root = parseString(want).firstChild |
| … |
… |
|
| 169 | 169 | # side effects on other tests. |
| 170 | 170 | transaction.rollback_unless_managed() |
| 171 | 171 | |
| 172 | | class TestCase(unittest.TestCase): |
| | 172 | def run(self, test, compileflags=None, out=None, clear_globs=True): |
| | 173 | """ |
| | 174 | Wraps the parent run() and encloses it in a transaction. |
| | 175 | """ |
| | 176 | tests_use_transactions = test.docstring.strip().startswith('# TESTS USE TRANSACTIONS') |
| | 177 | if not tests_use_transactions: |
| | 178 | transaction.enter_transaction_management() |
| | 179 | transaction.managed(True) |
| | 180 | result = doctest.DocTestRunner.run(self, test, compileflags, out, clear_globs) |
| | 181 | if not tests_use_transactions: |
| | 182 | transaction.rollback() |
| | 183 | transaction.leave_transaction_management() |
| | 184 | return result |
| | 185 | |
| | 186 | class TransactionTestCase(unittest.TestCase): |
| 173 | 187 | def _pre_setup(self): |
| 174 | 188 | """Performs any pre-test setup. This includes: |
| 175 | 189 | |
| 176 | 190 | * Flushing the database. |
| 177 | | * If the Test Case class has a 'fixtures' member, installing the |
| | 191 | * If the Test Case class has a 'fixtures' member, installing the |
| 178 | 192 | named fixtures. |
| 179 | 193 | * If the Test Case class has a 'urls' member, replace the |
| 180 | 194 | ROOT_URLCONF with it. |
| 181 | 195 | * Clearing the mail test outbox. |
| 182 | 196 | """ |
| | 197 | self._fixture_setup() |
| | 198 | self._urlconf_setup() |
| | 199 | mail.outbox = [] |
| | 200 | |
| | 201 | def _fixture_setup(self): |
| 183 | 202 | call_command('flush', verbosity=0, interactive=False) |
| 184 | 203 | if hasattr(self, 'fixtures'): |
| 185 | 204 | # We have to use this slightly awkward syntax due to the fact |
| 186 | 205 | # that we're using *args and **kwargs together. |
| 187 | 206 | call_command('loaddata', *self.fixtures, **{'verbosity': 0}) |
| | 207 | |
| | 208 | def _urlconf_setup(self): |
| 188 | 209 | if hasattr(self, 'urls'): |
| 189 | 210 | self._old_root_urlconf = settings.ROOT_URLCONF |
| 190 | 211 | settings.ROOT_URLCONF = self.urls |
| 191 | 212 | clear_url_caches() |
| 192 | | mail.outbox = [] |
| 193 | 213 | |
| 194 | 214 | def __call__(self, result=None): |
| 195 | 215 | """ |
| … |
… |
|
| 206 | 226 | import sys |
| 207 | 227 | result.addError(self, sys.exc_info()) |
| 208 | 228 | return |
| 209 | | super(TestCase, self).__call__(result) |
| | 229 | super(TransactionTestCase, self).__call__(result) |
| 210 | 230 | try: |
| 211 | 231 | self._post_teardown() |
| 212 | 232 | except (KeyboardInterrupt, SystemExit): |
| … |
… |
|
| 221 | 241 | |
| 222 | 242 | * Putting back the original ROOT_URLCONF if it was changed. |
| 223 | 243 | """ |
| | 244 | self._fixture_teardown() |
| | 245 | self._urlconf_teardown() |
| | 246 | |
| | 247 | def _fixture_teardown(self): |
| | 248 | pass |
| | 249 | |
| | 250 | def _urlconf_teardown(self): |
| 224 | 251 | if hasattr(self, '_old_root_urlconf'): |
| 225 | 252 | settings.ROOT_URLCONF = self._old_root_urlconf |
| 226 | 253 | clear_url_caches() |
| … |
… |
|
| 354 | 381 | self.failIf(template_name in template_names, |
| 355 | 382 | (u"Template '%s' was used unexpectedly in rendering the" |
| 356 | 383 | u" response") % template_name) |
| | 384 | |
| | 385 | class TestCase(TransactionTestCase): |
| | 386 | """ |
| | 387 | Does basically the same as TransactionTestCase, but surrounds every test |
| | 388 | with a transaction. You have to use TransactionTestCase, if you need |
| | 389 | transaction management inside a test. |
| | 390 | """ |
| | 391 | def _fixture_setup(self): |
| | 392 | transaction.enter_transaction_management() |
| | 393 | transaction.managed(True) |
| | 394 | |
| | 395 | if hasattr(self, 'fixtures'): |
| | 396 | call_command('loaddata', *self.fixtures, **{ |
| | 397 | 'verbosity': 0, |
| | 398 | 'no_commit': True |
| | 399 | }) |
| | 400 | |
| | 401 | def _fixture_teardown(self): |
| | 402 | transaction.rollback() |
| | 403 | transaction.leave_transaction_management() |
diff -r ed4d2e6277b2 tests/modeltests/fixtures/models.py
|
a
|
b
|
|
| 22 | 22 | ordering = ('-pub_date', 'headline') |
| 23 | 23 | |
| 24 | 24 | __test__ = {'API_TESTS': """ |
| | 25 | # TESTS USE TRANSACTIONS |
| 25 | 26 | >>> from django.core import management |
| 26 | 27 | >>> from django.db.models import get_app |
| 27 | 28 | |
| … |
… |
|
| 92 | 93 | def testClassFixtures(self): |
| 93 | 94 | "Check that test case has installed 4 fixture objects" |
| 94 | 95 | self.assertEqual(Article.objects.count(), 4) |
| 95 | | self.assertEquals(str(Article.objects.all()), "[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]") |
| | 96 | self.assertEquals(str(Article.objects.order_by('headline')), "[<Article: Copyright is fine the way it is>, <Article: Django conquers world!>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]") |
diff -r ed4d2e6277b2 tests/modeltests/test_client/models.py
|
a
|
b
|
|
| 20 | 20 | rather than the HTML rendered to the end-user. |
| 21 | 21 | |
| 22 | 22 | """ |
| 23 | | from django.test import Client, TestCase |
| | 23 | from django.test import Client, TransactionTestCase |
| 24 | 24 | from django.core import mail |
| 25 | 25 | |
| 26 | | class ClientTest(TestCase): |
| | 26 | class ClientTest(TransactionTestCase): |
| 27 | 27 | fixtures = ['testdata.json'] |
| 28 | 28 | |
| 29 | 29 | def test_get_view(self): |
diff -r ed4d2e6277b2 tests/regressiontests/admin_views/tests.py
|
a
|
b
|
|
| 1 | 1 | |
| 2 | | from django.test import TestCase |
| | 2 | from django.test import TestCase, TransactionTestCase |
| 3 | 3 | from django.contrib.auth.models import User, Permission |
| 4 | 4 | from django.contrib.contenttypes.models import ContentType |
| 5 | 5 | from django.contrib.admin.models import LogEntry |
| … |
… |
|
| 15 | 15 | ct = ContentType.objects.get_for_model(Model) |
| 16 | 16 | return Permission.objects.get(content_type=ct,codename=perm) |
| 17 | 17 | |
| 18 | | class AdminViewPermissionsTest(TestCase): |
| | 18 | class AdminViewPermissionsTest(TransactionTestCase): |
| 19 | 19 | """Tests for Admin Views Permissions.""" |
| 20 | 20 | |
| 21 | 21 | fixtures = ['admin-views-users.xml'] |
diff -r ed4d2e6277b2 tests/regressiontests/test_client_regress/models.py
|
a
|
b
|
|
| 2 | 2 | Regression tests for the Test Client, especially the customized assertions. |
| 3 | 3 | """ |
| 4 | 4 | |
| 5 | | from django.test import Client, TestCase |
| | 5 | from django.test import Client, TestCase, TransactionTestCase |
| 6 | 6 | from django.core.urlresolvers import reverse |
| 7 | 7 | from django.core.exceptions import SuspiciousOperation |
| 8 | 8 | |
| … |
… |
|
| 239 | 239 | except AssertionError, e: |
| 240 | 240 | self.assertEqual(str(e), "The form 'form' in context 0 does not contain the non-field error 'Some error.' (actual errors: )") |
| 241 | 241 | |
| 242 | | class LoginTests(TestCase): |
| | 242 | class LoginTests(TransactionTestCase): |
| 243 | 243 | fixtures = ['testdata'] |
| 244 | 244 | |
| 245 | 245 | def test_login_different_client(self): |
| … |
… |
|
| 283 | 283 | self.assertEqual(response.status_code, 200) |
| 284 | 284 | self.assertEqual(response.content, 'Hi, Arthur') |
| 285 | 285 | |
| 286 | | class ExceptionTests(TestCase): |
| | 286 | class ExceptionTests(TransactionTestCase): |
| 287 | 287 | fixtures = ['testdata.json'] |
| 288 | 288 | |
| 289 | 289 | def test_exception_cleared(self): |
diff -r ed4d2e6277b2 tests/regressiontests/views/tests/defaults.py
|
a
|
b
|
|
| 1 | 1 | from os import path |
| 2 | 2 | |
| 3 | 3 | from django.conf import settings |
| 4 | | from django.test import TestCase |
| | 4 | from django.test import TransactionTestCase |
| 5 | 5 | from django.contrib.contenttypes.models import ContentType |
| 6 | 6 | |
| 7 | 7 | from regressiontests.views.models import Author, Article |
| 8 | 8 | |
| 9 | | class DefaultsTests(TestCase): |
| | 9 | class DefaultsTests(TransactionTestCase): |
| 10 | 10 | """Test django views in django/views/defaults.py""" |
| 11 | 11 | fixtures = ['testdata.json'] |
| 12 | 12 | |
diff -r ed4d2e6277b2 tests/regressiontests/views/tests/generic/create_update.py
|
a
|
b
|
|
| 1 | 1 | import datetime |
| 2 | 2 | |
| 3 | | from django.test import TestCase |
| | 3 | from django.test import TransactionTestCase |
| 4 | 4 | from django.core.exceptions import ImproperlyConfigured |
| 5 | 5 | from regressiontests.views.models import Article, UrlArticle |
| 6 | 6 | |
| 7 | | class CreateObjectTest(TestCase): |
| | 7 | class CreateObjectTest(TransactionTestCase): |
| 8 | 8 | |
| 9 | 9 | fixtures = ['testdata.json'] |
| 10 | 10 | |
| … |
… |
|
| 65 | 65 | '/views/create_update/view/article/some-other-slug/', |
| 66 | 66 | target_status_code=404) |
| 67 | 67 | |
| 68 | | class UpdateDeleteObjectTest(TestCase): |
| | 68 | class UpdateDeleteObjectTest(TransactionTestCase): |
| 69 | 69 | |
| 70 | 70 | fixtures = ['testdata.json'] |
| 71 | 71 | |
| … |
… |
|
| 111 | 111 | else: |
| 112 | 112 | self.fail('Object was not deleted.') |
| 113 | 113 | |
| 114 | | class PostSaveRedirectTests(TestCase): |
| | 114 | class PostSaveRedirectTests(TransactionTestCase): |
| 115 | 115 | """ |
| 116 | 116 | Verifies that the views redirect to the correct locations depending on |
| 117 | 117 | if a post_save_redirect was passed and a get_absolute_url method exists |
diff -r ed4d2e6277b2 tests/regressiontests/views/tests/i18n.py
|
a
|
b
|
|
| 2 | 2 | import gettext |
| 3 | 3 | |
| 4 | 4 | from django.conf import settings |
| 5 | | from django.test import TestCase |
| | 5 | from django.test import TransactionTestCase |
| 6 | 6 | from django.utils.translation import activate |
| 7 | 7 | |
| 8 | 8 | from regressiontests.views.urls import locale_dir |
| 9 | 9 | |
| 10 | | class I18NTests(TestCase): |
| | 10 | class I18NTests(TransactionTestCase): |
| 11 | 11 | """ Tests django views in django/views/i18n.py """ |
| 12 | 12 | |
| 13 | 13 | def test_setlang(self): |