Fakultas Ilmu Komputer UI

Commit dc10a586 authored by addffa's avatar addffa
Browse files

[REFACTOR] refactor fungsi-fungsi sesuai dengan panduan dan kesepakatan

parent 80eb096c
from django import forms
from .models import Mustahik
class MustahikForm(forms.ModelForm):
class Meta:
model = Mustahik
fields = [
'name',
'no_ktp',
'phone',
'address',
'province',
'regency',
'rt',
'rw',
'birthdate',
'status',
'family_size',
'description',
]
from django.db import models
# Create your models here.
from django.core.validators import RegexValidator
numeric_only = RegexValidator(r'^[0-9]*$', 'Numeric only.')
class Mustahik(models.Model):
class Mustahik(models.Model):
class Status(models.TextChoices):
JANDA = ('JANDA', 'Janda')
MISKIN = ('MISKIN', 'Miskin')
YATIM = ('YATIM', 'Yatim')
name = models.CharField(max_length=32)
no_ktp = models.CharField(max_length=32, unique=True)
phone = models.CharField(max_length=32, blank=True, null=True)
name = models.CharField(max_length=150)
no_ktp = models.CharField(
max_length=32, unique=True,
validators=[numeric_only]
)
phone = models.CharField(
max_length=32, blank=True, null=True,
validators=[numeric_only]
)
address = models.TextField()
province = models.CharField(max_length=32)
regency = models.CharField(max_length=32)
rt = models.CharField(max_length=4)
rw = models.CharField(max_length=4)
regency = models.CharField(max_length=50)
rt = models.CharField(max_length=4, validators=[numeric_only])
rw = models.CharField(max_length=4, validators=[numeric_only])
birthdate = models.DateField()
status = models.CharField(
max_length=32,
......
import graphene
from django import forms
from graphene_django.forms.mutation import DjangoModelFormMutation
from .forms import MustahikForm
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
class DeleteMustahik(graphene.Mutation):
class DeleteMustahik(graphene.Mutation):
class Arguments:
id = graphene.ID()
message = graphene.String()
idMustahik = graphene.ID()
nama = graphene.String()
noKtp = graphene.String()
mustahik = graphene.Field(MustahikType)
deleted = graphene.Boolean()
id_mustahik = graphene.ID()
name = graphene.String()
no_ktp = graphene.String()
def mutate(self, info, id):
mustahik = Mustahik.objects.get(pk=id)
_nama = mustahik.name
_no_ktp = mustahik.no_ktp
name = mustahik.name
no_ktp = mustahik.no_ktp
mustahik.delete()
return DeleteMustahik(message = "Success", idMustahik=id, nama=_nama, noKtp=_no_ktp)
deleted = True
return DeleteMustahik(deleted=deleted, id_mustahik=id, name=name, no_ktp=no_ktp)
import graphene
from django.db.models import Q
from functools import reduce
from .models import Mustahik
from .types import MustahikType
class MustahikQuery(graphene.ObjectType):
mustahiks = graphene.List(MustahikType, statuses=graphene.List(graphene.String))
mustahik = graphene.Field(MustahikType, id=graphene.ID())
mustahikWithName = graphene.Field(lambda: graphene.List(MustahikType), name=graphene.String())
mustahiks = graphene.List(
MustahikType,
statuses=graphene.List(graphene.String),
name_contains=graphene.String()
)
mustahik = graphene.Field(MustahikType, id=graphene.ID(required=True))
def resolve_mustahiks(self, info, **kwargs):
statuses = kwargs.get('statuses', None)
name_contains = kwargs.get('name_contains', None)
mustahiks = Mustahik.objects.all()
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)
mustahiks = mustahiks.filter(reduce(
lambda a, b: a | b,
list(map(lambda s: Q(status=s), statuses))
))
if name_contains:
mustahiks = mustahiks.filter(name__icontains=name_contains)
return Mustahik.objects.all()
return mustahiks
def resolve_mustahik(self, info, id):
mustahik = Mustahik.objects.get(pk=id)
if mustahik is not None:
return mustahik
def resolve_mustahikWithName(self, info, name=""):
if name != "":
return Mustahik.objects.filter(name__icontains=name)
return Mustahik.objects.all()
return Mustahik.objects.get(pk=id)
......@@ -162,15 +162,15 @@ class MustahikGraphQLTestCase(GraphQLTestCase):
def test_query_mustahik_should_return_list_of_mustahiks(self):
response = self.query(
'''
'''
query mustahiksQuery{
mustahiks {
id,
name
}
}
''',
op_name='mustahiksQuery'
''',
op_name='mustahiksQuery'
)
self.assertResponseNoErrors(response)
......@@ -241,35 +241,29 @@ class MustahikGraphQLTestCase(GraphQLTestCase):
'''
mutation deleteMustahik($id: ID) {
deleteMustahik(id: $id) {
mustahik {
id
name
}
message
deleted
idMustahik
nama
name
noKtp
}
}
''',
op_name='deleteMustahik',
variables={'id':mustahik_id}
variables={'id': mustahik_id}
)
self.assertResponseNoErrors(response)
content = json.loads(response.content)
self.assertIsNone(content['data']['deleteMustahik']['mustahik'])
self.assertEquals(content['data']['deleteMustahik']['message'], "Success")
self.assertTrue(content['data']['deleteMustahik']['deleted'])
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(
'''
query detailMustahikQuery($id:ID){
query detailMustahikQuery($id:ID!){
mustahik(id:$id){
id
name
......@@ -288,35 +282,19 @@ class MustahikGraphQLTestCase(GraphQLTestCase):
}
''',
op_name='detailMustahikQuery',
variables={'id':mustahik_id}
variables={'id': mustahik_id}
)
self.assertResponseNoErrors(response)
content = json.loads(response.content)
self.assertEqual(len(content['data']), 1)
self.assertEqual(content['data']['mustahik']['name'], 'mustahik')
self.assertEqual(content['data']['mustahik']['noKtp'], '31751234567890')
self.assertEqual(content['data']['mustahik']['address'], 'Jalan raya depok')
self.assertEqual(content['data']['mustahik']
['noKtp'], '31751234567890')
self.assertEqual(content['data']['mustahik']
['address'], 'Jalan raya depok')
def test_mustahikWithName_should_return_list_of_mustahik(self):
response = self.query(
'''
query mustahikWithName($name:String){
mustahikWithName(name: $name){
id,
name
}
}
''',
op_name='mustahikWithName',
)
content = json.loads(response.content)
self.assertEqual(len(content['data']['mustahikWithName']),1)
self.assertEqual(content['data']['mustahikWithName'][0]['name'], 'mustahik')
def test_mustahikWithName_if_name_is_set_should_return_list_of_mustahiks_that_contain_the_name(self):
def test_mustahiks_if_name_is_set_should_return_list_of_mustahiks_that_contain_the_name(self):
Mustahik.objects.create(
name='test',
no_ktp='11751234567890',
......@@ -349,42 +327,39 @@ class MustahikGraphQLTestCase(GraphQLTestCase):
response = self.query(
'''
query mustahikWithName($name:String){
mustahikWithName(name: $name){
query mustahiks($nameContains:String){
mustahiks(nameContains: $nameContains){
id,
name
}
}
''',
op_name='mustahikWithName',
variables={'name':'es'}
op_name='mustahiks',
variables={'nameContains': 'es'}
)
content = json.loads(response.content)
self.assertEqual(len(content['data']['mustahikWithName']), 2)
self.assertEqual(content['data']['mustahikWithName'][0]['name'], 'test')
self.assertEqual(content['data']['mustahikWithName'][1]['name'], 'eslu')
self.assertEqual(len(content['data']['mustahiks']), 2)
self.assertEqual(
content['data']['mustahiks'][0]['name'], 'test')
self.assertEqual(
content['data']['mustahiks'][1]['name'], 'eslu')
def test_mustahikWithName_if_name_is_not_available_should_return_empty_list(self):
def test_mustahiks_if_name_is_not_available_should_return_empty_list(self):
response = self.query(
'''
query mustahikWithName($name:String){
mustahikWithName(name: $name){
query mustahiks($nameContains:String){
mustahiks(nameContains: $nameContains){
id,
name
}
}
''',
op_name='mustahikWithName',
variables={'name':'#'}
op_name='mustahiks',
variables={'nameContains': '#'}
)
content = json.loads(response.content)
self.assertEqual(len(content['data']['mustahikWithName']), 0)
self.assertEqual(len(content['data']['mustahiks']), 0)
......@@ -5,6 +5,5 @@ from .models import Mustahik
class MustahikType(DjangoObjectType):
class Meta:
model = Mustahik
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