Fakultas Ilmu Komputer UI

test_exportables.py 7.37 KB
Newer Older
1
import json
2
3
import csv
import io
4
5
6
7
8
9
10
from rest_framework import status
from rest_framework.test import APITestCase

from apps.cases.models import CaseSubject
from apps.cases.tests.factories.case_subjects import CaseSubjectFactory
from apps.cases.tests.factories.cases import InvestigationCaseFactory

11
from apps.exportables.renderers import INVESTIGATION_CASE_RENDERER_FIELDS
12

13
14
def init_data():
    InvestigationCaseFactory(
15
16
17
18
19
20
21
            case_subject=CaseSubjectFactory(
                district="Beji",
                age=19,
                is_male=True,
            ),
            is_positive=True,
        )
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    InvestigationCaseFactory(
        case_subject=CaseSubjectFactory(
            district="Cilodong",
            age=20,
            is_male=False,
        ),
        is_positive=True,
    )
    InvestigationCaseFactory(
        case_subject=CaseSubjectFactory(
            district="Tapos",
            age=21,
            is_male=True,
        ),
        is_positive=False,
    )
    InvestigationCaseFactory(
        case_subject=CaseSubjectFactory(
            district="Unknown",
            age=21,
            is_male=False,
        ),
        is_positive=False,
    )
    InvestigationCaseFactory(
        case_subject=CaseSubjectFactory(
            district="Sawangan",
            age=22,
            is_male=True,
        ),
        is_positive=None,
    )


class ExportableViewTest(APITestCase):
    @classmethod
    def setUpTestData(cls):
        init_data()
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185

    def test_exportable_data_return_values(self):
        url = "/exportables/"
        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = {
            "age": {
                "19": {
                    "positive": 1,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 1
                },
                "20": {
                    "positive": 1,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 1
                },
                "21": {
                    "positive": 0,
                    "negative": 2,
                    "undetermined": 0,
                    "total_count": 2
                },
                "22": {
                    "positive": 0,
                    "negative": 0,
                    "undetermined": 1,
                    "total_count": 1
                }
            },
            "district": {
                "Beji": {
                    "positive": 1,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 1
                },
                "Bojongsari": {
                    "positive": 0,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 0
                },
                "Cilodong": {
                    "positive": 1,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 1
                },
                "Cimanggis": {
                    "positive": 0,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 0
                },
                "Cinere": {
                    "positive": 0,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 0
                },
                "Cipayung": {
                    "positive": 0,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 0
                },
                "Limo": {
                    "positive": 0,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 0
                },
                "Pancoran Mas": {
                    "positive": 0,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 0
                },
                "Sawangan": {
                    "positive": 0,
                    "negative": 0,
                    "undetermined": 1,
                    "total_count": 1
                },
                "Sukmajaya": {
                    "positive": 0,
                    "negative": 0,
                    "undetermined": 0,
                    "total_count": 0
                },
                "Tapos": {
                    "positive": 0,
                    "negative": 1,
                    "undetermined": 0,
                    "total_count": 1
                },
                "Unknown": {
                    "positive": 0,
                    "negative": 1,
                    "undetermined": 0,
                    "total_count": 1
                }
            },
            "sex": {
                "male": {
                    "positive": 1,
                    "negative": 1,
                    "undetermined": 1,
                    "total_count": 3
                },
                "female": {
                    "positive": 1,
                    "negative": 1,
                    "undetermined": 0,
                    "total_count": 2
                }
            },
            "total_count": 5
        }

        self.assertJSONEqual(response.content, data)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241


class ExportInvestigationCaseViewTest(APITestCase):
    @classmethod
    def setUpTestData(cls):
        cls.BASE_URL = "/exportables/investigation-cases-csv/"
        init_data()

    def export_csv_test_util(self, filter):
        url = self.BASE_URL + filter
        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        content_disposition_header = response.get('Content-Disposition')
        self.assertIn('attachment;', content_disposition_header)
        self.assertIn('filename="cases-', content_disposition_header)
        self.assertIn('.csv', content_disposition_header)

        response_content = response.content.decode('utf-8')
        reader = csv.reader(io.StringIO(response_content))
        body = list(reader)
        headers = body.pop(0)

        self.assertEqual(headers, INVESTIGATION_CASE_RENDERER_FIELDS)
        return body

    def wrong_query_param_test_util(self, filter):
        url = self.BASE_URL + filter
        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_export_investigation_data_to_csv_all(self):
        body = self.export_csv_test_util("")
        self.assertEqual(len(body), 5)

    def test_export_investigation_data_to_csv_filter_gender(self):
        body = self.export_csv_test_util("?is_male=false")
        self.assertEqual(len(body), 2)

    def test_export_investigation_data_to_csv_filter_age(self):
        body = self.export_csv_test_util("?min_age=21&max_age=21")
        self.assertEqual(len(body), 2)

    def test_export_investigation_data_to_csv_filter_district(self):
        body = self.export_csv_test_util("?district=Beji")
        self.assertEqual(len(body), 1)

    def test_export_investigation_data_to_csv_wrong_gender_format(self):
        self.wrong_query_param_test_util("?is_male=asdf")

    def test_export_investigation_data_to_csv_wrong_min_age_format(self):
        self.wrong_query_param_test_util("?min_age=abc&max_age=12")

    def test_export_investigation_data_to_csv_wrong_max_age_format(self):
        self.wrong_query_param_test_util("?min_age=10&max_age=asd")