Fakultas Ilmu Komputer UI

Commit dc7526a8 authored by Mohammad Adli Daffa's avatar Mohammad Adli Daffa
Browse files

Merge branch 'PBI-15-pencatatan_mustahik-get_and_filter' into 'PBI-15-pencatatan_mustahik'

Filter Query for Mustahik

See merge request !2
parents 9d82d845 a3a0f3f9
import graphene
from django.db.models import Q
from .models import Mustahik
from .types import MustahikType
class MustahikQuery(graphene.ObjectType):
mustahiks = graphene.List(MustahikType, statuses=graphene.List(graphene.String))
def resolve_mustahiks(self, info, statuses=[], **kwargs):
if statuses and len(statuses) > 0:
filter = Q(status=statuses[0])
for status in statuses:
filter = filter | Q(status=status)
return Mustahik.objects.filter(filter)
return Mustahik.objects.all()
......@@ -159,3 +159,77 @@ class MustahikGraphQLTestCase(GraphQLTestCase):
mustahik = Mustahik.objects.get(no_ktp='31751234567890')
self.assertNotEqual(mustahik.description, old_desc)
self.assertEqual(mustahik.description, new_desc)
def test_query_mustahik_should_return_list_of_mustahiks(self):
response = self.query(
'''
query mustahiksQuery{
mustahiks {
id,
name
}
}
''',
op_name='mustahiksQuery'
)
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):
Mustahik.objects.create(
name='test',
no_ktp='11751234567890',
phone='081234567890',
address='Jalan raya depok',
province='Jawa Barat',
regency='Depok',
rt='003',
rw='002',
birthdate=date(1987, 6, 5),
status=Mustahik.Status.YATIM,
family_size=4,
description='desc'
)
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',
variables={'statuses': [Mustahik.Status.JANDA]}
)
content = json.loads(response.content)
self.assertEqual(len(content['data']['mustahiks']), 0)
......@@ -2,13 +2,14 @@ import graphene
from graphene_django import DjangoObjectType
from .apps.mustahik.mutations import MustahikMutation
from .apps.mustahik.query import MustahikQuery
ABOUT = 'Si Zakat merupakan sistem informasi untuk membantu masjid dalam \
mengelola transaksi zakat. Sistem ini dibuat oleh tim lab 1231, \
yang dipimpin oleh Prof. Dr. Wisnu Jatmiko.'
class Query(graphene.ObjectType):
class Query(MustahikQuery, graphene.ObjectType):
about = graphene.String()
def resolve_about(self, info):
......
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