Skip to content

Commit

Permalink
Converted more test assertions to assert[Not]Contains.
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17910 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
claudep committed Apr 14, 2012
1 parent 7492439 commit 0e01023
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 64 deletions.
22 changes: 6 additions & 16 deletions django/contrib/auth/tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ def test_confirm_valid(self):
url, path = self._test_confirm_start()
response = self.client.get(path)
# redirect to a 'complete' page:
self.assertEqual(response.status_code, 200)
self.assertTrue("Please enter your new password" in response.content)
self.assertContains(response, "Please enter your new password")

def test_confirm_invalid(self):
url, path = self._test_confirm_start()
Expand All @@ -124,20 +123,17 @@ def test_confirm_invalid(self):
path = path[:-5] + ("0" * 4) + path[-1]

response = self.client.get(path)
self.assertEqual(response.status_code, 200)
self.assertTrue("The password reset link was invalid" in response.content)
self.assertContains(response, "The password reset link was invalid")

def test_confirm_invalid_user(self):
# Ensure that we get a 200 response for a non-existant user, not a 404
response = self.client.get('/reset/123456-1-1/')
self.assertEqual(response.status_code, 200)
self.assertTrue("The password reset link was invalid" in response.content)
self.assertContains(response, "The password reset link was invalid")

def test_confirm_overflow_user(self):
# Ensure that we get a 200 response for a base36 user id that overflows int
response = self.client.get('/reset/zzzzzzzzzzzzz-1-1/')
self.assertEqual(response.status_code, 200)
self.assertTrue("The password reset link was invalid" in response.content)
self.assertContains(response, "The password reset link was invalid")

def test_confirm_invalid_post(self):
# Same as test_confirm_invalid, but trying
Expand Down Expand Up @@ -165,14 +161,12 @@ def test_confirm_complete(self):

# Check we can't use the link again
response = self.client.get(path)
self.assertEqual(response.status_code, 200)
self.assertTrue("The password reset link was invalid" in response.content)
self.assertContains(response, "The password reset link was invalid")

def test_confirm_different_passwords(self):
url, path = self._test_confirm_start()
response = self.client.post(path, {'new_password1': 'anewpassword',
'new_password2': 'x'})
self.assertEqual(response.status_code, 200)
self.assertContainsEscaped(response, SetPasswordForm.error_messages['password_mismatch'])


Expand All @@ -183,7 +177,6 @@ def fail_login(self, password='password'):
'username': 'testclient',
'password': password,
})
self.assertEqual(response.status_code, 200)
self.assertContainsEscaped(response, AuthenticationForm.error_messages['invalid_login'])

def logout(self):
Expand All @@ -196,7 +189,6 @@ def test_password_change_fails_with_invalid_old_password(self):
'new_password1': 'password1',
'new_password2': 'password1',
})
self.assertEqual(response.status_code, 200)
self.assertContainsEscaped(response, PasswordChangeForm.error_messages['password_incorrect'])

def test_password_change_fails_with_mismatched_passwords(self):
Expand All @@ -206,7 +198,6 @@ def test_password_change_fails_with_mismatched_passwords(self):
'new_password1': 'password1',
'new_password2': 'donuts',
})
self.assertEqual(response.status_code, 200)
self.assertContainsEscaped(response, SetPasswordForm.error_messages['password_mismatch'])

def test_password_change_succeeds(self):
Expand Down Expand Up @@ -363,8 +354,7 @@ def test_logout_default(self):
"Logout without next_page option renders the default template"
self.login()
response = self.client.get('/logout/')
self.assertEqual(200, response.status_code)
self.assertTrue('Logged out' in response.content)
self.assertContains(response, 'Logged out')
self.confirm_logged_out()

def test_14377(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/modeltests/test_client/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_post(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['data'], '37')
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):
"Check the value of HTTP headers returned in a response"
Expand Down
63 changes: 27 additions & 36 deletions tests/regressiontests/admin_views/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ def testBasicAddGet(self):
def testAddWithGETArgs(self):
response = self.client.get('/test_admin/%s/admin_views/section/add/' % self.urlbit, {'name': 'My Section'})
self.assertEqual(response.status_code, 200)
self.assertTrue(
'value="My Section"' in response.content,
"Couldn't find an input with the right value in the response."
self.assertContains(response, 'value="My Section"',
msg_prefix="Couldn't find an input with the right value in the response"
)

def testBasicEditGet(self):
Expand Down Expand Up @@ -393,13 +392,11 @@ def testLimitedFilter(self):
"""
response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit)
self.assertEqual(response.status_code, 200)
self.assertTrue(
'<div id="changelist-filter">' in response.content,
"Expected filter not found in changelist view."
self.assertContains(response, '<div id="changelist-filter">',
msg_prefix="Expected filter not found in changelist view"
)
self.assertFalse(
'<a href="?color__id__exact=3">Blue</a>' in response.content,
"Changelist filter not correctly limited by limit_choices_to."
self.assertNotContains(response, '<a href="?color__id__exact=3">Blue</a>',
msg_prefix="Changelist filter not correctly limited by limit_choices_to"
)

def testRelationSpanningFilters(self):
Expand Down Expand Up @@ -459,16 +456,16 @@ def testIsNullLookups(self):
"""Ensure is_null is handled correctly."""
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)
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'})
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'})
self.assertTrue('1 article' in response.content, '"1 article" missing from response')
self.assertContains(response, '1 article')

def testLogoutAndPasswordChangeURLs(self):
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.assertFalse('<a href="/test_admin/%s/password_change/">' % self.urlbit not in response.content)
self.assertContains(response, '<a href="/test_admin/%s/logout/">' % self.urlbit)
self.assertContains(response, '<a href="/test_admin/%s/password_change/">' % self.urlbit)

def testNamedGroupFieldChoicesChangeList(self):
"""
Expand All @@ -491,10 +488,7 @@ def testNamedGroupFieldChoicesFilter(self):
"""
response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit)
self.assertEqual(response.status_code, 200)
self.assertTrue(
'<div id="changelist-filter">' in response.content,
"Expected filter not found in changelist view."
)
self.assertContains(response, '<div id="changelist-filter">')
self.assertTrue(
'<a href="?surface__exact=x">Horizontal</a>' in response.content and
'<a href="?surface__exact=y">Vertical</a>' in response.content,
Expand All @@ -507,7 +501,7 @@ def testChangeListNullBooleanDisplay(self):
# against the 'admin2' custom admin (which doesn't have the
# Post model).
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):
"""
Expand Down Expand Up @@ -759,7 +753,7 @@ def testCustomAdminSitePasswordChangeDoneTemplate(self):
def testCustomAdminSiteView(self):
self.client.login(username='super', password='secret')
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):
"""Return the permission object, for the Model"""
Expand Down Expand Up @@ -1657,9 +1651,9 @@ def test_changelist_input_html(self):
# CSRF field = 1
# field to track 'select all' across paginated views = 1
# 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
self.assertEqual(response.content.count("<select"), 4)
self.assertContains(response, "<select", count=4)

def test_post_messages(self):
# Ticket 12707: Saving inline editable should not show admin
Expand Down Expand Up @@ -2187,7 +2181,8 @@ def test_non_localized_pk(self):
}
response = self.client.post('/test_admin/admin/admin_views/subscriber/', action_data)
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_L10N = self.old_USE_L10N

Expand Down Expand Up @@ -2266,27 +2261,23 @@ def test_model_without_action(self):
"Tests a ModelAdmin without any action"
response = self.client.get('/test_admin/admin/admin_views/oldsubscriber/')
self.assertEqual(response.context["action_form"], None)
self.assertTrue(
'<input type="checkbox" class="action-select"' not in response.content,
"Found an unexpected action toggle checkboxbox in response"
)
self.assertTrue('action-checkbox-column' not in response.content,
"Found unexpected action-checkbox-column class in response")
self.assertNotContains(response, '<input type="checkbox" class="action-select"',
msg_prefix="Found an unexpected action toggle checkboxbox in response")
self.assertNotContains(response, '<input type="checkbox" class="action-select"')

def test_model_without_action_still_has_jquery(self):
"Tests that a ModelAdmin without any actions still gets jQuery included in page"
response = self.client.get('/test_admin/admin/admin_views/oldsubscriber/')
self.assertEqual(response.context["action_form"], None)
self.assertTrue('jquery.min.js' in response.content,
"jQuery missing from admin pages for model with no admin actions"
self.assertContains(response, 'jquery.min.js',
msg_prefix="jQuery missing from admin pages for model with no admin actions"
)

def test_action_column_class(self):
"Tests that the checkbox column class is present in the response"
response = self.client.get('/test_admin/admin/admin_views/subscriber/')
self.assertNotEqual(response.context["action_form"], None)
self.assertTrue('action-checkbox-column' in response.content,
"Expected an action-checkbox-column in response")
self.assertContains(response, 'action-checkbox-column')

def test_multiple_actions_form(self):
"""
Expand Down Expand Up @@ -3061,7 +3052,7 @@ def test_readonly_get(self):
self.assertNotContains(response, 'name="posted"')
# 3 fields + 2 submit buttons + 4 inline management form fields, + 2
# 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,
"<label>Awesomeness level:</label>")
Expand Down Expand Up @@ -3376,8 +3367,8 @@ def tearDown(self):
)
def testLangNamePresent(self):
response = self.client.get('/test_admin/%s/admin_views/' % self.urlbit)
self.assertFalse(' lang=""' in response.content)
self.assertFalse(' xml:lang=""' in response.content)
self.assertNotContains(response, ' lang=""')
self.assertNotContains(response, ' xml:lang=""')


class DateHierarchyTests(TestCase):
Expand Down
6 changes: 3 additions & 3 deletions tests/regressiontests/admin_widgets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def testFilterChoicesByRequestUser(self):
"""
self.client.login(username="super", password="secret")
response = self.client.get("/widget_admin/admin_widgets/cartire/add/")
self.assertTrue("BMW M3" not in response.content)
self.assertTrue("Volkswagon Passat" in response.content)
self.assertNotContains(response, "BMW M3")
self.assertContains(response, "Volkswagon Passat")


class AdminForeignKeyWidgetChangeList(DjangoTestCase):
Expand All @@ -147,7 +147,7 @@ def tearDown(self):

def test_changelist_foreignkey(self):
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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,19 @@ def testActionsNonModerator(self):
comments = self.createSomeComments()
self.client.login(username="normaluser", password="normaluser")
response = self.client.get("/admin/comments/comment/")
self.assertEqual("approve_comments" in response.content, False)
self.assertNotContains(response, "approve_comments")

def testActionsModerator(self):
comments = self.createSomeComments()
makeModerator("normaluser")
self.client.login(username="normaluser", password="normaluser")
response = self.client.get("/admin/comments/comment/")
self.assertEqual("approve_comments" in response.content, True)
self.assertContains(response, "approve_comments")

def testActionsDisabledDelete(self):
"Tests a CommentAdmin where 'delete_selected' has been disabled."
comments = self.createSomeComments()
self.client.login(username="normaluser", password="normaluser")
response = self.client.get('/admin2/comments/comment/')
self.assertEqual(response.status_code, 200)
self.assertTrue(
'<option value="delete_selected">' not in response.content,
"Found an unexpected delete_selected in response"
)
self.assertNotContains(response, '<option value="delete_selected">')
4 changes: 2 additions & 2 deletions tests/regressiontests/views/tests/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def test_files(self):
'file_data.txt': SimpleUploadedFile('file_data.txt', 'haha'),
}
response = self.client.post('/raises/', data)
self.assertTrue('file_data.txt' in response.content)
self.assertFalse('haha' in response.content)
self.assertContains(response, 'file_data.txt', status_code=500)
self.assertNotContains(response, 'haha', status_code=500)

def test_403(self):
# Ensure no 403.html template exists to test the default case.
Expand Down

0 comments on commit 0e01023

Please sign in to comment.