From 268207f86de7b3aeb1e0fb259e956139fd961465 Mon Sep 17 00:00:00 2001 From: juuso-j Date: Wed, 6 Mar 2024 12:18:52 +0200 Subject: [PATCH 1/4] Test PostalCodeResult is updated after Profile PUT --- account/tests/test_api.py | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/account/tests/test_api.py b/account/tests/test_api.py index a1ff211..028b5cf 100644 --- a/account/tests/test_api.py +++ b/account/tests/test_api.py @@ -8,6 +8,7 @@ from rest_framework.reverse import reverse from account.models import MailingList, MailingListEmail, User +from profiles.models import PostalCode, PostalCodeResult from .utils import check_method_status_codes, patch @@ -75,6 +76,45 @@ def test_profile_created(api_client): assert user.profile.postal_code is None +@pytest.mark.django_db +def test_profile_put_creates_postal_code_result( + api_client_authenticated, users, profiles +): + user = users.get(username="test1") + url = reverse("account:profiles-detail", args=[user.id]) + data = { + "postal_code": "20210", + "optional_postal_code": "20220", + } + response = api_client_authenticated.put(url, data) + assert response.status_code == 200 + assert PostalCodeResult.objects.count() == 2 + assert ( + PostalCodeResult.objects.get( + postal_code=PostalCode.objects.get(postal_code="20210") + ).count + == 1 + ) + assert ( + PostalCodeResult.objects.get( + postal_code=PostalCode.objects.get(postal_code="20220") + ).count + == 1 + ) + + +@pytest.mark.django_db +def test_profile_put_not_creates_postal_code_result( + api_client_authenticated, users, profiles +): + user = users.get(username="test1") + url = reverse("account:profiles-detail", args=[user.id]) + data = {"year_of_birth": 42} + response = api_client_authenticated.put(url, data) + assert response.status_code == 200 + assert PostalCodeResult.objects.count() == 0 + + @pytest.mark.django_db def test_profile_put(api_client_authenticated, users, profiles): user = users.get(username="test1") @@ -123,6 +163,7 @@ def test_profile_patch_geneder(api_client_authenticated, users, profiles): patch(api_client_authenticated, url, {"gender": "X"}) user.refresh_from_db() assert user.profile.gender == "X" + assert PostalCodeResult.objects.count() == 0 @pytest.mark.django_db @@ -132,6 +173,7 @@ def test_profile_patch_postal_code(api_client_authenticated, users, profiles): patch(api_client_authenticated, url, {"postal_code": "20210"}) user.refresh_from_db() assert user.profile.postal_code == "20210" + assert PostalCodeResult.objects.count() == 0 @pytest.mark.django_db From c9da50c115b525b5980f6e7398da88de4ecd1af3 Mon Sep 17 00:00:00 2001 From: juuso-j Date: Wed, 6 Mar 2024 12:19:24 +0200 Subject: [PATCH 2/4] Add result to user fixture --- account/tests/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/account/tests/conftest.py b/account/tests/conftest.py index 958fc90..97b022d 100644 --- a/account/tests/conftest.py +++ b/account/tests/conftest.py @@ -26,8 +26,8 @@ def api_client_with_custom_ip_address(ip_address): @pytest.fixture -def users(): - User.objects.create(username="test1") +def users(results): + User.objects.create(username="test1", result=results.first()) return User.objects.all() From 5604d73946b39b1eb4e269356c22ba90aff56ae0 Mon Sep 17 00:00:00 2001 From: juuso-j Date: Wed, 6 Mar 2024 12:19:45 +0200 Subject: [PATCH 3/4] Update postal code result on Profile PUT --- account/api/views.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/account/api/views.py b/account/api/views.py index 37a580f..de2cfe3 100644 --- a/account/api/views.py +++ b/account/api/views.py @@ -10,6 +10,7 @@ from rest_framework.throttling import AnonRateThrottle from account.models import MailingList, MailingListEmail, Profile +from profiles.api.views import update_postal_code_result from profiles.models import Result from .serializers import ProfileSerializer, SubscribeSerializer, UnSubscribeSerializer @@ -46,6 +47,16 @@ def update(self, request, *args, **kwargs): ) if serializer.is_valid(): serializer.save() + """ + To ensure the postal code results are updated before the user exists the poll. + After the questions are answered the front-end updates the profile information with a put request. + """ + + if ( + request.method == "PUT" + and serializer.data.get("postal_code", None) is not None + ): + update_postal_code_result(user) return Response(data=serializer.data, status=status.HTTP_200_OK) else: return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST) From 02feec7200693236329ff065e7e1999f76a5cf1c Mon Sep 17 00:00:00 2001 From: juuso-j Date: Wed, 6 Mar 2024 12:37:04 +0200 Subject: [PATCH 4/4] Format --- account/api/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/account/api/views.py b/account/api/views.py index de2cfe3..588a214 100644 --- a/account/api/views.py +++ b/account/api/views.py @@ -51,7 +51,7 @@ def update(self, request, *args, **kwargs): To ensure the postal code results are updated before the user exists the poll. After the questions are answered the front-end updates the profile information with a put request. """ - + if ( request.method == "PUT" and serializer.data.get("postal_code", None) is not None