Skip to content

Commit

Permalink
[1.2.X] Removed all usages of deprecated TestCase methods (self.fail*…
Browse files Browse the repository at this point in the history
…). This removed most of the Warnings emitted (with -Wall) during the test suite. Backport of [14803].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14805 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
alex committed Dec 4, 2010
1 parent 5a792ec commit cc48f66
Show file tree
Hide file tree
Showing 27 changed files with 402 additions and 387 deletions.
10 changes: 5 additions & 5 deletions django/test/testcases.py
Expand Up @@ -313,7 +313,7 @@ def assertRedirects(self, response, expected_url, status_code=302,

if hasattr(response, 'redirect_chain'):
# The request was a followed redirect
self.failUnless(len(response.redirect_chain) > 0,
self.assertTrue(len(response.redirect_chain) > 0,
msg_prefix + "Response didn't redirect as expected: Response"
" code was %d (expected %d)" %
(response.status_code, status_code))
Expand Down Expand Up @@ -427,7 +427,7 @@ def assertFormError(self, response, form, field, errors, msg_prefix=''):
if field:
if field in context[form].errors:
field_errors = context[form].errors[field]
self.failUnless(err in field_errors,
self.assertTrue(err in field_errors,
msg_prefix + "The field '%s' on form '%s' in"
" context %d does not contain the error '%s'"
" (actual errors: %s)" %
Expand All @@ -442,7 +442,7 @@ def assertFormError(self, response, form, field, errors, msg_prefix=''):
(form, i, field))
else:
non_field_errors = context[form].non_field_errors()
self.failUnless(err in non_field_errors,
self.assertTrue(err in non_field_errors,
msg_prefix + "The form '%s' in context %d does not"
" contain the non-field error '%s'"
" (actual errors: %s)" %
Expand All @@ -462,7 +462,7 @@ def assertTemplateUsed(self, response, template_name, msg_prefix=''):
template_names = [t.name for t in to_list(response.template)]
if not template_names:
self.fail(msg_prefix + "No templates used to render the response")
self.failUnless(template_name in template_names,
self.assertTrue(template_name in template_names,
msg_prefix + "Template '%s' was not a template used to render"
" the response. Actual template(s) used: %s" %
(template_name, u', '.join(template_names)))
Expand All @@ -476,7 +476,7 @@ def assertTemplateNotUsed(self, response, template_name, msg_prefix=''):
msg_prefix += ": "

template_names = [t.name for t in to_list(response.template)]
self.failIf(template_name in template_names,
self.assertFalse(template_name in template_names,
msg_prefix + "Template '%s' was used unexpectedly in rendering"
" the response" % template_name)

Expand Down
10 changes: 5 additions & 5 deletions tests/modeltests/model_formsets/tests.py
Expand Up @@ -2,13 +2,13 @@
import re
from datetime import date
from decimal import Decimal

from django import forms
from django.db import models
from django.forms.models import _get_foreign_key
from django.forms.models import inlineformset_factory
from django.forms.models import modelformset_factory
from django.forms.models import modelformset_factory
from django.forms.models import (_get_foreign_key, inlineformset_factory,
modelformset_factory, modelformset_factory)
from django.test import TestCase

from modeltests.model_formsets.models import (
Author, BetterAuthor, Book, BookWithCustomPK, Editor,
BookWithOptionalAltEditor, AlternateBook, AuthorMeeting, CustomPrimaryKey,
Expand All @@ -30,7 +30,7 @@ def test_deletion(self):
}
formset = PoetFormSet(data, queryset=Poet.objects.all())
formset.save()
self.failUnless(formset.is_valid())
self.assertTrue(formset.is_valid())
self.assertEqual(Poet.objects.count(), 0)

def test_add_form_deletion_when_invalid(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/modeltests/signals/tests.py
Expand Up @@ -143,6 +143,6 @@ def test_disconnect_in_dispatch(self):
signals.post_save.connect(sender=Person, receiver=b)
p = Person.objects.create(first_name='John', last_name='Smith')

self.failUnless(a._run)
self.failUnless(b._run)
self.assertTrue(a._run)
self.assertTrue(b._run)
self.assertEqual(signals.post_save.receivers, [])
16 changes: 8 additions & 8 deletions tests/modeltests/test_client/models.py
Expand Up @@ -70,7 +70,7 @@ def test_post(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['data'], '37')
self.assertEqual(response.template.name, 'POST Template')
self.failUnless('Data received' in response.content)
self.assertTrue('Data received' in response.content)

def test_response_headers(self):
"Check the value of HTTP headers returned in a response"
Expand Down Expand Up @@ -275,7 +275,7 @@ def test_view_with_login(self):

# Log in
login = self.client.login(username='testclient', password='password')
self.failUnless(login, 'Could not log in')
self.assertTrue(login, 'Could not log in')

# Request a page that requires a login
response = self.client.get('/test_client/login_protected_view/')
Expand All @@ -291,7 +291,7 @@ def test_view_with_method_login(self):

# Log in
login = self.client.login(username='testclient', password='password')
self.failUnless(login, 'Could not log in')
self.assertTrue(login, 'Could not log in')

# Request a page that requires a login
response = self.client.get('/test_client/login_protected_method_view/')
Expand All @@ -307,7 +307,7 @@ def test_view_with_login_and_custom_redirect(self):

# Log in
login = self.client.login(username='testclient', password='password')
self.failUnless(login, 'Could not log in')
self.assertTrue(login, 'Could not log in')

# Request a page that requires a login
response = self.client.get('/test_client/login_protected_view_custom_redirect/')
Expand All @@ -318,13 +318,13 @@ def test_view_with_bad_login(self):
"Request a page that is protected with @login, but use bad credentials"

login = self.client.login(username='otheruser', password='nopassword')
self.failIf(login)
self.assertFalse(login)

def test_view_with_inactive_login(self):
"Request a page that is protected with @login, but use an inactive login"

login = self.client.login(username='inactive', password='password')
self.failIf(login)
self.assertFalse(login)

def test_logout(self):
"Request a logout after logging in"
Expand Down Expand Up @@ -352,7 +352,7 @@ def test_view_with_permissions(self):

# Log in
login = self.client.login(username='testclient', password='password')
self.failUnless(login, 'Could not log in')
self.assertTrue(login, 'Could not log in')

# Log in with wrong permissions. Should result in 302.
response = self.client.get('/test_client/permission_protected_view/')
Expand All @@ -369,7 +369,7 @@ def test_view_with_method_permissions(self):

# Log in
login = self.client.login(username='testclient', password='password')
self.failUnless(login, 'Could not log in')
self.assertTrue(login, 'Could not log in')

# Log in with wrong permissions. Should result in 302.
response = self.client.get('/test_client/permission_protected_method_view/')
Expand Down
19 changes: 10 additions & 9 deletions tests/modeltests/update/tests.py
Expand Up @@ -2,6 +2,7 @@

from models import A, B, C, D, DataPoint, RelatedPoint


class SimpleTest(TestCase):
def setUp(self):
self.a1 = A.objects.create()
Expand All @@ -15,38 +16,38 @@ def test_nonempty_update(self):
Test that update changes the right number of rows for a nonempty queryset
"""
num_updated = self.a1.b_set.update(y=100)
self.failUnlessEqual(num_updated, 20)
self.assertEqual(num_updated, 20)
cnt = B.objects.filter(y=100).count()
self.failUnlessEqual(cnt, 20)
self.assertEqual(cnt, 20)

def test_empty_update(self):
"""
Test that update changes the right number of rows for an empty queryset
"""
num_updated = self.a2.b_set.update(y=100)
self.failUnlessEqual(num_updated, 0)
self.assertEqual(num_updated, 0)
cnt = B.objects.filter(y=100).count()
self.failUnlessEqual(cnt, 0)
self.assertEqual(cnt, 0)

def test_nonempty_update_with_inheritance(self):
"""
Test that update changes the right number of rows for an empty queryset
when the update affects only a base table
"""
num_updated = self.a1.d_set.update(y=100)
self.failUnlessEqual(num_updated, 20)
self.assertEqual(num_updated, 20)
cnt = D.objects.filter(y=100).count()
self.failUnlessEqual(cnt, 20)
self.assertEqual(cnt, 20)

def test_empty_update_with_inheritance(self):
"""
Test that update changes the right number of rows for an empty queryset
when the update affects only a base table
"""
num_updated = self.a2.d_set.update(y=100)
self.failUnlessEqual(num_updated, 0)
self.assertEqual(num_updated, 0)
cnt = D.objects.filter(y=100).count()
self.failUnlessEqual(cnt, 0)
self.assertEqual(cnt, 0)

class AdvancedTests(TestCase):

Expand Down Expand Up @@ -112,4 +113,4 @@ def test_update_slice_fail(self):
"""
method = DataPoint.objects.all()[:2].update
self.assertRaises(AssertionError, method,
another_value='another thing')
another_value='another thing')
22 changes: 11 additions & 11 deletions tests/regressiontests/admin_changelist/tests.py
Expand Up @@ -11,8 +11,8 @@ def test_select_related_preserved(self):
overwrite a custom select_related provided by ModelAdmin.queryset().
"""
m = ChildAdmin(Child, admin.site)
cl = ChangeList(MockRequest(), Child, m.list_display, m.list_display_links,
m.list_filter, m.date_hierarchy, m.search_fields,
cl = ChangeList(MockRequest(), Child, m.list_display, m.list_display_links,
m.list_filter, m.date_hierarchy, m.search_fields,
m.list_select_related, m.list_per_page, m.list_editable, m)
self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})

Expand All @@ -25,24 +25,24 @@ def test_result_list_html(self):
new_child = Child.objects.create(name='name', parent=new_parent)
request = MockRequest()
m = ChildAdmin(Child, admin.site)
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
m.list_filter, m.date_hierarchy, m.search_fields,
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
m.list_filter, m.date_hierarchy, m.search_fields,
m.list_select_related, m.list_per_page, m.list_editable, m)
cl.formset = None
template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
context = Context({'cl': cl})
table_output = template.render(context)
row_html = '<tbody><tr class="row1"><td><input type="checkbox" class="action-select" value="1" name="_selected_action" /></td><th><a href="1/">name</a></th><td>Parent object</td></tr></tbody>'
self.failIf(table_output.find(row_html) == -1,
self.assertFalse(table_output.find(row_html) == -1,
'Failed to find expected row element: %s' % table_output)

def test_result_list_editable_html(self):
"""
Regression tests for #11791: Inclusion tag result_list generates a
Regression tests for #11791: Inclusion tag result_list generates a
table and this checks that the items are nested within the table
element tags.
Also a regression test for #13599, verifies that hidden fields
when list_editable is enabled are rendered in a div outside the
when list_editable is enabled are rendered in a div outside the
table.
"""
new_parent = Parent.objects.create(name='parent')
Expand All @@ -54,8 +54,8 @@ def test_result_list_editable_html(self):
m.list_display = ['id', 'name', 'parent']
m.list_display_links = ['id']
m.list_editable = ['name']
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
m.list_filter, m.date_hierarchy, m.search_fields,
cl = ChangeList(request, Child, m.list_display, m.list_display_links,
m.list_filter, m.date_hierarchy, m.search_fields,
m.list_select_related, m.list_per_page, m.list_editable, m)
FormSet = m.get_changelist_formset(request)
cl.formset = FormSet(queryset=cl.result_list)
Expand All @@ -64,11 +64,11 @@ def test_result_list_editable_html(self):
table_output = template.render(context)
# make sure that hidden fields are in the correct place
hiddenfields_div = '<div class="hiddenfields"><input type="hidden" name="form-0-id" value="1" id="id_form-0-id" /></div>'
self.failIf(table_output.find(hiddenfields_div) == -1,
self.assertFalse(table_output.find(hiddenfields_div) == -1,
'Failed to find hidden fields in: %s' % table_output)
# make sure that list editable fields are rendered in divs correctly
editable_name_field = '<input name="form-0-name" value="name" class="vTextField" maxlength="30" type="text" id="id_form-0-name" />'
self.failIf('<td>%s</td>' % editable_name_field == -1,
self.assertFalse('<td>%s</td>' % editable_name_field == -1,
'Failed to find "name" list_editable field in: %s' % table_output)

class ChildAdmin(admin.ModelAdmin):
Expand Down
14 changes: 6 additions & 8 deletions tests/regressiontests/admin_inlines/tests.py
@@ -1,12 +1,10 @@
from django.test import TestCase

from django.contrib.admin.helpers import InlineAdminForm
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase

# local test models
from models import Holder, Inner, InnerInline
from models import Holder2, Inner2, Holder3, Inner3
from models import Person, OutfitItem, Fashionista
from models import Teacher, Parent, Child
from models import (Holder, Inner, InnerInline, Holder2, Inner2, Holder3,
Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child)


class TestInline(TestCase):
Expand All @@ -19,7 +17,7 @@ def setUp(self):
self.change_url = '/test_admin/admin/admin_inlines/holder/%i/' % holder.id

result = self.client.login(username='super', password='secret')
self.failUnlessEqual(result, True)
self.assertEqual(result, True)

def tearDown(self):
self.client.logout()
Expand Down Expand Up @@ -75,7 +73,7 @@ class TestInlineMedia(TestCase):
def setUp(self):

result = self.client.login(username='super', password='secret')
self.failUnlessEqual(result, True)
self.assertEqual(result, True)

def tearDown(self):
self.client.logout()
Expand Down
3 changes: 2 additions & 1 deletion tests/regressiontests/admin_scripts/tests.py
Expand Up @@ -12,6 +12,7 @@
from django import conf, bin, get_version
from django.conf import settings


class AdminScriptTestCase(unittest.TestCase):
def write_settings(self, filename, apps=None, is_dir=False, sdict=None):
test_dir = os.path.dirname(os.path.dirname(__file__))
Expand Down Expand Up @@ -156,7 +157,7 @@ def assertNoOutput(self, stream):
self.assertEquals(len(stream), 0, "Stream should be empty: actually contains '%s'" % stream)
def assertOutput(self, stream, msg):
"Utility assertion: assert that the given message exists in the output"
self.failUnless(msg in stream, "'%s' does not match actual output text '%s'" % (msg, stream))
self.assertTrue(msg in stream, "'%s' does not match actual output text '%s'" % (msg, stream))

##########################################################################
# DJANGO ADMIN TESTS
Expand Down

0 comments on commit cc48f66

Please sign in to comment.