From 0e0102389787444f3981aea19867a9dad6a4999b Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 14 Apr 2012 13:35:25 +0000 Subject: [PATCH] Converted more test assertions to assert[Not]Contains. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17910 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/auth/tests/views.py | 22 ++----- tests/modeltests/test_client/models.py | 2 +- tests/regressiontests/admin_views/tests.py | 63 ++++++++----------- tests/regressiontests/admin_widgets/tests.py | 6 +- .../tests/moderation_view_tests.py | 9 +-- tests/regressiontests/views/tests/debug.py | 4 +- 6 files changed, 42 insertions(+), 64 deletions(-) diff --git a/django/contrib/auth/tests/views.py b/django/contrib/auth/tests/views.py index b8a16a8c17..ea550f955d 100644 --- a/django/contrib/auth/tests/views.py +++ b/django/contrib/auth/tests/views.py @@ -114,8 +114,7 @@ class PasswordResetTest(AuthViewsTestCase): 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() @@ -124,20 +123,17 @@ class PasswordResetTest(AuthViewsTestCase): 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 @@ -165,14 +161,12 @@ class PasswordResetTest(AuthViewsTestCase): # 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']) @@ -183,7 +177,6 @@ class ChangePasswordTest(AuthViewsTestCase): 'username': 'testclient', 'password': password, }) - self.assertEqual(response.status_code, 200) self.assertContainsEscaped(response, AuthenticationForm.error_messages['invalid_login']) def logout(self): @@ -196,7 +189,6 @@ class ChangePasswordTest(AuthViewsTestCase): '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): @@ -206,7 +198,6 @@ class ChangePasswordTest(AuthViewsTestCase): '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): @@ -363,8 +354,7 @@ class LogoutTest(AuthViewsTestCase): "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): diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py index df872cd190..02666cef52 100644 --- a/tests/modeltests/test_client/models.py +++ b/tests/modeltests/test_client/models.py @@ -75,7 +75,7 @@ class ClientTest(TestCase): 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" diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 7a0a3bf868..55a2639028 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -92,9 +92,8 @@ class AdminViewBasicTest(TestCase): 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): @@ -393,13 +392,11 @@ class AdminViewBasicTest(TestCase): """ response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit) self.assertEqual(response.status_code, 200) - self.assertTrue( - '
' in response.content, - "Expected filter not found in changelist view." + self.assertContains(response, '
', + msg_prefix="Expected filter not found in changelist view" ) - self.assertFalse( - 'Blue' in response.content, - "Changelist filter not correctly limited by limit_choices_to." + self.assertNotContains(response, 'Blue', + msg_prefix="Changelist filter not correctly limited by limit_choices_to" ) def testRelationSpanningFilters(self): @@ -459,16 +456,16 @@ class AdminViewBasicTest(TestCase): """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('' % self.urlbit not in response.content) - self.assertFalse('' % self.urlbit not in response.content) + self.assertContains(response, '' % self.urlbit) + self.assertContains(response, '' % self.urlbit) def testNamedGroupFieldChoicesChangeList(self): """ @@ -491,10 +488,7 @@ class AdminViewBasicTest(TestCase): """ response = self.client.get('/test_admin/%s/admin_views/fabric/' % self.urlbit) self.assertEqual(response.status_code, 200) - self.assertTrue( - '
' in response.content, - "Expected filter not found in changelist view." - ) + self.assertContains(response, '
') self.assertTrue( 'Horizontal' in response.content and 'Vertical' in response.content, @@ -507,7 +501,7 @@ class AdminViewBasicTest(TestCase): # 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): """ @@ -759,7 +753,7 @@ class CustomModelAdminTest(AdminViewBasicTest): 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""" @@ -1657,9 +1651,9 @@ class AdminViewListEditable(TestCase): # 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("