Fakultas Ilmu Komputer UI

Commit be1ac84b authored by Dave Nathanael's avatar Dave Nathanael
Browse files

[RED] Add test for reworked CaseSubject and its history object

parent c9bfaedb
Pipeline #40879 failed with stages
in 1 minute and 36 seconds
......@@ -6,7 +6,7 @@ from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase, APIClient
from apps.accounts.tests.factories.accounts import AccountFactory, UserFactory
from apps.cases.models import CaseSubject
from apps.cases.models import CaseSubject, CaseSubjectHistory
from apps.cases.tests.factories.case_subjects import CaseSubjectFactory
from apps.constants import (
......@@ -29,24 +29,17 @@ class CaseSubjectViewTest(APITestCase):
self.officer = AccountFactory(admin=False, user=self.user_2)
self.token_1, _ = Token.objects.get_or_create(user=self.user_1)
self.case_subject_1 = CaseSubjectFactory(is_active=True)
self.case_subject_1 = CaseSubjectFactory()
self.case_subject_2 = CaseSubjectFactory(
is_active=True, deleted_at=datetime.now(tz=pytz.timezone(TIMEZONE))
deleted_at=datetime.now(tz=pytz.timezone(TIMEZONE)),
)
self.case_subject_3 = CaseSubjectFactory(is_active=False)
def setUp(self):
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.token_1.key)
def test_string_representation(self):
inactive_case_subject_str = (
f"[Inactive] Rev. {self.case_subject_3.revision_id} - {self.case_subject_3.name} | "
+ f"{self.case_subject_3.district}, {self.case_subject_3.sub_district}"
)
self.assertEqual(inactive_case_subject_str, str(self.case_subject_3))
active_case_subject_str = (
f"[Active] Rev. {self.case_subject_1.revision_id} - {self.case_subject_1.name} | "
f"[Active] 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))
......@@ -62,15 +55,13 @@ class CaseSubjectViewTest(APITestCase):
"previous": None,
"results": [
{
"revision_id": str(self.case_subject_1.revision_id),
"subject_id": str(self.case_subject_1.subject_id),
"id": str(self.case_subject_1.id),
"name": self.case_subject_1.name,
"age": self.case_subject_1.age,
"is_male": self.case_subject_1.is_male,
"address": self.case_subject_1.address,
"district": self.case_subject_1.district,
"sub_district": self.case_subject_1.sub_district,
"is_active": self.case_subject_1.is_active,
},
],
}
......@@ -106,36 +97,54 @@ class CaseSubjectViewTest(APITestCase):
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.revision_id) + "/"
url = self.BASE_URL + str(self.case_subject_1.id) + "/"
response = self.client.get(url)
data = {
"revision_id": str(self.case_subject_1.revision_id),
"subject_id": str(self.case_subject_1.subject_id),
"id": str(self.case_subject_1.id),
"name": self.case_subject_1.name,
"age": self.case_subject_1.age,
"is_male": self.case_subject_1.is_male,
"address": self.case_subject_1.address,
"district": self.case_subject_1.district,
"sub_district": self.case_subject_1.sub_district,
"is_active": self.case_subject_1.is_active,
}
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertJSONEqual(json.dumps(response.data), data)
def test_retrieve_case_subject_fails_on_deleted_subject(self):
url = self.BASE_URL + str(self.case_subject_2.revision_id) + "/"
url = self.BASE_URL + str(self.case_subject_2.id) + "/"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_create_new_case_subject_success(self):
url = self.BASE_URL
case_subject_prev_active_count = CaseSubject.objects.active_revisions().count()
case_subject_prev_all_count = CaseSubject.objects.all().count()
case_subject_prev_count = CaseSubject.objects.all().count()
case_subject_history_prev_count = CaseSubjectHistory.objects.all().count()
data = {
"name": self.case_subject_1.name,
......@@ -147,33 +156,18 @@ class CaseSubjectViewTest(APITestCase):
}
response = self.client.post(path=url, data=data, format="json",)
response.data["is_active"] = self.case_subject_1.is_active
case_subject_current_active_count = (
CaseSubject.objects.active_revisions().count()
)
case_subject_current_all_count = CaseSubject.objects.all().count()
case_subject_current_count = CaseSubject.objects.all().count()
case_subject_history_current_count = CaseSubjectHistory.objects.all().count()
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(
case_subject_current_active_count, case_subject_prev_active_count + 1
case_subject_current_count, case_subject_prev_count + 1
)
self.assertEqual(
case_subject_current_all_count, case_subject_prev_all_count + 1
)
# Have case subject creation log
logs_response = self.client.get("/logs/")
response_string = logs_response.rendered_content.decode("utf-8")
self.assertIn(
'"object_id":"{}"'.format(response.data["subject_id"]), response_string
)
self.assertIn(
'"revision_id":"{}"'.format(response.data["revision_id"]), response_string
)
self.assertIn(
'"action_type":"{}"'.format(ACTIVITY_TYPE_CREATE), response_string
# New CaseSubjectHistory added
self.assertEqual(
case_subject_history_current_count, case_subject_history_prev_count + 1
)
def test_create_new_case_subject_fails_with_incomplete_data(self):
......@@ -190,10 +184,10 @@ class CaseSubjectViewTest(APITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_edit_case_subject_success(self):
url = self.BASE_URL + str(self.case_subject_1.revision_id) + "/"
url = self.BASE_URL + str(self.case_subject_1.id) + "/"
data = {
"subject_id": str(self.case_subject_1.subject_id),
"id": str(self.case_subject_1.id),
"name": self.case_subject_1.name,
"age": self.case_subject_1.age,
"is_male": not self.case_subject_1.is_male,
......@@ -202,45 +196,27 @@ class CaseSubjectViewTest(APITestCase):
"sub_district": self.case_subject_1.sub_district,
}
prev_all_case_subject_revision_count = CaseSubject.objects.all().count()
prev_active_case_subject_revision_count = (
CaseSubject.objects.active_revisions().count()
)
case_subject_prev_count = CaseSubject.objects.all().count()
case_subject_history_prev_count = CaseSubjectHistory.objects.all().count()
response = self.client.put(path=url, data=data, format="json",)
response.data.pop("revision_id")
response.data.pop("is_active")
response.data.pop("id")
current_case_subject_revision_count = CaseSubject.objects.all().count()
current_active_case_subject_revision_count = (
CaseSubject.objects.active_revisions().count()
)
case_subject_prev_count = CaseSubject.objects.all().count()
case_subject_history_prev_count = CaseSubjectHistory.objects.all().count()
self.assertJSONEqual(json.dumps(response.data), data)
self.assertEqual(
current_case_subject_revision_count,
prev_all_case_subject_revision_count + 1,
)
self.assertEqual(
current_active_case_subject_revision_count,
prev_active_case_subject_revision_count,
case_subject_current_count, case_subject_prev_count
)
# Have case subject update log
response = self.client.get("/logs/")
response_string = response.rendered_content.decode("utf-8")
self.assertIn(
'"object_id":"{}"'.format(self.case_subject_1.subject_id), response_string
)
self.assertIn(
'"revision_id":"{}"'.format(self.case_subject_1.revision_id),
response_string,
# New CaseSubjectHistory added
self.assertEqual(
case_subject_history_current_count, case_subject_history_prev_count + 1
)
self.assertIn('"action_type":"{}"'.format(ACTIVITY_TYPE_EDIT), response_string)
def test_edit_case_subject_fails_with_incomplete_data(self):
url = self.BASE_URL + str(self.case_subject_1.revision_id) + "/"
url = self.BASE_URL + str(self.case_subject_1.id) + "/"
data = {
"address": self.case_subject_1.address,
......@@ -260,41 +236,20 @@ class CaseSubjectViewTest(APITestCase):
)
def test_soft_delete_case_subject_success(self):
url = self.BASE_URL + str(self.case_subject_1.revision_id) + "/"
prev_deleted_all_count = CaseSubject.objects.all(with_deleted=True).count()
prev_deleted_count = CaseSubject.objects.deleted().count()
url = self.BASE_URL + str(self.case_subject_1.id) + "/"
case_subject_prev_count = CaseSubject.objects.all().count()
case_subject_history_prev_count = CaseSubjectHistory.objects.all().count()
response = self.client.delete(path=url, format="json",)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response = self.client.delete(path=url, format="json")
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertIsNotNone(
CaseSubject.objects.all(with_deleted=True)
.filter(revision_id=self.case_subject_1.revision_id)
.filter(id=self.case_subject_1.id)
.first()
)
current_deleted_all_count = CaseSubject.objects.all(with_deleted=True).count()
current_deleted_count = CaseSubject.objects.deleted().count()
self.assertEqual(current_deleted_all_count, prev_deleted_all_count)
self.assertEqual(current_deleted_count, prev_deleted_count + 1)
case_subject_current_count = CaseSubject.objects.all().count()
case_subject_history_current_count = CaseSubjectHistory.objects.all().count()
# Have case subject deletion log
response = self.client.get("/logs/")
response_string = response.rendered_content.decode("utf-8")
self.assertIn(
'"object_id":"{}"'.format(self.case_subject_1.subject_id), response_string
)
self.assertIn(
'"revision_id":"{}"'.format(self.case_subject_1.revision_id),
response_string,
)
self.assertIn(
'"action_type":"{}"'.format(ACTIVITY_TYPE_DELETE), response_string
)
def test_delete_inactive_case_subject_fails(self):
url = self.BASE_URL + str(self.case_subject_3.revision_id) + "/"
response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(case_subject_current_count, case_subject_prev_count - 1)
self.assertEqual(case_subject_history_current_count, case_subject_history_prev_count + 1)
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