Ticket #8138: django_transaction_tests.diff
File django_transaction_tests.diff, 6.9 KB (added by , 16 years ago) |
---|
-
django/test/testcases.py
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 transaction.enter_transaction_management() 177 transaction.managed(True) 178 result = doctest.DocTestRunner.run(self, test, compileflags, out, clear_globs) 179 transaction.rollback() 180 transaction.leave_transaction_management() 181 return result 182 183 class TransactionTestCase(unittest.TestCase): 173 184 def _pre_setup(self): 174 185 """Performs any pre-test setup. This includes: 175 186 176 187 * Flushing the database. 177 * If the Test Case class has a 'fixtures' member, installing the 188 * If the Test Case class has a 'fixtures' member, installing the 178 189 named fixtures. 179 190 * If the Test Case class has a 'urls' member, replace the 180 191 ROOT_URLCONF with it. … … 206 217 import sys 207 218 result.addError(self, sys.exc_info()) 208 219 return 209 super(T estCase, self).__call__(result)220 super(TransactionTestCase, self).__call__(result) 210 221 try: 211 222 self._post_teardown() 212 223 except (KeyboardInterrupt, SystemExit): … … 354 365 self.failIf(template_name in template_names, 355 366 (u"Template '%s' was used unexpectedly in rendering the" 356 367 u" response") % template_name) 368 369 370 class TestCase(TransactionTestCase): 371 """ 372 Does basically the same as TransactionTestCase, but surrounds every test 373 with a transaction. You have to use TransactionTestCase, if you need 374 transaction management inside a test. 375 """ 376 def _pre_setup(self): 377 transaction.enter_transaction_management() 378 transaction.managed(True) 379 380 if hasattr(self, 'fixtures'): 381 call_command('loaddata', *self.fixtures, **{ 382 'verbosity': 0, 383 'no_commit': True 384 }) 385 if hasattr(self, 'urls'): 386 self._old_root_urlconf = settings.ROOT_URLCONF 387 settings.ROOT_URLCONF = self.urls 388 clear_url_caches() 389 mail.outbox = [] 390 391 def _post_teardown(self): 392 if hasattr(self, '_old_root_urlconf'): 393 settings.ROOT_URLCONF = self._old_root_urlconf 394 clear_url_caches() 395 transaction.rollback() 396 transaction.leave_transaction_management() 397 No newline at end of file -
django/core/management/commands/loaddata.py
14 14 make_option('--verbosity', action='store', dest='verbosity', default='1', 15 15 type='choice', choices=['0', '1', '2'], 16 16 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'), 17 make_option('--no-commit', action='store_false', dest='no_commit', default=False, 18 help='Tells Django to not commit the loaded fixtures to the DB.'), 17 19 ) 18 20 help = 'Installs the named fixture(s) in the database.' 19 21 args = "fixture [fixture ...]" … … 28 30 29 31 verbosity = int(options.get('verbosity', 1)) 30 32 show_traceback = options.get('traceback', False) 33 no_commit = options.get('no_commit', False) 31 34 32 35 # Keep a count of the installed objects and fixtures 33 36 fixture_count = 0 … … 44 47 45 48 # Start transaction management. All fixtures are installed in a 46 49 # single transaction to ensure that all references are resolved. 47 transaction.commit_unless_managed() 48 transaction.enter_transaction_management() 49 transaction.managed(True) 50 if not no_commit: 51 transaction.commit_unless_managed() 52 transaction.enter_transaction_management() 53 transaction.managed(True) 50 54 51 55 app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()] 52 56 for fixture_label in fixture_labels: … … 133 137 (format, fixture_name, humanize(fixture_dir)) 134 138 135 139 136 # If any of the fixtures we loaded contain 0 objects, assume that an 140 # If any of the fixtures we loaded contain 0 objects, assume that an 137 141 # error was encountered during fixture loading. 138 142 if 0 in objects_per_fixture: 139 143 sys.stderr.write( … … 142 146 transaction.rollback() 143 147 transaction.leave_transaction_management() 144 148 return 145 146 # If we found even one object in a fixture, we need to reset the 149 150 # If we found even one object in a fixture, we need to reset the 147 151 # database sequences. 148 152 if object_count > 0: 149 153 sequence_sql = connection.ops.sequence_reset_sql(self.style, models) … … 152 156 print "Resetting sequences" 153 157 for line in sequence_sql: 154 158 cursor.execute(line) 155 156 transaction.commit()157 transaction.leave_transaction_management()158 159 160 if not no_commit: 161 transaction.commit() 162 transaction.leave_transaction_management() 163 159 164 if object_count == 0: 160 165 if verbosity > 1: 161 166 print "No fixtures found." 162 167 else: 163 168 if verbosity > 0: 164 169 print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count) 165 170 166 171 # Close the DB connection. This is required as a workaround for an 167 172 # edge case in MySQL: if the same connection is used to 168 173 # create tables, load data, and query, the query can return 169 174 # incorrect results. See Django #7572, MySQL #37735. 170 connection.close() 175 if not no_commit: 176 connection.close()