Converted more test assertions to assert[Not]Contains.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17910 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Claude Paroz 2012-04-14 13:35:25 +00:00
parent 749243941c
commit 0e01023897
6 changed files with 42 additions and 64 deletions

View File

@ -114,8 +114,7 @@ class PasswordResetTest(AuthViewsTestCase):
url, path = self._test_confirm_start() url, path = self._test_confirm_start()
response = self.client.get(path) response = self.client.get(path)
# redirect to a 'complete' page: # redirect to a 'complete' page:
self.assertEqual(response.status_code, 200) self.assertContains(response, "Please enter your new password")
self.assertTrue("Please enter your new password" in response.content)
def test_confirm_invalid(self): def test_confirm_invalid(self):
url, path = self._test_confirm_start() url, path = self._test_confirm_start()
@ -124,20 +123,17 @@ class PasswordResetTest(AuthViewsTestCase):
path = path[:-5] + ("0" * 4) + path[-1] path = path[:-5] + ("0" * 4) + path[-1]
response = self.client.get(path) response = self.client.get(path)
self.assertEqual(response.status_code, 200) self.assertContains(response, "The password reset link was invalid")
self.assertTrue("The password reset link was invalid" in response.content)
def test_confirm_invalid_user(self): def test_confirm_invalid_user(self):
# Ensure that we get a 200 response for a non-existant user, not a 404 # Ensure that we get a 200 response for a non-existant user, not a 404
response = self.client.get('/reset/123456-1-1/') response = self.client.get('/reset/123456-1-1/')
self.assertEqual(response.status_code, 200) self.assertContains(response, "The password reset link was invalid")
self.assertTrue("The password reset link was invalid" in response.content)
def test_confirm_overflow_user(self): def test_confirm_overflow_user(self):
# Ensure that we get a 200 response for a base36 user id that overflows int # Ensure that we get a 200 response for a base36 user id that overflows int
response = self.client.get('/reset/zzzzzzzzzzzzz-1-1/') response = self.client.get('/reset/zzzzzzzzzzzzz-1-1/')
self.assertEqual(response.status_code, 200) self.assertContains(response, "The password reset link was invalid")
self.assertTrue("The password reset link was invalid" in response.content)
def test_confirm_invalid_post(self): def test_confirm_invalid_post(self):
# Same as test_confirm_invalid, but trying # Same as test_confirm_invalid, but trying
@ -165,14 +161,12 @@ class PasswordResetTest(AuthViewsTestCase):
# Check we can't use the link again # Check we can't use the link again
response = self.client.get(path) response = self.client.get(path)
self.assertEqual(response.status_code, 200) self.assertContains(response, "The password reset link was invalid")
self.assertTrue("The password reset link was invalid" in response.content)
def test_confirm_different_passwords(self): def test_confirm_different_passwords(self):
url, path = self._test_confirm_start() url, path = self._test_confirm_start()
response = self.client.post(path, {'new_password1': 'anewpassword', response = self.client.post(path, {'new_password1': 'anewpassword',
'new_password2': 'x'}) 'new_password2': 'x'})
self.assertEqual(response.status_code, 200)
self.assertContainsEscaped(response, SetPasswordForm.error_messages['password_mismatch']) self.assertContainsEscaped(response, SetPasswordForm.error_messages['password_mismatch'])
@ -183,7 +177,6 @@ class ChangePasswordTest(AuthViewsTestCase):
'username': 'testclient', 'username': 'testclient',
'password': password, 'password': password,
}) })
self.assertEqual(response.status_code, 200)
self.assertContainsEscaped(response, AuthenticationForm.error_messages['invalid_login']) self.assertContainsEscaped(response, AuthenticationForm.error_messages['invalid_login'])
def logout(self): def logout(self):
@ -196,7 +189,6 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password1': 'password1', 'new_password1': 'password1',
'new_password2': 'password1', 'new_password2': 'password1',
}) })
self.assertEqual(response.status_code, 200)
self.assertContainsEscaped(response, PasswordChangeForm.error_messages['password_incorrect']) self.assertContainsEscaped(response, PasswordChangeForm.error_messages['password_incorrect'])
def test_password_change_fails_with_mismatched_passwords(self): def test_password_change_fails_with_mismatched_passwords(self):
@ -206,7 +198,6 @@ class ChangePasswordTest(AuthViewsTestCase):
'new_password1': 'password1', 'new_password1': 'password1',
'new_password2': 'donuts', 'new_password2': 'donuts',
}) })
self.assertEqual(response.status_code, 200)
self.assertContainsEscaped(response, SetPasswordForm.error_messages['password_mismatch']) self.assertContainsEscaped(response, SetPasswordForm.error_messages['password_mismatch'])
def test_password_change_succeeds(self): def test_password_change_succeeds(self):
@ -363,8 +354,7 @@ class LogoutTest(AuthViewsTestCase):
"Logout without next_page option renders the default template" "Logout without next_page option renders the default template"
self.login() self.login()
response = self.client.get('/logout/') response = self.client.get('/logout/')
self.assertEqual(200, response.status_code) self.assertContains(response, 'Logged out')
self.assertTrue('Logged out' in response.content)
self.confirm_logged_out() self.confirm_logged_out()
def test_14377(self): def test_14377(self):

View File

@ -75,7 +75,7 @@ class ClientTest(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['data'], '37') self.assertEqual(response.context['data'], '37')
self.assertEqual(response.templates[0].name, 'POST Template') self.assertEqual(response.templates[0].name, 'POST Template')
self.assertTrue('Data received' in response.content) self.assertContains(response, 'Data received')
def test_response_headers(self): def test_response_headers(self):
"Check the value of HTTP headers returned in a response" "Check the value of HTTP headers returned in a response"

View File

@ -92,9 +92,8 @@ class AdminViewBasicTest(TestCase):
def testAddWithGETArgs(self): def testAddWithGETArgs(self):
response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit, {'name': 'My Section'}) response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit, {'name': 'My Section'})
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTrue( self.assertContains(response, 'value="My Section"',
'value="My Section"' in response.content, msg_prefix="Couldn't find an input with the right value in the response"
"Couldn't find an input with the right value in the response."
) )
def testBasicEditGet(self): def testBasicEditGet(self):
@ -393,13 +392,11 @@ class AdminViewBasicTest(TestCase):
""" """
response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit) response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTrue( self.assertContains(response, '<div id="changelist-filter">',
'<div id="changelist-filter">' in response.content, msg_prefix="Expected filter not found in changelist view"
"Expected filter not found in changelist view."
) )
self.assertFalse( self.assertNotContains(response, '<a href="?color__id__exact=3">Blue</a>',
'<a href="?color__id__exact=3">Blue</a>' in response.content, msg_prefix="Changelist filter not correctly limited by limit_choices_to"
"Changelist filter not correctly limited by limit_choices_to."
) )
def testRelationSpanningFilters(self): def testRelationSpanningFilters(self):
@ -459,16 +456,16 @@ class AdminViewBasicTest(TestCase):
"""Ensure is_null is handled correctly.""" """Ensure is_null is handled correctly."""
Article.objects.create(title="I Could Go Anywhere", content="Versatile", date=datetime.datetime.now()) Article.objects.create(title="I Could Go Anywhere", content="Versatile", date=datetime.datetime.now())
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit) response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit)
self.assertTrue('4 articles' in response.content, '"4 articles" missing from response') self.assertContains(response, '4 articles')
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'section__isnull': 'false'}) response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'section__isnull': 'false'})
self.assertTrue('3 articles' in response.content, '"3 articles" missing from response') self.assertContains(response, '3 articles')
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'section__isnull': 'true'}) response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'section__isnull': 'true'})
self.assertTrue('1 article' in response.content, '"1 article" missing from response') self.assertContains(response, '1 article')
def testLogoutAndPasswordChangeURLs(self): def testLogoutAndPasswordChangeURLs(self):
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit) response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit)
self.assertFalse('<a href="/test_admin/%s/logout/">' % self.urlbit not in response.content) self.assertContains(response, '<a href="/test_admin/%s/logout/">' % self.urlbit)
self.assertFalse('<a href="/test_admin/%s/password_change/">' % self.urlbit not in response.content) self.assertContains(response, '<a href="/test_admin/%s/password_change/">' % self.urlbit)
def testNamedGroupFieldChoicesChangeList(self): def testNamedGroupFieldChoicesChangeList(self):
""" """
@ -491,10 +488,7 @@ class AdminViewBasicTest(TestCase):
""" """
response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit) response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTrue( self.assertContains(response, '<div id="changelist-filter">')
'<div id="changelist-filter">' in response.content,
"Expected filter not found in changelist view."
)
self.assertTrue( self.assertTrue(
'<a href="?surface__exact=x">Horizontal</a>' in response.content and '<a href="?surface__exact=x">Horizontal</a>' in response.content and
'<a href="?surface__exact=y">Vertical</a>' in response.content, '<a href="?surface__exact=y">Vertical</a>' in response.content,
@ -507,7 +501,7 @@ class AdminViewBasicTest(TestCase):
# against the 'admin2' custom admin (which doesn't have the # against the 'admin2' custom admin (which doesn't have the
# Post model). # Post model).
response = self.client.get("/test_admin/admin/admin_views/post/") response = self.client.get("/test_admin/admin/admin_views/post/")
self.assertTrue('icon-unknown.gif' in response.content) self.assertContains(response, 'icon-unknown.gif')
def testI18NLanguageNonEnglishDefault(self): def testI18NLanguageNonEnglishDefault(self):
""" """
@ -759,7 +753,7 @@ class CustomModelAdminTest(AdminViewBasicTest):
def testCustomAdminSiteView(self): def testCustomAdminSiteView(self):
self.client.login(username='super', password='secret') self.client.login(username='super', password='secret')
response = self.client.get('/test_admin/%s/my_view/' % self.urlbit) response = self.client.get('/test_admin/%s/my_view/' % self.urlbit)
self.assertTrue(response.content == "Django is a magical pony!", response.content) self.assertEqual(response.content, "Django is a magical pony!")
def get_perm(Model, perm): def get_perm(Model, perm):
"""Return the permission object, for the Model""" """Return the permission object, for the Model"""
@ -1657,9 +1651,9 @@ class AdminViewListEditable(TestCase):
# CSRF field = 1 # CSRF field = 1
# field to track 'select all' across paginated views = 1 # field to track 'select all' across paginated views = 1
# 6 + 3 + 4 + 1 + 2 + 1 + 1 = 18 inputs # 6 + 3 + 4 + 1 + 2 + 1 + 1 = 18 inputs
self.assertEqual(response.content.count("<input"), 18) self.assertContains(response, "<input", count=18)
# 1 select per object = 3 selects # 1 select per object = 3 selects
self.assertEqual(response.content.count("<select"), 4) self.assertContains(response, "<select", count=4)
def test_post_messages(self): def test_post_messages(self):
# Ticket 12707: Saving inline editable should not show admin # Ticket 12707: Saving inline editable should not show admin
@ -2187,7 +2181,8 @@ class AdminActionsTest(TestCase):
} }
response = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data) response = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data)
self.assertTemplateUsed(response, 'admin/delete_selected_confirmation.html') self.assertTemplateUsed(response, 'admin/delete_selected_confirmation.html')
self.assertTrue('value="9999"' in response.content and 'value="2"' in response.content) # Instead of 9,999 self.assertContains(response, 'value="9999"') # Instead of 9,999
self.assertContains(response, 'value="2"')
settings.USE_THOUSAND_SEPARATOR = self.old_USE_THOUSAND_SEPARATOR settings.USE_THOUSAND_SEPARATOR = self.old_USE_THOUSAND_SEPARATOR
settings.USE_L10N = self.old_USE_L10N settings.USE_L10N = self.old_USE_L10N
@ -2266,27 +2261,23 @@ class AdminActionsTest(TestCase):
"Tests a ModelAdmin without any action" "Tests a ModelAdmin without any action"
response = self.client.get('/test_admin/admin/admin_views/oldsubscriber/') response = self.client.get('/test_admin/admin/admin_views/oldsubscriber/')
self.assertEqual(response.context["action_form"], None) self.assertEqual(response.context["action_form"], None)
self.assertTrue( self.assertNotContains(response, '<input type="checkbox" class="action-select"',
'<input type="checkbox" class="action-select"' not in response.content, msg_prefix="Found an unexpected action toggle checkboxbox in response")
"Found an unexpected action toggle checkboxbox in response" self.assertNotContains(response, '<input type="checkbox" class="action-select"')
)
self.assertTrue('action-checkbox-column' not in response.content,
"Found unexpected action-checkbox-column class in response")
def test_model_without_action_still_has_jquery(self): def test_model_without_action_still_has_jquery(self):
"Tests that a ModelAdmin without any actions still gets jQuery included in page" "Tests that a ModelAdmin without any actions still gets jQuery included in page"
response = self.client.get('/test_admin/admin/admin_views/oldsubscriber/') response = self.client.get('/test_admin/admin/admin_views/oldsubscriber/')
self.assertEqual(response.context["action_form"], None) self.assertEqual(response.context["action_form"], None)
self.assertTrue('jquery.min.js' in response.content, self.assertContains(response, 'jquery.min.js',
"jQuery missing from admin pages for model with no admin actions" msg_prefix="jQuery missing from admin pages for model with no admin actions"
) )
def test_action_column_class(self): def test_action_column_class(self):
"Tests that the checkbox column class is present in the response" "Tests that the checkbox column class is present in the response"
response = self.client.get('/test_admin/admin/admin_views/subscriber/') response = self.client.get('/test_admin/admin/admin_views/subscriber/')
self.assertNotEqual(response.context["action_form"], None) self.assertNotEqual(response.context["action_form"], None)
self.assertTrue('action-checkbox-column' in response.content, self.assertContains(response, 'action-checkbox-column')
"Expected an action-checkbox-column in response")
def test_multiple_actions_form(self): def test_multiple_actions_form(self):
""" """
@ -3061,7 +3052,7 @@ class ReadonlyTest(TestCase):
self.assertNotContains(response, 'name="posted"') self.assertNotContains(response, 'name="posted"')
# 3 fields + 2 submit buttons + 4 inline management form fields, + 2 # 3 fields + 2 submit buttons + 4 inline management form fields, + 2
# hidden fields for inlines + 1 field for the inline + 2 empty form # hidden fields for inlines + 1 field for the inline + 2 empty form
self.assertEqual(response.content.count("<input"), 14) self.assertContains(response, "<input", count=14)
self.assertContains(response, formats.localize(datetime.date.today())) self.assertContains(response, formats.localize(datetime.date.today()))
self.assertContains(response, self.assertContains(response,
"<label>Awesomeness level:</label>") "<label>Awesomeness level:</label>")
@ -3376,8 +3367,8 @@ class ValidXHTMLTests(TestCase):
) )
def testLangNamePresent(self): def testLangNamePresent(self):
response = self.client.get('/test_admin/%s/admin_views/' % self.urlbit) response = self.client.get('/test_admin/%s/admin_views/' % self.urlbit)
self.assertFalse(' lang=""' in response.content) self.assertNotContains(response, ' lang=""')
self.assertFalse(' xml:lang=""' in response.content) self.assertNotContains(response, ' xml:lang=""')
class DateHierarchyTests(TestCase): class DateHierarchyTests(TestCase):

View File

@ -131,8 +131,8 @@ class AdminFormfieldForDBFieldWithRequestTests(DjangoTestCase):
""" """
self.client.login(username="super", password="secret") self.client.login(username="super", password="secret")
response = self.client.get("/widget_admin/admin_widgets/cartire/add/") response = self.client.get("/widget_admin/admin_widgets/cartire/add/")
self.assertTrue("BMW M3" not in response.content) self.assertNotContains(response, "BMW M3")
self.assertTrue("Volkswagon Passat" in response.content) self.assertContains(response, "Volkswagon Passat")
class AdminForeignKeyWidgetChangeList(DjangoTestCase): class AdminForeignKeyWidgetChangeList(DjangoTestCase):
@ -147,7 +147,7 @@ class AdminForeignKeyWidgetChangeList(DjangoTestCase):
def test_changelist_foreignkey(self): def test_changelist_foreignkey(self):
response = self.client.get('%s/admin_widgets/car/' % self.admin_root) response = self.client.get('%s/admin_widgets/car/' % self.admin_root)
self.assertTrue('%s/auth/user/add/' % self.admin_root in response.content) self.assertContains(response, '%s/auth/user/add/' % self.admin_root)
class AdminForeignKeyRawIdWidget(DjangoTestCase): class AdminForeignKeyRawIdWidget(DjangoTestCase):

View File

@ -184,14 +184,14 @@ class AdminActionsTests(CommentTestCase):
comments = self.createSomeComments() comments = self.createSomeComments()
self.client.login(username="normaluser", password="normaluser") self.client.login(username="normaluser", password="normaluser")
response = self.client.get("/admin/comments/comment/") response = self.client.get("/admin/comments/comment/")
self.assertEqual("approve_comments" in response.content, False) self.assertNotContains(response, "approve_comments")
def testActionsModerator(self): def testActionsModerator(self):
comments = self.createSomeComments() comments = self.createSomeComments()
makeModerator("normaluser") makeModerator("normaluser")
self.client.login(username="normaluser", password="normaluser") self.client.login(username="normaluser", password="normaluser")
response = self.client.get("/admin/comments/comment/") response = self.client.get("/admin/comments/comment/")
self.assertEqual("approve_comments" in response.content, True) self.assertContains(response, "approve_comments")
def testActionsDisabledDelete(self): def testActionsDisabledDelete(self):
"Tests a CommentAdmin where 'delete_selected' has been disabled." "Tests a CommentAdmin where 'delete_selected' has been disabled."
@ -199,7 +199,4 @@ class AdminActionsTests(CommentTestCase):
self.client.login(username="normaluser", password="normaluser") self.client.login(username="normaluser", password="normaluser")
response = self.client.get('/admin2/comments/comment/') response = self.client.get('/admin2/comments/comment/')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTrue( self.assertNotContains(response, '<option value="delete_selected">')
'<option value="delete_selected">' not in response.content,
"Found an unexpected delete_selected in response"
)

View File

@ -40,8 +40,8 @@ class DebugViewTests(TestCase):
'file_data.txt': SimpleUploadedFile('file_data.txt', 'haha'), 'file_data.txt': SimpleUploadedFile('file_data.txt', 'haha'),
} }
response = self.client.post('/raises/', data) response = self.client.post('/raises/', data)
self.assertTrue('file_data.txt' in response.content) self.assertContains(response, 'file_data.txt', status_code=500)
self.assertFalse('haha' in response.content) self.assertNotContains(response, 'haha', status_code=500)
def test_403(self): def test_403(self):
# Ensure no 403.html template exists to test the default case. # Ensure no 403.html template exists to test the default case.