Fakultas Ilmu Komputer UI

Commit be1cfd5f authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

[RED] Fix tests according to rework criteria

parent 1c9a5205
......@@ -22,6 +22,8 @@ class InvestigationCaseFactory(factory.DjangoModelFactory):
case_relation = "Family"
medical_facility_reference = "Hospital"
author = factory.SubFactory(AccountFactory)
case_subject = factory.SubFactory(CaseSubjectFactory)
author = factory.SubFactory(AccountFactory)
class MonitoringCaseFactory(factory.DjangoModelFactory):
......@@ -29,3 +31,4 @@ class MonitoringCaseFactory(factory.DjangoModelFactory):
model = MonitoringCase
author = factory.SubFactory(AccountFactory)
investigation_case = factory.SubFactory(InvestigationCaseFactory)
......@@ -38,17 +38,16 @@ class CaseSubjectViewTest(APITestCase):
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.token_1.key)
def test_string_representation(self):
active_case_subject_str = (
f"[Active] ID. {self.case_subject_1.id} - {self.case_subject_1.name} | "
case_subject_str = (
f"ID. {self.case_subject_1.id} - {self.case_subject_1.name} | "
+ f"{self.case_subject_1.district}, {self.case_subject_1.sub_district}"
)
self.assertEqual(active_case_subject_str, str(self.case_subject_1))
self.assertEqual(case_subject_str, str(self.case_subject_1))
def test_list_case_subjects_only_shows_active_entries_success(self):
def test_list_case_subjects_success(self):
url = self.BASE_URL
response = self.client.get(url)
response.data.results[0].pop("created_at")
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = {
"count": 1,
......@@ -63,61 +62,12 @@ class CaseSubjectViewTest(APITestCase):
"address": self.case_subject_1.address,
"district": self.case_subject_1.district,
"sub_district": self.case_subject_1.sub_district,
"created_at": response.data["results"][0]["created_at"],
},
],
}
self.assertJSONEqual(json.dumps(response.data), data)
def test_list_case_subjects_paginate_failed(self):
url = self.BASE_URL + "?page=100"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
def test_list_case_subjects_filter_success(self):
url = self.BASE_URL + "?district=" + self.case_subject_1.district
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
def test_list_case_subjects_filter_failed(self):
url = self.BASE_URL + "?district=1234567890"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_list_case_subjects_search_success(self):
url = self.BASE_URL + "?search=" + self.case_subject_1.address[:-1]
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
def test_list_case_subjects_search_failed(self):
url = self.BASE_URL + "?search=1234567890"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_retrieve_case_subject_success(self):
url = self.BASE_URL + str(self.case_subject_1.id) + "/"
......
......@@ -51,10 +51,10 @@ class InvestigationCaseViewTest(APITestCase):
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.token_1.key)
def test_string_representation(self):
active_case_str = (
f"[Active] ID. {self.case.id} | by {self.case.author}"
case_subject_str = (
f"ID. {self.case.id} | by {self.case.author}"
)
self.assertEqual(active_case_str, str(self.case))
self.assertEqual(case_subject_str, str(self.case))
def test_list_investigation_cases_success(self):
url = self.BASE_URL
......@@ -98,62 +98,6 @@ class InvestigationCaseViewTest(APITestCase):
self.assertIn(str(self.reference_case.case_subject.id), response_string)
self.assertIn(str(self.reference_case.author.id), response_string)
def test_list_positive_investigation_case_paginate_failed(self):
url = self.BASE_POSITIVE_URL + "?page=100"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
def test_list_investigation_case_filter_success(self):
url = self.BASE_URL + "?risk_factors=" + self.case.risk_factors
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
def test_list_investigation_case_filter_failed(self):
url = self.BASE_URL + "?risk_factors=1234567890"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_list_investigation_cases_search_success(self):
url = self.BASE_URL + "?search=" + self.case.risk_factors[:-1]
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
def test_list_investigation_cases_search_failed(self):
url = self.BASE_URL + "?search=1234567890"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_retrieve_investigation_case_success(self):
url = self.BASE_URL + str(self.case.id) + "/"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_retrieve_investigation_case_fails_on_deleted_subject(self):
url = self.BASE_URL + str(self.other_deleted_case.id) + "/"
......@@ -267,7 +211,7 @@ class InvestigationCaseViewTest(APITestCase):
investigation_case_history_prev_count = InvestigationCaseHistory.objects.all().count()
response = self.client.delete(path=url, format="json",)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertIsNotNone(
InvestigationCase.objects.all(with_deleted=True)
.filter(id=self.case.id)
......
......@@ -35,44 +35,34 @@ class MonitoringCaseViewTest(APITestCase):
self.case_subject = CaseSubjectFactory()
self.positive_investigation_case = InvestigationCaseFactory(
author=self.author,
case_subject_id=self.case_subject.subject_id,
reference_case_id=None,
case_subject=self.case_subject,
reference_case=None,
)
self.case_1 = MonitoringCaseFactory(
author=self.author, investigation_case_id=self.positive_investigation_case.case_id,
author=self.author, investigation_case=self.positive_investigation_case,
)
self.investigation_case = InvestigationCaseFactory(
author=self.author,
case_subject_id=self.case_subject.subject_id,
reference_case_id=None,
case_subject=self.case_subject,
reference_case=None,
)
self.case_2 = MonitoringCaseFactory(
author=self.author, investigation_case_id=self.investigation_case.case_id,
author=self.author, investigation_case=self.investigation_case,
)
self.other_deleted_case = MonitoringCaseFactory(
author=self.author,
investigation_case_id=self.positive_investigation_case.case_id,
investigation_case=self.positive_investigation_case,
deleted_at=datetime.now(tz=pytz.timezone(TIMEZONE)),
)
self.inactive_case = MonitoringCaseFactory(
author=self.author,
investigation_case_id=self.positive_investigation_case.case_id,
is_active=False,
)
def setUp(self):
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.token.key)
def test_string_representation(self):
inactive_case_str = (
f"[Inactive] Rev. {self.inactive_case.revision_id} | by {self.inactive_case.author}"
)
self.assertEqual(inactive_case_str, str(self.inactive_case))
active_case_str = (
f"[Active] Rev. {self.case_1.revision_id} | by {self.case_1.author}"
case_subject_str = (
f"ID. {self.case_1.id} | by {self.case_1.author}"
)
self.assertEqual(active_case_str, str(self.case_1))
self.assertEqual(case_subject_str, str(self.case_1))
def test_list_monitoring_cases_success(self):
url = self.BASE_URL
......@@ -80,69 +70,28 @@ class MonitoringCaseViewTest(APITestCase):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":2', response_string)
self.assertIn('"next":null', response_string)
self.assertIn('"previous":null', response_string)
def test_list_monitoring_case_paginate_failed(self):
url = self.BASE_URL + "?page=100"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
def test_list_monitoring_case_filter_success(self):
url = self.BASE_URL + "?is_checked=" + str(self.case_1.is_checked)
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":2', response_string)
def test_list_monitoring_case_filter_failed(self):
url = self.BASE_URL + "?is_checked=False"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_retrieve_monitoring_case_success(self):
url = self.BASE_URL + str(self.case_2.revision_id) + "/"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_retrieve_monitoring_case_fails_on_deleted_subject(self):
url = self.BASE_URL + str(self.other_deleted_case.revision_id) + "/"
url = self.BASE_URL + str(self.other_deleted_case.id) + "/"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_create_new_monitoring_case_success(self):
url = self.BASE_URL
case_prev_active_count = MonitoringCase.objects.active_revisions().count()
case_prev_all_count = MonitoringCase.objects.all().count()
data = {
"investigation_case_id": str(self.positive_investigation_case.case_id),
"investigation_case": str(self.positive_investigation_case.id),
"is_referred": True,
"is_checked": False,
"regular_medicine_intake": "[]",
"treatment_start_date": "1111-11-11",
"treatment_end_date": "1111-11-12"
}
response = self.client.post(path=url, data=data, format="json",)
case_current_active_count = MonitoringCase.objects.active_revisions().count()
case_current_all_count = MonitoringCase.objects.all().count()
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(case_current_active_count, case_prev_active_count + 1)
self.assertEqual(case_current_all_count, case_prev_all_count + 1)
# Have monitoring case creation log
......@@ -150,74 +99,38 @@ class MonitoringCaseViewTest(APITestCase):
response_string = logs_response.rendered_content.decode("utf-8")
self.assertIn(
'"object_id":"{}"'.format(response.data["case_id"]), response_string
)
self.assertIn(
'"revision_id":"{}"'.format(response.data["revision_id"]), response_string
'"object_id":"{}"'.format(str(self.positive_investigation_case.id)), response_string
)
self.assertIn(
'"action_type":"{}"'.format(ACTIVITY_TYPE_CREATE), response_string
)
def test_edit_monitoring_case_success(self):
url = self.BASE_URL + str(self.case_1.revision_id) + "/"
url = self.BASE_URL + str(self.case_1.id) + "/"
data = {
"investigation_case_id": str(self.positive_investigation_case.revision_id),
"investigation_case": str(self.positive_investigation_case.id),
"author": str(self.case_1.author.id),
}
prev_all_monitoring_case_revision_count = MonitoringCase.objects.all().count()
prev_active_monitoring_case_revision_count = (
MonitoringCase.objects.active_revisions().count()
)
response = self.client.put(path=url, data=data, format="json",)
self.assertEqual(response.status_code, status.HTTP_200_OK)
current_monitoring_case_revision_count = MonitoringCase.objects.all().count()
current_active_monitoring_case_revision_count = (
MonitoringCase.objects.active_revisions().count()
)
self.assertFalse(
MonitoringCase.objects.get(revision_id=self.case_1.revision_id).is_active
)
self.assertEqual(
current_monitoring_case_revision_count,
prev_all_monitoring_case_revision_count + 1,
)
self.assertEqual(
current_active_monitoring_case_revision_count,
prev_active_monitoring_case_revision_count,
)
# Have monitoring case update log
response = self.client.get("/logs/")
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"object_id":"{}"'.format(self.case_1.case_id), response_string)
self.assertIn(
'"revision_id":"{}"'.format(self.case_1.revision_id), response_string
)
self.assertIn('"object_id":"{}"'.format(self.case_1.id), response_string)
self.assertIn('"action_type":"{}"'.format(ACTIVITY_TYPE_EDIT), response_string)
def test_edit_inactive_monitoring_case_fails(self):
url = self.BASE_URL + str(self.inactive_case.revision_id) + "/"
data = {
"investigation_case_id": str(self.positive_investigation_case.revision_id),
"author": str(self.case_1.author.id),
}
response = self.client.put(path=url, data=data, format="json",)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_soft_delete_monitoring_case_success(self):
url = self.BASE_URL + str(self.case_1.revision_id) + "/"
url = self.BASE_URL + str(self.case_1.id) + "/"
response = self.client.delete(path=url, format="json",)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertIsNotNone(
MonitoringCase.objects.all(with_deleted=True)
.filter(revision_id=self.case_1.revision_id)
.filter(id=self.case_1.id)
.first()
)
......@@ -225,15 +138,7 @@ class MonitoringCaseViewTest(APITestCase):
response = self.client.get("/logs/")
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"object_id":"{}"'.format(self.case_1.case_id), response_string)
self.assertIn(
'"revision_id":"{}"'.format(self.case_1.revision_id), response_string
)
self.assertIn('"object_id":"{}"'.format(self.case_1.id), response_string)
self.assertIn(
'"action_type":"{}"'.format(ACTIVITY_TYPE_DELETE), response_string
)
def test_delete_inactive_monitoring_case_fails(self):
url = self.BASE_URL + str(self.inactive_case.revision_id) + "/"
response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
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