Fakultas Ilmu Komputer UI

Commit 9d82d845 authored by Naufal Alauddin Hilmi's avatar Naufal Alauddin Hilmi 🍦
Browse files

Merge branch 'add-update-mustahik' into 'PBI-15-pencatatan_mustahik'

Add and update mutation for mustahik

See merge request !1
parents bcfc1ad8 493026c6
......@@ -12,7 +12,7 @@ class Mustahik(models.Model):
name = models.CharField(max_length=32)
no_ktp = models.CharField(max_length=32, unique=True)
phone = models.CharField(max_length=32, blank=True)
phone = models.CharField(max_length=32, blank=True, null=True)
address = models.TextField()
province = models.CharField(max_length=32)
regency = models.CharField(max_length=32)
......@@ -25,4 +25,4 @@ class Mustahik(models.Model):
default=Status.MISKIN,
)
family_size = models.PositiveSmallIntegerField()
description = models.TextField()
description = models.TextField(blank=True, null=True)
import graphene
from django import forms
from graphene_django.forms.mutation import DjangoModelFormMutation
from .models import Mustahik
from .types import MustahikType
class MustahikForm(forms.ModelForm):
class Meta:
model = Mustahik
fields = [
'name',
'no_ktp',
'phone',
'address',
'province',
'regency',
'rt',
'rw',
'birthdate',
'status',
'family_size',
'description',
]
class MustahikMutation(DjangoModelFormMutation):
mustahik = graphene.Field(MustahikType)
class Meta:
form_class = MustahikForm
import json
from datetime import date
from django.db.utils import IntegrityError
from django.test import TestCase
from graphene_django.utils.testing import GraphQLTestCase
from sizakat.schema import schema
from .models import Mustahik
......@@ -42,3 +46,116 @@ class MustahikModelTestCase(TestCase):
family_size=1,
description='no_ktp is unique'
)
class MustahikGraphQLTestCase(GraphQLTestCase):
GRAPHQL_SCHEMA = schema
def setUp(self):
Mustahik.objects.create(
name='mustahik',
no_ktp='31751234567890',
phone='081234567890',
address='Jalan raya depok',
province='Jawa Barat',
regency='Depok',
rt='003',
rw='002',
birthdate=date(1987, 6, 5),
status=Mustahik.Status.MISKIN,
family_size=4,
description='desc'
)
def test_mustahik_mutation_can_add_new_mustahik(self):
no_ktp = '123891210121'
response = self.query(
'''
mutation mustahikMutation($input: MustahikMutationInput!) {
mustahikMutation(input: $input) {
mustahik {
id
name
noKtp
status
description
}
errors {
field
messages
}
}
}
''',
op_name='mustahikMutation',
input_data={
"name": "jumat",
"noKtp": no_ktp,
"phone": "02132132180",
"address": "jalan swadaya",
"province": "jakarta",
"regency": "manggarai",
"rt": "001",
"rw": "001",
"birthdate": "1998-03-12",
"status": "YATIM",
"familySize": 3,
"description": "anak yatim"
}
)
# 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']
['mustahik']['status'], 'YATIM')
# Validate success save to db
self.assertNotEqual(Mustahik.objects.count(), 0)
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')
mustahik_id = mustahik.pk
old_desc = mustahik.description
new_desc = 'keluarga tidak mampu'
response = self.query(
'''
mutation mustahikMutation($input: MustahikMutationInput!) {
mustahikMutation(input: $input) {
mustahik {
id
description
}
errors {
field
messages
}
}
}
''',
op_name='mustahikMutation',
input_data={
"name": "mustahik",
"noKtp": "31751234567890",
"phone": "081234567890",
"address": "Jalan raya depok",
"province": "Jawa Barat",
"regency": "Depok",
"rt": "003",
"rw": "002",
"birthdate": "1987-06-05",
"status": "MISKIN",
"familySize": 4,
"description": new_desc,
"id": mustahik.pk
}
)
# Validate success update desc mustahik
mustahik = Mustahik.objects.get(no_ktp='31751234567890')
self.assertNotEqual(mustahik.description, old_desc)
self.assertEqual(mustahik.description, new_desc)
import graphene
from graphene_django.types import DjangoObjectType
from .models import Mustahik
class MustahikType(DjangoObjectType):
class Meta:
model = Mustahik
from graphene_django import DjangoObjectType
import graphene
from graphene_django import DjangoObjectType
from .apps.mustahik.mutations import MustahikMutation
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):
about = graphene.String()
def resolve_about(self, info):
return ABOUT
schema = graphene.Schema(query=Query)
class Mutation(graphene.ObjectType):
mustahik_mutation = MustahikMutation.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
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