Fakultas Ilmu Komputer UI

tests.py 15 KB
Newer Older
1
2
import json

addffa's avatar
addffa committed
3
from datetime import date
4
from django.db.utils import IntegrityError
addffa's avatar
addffa committed
5
from django.test import TestCase
6
from django.utils import timezone
7
8
from graphene_django.utils.testing import GraphQLTestCase
from sizakat.schema import schema
addffa's avatar
addffa committed
9

10
from .models import DataSource, DataSourceInstitusi, DataSourcePekerja, DataSourceWarga, Mustahik
addffa's avatar
addffa committed
11
12
13
14


class MustahikModelTestCase(TestCase):
    def setUp(self):
15
16
17
18
19
20
        data_source_detail = {
            'pic_name': 'pic test',
            'pic_ktp': '1234567890',
            'pic_phone': '0812389120',
            'pic_position': 'test',
        }
21
22
23
24
25
26
27
28
        data_source_pekerja = DataSource.objects.create(
            category=DataSource.Category.PEKERJA
        )
        pekerja_detail = DataSourcePekerja.objects.create(
            data_source=data_source_pekerja,
            profession='tester',
            location='jl tester'
        )
29
30
31
32
33
34
35
        mustahik_base = {
            'phone': '081234567890',
            'address': 'Jalan raya depok',
            'birthdate': date(1987, 6, 5),
            'status': Mustahik.Status.MISKIN,
            'gender': Mustahik.Gender.LAKILAKI,
        }
36
        mustahik = Mustahik.objects.create(
37
38
39
40
            name='mustahik1',
            no_ktp='31751234567891',
            data_source=data_source_pekerja,
            **mustahik_base
addffa's avatar
addffa committed
41
42
        )

43
44
    def test_mustahik_creation(self):
        mustahik = Mustahik.objects.get(no_ktp='31751234567891')
addffa's avatar
addffa committed
45
        self.assertTrue(isinstance(mustahik, Mustahik))
46

47
    def test_calculate_mustahik_age(self):
48
        mustahik = Mustahik.objects.get(no_ktp='31751234567891')
49
50
51
52
53
        self.assertEqual(
            mustahik.calculate_age(),
            timezone.now().year - mustahik.birthdate.year
        )

54
55
56
57
58

class MustahikGraphQLTestCase(GraphQLTestCase):
    GRAPHQL_SCHEMA = schema

    def setUp(self):
59
60
61
        data_source_warga = DataSource.objects.create(
            category=DataSource.Category.WARGA
        )
addffa's avatar
addffa committed
62
        data_source_detail = DataSourceWarga.objects.create(
63
            data_source=data_source_warga,
64
65
66
67
            pic_name='pic test',
            pic_ktp='1234567892',
            pic_phone='0812389122',
            pic_position='test',
68
69
70
71
72
73
74
75
            province='jakarta',
            regency='jakarta timur',
            sub_district='makasar',
            village='pinangranti',
            rt='001',
            rw='001'
        )
        mustahik = Mustahik.objects.create(
76
77
78
79
80
81
            name='mustahik',
            no_ktp='31751234567890',
            phone='081234567890',
            address='Jalan raya depok',
            birthdate=date(1987, 6, 5),
            status=Mustahik.Status.MISKIN,
82
83
            gender=Mustahik.Gender.LAKILAKI,
            data_source=data_source_warga
84
85
        )

86
87
88
89
    def test_about_query(self):
        response = self.query('{ about }')
        self.assertResponseNoErrors(response)

90
91
    def test_mustahik_mutation_can_add_new_mustahik(self):
        no_ktp = '123891210121'
92
93
        data_source_warga = DataSourceWarga.objects.get(pic_ktp='1234567892')
        data_source = data_source_warga.data_source
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        response = self.query(
            '''
            mutation mustahikMutation($input: MustahikMutationInput!) {
                mustahikMutation(input: $input) {
                    mustahik {
                        name
                        noKtp
                        status
                    }
                }
            }
            ''',
            op_name='mustahikMutation',
            input_data={
                "name": "jumat",
                "noKtp": no_ktp,
                "phone": "02132132180",
                "address": "jalan swadaya",
                "birthdate": "1998-03-12",
113
114
115
                "status": "MISKIN",
                "gender": "L",
                "dataSource": data_source.pk
116
117
118
119
120
121
122
123
124
            }
        )

        # This validates the status code and if you get errors
        self.assertResponseNoErrors(response)

        # Validate content
        content = json.loads(response.content)
        self.assertEqual(content['data']['mustahikMutation']
125
                         ['mustahik']['status'], 'MISKIN')
126
127

        # Validate success save to db
128
        self.assertEqual(Mustahik.objects.count(), 2)
129
130
131
132
133
        mustahik = Mustahik.objects.get(no_ktp=no_ktp)
        self.assertEqual(mustahik.name, 'jumat')

    def test_mustahik_mutation_can_update_mustahik(self):
        mustahik = Mustahik.objects.get(no_ktp='31751234567890')
134
135
        data_source_warga = DataSourceWarga.objects.get(pic_ktp='1234567892')
        data_source = data_source_warga.data_source
136
        mustahik_id = mustahik.pk
137
138
        old_status = mustahik.status
        new_status = 'MUSAFIR'
139
140
141
142
143
144
        response = self.query(
            '''
            mutation mustahikMutation($input: MustahikMutationInput!) {
                mustahikMutation(input: $input) {
                    mustahik {
                        id
145
                        status
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
                    }
                    errors {
                        field
                        messages
                    }
                }
            }
            ''',
            op_name='mustahikMutation',
            input_data={
                "name": "mustahik",
                "noKtp": "31751234567890",
                "phone": "081234567890",
                "address": "Jalan raya depok",
                "birthdate": "1987-06-05",
161
                "status": "MUSAFIR",
162
                "gender": "L",
163
164
                "id": mustahik.pk,
                "dataSource": data_source.pk
165
166
167
168
169
            }
        )

        # Validate success update desc mustahik
        mustahik = Mustahik.objects.get(no_ktp='31751234567890')
170
        self.assertEqual(mustahik.status, new_status)
171
172
173

    def test_query_mustahik_should_return_list_of_mustahiks(self):
        response = self.query(
174
            '''
175
176
177
178
179
180
            query mustahiksQuery{
                mustahiks {
                    id,
                    name
                }
            }
181
182
            ''',
            op_name='mustahiksQuery'
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
216
217
218
219
220
221
222
223
        )

        self.assertResponseNoErrors(response)

        content = json.loads(response.content)
        self.assertEqual(len(content['data']['mustahiks']), 1)
        self.assertEqual(content['data']['mustahiks'][0]['name'], 'mustahik')

    def test_query_mustahiks_if_statuses_is_set_should_return_list_of_mustahiks_with_coresponding_status(self):
        response = self.query(
            '''
                query mustahiksQuery($statuses: [String]) {
                    mustahiks(statuses: $statuses) {
                        id,
                        name,
                        status
                    }
                }
            ''',
            op_name='mustahiksQuery',
            variables={'statuses': [Mustahik.Status.MISKIN]}
        )

        content = json.loads(response.content)
        self.assertEqual(len(content['data']['mustahiks']), 1)

        for mustahik in content['data']['mustahiks']:
            self.assertEqual(mustahik['status'], Mustahik.Status.MISKIN)

    def test_query_mustahiks_if_statuses_is_provided_and_no_mustahiks_are_qualified_it_should_return_empty_list(self):
        response = self.query(
            '''
                query mustahiksQuery($statuses: [String]) {
                    mustahiks(statuses: $statuses) {
                        id,
                        name,
                        status
                    }
                }
            ''',
            op_name='mustahiksQuery',
224
            variables={'statuses': [Mustahik.Status.GHARIM]}
225
226
227
228
229
        )

        content = json.loads(response.content)
        self.assertEqual(len(content['data']['mustahiks']), 0)

230
231
    def test_mustahik_mutation_can_delete_mustahik(self):
        count = Mustahik.objects.count()
232
233
        mustahik = Mustahik.objects.get(no_ktp='31751234567890')
        mustahik_id = mustahik.pk
234
235
        response = self.query(
            '''
236
237
                mutation deleteMustahik($id: ID) {
                    deleteMustahik(id: $id) {
238
                        deleted
239
                        idMustahik
240
                        name
241
                        noKtp
242
243
244
                    }
                }
            ''',
245
            op_name='deleteMustahik',
246
            variables={'id': mustahik_id}
247
        )
248

249
250
251
        self.assertResponseNoErrors(response)

        content = json.loads(response.content)
252
        self.assertTrue(content['data']['deleteMustahik']['deleted'])
253
254
255
256
257
258
259
        self.assertEquals(Mustahik.objects.count(), count-1)

    def test_mustahik_query_can_read_detail_mustahik(self):
        mustahik = Mustahik.objects.get(no_ktp='31751234567890')
        mustahik_id = mustahik.pk
        response = self.query(
            '''
260
                query detailMustahikQuery($id:ID!){
261
262
263
264
                    mustahik(id:$id){
                        name
                        noKtp
                        address
265
                        age
266
267
268
269
                    }
                }
            ''',
            op_name='detailMustahikQuery',
270
            variables={'id': mustahik_id}
271
        )
272
        self.assertResponseNoErrors(response)
273

274
        content = json.loads(response.content)
275
        data_mustahik = content['data']['mustahik']
276
        self.assertEqual(len(content['data']), 1)
277
278
279
280
        self.assertEqual(data_mustahik['name'], 'mustahik')
        self.assertEqual(data_mustahik['noKtp'], '31751234567890')
        self.assertEqual(data_mustahik['address'], 'Jalan raya depok')
        self.assertEqual(data_mustahik['age'], mustahik.calculate_age())
281

282
    def test_mustahiks_if_name_is_set_should_return_list_of_mustahiks_that_contain_the_name(self):
283
284
        response = self.query(
            '''
285
286
            query mustahiks($nameContains:String){
                mustahiks(nameContains: $nameContains){
287
288
289
                    id,
                    name
                }
290

291
292
            }
            ''',
293
            op_name='mustahiks',
294
            variables={'nameContains': 'hik'}
295
296
297
        )

        content = json.loads(response.content)
298
        self.assertEqual(len(content['data']['mustahiks']), 1)
299
        self.assertEqual(
300
            content['data']['mustahiks'][0]['name'], 'mustahik')
301

302
    def test_mustahiks_if_name_is_not_available_should_return_empty_list(self):
303
304
        response = self.query(
            '''
305
306
            query mustahiks($nameContains:String){
                mustahiks(nameContains: $nameContains){
307
308
309
                    id,
                    name
                }
310

311
312
            }
            ''',
313
314
            op_name='mustahiks',
            variables={'nameContains': '#'}
315
316
317
        )

        content = json.loads(response.content)
318
        self.assertEqual(len(content['data']['mustahiks']), 0)
addffa's avatar
addffa committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

    def test_data_sources_query_should_return_list_data_sources(self):
        response = self.query(
            '''
            query dataSourcesQuery{
                dataSources {
                    id
                    category
                    dataSourceDetail {
                        __typename
                        ... on DataSourceWargaType {
                            rt
                        }
                        ... on DataSourcePekerjaType {
                            profession
                        }
                        ... on DataSourceInstitusiType {
                            name
                        }
                    }
                }
            }
            ''',
            op_name='dataSourcesQuery'
        )

        self.assertResponseNoErrors(response)

        content = json.loads(response.content)
        self.assertEqual(len(content['data']['dataSources']), 1)
        self.assertEqual(content['data']['dataSources'][0]['category'], 'WARGA')

        first_data_source = content['data']['dataSources'][0]['dataSourceDetail']
        self.assertEqual(first_data_source['__typename'], 'DataSourceWargaType')
        self.assertTrue('rt' in first_data_source.keys())

    def test_data_sources_query_with_category_selection(self):
        response = self.query(
            '''
            query dataSourcesQuery($category: String) {
                dataSources(category: $category) {
                    id
                    category
                }
            }
            ''',
            op_name='dataSourcesQuery',
            variables={'category': DataSource.Category.WARGA}
        )

        content = json.loads(response.content)
        categories = [source['category'] for source in content['data']['dataSources']]
        self.assertTrue(all([category == DataSource.Category.WARGA for category in categories]))

    def test_data_source_query_detail_with_given_id(self):
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
        data_source_detail = {
            'pic_name': 'pic test',
            'pic_ktp': '1234567890',
            'pic_phone': '0812389120',
            'pic_position': 'test',
        }
        data_source_pekerja = DataSource.objects.create(
            category=DataSource.Category.PEKERJA
        )
        pekerja_detail = DataSourcePekerja.objects.create(
            data_source=data_source_pekerja,
            profession='tester',
            location='jl tester',
            **data_source_detail
        )
        data_source_institusi = DataSource.objects.create(
            category=DataSource.Category.INSTITUSI
        )
        pekerja_detail = DataSourceInstitusi.objects.create(
            data_source=data_source_institusi,
            name='test',
            province='test',
            regency='test',
            sub_district='test',
            village='test',
            rw='01',
            rt='01',
            address='test',
            **data_source_detail
        )
addffa's avatar
addffa committed
404
405
        response = self.query(
            '''
406
407
408
409
410
411
412
413
414
415
            query dataSourceQuery($id1: ID!, $id2: ID!, $id3: ID!) {
                q1: dataSource(id: $id1) {
                    id
                    detail: dataSourceDetail { __typename }
                }
                q2: dataSource(id: $id2) {
                    id
                    detail: dataSourceDetail { __typename }
                }
                q3: dataSource(id: $id3) {
addffa's avatar
addffa committed
416
                    id
417
                    detail: dataSourceDetail { __typename }
addffa's avatar
addffa committed
418
419
420
421
                }
            }
            ''',
            op_name='dataSourceQuery',
422
            variables={'id1': 2, 'id2': 3, 'id3': 4}
addffa's avatar
addffa committed
423
424
        )
        content = json.loads(response.content)
425
426
427
428
429
430
        self.assertEqual(content['data']['q1']['id'], '2')
        self.assertEqual(content['data']['q1']['detail']['__typename'], 'DataSourceWargaType')
        self.assertEqual(content['data']['q2']['id'], '3')
        self.assertEqual(content['data']['q2']['detail']['__typename'], 'DataSourcePekerjaType')
        self.assertEqual(content['data']['q3']['id'], '4')
        self.assertEqual(content['data']['q3']['detail']['__typename'], 'DataSourceInstitusiType')