Skip to content

converted testing test asserts to pytest #4949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_credentials(self):
self.client.credentials(HTTP_AUTHORIZATION='example')
for _ in range(0, 3):
response = self.client.get('/view/')
self.assertEqual(response.data['auth'], 'example')
assert response.data['auth'] == 'example'

def test_force_authenticate(self):
"""
Expand All @@ -78,7 +78,7 @@ def test_force_authenticate(self):
user = User.objects.create_user('example', 'example@example.com')
self.client.force_authenticate(user)
response = self.client.get('/view/')
self.assertEqual(response.data['user'], 'example')
assert response.data['user'] == 'example'

def test_force_authenticate_with_sessions(self):
"""
Expand All @@ -89,16 +89,16 @@ def test_force_authenticate_with_sessions(self):

# First request does not yet have an active session
response = self.client.get('/session-view/')
self.assertEqual(response.data['active_session'], False)
assert response.data['active_session'] is False

# Subsequent requests have an active session
response = self.client.get('/session-view/')
self.assertEqual(response.data['active_session'], True)
assert response.data['active_session'] is True

# Force authenticating as `None` should also logout the user session.
self.client.force_authenticate(None)
response = self.client.get('/session-view/')
self.assertEqual(response.data['active_session'], False)
assert response.data['active_session'] is False

def test_csrf_exempt_by_default(self):
"""
Expand All @@ -107,7 +107,7 @@ def test_csrf_exempt_by_default(self):
User.objects.create_user('example', 'example@example.com', 'password')
self.client.login(username='example', password='password')
response = self.client.post('/view/')
self.assertEqual(response.status_code, 200)
assert response.status_code == 200

def test_explicitly_enforce_csrf_checks(self):
"""
Expand All @@ -118,19 +118,19 @@ def test_explicitly_enforce_csrf_checks(self):
client.login(username='example', password='password')
response = client.post('/view/')
expected = {'detail': 'CSRF Failed: CSRF cookie not set.'}
self.assertEqual(response.status_code, 403)
self.assertEqual(response.data, expected)
assert response.status_code == 403
assert response.data == expected

def test_can_logout(self):
"""
`logout()` resets stored credentials
"""
self.client.credentials(HTTP_AUTHORIZATION='example')
response = self.client.get('/view/')
self.assertEqual(response.data['auth'], 'example')
assert response.data['auth'] == 'example'
self.client.logout()
response = self.client.get('/view/')
self.assertEqual(response.data['auth'], b'')
assert response.data['auth'] == b''

def test_logout_resets_force_authenticate(self):
"""
Expand All @@ -139,50 +139,50 @@ def test_logout_resets_force_authenticate(self):
user = User.objects.create_user('example', 'example@example.com', 'password')
self.client.force_authenticate(user)
response = self.client.get('/view/')
self.assertEqual(response.data['user'], 'example')
assert response.data['user'] == 'example'
self.client.logout()
response = self.client.get('/view/')
self.assertEqual(response.data['user'], '')
assert response.data['user'] == ''

def test_follow_redirect(self):
"""
Follow redirect by setting follow argument.
"""
response = self.client.get('/redirect-view/')
self.assertEqual(response.status_code, 302)
assert response.status_code == 302
response = self.client.get('/redirect-view/', follow=True)
self.assertIsNotNone(response.redirect_chain)
self.assertEqual(response.status_code, 200)
assert response.redirect_chain is not None
assert response.status_code == 200

response = self.client.post('/redirect-view/')
self.assertEqual(response.status_code, 302)
assert response.status_code == 302
response = self.client.post('/redirect-view/', follow=True)
self.assertIsNotNone(response.redirect_chain)
self.assertEqual(response.status_code, 200)
assert response.redirect_chain is not None
assert response.status_code == 200

response = self.client.put('/redirect-view/')
self.assertEqual(response.status_code, 302)
assert response.status_code == 302
response = self.client.put('/redirect-view/', follow=True)
self.assertIsNotNone(response.redirect_chain)
self.assertEqual(response.status_code, 200)
assert response.redirect_chain is not None
assert response.status_code == 200

response = self.client.patch('/redirect-view/')
self.assertEqual(response.status_code, 302)
assert response.status_code == 302
response = self.client.patch('/redirect-view/', follow=True)
self.assertIsNotNone(response.redirect_chain)
self.assertEqual(response.status_code, 200)
assert response.redirect_chain is not None
assert response.status_code == 200

response = self.client.delete('/redirect-view/')
self.assertEqual(response.status_code, 302)
assert response.status_code == 302
response = self.client.delete('/redirect-view/', follow=True)
self.assertIsNotNone(response.redirect_chain)
self.assertEqual(response.status_code, 200)
assert response.redirect_chain is not None
assert response.status_code == 200

response = self.client.options('/redirect-view/')
self.assertEqual(response.status_code, 302)
assert response.status_code == 302
response = self.client.options('/redirect-view/', follow=True)
self.assertIsNotNone(response.redirect_chain)
self.assertEqual(response.status_code, 200)
assert response.redirect_chain is not None
assert response.status_code == 200

def test_invalid_multipart_data(self):
"""
Expand All @@ -200,8 +200,8 @@ def test_empty_post_uses_default_boolean_value(self):
data=None,
content_type='application/json'
)
self.assertEqual(response.status_code, 200, response.content)
self.assertEqual(response.data, {"flag": True})
assert response.status_code == 200
assert response.data == {"flag": True}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how this should be formatted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Third arg there is for an optional msg according to docs. I think we can just drop it. I don't think we do that anywhere else.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropped 3rd arg



class TestAPIRequestFactory(TestCase):
Expand All @@ -214,7 +214,7 @@ def test_csrf_exempt_by_default(self):
request = factory.post('/view/')
request.user = user
response = view(request)
self.assertEqual(response.status_code, 200)
assert response.status_code == 200

def test_explicitly_enforce_csrf_checks(self):
"""
Expand All @@ -226,8 +226,8 @@ def test_explicitly_enforce_csrf_checks(self):
request.user = user
response = view(request)
expected = {'detail': 'CSRF Failed: CSRF cookie not set.'}
self.assertEqual(response.status_code, 403)
self.assertEqual(response.data, expected)
assert response.status_code == 403
assert response.data == expected

def test_invalid_format(self):
"""
Expand All @@ -249,7 +249,7 @@ def test_force_authenticate(self):
request = factory.get('/view')
force_authenticate(request, user=user)
response = view(request)
self.assertEqual(response.data['user'], 'example')
assert response.data['user'] == 'example'

def test_upload_file(self):
# This is a 1x1 black png
Expand All @@ -264,13 +264,13 @@ def test_request_factory_url_arguments(self):
"""
factory = APIRequestFactory()
request = factory.get('/view/?demo=test')
self.assertEqual(dict(request.GET), {'demo': ['test']})
assert dict(request.GET) == {'demo': ['test']}
request = factory.get('/view/', {'demo': 'test'})
self.assertEqual(dict(request.GET), {'demo': ['test']})
assert dict(request.GET) == {'demo': ['test']}

def test_request_factory_url_arguments_with_unicode(self):
factory = APIRequestFactory()
request = factory.get('/view/?demo=testé')
self.assertEqual(dict(request.GET), {'demo': ['testé']})
assert dict(request.GET) == {'demo': ['testé']}
request = factory.get('/view/', {'demo': 'testé'})
self.assertEqual(dict(request.GET), {'demo': ['testé']})
assert dict(request.GET) == {'demo': ['testé']}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy