Fakultas Ilmu Komputer UI

Commit 8bcfd55f authored by Glenda Emanuella Sutanto's avatar Glenda Emanuella Sutanto
Browse files

[REFACTOR] Enhanced tests to improve quality

parent 1c604c5e
Pipeline #78376 passed with stages
in 15 minutes and 13 seconds
......@@ -65,8 +65,16 @@ class DietQuestionnaireTests(APITestCase):
def test_string_representation(self):
self.assertEqual(str(self.diet_questionnaire), 'email@email.com')
def test_create_diet_questionnaire(self):
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'), CustomUserDetailsSerializer(self.custom_user_1).data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_create_diet_questionnaire_succeed(self):
login_data = {
'email': 'email2@email.com',
'password': 'abc',
......@@ -81,19 +89,28 @@ class DietQuestionnaireTests(APITestCase):
json_response = json.loads(response.content)
self.assertEqual(DietQuestionnaire.objects.count(), 2)
self.assertEqual(json_response.get('user'), CustomUserDetailsSerializer(self.custom_user_2).data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
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'), CustomUserDetailsSerializer(self.custom_user_1).data)
def test_create_diet_questionnaire_failed(self):
login_data = {
'email': 'email2@email.com',
'password': 'abc',
'role': 'client'
}
login_response = self.client.post('/auth/user-login/', login_data, format='json')
json_login_response = json.loads(login_response.content)
login_credentials = "Bearer " + json_login_response['access_token']
self.client.credentials(HTTP_AUTHORIZATION=login_credentials)
response = self.client.post(self.BASE_URL, {"date_of_birth":"30-12-1995"}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
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'), CustomUserDetailsSerializer(self.custom_user_1).data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_single_diet_questionnaire_failed(self):
self.client.credentials(HTTP_AUTHORIZATION=self.login_credentials)
......@@ -144,3 +161,12 @@ class DietQuestionnaireTests(APITestCase):
format='json'
)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_update_diet_questionnaire_invalid_failed(self):
self.client.credentials(HTTP_AUTHORIZATION=self.login_credentials)
response = self.client.patch(
self.SPECIFIC_URL,
{"date_of_birth":"30-12-1995"},
format='json'
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Markdown is supported
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