Fakultas Ilmu Komputer UI

test_investigation_cases.py 11.2 KB
Newer Older
1
2
3
4
import json
import pytz
from datetime import datetime
from rest_framework import status
5
6
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase, APIClient
7
8
9
10
11
12
13
14

from apps.accounts.tests.factories.accounts import AccountFactory, UserFactory
from apps.cases.models import PositiveCase, InvestigationCase
from apps.cases.tests.factories.case_subjects import CaseSubjectFactory
from apps.cases.tests.factories.cases import (
    PositiveCaseFactory,
    InvestigationCaseFactory,
)
15
16
from apps.constants import HEADER_PREFIX
from apps.constants import TIMEZONE
17
18


19
class InvestigationCaseViewTest(APITestCase):
20
21
    @classmethod
    def setUpTestData(self):
22
23
24
25
        self.user_1 = UserFactory(username="user_1", password="justpass")
        self.author = AccountFactory(user=self.user_1, admin=True)
        self.token_1, _ = Token.objects.get_or_create(user=self.user_1)

26
27
28
29
30
31
32
33
34
35
36
37
38
        self.case_subject = CaseSubjectFactory()
        self.reference_case = PositiveCaseFactory(
            case_subject_id=self.case_subject.subject_id, author=self.author,
        )
        self.case = InvestigationCaseFactory(
            author=self.author,
            case_subject_id=self.case_subject.subject_id,
            reference_case_id=self.reference_case.case_id,
        )
        self.other_deleted_case = InvestigationCaseFactory(
            author=self.author,
            case_subject_id=self.case_subject.subject_id,
            reference_case_id=self.reference_case.case_id,
39
            deleted_at=datetime.now(tz=pytz.timezone(TIMEZONE)),
40
41
42
43
44
45
46
47
        )
        self.other_inactive_case = InvestigationCaseFactory(
            author=self.author,
            case_subject_id=self.case_subject.subject_id,
            reference_case_id=self.reference_case.case_id,
            is_active=False,
        )

48
    def setUp(self):
49
        self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.token_1.key)
50

51
52
53
54
55
56
    def test_list_investigation_cases_success(self):
        url = "/cases/investigation-cases/"

        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        response_string = response.rendered_content.decode("utf-8")

        self.assertIn('"count":1', response_string)
        self.assertIn('"next":null', response_string)
        self.assertIn('"previous":null', response_string)
        self.assertIn(str(self.case.revision_id), response_string)
        self.assertIn(str(self.case.case_id), response_string)
        self.assertIn(str(self.case.case_subject.revision_id), response_string)
        self.assertIn(str(self.case.case_subject.subject_id), response_string)
        self.assertIn(str(self.case.reference_case.revision_id), response_string)
        self.assertIn(str(self.case.reference_case.case_id), response_string)
        self.assertIn(str(self.case.reference_case.author.id), response_string)
        self.assertIn(str(self.case.author.id), response_string)

    def test_list_investigation_case_paginate_failed(self):
        url = "/cases/investigation-cases/?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 = "/cases/investigation-cases/?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 = "/cases/investigation-cases/?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)

101
102
103
104
105
106
107
108
109
110
111
112
    def test_retrieve_investigation_case_success(self):
        url = "/cases/investigation-cases/" + str(self.case.revision_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 = (
            "/cases/investigation-cases/"
            + str(self.other_deleted_case.revision_id)
            + "/"
        )
113

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_create_new_investigation_case_success(self):
        url = "/cases/investigation-cases/"

        case_subject_prev_active_count = (
            InvestigationCase.objects.active_revisions().count()
        )
        case_prev_all_count = InvestigationCase.objects.all().count()

        data = {
            "reference_case_id": str(self.reference_case.case_id),
            "case_relation": self.case.case_relation,
            "case_subject_id": str(self.case.case_subject.subject_id),
            "medical_symptoms": self.case.medical_symptoms,
            "risk_factors": self.case.risk_factors,
            "is_referral_needed": self.case.is_referral_needed,
            "medical_facility_reference": self.case.medical_facility_reference,
            "author": str(self.case.author.id),
        }

136
        response = self.client.post(path=url, data=data, format="json",)
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        case_current_active_count = InvestigationCase.objects.active_revisions().count()
        case_current_all_count = InvestigationCase.objects.all().count()

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(case_current_active_count, case_subject_prev_active_count + 1)
        self.assertEqual(case_current_all_count, case_prev_all_count + 1)

    def test_create_new_investigation_case_fails_with_incomplete_data(self):
        url = "/cases/investigation-cases/"

        data = {
            "medical_facility_reference": self.case.medical_facility_reference,
            "author": str(self.case.author.id),
        }

152
        response = self.client.post(path=url, data=data, format="json",)
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_edit_investigation_success(self):
        url = "/cases/investigation-cases/" + str(self.case.revision_id) + "/"

        data = {
            "case_subject_id": str(self.case_subject.subject_id),
            "reference_case_id": str(self.reference_case.case_id),
            "case_relation": self.case.case_relation,
            "medical_symptoms": self.case.medical_symptoms,
            "risk_factors": self.case.risk_factors,
            "is_referral_needed": not self.case.is_referral_needed,
            "medical_facility_reference": self.case.medical_facility_reference,
            "author": str(self.case.author.id),
        }
        prev_all_investigation_case_revision_count = (
            InvestigationCase.objects.all().count()
        )
        prev_active_investigation_case_revision_count = (
            InvestigationCase.objects.active_revisions().count()
        )

175
        response = self.client.put(path=url, data=data, format="json",)
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data["is_referral_needed"], not self.case.is_referral_needed
        )

        current_investigation_case_revision_count = (
            InvestigationCase.objects.all().count()
        )
        current_active_investigation_case_revision_count = (
            InvestigationCase.objects.active_revisions().count()
        )

        self.assertFalse(
            InvestigationCase.objects.get(revision_id=self.case.revision_id).is_active
        )
        self.assertEqual(
            current_investigation_case_revision_count,
            prev_all_investigation_case_revision_count + 1,
        )
        self.assertEqual(
            current_active_investigation_case_revision_count,
            prev_active_investigation_case_revision_count,
        )

    def test_edit_inactive_investigation_case_fails(self):
        url = (
            "/cases/investigation-cases/"
            + str(self.other_inactive_case.revision_id)
            + "/"
        )
        data = {
            "case_subject_id": str(self.other_inactive_case.case_subject_id),
            "reference_case_id": str(self.reference_case.case_id),
            "case_relation": self.other_inactive_case.case_relation,
            "medical_symptoms": self.other_inactive_case.medical_symptoms,
            "risk_factors": self.other_inactive_case.risk_factors,
            "is_referral_needed": not self.other_inactive_case.is_referral_needed,
            "medical_facility_reference": self.other_inactive_case.medical_facility_reference,
            "author": str(self.other_inactive_case.author.id),
        }
216
217

        response = self.client.put(path=url, data=data, format="json",)
218
219
220
221
222
223
224
225
226
227
228
229
230
231
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_edit_investigation_case_fails_with_incomplete_data(self):
        url = "/cases/investigation-cases/" + str(self.case.revision_id) + "/"

        data = {
            "medical_facility_reference": "KUBURAN",
            "author": str(self.case.author.id),
            "case_subject_id": None,
        }

        prev_active_investigation_case_revision_count = (
            InvestigationCase.objects.active_revisions().count()
        )
232
233

        response = self.client.put(path=url, data=data, format="json",)
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        current_active_investigation_case_revision_count = (
            InvestigationCase.objects.active_revisions().count()
        )
        self.assertEqual(
            current_active_investigation_case_revision_count,
            prev_active_investigation_case_revision_count,
        )
        self.assertTrue(
            InvestigationCase.objects.get(revision_id=self.case.revision_id).is_active
        )

    def test_soft_delete_investigation_case_success(self):
        url = "/cases/investigation-cases/" + str(self.case.revision_id) + "/"

        prev_deleted_all_count = InvestigationCase.objects.all(
            with_deleted=True
        ).count()
        prev_deleted_count = InvestigationCase.objects.deleted().count()

254
        response = self.client.delete(path=url, format="json",)
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIsNotNone(
            InvestigationCase.objects.all(with_deleted=True)
            .filter(revision_id=self.case.revision_id)
            .first()
        )

        current_deleted_all_count = InvestigationCase.objects.all(
            with_deleted=True
        ).count()
        current_deleted_count = InvestigationCase.objects.deleted().count()

        self.assertEqual(current_deleted_all_count, prev_deleted_all_count)
        self.assertEqual(current_deleted_count, prev_deleted_count + 1)

    def test_delete_inactive_investigation_case_fails(self):
        url = (
            "/cases/investigation-cases/"
            + str(self.other_inactive_case.revision_id)
            + "/"
        )
276

277
278
        response = self.client.delete(url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)