~ubuntu-branches/ubuntu/oneiric/python-django/oneiric

« back to all changes in this revision

Viewing changes to django/test/testcases.py

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2011-02-17 13:34:07 UTC
  • mfrom: (1.1.13 upstream) (4.4.12 sid)
  • Revision ID: james.westby@ubuntu.com-20110217133407-rwr88elhhq6j7ba0
Tags: 1.2.5-1ubuntu1
* Merge from Debian for security fixes (LP: #719031). Remaining changes:
  - debian/control: don't Build-Depends on locales-all, which doesn't exist
    in natty
* Drop the following patches, now included upstream:
  - debian/patches/07_security_admin_infoleak.diff
  - debian/patches/08_security_pasword_reset_dos.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
from django.http import QueryDict
12
12
from django.test import _doctest as doctest
13
13
from django.test.client import Client
 
14
from django.test.utils import get_warnings_state, restore_warnings_state
14
15
from django.utils import simplejson
15
16
from django.utils.encoding import smart_str
16
17
 
274
275
        """ Performs any post-test things. This includes:
275
276
 
276
277
            * Putting back the original ROOT_URLCONF if it was changed.
 
278
            * Force closing the connection, so that the next test gets
 
279
              a clean cursor.
277
280
        """
278
281
        self._fixture_teardown()
279
282
        self._urlconf_teardown()
 
283
        # Some DB cursors include SQL statements as part of cursor
 
284
        # creation. If you have a test that does rollback, the effect
 
285
        # of these statements is lost, which can effect the operation
 
286
        # of tests (e.g., losing a timezone setting causing objects to
 
287
        # be created with the wrong time).
 
288
        # To make sure this doesn't happen, get a clean connection at the
 
289
        # start of every test.
 
290
        for connection in connections.all():
 
291
            connection.close()
280
292
 
281
293
    def _fixture_teardown(self):
282
294
        pass
286
298
            settings.ROOT_URLCONF = self._old_root_urlconf
287
299
            clear_url_caches()
288
300
 
 
301
    def save_warnings_state(self):
 
302
        """
 
303
        Saves the state of the warnings module
 
304
        """
 
305
        self._warnings_state = get_warnings_state()
 
306
 
 
307
    def restore_warnings_state(self):
 
308
        """
 
309
        Restores the sate of the warnings module to the state
 
310
        saved by save_warnings_state()
 
311
        """
 
312
        restore_warnings_state(self._warnings_state)
 
313
 
289
314
    def assertRedirects(self, response, expected_url, status_code=302,
290
315
                        target_status_code=200, host=None, msg_prefix=''):
291
316
        """Asserts that a response redirected to a specific URL, and that the
299
324
 
300
325
        if hasattr(response, 'redirect_chain'):
301
326
            # The request was a followed redirect
302
 
            self.failUnless(len(response.redirect_chain) > 0,
 
327
            self.assertTrue(len(response.redirect_chain) > 0,
303
328
                msg_prefix + "Response didn't redirect as expected: Response"
304
329
                " code was %d (expected %d)" %
305
330
                    (response.status_code, status_code))
413
438
                if field:
414
439
                    if field in context[form].errors:
415
440
                        field_errors = context[form].errors[field]
416
 
                        self.failUnless(err in field_errors,
 
441
                        self.assertTrue(err in field_errors,
417
442
                            msg_prefix + "The field '%s' on form '%s' in"
418
443
                            " context %d does not contain the error '%s'"
419
444
                            " (actual errors: %s)" %
428
453
                                      (form, i, field))
429
454
                else:
430
455
                    non_field_errors = context[form].non_field_errors()
431
 
                    self.failUnless(err in non_field_errors,
 
456
                    self.assertTrue(err in non_field_errors,
432
457
                        msg_prefix + "The form '%s' in context %d does not"
433
458
                        " contain the non-field error '%s'"
434
459
                        " (actual errors: %s)" %
448
473
        template_names = [t.name for t in to_list(response.template)]
449
474
        if not template_names:
450
475
            self.fail(msg_prefix + "No templates used to render the response")
451
 
        self.failUnless(template_name in template_names,
 
476
        self.assertTrue(template_name in template_names,
452
477
            msg_prefix + "Template '%s' was not a template used to render"
453
478
            " the response. Actual template(s) used: %s" %
454
479
                (template_name, u', '.join(template_names)))
462
487
            msg_prefix += ": "
463
488
 
464
489
        template_names = [t.name for t in to_list(response.template)]
465
 
        self.failIf(template_name in template_names,
 
490
        self.assertFalse(template_name in template_names,
466
491
            msg_prefix + "Template '%s' was used unexpectedly in rendering"
467
492
            " the response" % template_name)
468
493
 
527
552
        for db in databases:
528
553
            transaction.rollback(using=db)
529
554
            transaction.leave_transaction_management(using=db)
530
 
 
531
 
        for connection in connections.all():
532
 
            connection.close()