From 30fc7d212882c721a2dc86d9ffa2615fced29181 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Fri, 3 Mar 2023 23:19:30 -0500 Subject: [PATCH] Correct unittests and urls to pass etc tests --- .../test_app/tests/base_tests/test_models.py | 17 ++++++++------ .../test_app/tests/base_tests/test_views.py | 17 ++++++++------ .../test_app/tests/etc_tests/test_models.py | 21 ++++++++++++------ .../test_app/tests/etc_tests/test_views.py | 17 ++++++++------ django_v2/test_app/urls.py | 6 ++--- .../test_app/tests/base_tests/test_models.py | 16 +++++++++----- .../test_app/tests/base_tests/test_views.py | 17 ++++++++------ .../test_app/tests/etc_tests/test_models.py | 22 ++++++++++++------- .../test_app/tests/etc_tests/test_views.py | 17 ++++++++------ django_v3/test_app/urls.py | 6 ++--- .../test_app/tests/base_tests/test_models.py | 16 +++++++++----- .../test_app/tests/base_tests/test_views.py | 17 ++++++++------ .../test_app/tests/etc_tests/test_models.py | 20 ++++++++++++----- .../test_app/tests/etc_tests/test_views.py | 19 +++++++++------- django_v4/test_app/urls.py | 6 ++--- 15 files changed, 142 insertions(+), 92 deletions(-) diff --git a/django_v2/test_app/tests/base_tests/test_models.py b/django_v2/test_app/tests/base_tests/test_models.py index 5ab609d..2bf5ccf 100644 --- a/django_v2/test_app/tests/base_tests/test_models.py +++ b/django_v2/test_app/tests/base_tests/test_models.py @@ -76,13 +76,16 @@ class ModelTestCase(TestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - if response.context is not None: - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" diff --git a/django_v2/test_app/tests/base_tests/test_views.py b/django_v2/test_app/tests/base_tests/test_views.py index 5e07b92..6be5652 100644 --- a/django_v2/test_app/tests/base_tests/test_views.py +++ b/django_v2/test_app/tests/base_tests/test_views.py @@ -74,13 +74,16 @@ class ViewTestCase(TestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - if response.context is not None: - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" diff --git a/django_v2/test_app/tests/etc_tests/test_models.py b/django_v2/test_app/tests/etc_tests/test_models.py index 444c9dd..926fdab 100644 --- a/django_v2/test_app/tests/etc_tests/test_models.py +++ b/django_v2/test_app/tests/etc_tests/test_models.py @@ -43,13 +43,16 @@ class ModelTestCase(IntegrationTestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - if response.context is not None: - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" @@ -159,12 +162,16 @@ class ModelTestCase(IntegrationTestCase): self.assertTrue(isinstance(uwsgi_user, AnonymousUser)) self.assertFalse(isinstance(uwsgi_user, get_user_model())) self.assertNotEqual(self.test_inactive_user, uwsgi_user) + self.assertTrue(isinstance(response.user, AnonymousUser)) + self.assertFalse(isinstance(response.user, get_user_model())) # Try again, to make sure that accessing any of the above values didn't somehow clear the client. self.assertEqual(self.test_inactive_user.pk, int(self.client.session.get('_auth_user_id', None))) self.assertTrue(isinstance(uwsgi_user, AnonymousUser)) self.assertFalse(isinstance(uwsgi_user, get_user_model())) self.assertNotEqual(self.test_inactive_user, uwsgi_user) + self.assertTrue(isinstance(response.user, AnonymousUser)) + self.assertFalse(isinstance(response.user, get_user_model())) with self.subTest('Check login using standard user'): # Get response object. diff --git a/django_v2/test_app/tests/etc_tests/test_views.py b/django_v2/test_app/tests/etc_tests/test_views.py index f65fded..e31e1f3 100644 --- a/django_v2/test_app/tests/etc_tests/test_views.py +++ b/django_v2/test_app/tests/etc_tests/test_views.py @@ -41,13 +41,16 @@ class ViewTestCase(IntegrationTestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - if response.context is not None: - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" diff --git a/django_v2/test_app/urls.py b/django_v2/test_app/urls.py index 64e1c14..84b9479 100644 --- a/django_v2/test_app/urls.py +++ b/django_v2/test_app/urls.py @@ -12,9 +12,9 @@ from . import views app_name = 'test_app' urlpatterns = [ # Test views that have various login/permission requirements. - path('view_with_login_check', views.view_with_login_check, name='view_with_login_check'), - path('view_with_permission_check', views.view_with_permission_check, name='view_with_permission_check'), - path('view_with_group_check', views.view_with_group_check, name='view_with_group_check'), + path('view_with_login_check/', views.view_with_login_check, name='view_with_login_check'), + path('view_with_permission_check/', views.view_with_permission_check, name='view_with_permission_check'), + path('view_with_group_check/', views.view_with_group_check, name='view_with_group_check'), # App root. path('', views.index, name='index') diff --git a/django_v3/test_app/tests/base_tests/test_models.py b/django_v3/test_app/tests/base_tests/test_models.py index f187c64..737ea48 100644 --- a/django_v3/test_app/tests/base_tests/test_models.py +++ b/django_v3/test_app/tests/base_tests/test_models.py @@ -76,12 +76,16 @@ class ModelTestCase(TestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" diff --git a/django_v3/test_app/tests/base_tests/test_views.py b/django_v3/test_app/tests/base_tests/test_views.py index d8d5374..43ca308 100644 --- a/django_v3/test_app/tests/base_tests/test_views.py +++ b/django_v3/test_app/tests/base_tests/test_views.py @@ -74,13 +74,16 @@ class ViewTestCase(TestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - if response.context is not None: - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" diff --git a/django_v3/test_app/tests/etc_tests/test_models.py b/django_v3/test_app/tests/etc_tests/test_models.py index 14a0a84..8b4bea1 100644 --- a/django_v3/test_app/tests/etc_tests/test_models.py +++ b/django_v3/test_app/tests/etc_tests/test_models.py @@ -43,12 +43,16 @@ class ModelTestCase(IntegrationTestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" @@ -162,14 +166,16 @@ class ModelTestCase(IntegrationTestCase): self.assertTrue(isinstance(uwsgi_user, AnonymousUser)) self.assertFalse(isinstance(uwsgi_user, get_user_model())) self.assertNotEqual(self.test_inactive_user, uwsgi_user) - self.assertIsNone(response.user) + self.assertTrue(isinstance(response.user, AnonymousUser)) + self.assertFalse(isinstance(response.user, get_user_model())) # Try again, to make sure that accessing any of the above values didn't somehow clear the client. self.assertEqual(self.test_inactive_user.pk, int(self.client.session.get('_auth_user_id', None))) self.assertTrue(isinstance(uwsgi_user, AnonymousUser)) self.assertFalse(isinstance(uwsgi_user, get_user_model())) self.assertNotEqual(self.test_inactive_user, uwsgi_user) - self.assertIsNone(response.user) + self.assertTrue(isinstance(response.user, AnonymousUser)) + self.assertFalse(isinstance(response.user, get_user_model())) with self.subTest('Check login using standard user'): # Get response object. diff --git a/django_v3/test_app/tests/etc_tests/test_views.py b/django_v3/test_app/tests/etc_tests/test_views.py index 8ab1beb..abc2ec4 100644 --- a/django_v3/test_app/tests/etc_tests/test_views.py +++ b/django_v3/test_app/tests/etc_tests/test_views.py @@ -41,13 +41,16 @@ class ViewTestCase(IntegrationTestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - if response.context is not None: - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" diff --git a/django_v3/test_app/urls.py b/django_v3/test_app/urls.py index d914543..116c5b7 100644 --- a/django_v3/test_app/urls.py +++ b/django_v3/test_app/urls.py @@ -12,9 +12,9 @@ from . import views app_name = 'test_app' urlpatterns = [ # Test views that have various login/permission requirements. - path('view_with_login_check', views.view_with_login_check, name='view_with_login_check'), - path('view_with_permission_check', views.view_with_permission_check, name='view_with_permission_check'), - path('view_with_group_check', views.view_with_group_check, name='view_with_group_check'), + path('view_with_login_check/', views.view_with_login_check, name='view_with_login_check'), + path('view_with_permission_check/', views.view_with_permission_check, name='view_with_permission_check'), + path('view_with_group_check/', views.view_with_group_check, name='view_with_group_check'), # App root. path('', views.index, name='index') diff --git a/django_v4/test_app/tests/base_tests/test_models.py b/django_v4/test_app/tests/base_tests/test_models.py index 205ed38..250373a 100644 --- a/django_v4/test_app/tests/base_tests/test_models.py +++ b/django_v4/test_app/tests/base_tests/test_models.py @@ -76,12 +76,16 @@ class ModelTestCase(TestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" diff --git a/django_v4/test_app/tests/base_tests/test_views.py b/django_v4/test_app/tests/base_tests/test_views.py index 56fe7a9..181c20e 100644 --- a/django_v4/test_app/tests/base_tests/test_views.py +++ b/django_v4/test_app/tests/base_tests/test_views.py @@ -74,13 +74,16 @@ class ViewTestCase(TestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - if response.context is not None: - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" diff --git a/django_v4/test_app/tests/etc_tests/test_models.py b/django_v4/test_app/tests/etc_tests/test_models.py index 6f3ed21..ed5139d 100644 --- a/django_v4/test_app/tests/etc_tests/test_models.py +++ b/django_v4/test_app/tests/etc_tests/test_models.py @@ -43,12 +43,16 @@ class ModelTestCase(IntegrationTestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" @@ -158,12 +162,16 @@ class ModelTestCase(IntegrationTestCase): self.assertTrue(isinstance(uwsgi_user, AnonymousUser)) self.assertFalse(isinstance(uwsgi_user, get_user_model())) self.assertNotEqual(self.test_inactive_user, uwsgi_user) + self.assertTrue(isinstance(response.user, AnonymousUser)) + self.assertFalse(isinstance(response.user, get_user_model())) # Try again, to make sure that accessing any of the above values didn't somehow clear the client. self.assertEqual(self.test_inactive_user.pk, int(self.client.session.get('_auth_user_id', None))) self.assertTrue(isinstance(uwsgi_user, AnonymousUser)) self.assertFalse(isinstance(uwsgi_user, get_user_model())) self.assertNotEqual(self.test_inactive_user, uwsgi_user) + self.assertTrue(isinstance(response.user, AnonymousUser)) + self.assertFalse(isinstance(response.user, get_user_model())) with self.subTest('Check login using standard user'): # Get response object. diff --git a/django_v4/test_app/tests/etc_tests/test_views.py b/django_v4/test_app/tests/etc_tests/test_views.py index 54a17a1..af9f44c 100644 --- a/django_v4/test_app/tests/etc_tests/test_views.py +++ b/django_v4/test_app/tests/etc_tests/test_views.py @@ -42,13 +42,16 @@ class ViewTestCase(IntegrationTestCase): """Prints out all context values to terminal.""" print('{0} {1} {0}'.format('=' * 10, 'response.context')) - if response.context is not None: - for key in response.context.keys(): - context_value = str(response.context.get(key)) - # Truncate display if very long. - if len(context_value) > 80: - context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) - print(' * {0}: {1}'.format(key, context_value)) + try: + if response.context is not None: + for key in response.context.keys(): + context_value = str(response.context.get(key)) + # Truncate display if very long. + if len(context_value) > 80: + context_value = '"{0}"..."{1}"'.format(context_value[:40], context_value[-40:]) + print(' * {0}: {1}'.format(key, context_value)) + except AttributeError: + pass def display_session(self): """Prints out all session values to terminal.""" @@ -227,7 +230,7 @@ class ViewTestCase(IntegrationTestCase): with self.subTest('Check views without login'): # Get response object. - response = self.assertGetResponse('test_app:view_with_permission_check', login=False) + response = self.assertGetResponse('test_app:view_with_permission_check', auto_login=False) # Display debug data to console on test failure. self.debug_data(response) diff --git a/django_v4/test_app/urls.py b/django_v4/test_app/urls.py index fdf8584..a71f531 100644 --- a/django_v4/test_app/urls.py +++ b/django_v4/test_app/urls.py @@ -12,9 +12,9 @@ from . import views app_name = 'test_app' urlpatterns = [ # Test views that have various login/permission requirements. - path('view_with_login_check', views.view_with_login_check, name='view_with_login_check'), - path('view_with_permission_check', views.view_with_permission_check, name='view_with_permission_check'), - path('view_with_group_check', views.view_with_group_check, name='view_with_group_check'), + path('view_with_login_check/', views.view_with_login_check, name='view_with_login_check'), + path('view_with_permission_check/', views.view_with_permission_check, name='view_with_permission_check'), + path('view_with_group_check/', views.view_with_group_check, name='view_with_group_check'), # App root. path('', views.index, name='index') -- GitLab