generated from NHSDigital/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 4
refactor medical history views #824
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c8851e3
fix link text including a space character
MatMoore 6b2a25b
remove unused import
MatMoore b05fb5e
create generic views for adding and updating
MatMoore 1a483a3
create a mixin for medical information pages
MatMoore 065130f
use the new generic views for medical history
MatMoore 3ab679b
align page titles with the prototype
MatMoore bd1a084
align flash messages
MatMoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
140 changes: 140 additions & 0 deletions
140
manage_breast_screening/core/tests/views/test_generic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
| from django import forms | ||
| from django.contrib.contenttypes.models import ContentType | ||
| from django.contrib.messages.middleware import MessageMiddleware | ||
| from django.contrib.sessions.middleware import SessionMiddleware | ||
| from django.test import RequestFactory | ||
|
|
||
| from manage_breast_screening.auth.tests.factories import UserFactory | ||
| from manage_breast_screening.core.models import AuditLog | ||
| from manage_breast_screening.core.views.generic import ( | ||
| AddWithAuditView, | ||
| UpdateWithAuditView, | ||
| ) | ||
| from manage_breast_screening.users.models import User | ||
|
|
||
|
|
||
| def apply_middleware(request): | ||
| for middleware_class in [SessionMiddleware, MessageMiddleware]: | ||
| middleware = middleware_class(get_response=MagicMock()) | ||
| middleware.process_request(request) | ||
| return request | ||
|
|
||
|
|
||
| class DummyForm(forms.Form): | ||
| first_name = forms.CharField() | ||
| last_name = forms.CharField() | ||
|
|
||
| def __init__(self, *args, instance=None, **kwargs): | ||
| self.instance = instance | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def create(self): | ||
| return UserFactory.create( | ||
| first_name=self.cleaned_data["first_name"], | ||
| last_name=self.cleaned_data["last_name"], | ||
| nhs_uid="create_uid", | ||
| ) | ||
|
|
||
| def update(self): | ||
| return UserFactory.create( | ||
| first_name=self.cleaned_data["first_name"], | ||
| last_name=self.cleaned_data["last_name"], | ||
| nhs_uid="update_uid", | ||
| ) | ||
|
|
||
|
|
||
| class AddView(AddWithAuditView): | ||
| form_class = DummyForm | ||
| thing_name = "test" | ||
| success_url = "/success" | ||
|
|
||
|
|
||
| class UpdateView(UpdateWithAuditView): | ||
| form_class = DummyForm | ||
| thing_name = "test" | ||
| success_url = "/success" | ||
|
|
||
|
|
||
| class TestAddWithAuditView: | ||
| def test_success_message_content(self): | ||
| obj = UserFactory.build() | ||
|
|
||
| assert AddView().get_success_message_content(obj) == "Added test" | ||
|
|
||
| def test_get_context_data(self): | ||
| request = RequestFactory().get("/") | ||
| context = AddView(request=request).get_context_data() | ||
|
|
||
| assert context["heading"] == "Add test" | ||
| assert context["page_title"] == "Add test" | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_audits_if_form_valid(self): | ||
| request = apply_middleware(RequestFactory().post("/")) | ||
| request.user = UserFactory() | ||
| form = DummyForm({"first_name": "abc", "last_name": "def"}) | ||
|
|
||
| assert form.is_valid() | ||
| AddView(request=request).form_valid(form) | ||
|
|
||
| last_audit = AuditLog.objects.last() | ||
| assert last_audit is not None | ||
| assert last_audit.content_type == ContentType.objects.get_for_model(User) | ||
| assert last_audit.actor == request.user | ||
| assert last_audit.operation == AuditLog.Operations.CREATE | ||
| assert last_audit.snapshot == { | ||
| "email": "", | ||
| "first_name": "abc", | ||
| "last_name": "def", | ||
| "is_staff": False, | ||
| "is_active": True, | ||
| "last_login": None, | ||
| "nhs_uid": "create_uid", | ||
| } | ||
| assert last_audit.system_update_id is None | ||
|
|
||
|
|
||
| class TestUpdateWithAuditView: | ||
| def test_success_message_content(self): | ||
| obj = UserFactory.build() | ||
|
|
||
| assert UpdateView().get_success_message_content(obj) == "Updated test" | ||
|
|
||
| def test_get_context_data(self): | ||
| obj = UserFactory.build() | ||
| request = RequestFactory().get("/") | ||
| view = UpdateView(request=request) | ||
| view.object = obj | ||
|
|
||
| context = view.get_context_data() | ||
|
|
||
| assert context["heading"] == "Edit test" | ||
| assert context["page_title"] == "Edit test" | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_audits_if_form_valid(self): | ||
| request = apply_middleware(RequestFactory().post("/")) | ||
| request.user = UserFactory() | ||
| form = DummyForm({"first_name": "new", "last_name": "name"}) | ||
|
|
||
| assert form.is_valid() | ||
| UpdateView(request=request).form_valid(form) | ||
|
|
||
| last_audit = AuditLog.objects.last() | ||
| assert last_audit is not None | ||
| assert last_audit.content_type == ContentType.objects.get_for_model(User) | ||
| assert last_audit.actor == request.user | ||
| assert last_audit.operation == AuditLog.Operations.UPDATE | ||
| assert last_audit.snapshot == { | ||
| "email": "", | ||
| "first_name": "new", | ||
| "last_name": "name", | ||
| "is_staff": False, | ||
| "is_active": True, | ||
| "last_login": None, | ||
| "nhs_uid": "update_uid", | ||
| } | ||
| assert last_audit.system_update_id is None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea here is that each concrete view defines this variable and we will generate default messages/titles according to the pattern.
Alternatively we could use
modelinstead ofthing_nameand use theverbose_nameof the model, like some of the built in generic views do, but that would be a bit less flexible.