Fakultas Ilmu Komputer UI

test_positive_cases.py 8.38 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

from apps.accounts.tests.factories.accounts import AccountFactory, UserFactory
from apps.cases.models import PositiveCase
from apps.cases.tests.factories.case_subjects import CaseSubjectFactory
from apps.cases.tests.factories.cases import PositiveCaseFactory
12
13
from apps.constants import HEADER_PREFIX
from apps.constants import TIMEZONE
14
15


16
class PositiveCaseViewTest(APITestCase):
17
18
    @classmethod
    def setUpTestData(self):
19
20
21
22
        self.user_1 = UserFactory(username="user_1", password="justpass")
        self.author = AccountFactory(user=self.user_1, admin=False)
        self.token_1, _ = Token.objects.get_or_create(user=self.user_1)

23
24
25
26
27
28
29
        self.case_subject = CaseSubjectFactory()
        self.case = PositiveCaseFactory(
            author=self.author, case_subject_id=self.case_subject.subject_id,
        )
        self.other_deleted_case = PositiveCaseFactory(
            author=self.author,
            case_subject_id=self.case_subject.subject_id,
30
            deleted_at=datetime.now(tz=pytz.timezone(TIMEZONE)),
31
32
33
34
35
36
37
        )
        self.other_inactive_case = PositiveCaseFactory(
            author=self.author,
            case_subject_id=self.case_subject.subject_id,
            is_active=False,
        )

38
    def setUp(self):
39
        self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.token_1.key)
40

41
42
43
44
45
46
    def test_list_positive_cases_success(self):
        url = "/cases/positive-cases/"

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

47
48
49
50
51
52
53
54
55
56
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
        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.author.id), response_string)

    def test_list_positive_case_paginate_failed(self):
        url = "/cases/positive-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_positive_case_filter_success(self):
        url = "/cases/positive-cases/?outcome=" + self.case.outcome

        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_positive_case_filter_failed(self):
        url = "/cases/positive-cases/?outcome=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)

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    def test_retrieve_positive_case_success(self):
        url = "/cases/positive-cases/" + str(self.case.revision_id) + "/"

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

    def test_retrieve_positive_case_fails_on_deleted_subject(self):
        url = "/cases/positive-cases/" + str(self.other_deleted_case.revision_id) + "/"
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_create_new_positive_case_success(self):
        url = "/cases/positive-cases/"

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

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

111
        response = self.client.post(path=url, data=data, format="json",)
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        case_current_active_count = PositiveCase.objects.active_revisions().count()
        case_current_all_count = PositiveCase.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_positive_case_fails_with_incomplete_data(self):
        url = "/cases/positive-cases/"

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

127
        response = self.client.post(path=url, data=data, format="json",)
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

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

        data = {
            "case_subject_id": str(self.case.case_subject_id),
            "outcome": "Negative",
            "author": str(self.case.author.id),
        }
        prev_all_positive_case_revision_count = PositiveCase.objects.all().count()
        prev_active_positive_case_revision_count = (
            PositiveCase.objects.active_revisions().count()
        )

143
        response = self.client.put(path=url, data=data, format="json",)
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["outcome"], "Negative")

        current_positive_case_revision_count = PositiveCase.objects.all().count()
        current_active_positive_case_revision_count = (
            PositiveCase.objects.active_revisions().count()
        )

        self.assertFalse(
            PositiveCase.objects.get(revision_id=self.case.revision_id).is_active
        )
        self.assertEqual(
            current_positive_case_revision_count,
            prev_all_positive_case_revision_count + 1,
        )
        self.assertEqual(
            current_active_positive_case_revision_count,
            prev_active_positive_case_revision_count,
        )

    def test_edit_inactive_positive_case_fails(self):
        url = "/cases/positive-cases/" + str(self.other_inactive_case.revision_id) + "/"

        data = {
            "case_subject_id": str(self.case.case_subject_id),
            "outcome": "Negative",
            "author": str(self.case.author.id),
        }
172
        response = self.client.put(path=url, data=data, format="json",)
173
174
175
176
177
178
179
180
181
182
183
184
185
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

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

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

        prev_active_positive_case_revision_count = (
            PositiveCase.objects.active_revisions().count()
        )
186
        response = self.client.put(path=url, data=data, format="json",)
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        current_active_positive_case_revision_count = (
            PositiveCase.objects.active_revisions().count()
        )
        self.assertEqual(
            current_active_positive_case_revision_count,
            prev_active_positive_case_revision_count,
        )
        self.assertTrue(
            PositiveCase.objects.get(revision_id=self.case.revision_id).is_active
        )

    def test_soft_delete_positive_case_success(self):
        url = "/cases/positive-cases/" + str(self.case.revision_id) + "/"
201
        response = self.client.delete(path=url, format="json",)
202
203
204
205
206
207
208
209
210
211
212
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIsNotNone(
            PositiveCase.objects.all(with_deleted=True)
            .filter(revision_id=self.case.revision_id)
            .first()
        )

    def test_delete_inactive_positive_case_fails(self):
        url = "/cases/positive-cases/" + str(self.other_inactive_case.revision_id) + "/"
        response = self.client.delete(url)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)