Fakultas Ilmu Komputer UI

Commit 150cab90 authored by Ami's avatar Ami
Browse files

[RED] creating test for weekly report

parent 764f738f
Pipeline #79472 passed with stages
in 11 minutes and 50 seconds
import json
from unittest.mock import patch
from rest_framework.test import APITestCase
from django.test import TestCase
from rest_framework import status
from rest_framework.response import Response
from nutritionists.models import Nutritionist
from authentication.models import CustomUser
from .models import WeeklyReport
from .serializers import WeeklyReportSerializer
from .views import WeeklyReportViewSet
class WeeklyReportTest(APITestCase):
@classmethod
def setUpTestData(cls):
cls.BASE_URL = "/progress/user_report/"
cls.nutritionist_1 = Nutritionist.objects.create(
id=1,
full_name_and_degree="Test, S.Gz",
registration_certificate_no="1234567890",
university="Universitas Indonesia",
mastered_nutritional_problems="Manajemen berat badan, hipertensi",
handled_age_group="12-17 tahun (Remaja)",
another_practice_place="RSCM",
languages="Bahasa Indonesia, Bahasa Inggris",
)
cls.custom_user_1 = CustomUser.objects.create_user(
name='tes1',
email='email@email.com',
password='abc',
nutritionist=cls.nutritionist_1,
)
def test_post_reports_api(self):
url = f"{self.BASE_URL}"
login_data = {
'email': 'email@email.com',
'password': 'abc',
'role': 'client',
}
response = self.client.post('/auth/user-login/', login_data, format='json')
json_response = json.loads(response.content)
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + json_response['access_token'])
data = {
"weight": 155,
"height": 188,
"waist_size": 100,
"changes_felt": 1,
"hunger_level" : 1,
"fullness_level": 1,
"heavy_meal": 1,
"snacks": 1,
"sweet_beverages":1,
"fried_snacks":1,
"umami_snacks":1,
"sweet_snacks":1,
"fruits_portion":1,
"vegetables_portion":1,
"water_consumption" : "aw",
"physical_activity": [1,2],
"physical_activity_other" : "n",
"time_for_activity":1,
"feeling_rating":2,
"lesson_learned":"a",
"problem_faced_and_feedbacks":"x"
}
response = self.client.post(
url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_post_reports_api_fail(self):
url = f"{self.BASE_URL}"
login_data = {
'email': 'email@email.com',
'password': 'abc',
'role': 'client',
}
response = self.client.post('/auth/user-login/', login_data, format='json')
json_response = json.loads(response.content)
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + json_response['access_token'])
data = {
"weight": "155",
"height": 188,
"waist_size": 100,
"changes_felt": 1,
"hunger_level" : 1,
"fullness_level": 1,
"heavy_meal": 1,
"snacks": 1,
}
response = self.client.post(
url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_get_all_reports(self):
response = self.client.get(self.BASE_URL)
reports = WeeklyReport.objects.all()
serializer = WeeklyReportSerializer(reports, many=True)
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
\ No newline at end of file
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