Fakultas Ilmu Komputer UI

Commit 781e77e5 authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto Committed by Muzaki Azami
Browse files

Modify diet questionnaire serializer to display user details

parent 2a191a23
......@@ -69,7 +69,7 @@ def get_diet_questionnaire_payload_sample_with_step():
"step": 1
}
def get_diet_questionnaire_response_sample_with_step(id, user_id):
def get_diet_questionnaire_response_sample_with_step(id, user):
return {
"id": id,
......@@ -141,7 +141,7 @@ def get_diet_questionnaire_response_sample_with_step(id, user_id):
"finished_steps": [
1
],
"user": user_id
"user": user
}
def get_diet_questionnaire_payload_sample_without_step():
......@@ -214,7 +214,7 @@ def get_diet_questionnaire_payload_sample_without_step():
"dietela_program_expectation": "-",
}
def get_diet_questionnaire_response_sample_without_step(id, user_id):
def get_diet_questionnaire_response_sample_without_step(id, user):
return {
"id": id,
......@@ -284,5 +284,5 @@ def get_diet_questionnaire_response_sample_without_step(id, user_id):
"dietela_nutritionist_expectation": "-",
"dietela_program_expectation": "-",
"finished_steps": [],
"user": user_id
"user": user
}
......@@ -6,10 +6,12 @@ from constants.model_choices import (
PhysicalActivity,
Disease
)
from authentication.serializers import CustomUserDetailsSerializer
from .models import DietQuestionnaire
class DietQuestionnaireSerializer(serializers.ModelSerializer):
meal_provider = fields.MultipleChoiceField(
choices = MealProvider.choices, required=False
)
......@@ -30,6 +32,8 @@ class DietQuestionnaireSerializer(serializers.ModelSerializer):
choices = Disease.choices, required=False
)
user = CustomUserDetailsSerializer(read_only=True)
class Meta:
model = DietQuestionnaire
fields = "__all__"
......@@ -5,6 +5,7 @@ from rest_framework.test import APITestCase, APIClient
from rest_framework import status
from .models import DietQuestionnaire
from authentication.models import CustomUser
from authentication.serializers import CustomUserDetailsSerializer
from constants.diet_questionnaire_sample import (
get_diet_questionnaire_payload_sample_with_step,
get_diet_questionnaire_response_sample_with_step,
......@@ -42,7 +43,7 @@ class DietQuestionnaireTests(APITestCase):
cls.login_credentials = "Bearer " + cls.json_login_response['access_token']
cls.diet_questionnaire = DietQuestionnaire.objects.create(
user_id = cls.custom_user_1.id
user = cls.custom_user_1
)
cls.SPECIFIC_URL = f"{cls.BASE_URL}{cls.diet_questionnaire.id}/"
......@@ -51,14 +52,14 @@ class DietQuestionnaireTests(APITestCase):
cls.sample_response_with_step = \
get_diet_questionnaire_response_sample_with_step(
cls.diet_questionnaire.id,
cls.custom_user_1.id
CustomUserDetailsSerializer(cls.custom_user_1).data
)
cls.sample_payload_without_step = get_diet_questionnaire_payload_sample_without_step()
cls.sample_response_without_step = \
get_diet_questionnaire_response_sample_without_step(
cls.diet_questionnaire.id,
cls.custom_user_1.id
CustomUserDetailsSerializer(cls.custom_user_1).data
)
......@@ -79,20 +80,20 @@ class DietQuestionnaireTests(APITestCase):
response = self.client.post(self.BASE_URL, {}, format='json')
json_response = json.loads(response.content)
self.assertEqual(DietQuestionnaire.objects.count(), 2)
self.assertEqual(json_response.get('user'), self.custom_user_2.id)
self.assertEqual(json_response.get('user'), CustomUserDetailsSerializer(self.custom_user_2).data)
def test_get_all_diet_questionnaire(self):
self.client.credentials(HTTP_AUTHORIZATION=self.login_credentials)
response = self.client.get(self.BASE_URL)
json_response = json.loads(response.content)
self.assertEqual(len(json_response), 1)
self.assertEqual(json_response[0].get('user'), self.custom_user_1.id)
self.assertEqual(json_response[0].get('user'), CustomUserDetailsSerializer(self.custom_user_1).data)
def test_get_single_diet_questionnaire_succeed(self):
self.client.credentials(HTTP_AUTHORIZATION=self.login_credentials)
response = self.client.get(f"{self.BASE_URL}{self.diet_questionnaire.id}/")
json_response = json.loads(response.content)
self.assertEqual(json_response.get('user'), self.custom_user_1.id)
self.assertEqual(json_response.get('user'), CustomUserDetailsSerializer(self.custom_user_1).data)
def test_get_single_diet_questionnaire_failed(self):
self.client.credentials(HTTP_AUTHORIZATION=self.login_credentials)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment