Skip to content
This repository has been archived by the owner on Mar 26, 2023. It is now read-only.

Rewrite unit tests for views #72

Merged
merged 29 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9baa99b
Add unit tests for core views
harisonmg Nov 6, 2021
0602cd7
Add unit tests for TemperatureRecordsListView
harisonmg Nov 6, 2021
23a6186
Add unit tests for PeopleListView and InterpersonalRelationshipsListView
harisonmg Nov 6, 2021
175b00f
Add the object list explicitly in list view tests
harisonmg Nov 6, 2021
c2e7331
Add unit tests for PersonDetailView
harisonmg Nov 6, 2021
9ce7a8d
Add unit tests for class TemperatureRecordCreateView
harisonmg Nov 18, 2021
7826bdb
Add comments in list and detail view unit tests
harisonmg Nov 18, 2021
43803d5
Add context data tests for list and detail views
harisonmg Nov 18, 2021
9636e11
Fix bugs in PersonDetailView unit tests and refactor
harisonmg Nov 18, 2021
4bd5194
Fix bug in TemperatureRecordCreateViewTestCase.test_success_url
harisonmg Nov 18, 2021
8c00002
Fix bug in TemperatureRecordCreateViewTestCase and refactor
harisonmg Nov 19, 2021
8e05cdb
Add unit tests for PersonCreateView
harisonmg Nov 19, 2021
67c694e
Add unit tests for RelationshipCreateView
harisonmg Nov 19, 2021
81ea1e0
Test attributes added in overridden create view form_valid method
harisonmg Nov 19, 2021
3549642
Test attribute added in overridden get_context_data method
harisonmg Nov 19, 2021
84b97e3
Use POST requests to test views with forms
harisonmg Nov 19, 2021
a3fffde
Add unit tests for PersonUpdateView
harisonmg Nov 19, 2021
f4e2a74
Fix bug in TemperatureRecordCreateViewTestCase
harisonmg Nov 19, 2021
305d3c1
Add unit tests for AdultCreateView
harisonmg Dec 4, 2021
772d5a4
Add unit tests for AdultSelfRegisterView
harisonmg Dec 4, 2021
be335a9
Add unit tests for ChildCreateView
harisonmg Dec 4, 2021
71e7161
Don't create objects when testing for zero search results
harisonmg Feb 4, 2022
28c22bc
Create empty tuples using literals
harisonmg Feb 4, 2022
21f3444
Remove extra data from form_data
harisonmg Feb 4, 2022
2ae881a
Test the value of person in context_data
harisonmg Feb 4, 2022
d17a2e0
Add unit tests for ParentChildRelationshipCreateView
harisonmg Feb 4, 2022
06832a9
Add comments in core view tests
harisonmg Feb 4, 2022
4ac1dd5
Replace older view tests with unit tests
harisonmg Feb 4, 2022
2602f36
Update README
harisonmg Feb 4, 2022
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ to store and retrieve church records online.
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![Documentation Status](https://readthedocs.org/projects/church-ims/badge/?version=latest)](https://church-ims.readthedocs.io/en/latest/?badge=latest)
[![Maintainability](https://api.codeclimate.com/v1/badges/669adf02ec70d3484662/maintainability)](https://codeclimate.com/github/harisonmg/church-ims/maintainability)
130 changes: 76 additions & 54 deletions core/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,100 @@
from django.test import RequestFactory, SimpleTestCase, TestCase
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied
from django.test import RequestFactory, TestCase
from django.urls import reverse

from accounts.factories import UserFactory
from core import views
from people.factories import AdultFactory


class IndexViewTestCase(SimpleTestCase):
class IndexViewTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.request = self.factory.get("dummy_path/")
self.view = views.IndexView

def test_response_status_code(self):
response = self.view.as_view()(self.request)
self.view_class = views.IndexView
self.view_func = self.view_class.as_view()
self.view = self.view_class()

# TemplateResponseMixin
def test_template_name(self):
self.view.setup(self.request)
template_names = self.view.get_template_names()
self.assertIn("core/index.html", template_names)

# View
def test_response(self):
response = self.view_func(self.request)
self.assertEqual(response.status_code, 200)

def test_template_used(self):
response = self.view.as_view()(self.request)
with self.assertTemplateUsed("core/index.html"):
response.render()


class LoginRedirectViewTestCase(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()

cls.url = "/login/redirect/"
cls.fully_registered_user = UserFactory()
cls.partially_registered_user = UserFactory()

# personal details
AdultFactory(user=cls.fully_registered_user)

def test_anonymous_user_response(self):
response = self.client.get(self.url)
self.assertRedirects(response, f"/accounts/login/?next={self.url}")

def test_fully_registered_user_response(self):
self.client.force_login(self.fully_registered_user)
response = self.client.get(self.url)
def setUp(self):
self.factory = RequestFactory()
self.request = self.factory.get("dummy_path/")
self.view_class = views.LoginRedirectView
self.view_func = self.view_class.as_view()
self.view = self.view_class()

# LoginRequiredMixin
def test_login_required(self):
self.request.user = AnonymousUser()
response = self.view_func(self.request)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse("core:dashboard"))
self.assertIn(reverse("account_login"), response.url)

def test_partially_registered_user_response(self):
self.client.force_login(self.partially_registered_user)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse("people:adult_self_register"))
# RedirectView
def test_redirect_url_for_user_with_person(self):
# setup
user = UserFactory()
AdultFactory(user=user)
self.request.user = user
self.view.setup(self.request)

# test
url = self.view.get_redirect_url()
self.assertEqual(url, reverse("core:dashboard"))

class DashboardViewTestCase(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
def test_redirect_url_for_user_without_person(self):
self.request.user = UserFactory()
self.view.setup(self.request)
url = self.view.get_redirect_url()
self.assertEqual(url, reverse("people:adult_self_register"))

cls.url = "/dashboard/"
cls.fully_registered_user = UserFactory()
cls.partially_registered_user = UserFactory()

# personal details
AdultFactory(user=cls.fully_registered_user)
class DashboardViewTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.request = self.factory.get("dummy_path/")
self.view_class = views.DashboardView
self.view_func = self.view_class.as_view()
self.view = self.view_class()

# TemplateResponseMixin
def test_template_name(self):
self.view.setup(self.request)
template_names = self.view.get_template_names()
self.assertIn("core/dashboard.html", template_names)

# LoginRequiredMixin
def test_login_required(self):
self.request.user = AnonymousUser()
response = self.view_func(self.request)
self.assertEqual(response.status_code, 302)
self.assertIn(reverse("account_login"), response.url)

def test_anonymous_user_response(self):
response = self.client.get(self.url)
self.assertRedirects(response, f"/accounts/login/?next={self.url}")
# View
def test_response_for_user_with_person(self):
# setup
user = UserFactory()
AdultFactory(user=user)
self.request.user = user

def test_fully_registered_user_response(self):
self.client.force_login(self.fully_registered_user)
response = self.client.get(self.url)
# test
response = self.view_func(self.request)
self.assertEqual(response.status_code, 200)

def test_partially_registered_user_response(self):
self.client.force_login(self.partially_registered_user)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)
def test_response_for_user_without_person(self):
self.request.user = UserFactory()
with self.assertRaises(PermissionDenied):
self.view_func(self.request)
Loading