From efe9d93e6f26515d5a53c336c39d9c5179a3bab1 Mon Sep 17 00:00:00 2001
From: Naufal Alauddin Hilmi <naufal.alauddin@ui.ac.id>
Date: Wed, 2 Sep 2020 11:19:31 +0700
Subject: [PATCH 1/3] [RED] test for query mustahiks with dataSources

---
 sizakat/mustahik/tests.py | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/sizakat/mustahik/tests.py b/sizakat/mustahik/tests.py
index 4aa61b9..f82c5f6 100644
--- a/sizakat/mustahik/tests.py
+++ b/sizakat/mustahik/tests.py
@@ -361,6 +361,44 @@ class MustahikGraphQLTestCase(GraphQLTestCase):
         content = json.loads(response.content)
         self.assertEqual(len(content['data']['mustahiks']), 0)
 
+    def test_mustahiks_query_if_data_sources_provided_should_return_mustahiks_with_coresponding_data_sources(self):
+        dataSourceId = DataSource.objects.all()[0].id
+        response = self.query(
+            '''
+            query mustahiks($dataSources: [ID]) {
+                mustahiks(dataSources: $dataSources) {
+                    dataSource {
+                        id
+                    }
+                }
+            }
+            ''',
+            op_name='mustahiks',
+            variables={'dataSources': [dataSourceId]}
+        )
+
+        content = json.loads(response.content)
+        self.assertEqual(int(content['data']['mustahiks'][0]['dataSource']['id']), dataSourceId)
+
+    def test_mustahiks_query_if_data_sources_provided_has_no_corresponding_mustahiks_it_should_return_empty_list(self):
+        dataSourceId = 99999
+        response = self.query(
+            '''
+            query mustahiks($dataSources: [ID]) {
+                mustahiks(dataSources: $dataSources) {
+                    dataSource {
+                        id
+                    }
+                }
+            }
+            ''',
+            op_name='mustahiks',
+            variables={'dataSources': [dataSourceId]}
+        )
+
+        content = json.loads(response.content)
+        self.assertEqual(len(content['data']['mustahiks']), 0)
+
     def test_data_sources_query_should_return_list_data_sources(self):
         response = self.query(
             '''
-- 
GitLab


From faa76f2d663ce7d4e8d2999b8472b7473af86893 Mon Sep 17 00:00:00 2001
From: Naufal Alauddin Hilmi <naufal.alauddin@ui.ac.id>
Date: Wed, 2 Sep 2020 11:20:05 +0700
Subject: [PATCH 2/3] [GREEN] create resolve query mustahiks with dataSources
 id

---
 sizakat/mustahik/query.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/sizakat/mustahik/query.py b/sizakat/mustahik/query.py
index e3e0d43..5e786e2 100644
--- a/sizakat/mustahik/query.py
+++ b/sizakat/mustahik/query.py
@@ -11,7 +11,8 @@ class MustahikQuery(graphene.ObjectType):
     mustahiks = graphene.List(
         MustahikType,
         statuses=graphene.List(graphene.String),
-        name_contains=graphene.String()
+        name_contains=graphene.String(),
+        data_sources=graphene.List(graphene.ID)
     )
     mustahik = graphene.Field(MustahikType, id=graphene.ID(required=True))
     data_sources = graphene.List(
@@ -23,6 +24,7 @@ class MustahikQuery(graphene.ObjectType):
 
     def resolve_mustahiks(self, info, **kwargs):
         statuses = kwargs.get('statuses', None)
+        data_sources = kwargs.get('data_sources', None)
         name_contains = kwargs.get('name_contains', None)
         filter_query = Q()
 
@@ -32,6 +34,12 @@ class MustahikQuery(graphene.ObjectType):
                 [Q(status=status) for status in statuses]
             )
 
+        if data_sources and len(data_sources) > 0:
+            filter_query &= reduce(
+                lambda a, b: a | b,
+                [Q(data_source=data_source) for data_source in data_sources]
+            )
+
         if name_contains:
             filter_query &= Q(name__icontains=name_contains)
 
-- 
GitLab


From 900e76024854a7bb8660fdf7e6c77baeda9a552e Mon Sep 17 00:00:00 2001
From: Naufal Alauddin Hilmi <naufal.alauddin@ui.ac.id>
Date: Thu, 3 Sep 2020 11:02:38 +0700
Subject: [PATCH 3/3] [REFACTOR] use `&=` consistently

---
 sizakat/mustahik/query.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sizakat/mustahik/query.py b/sizakat/mustahik/query.py
index 5e786e2..72667bc 100644
--- a/sizakat/mustahik/query.py
+++ b/sizakat/mustahik/query.py
@@ -29,7 +29,7 @@ class MustahikQuery(graphene.ObjectType):
         filter_query = Q()
 
         if statuses and len(statuses) > 0:
-            filter_query |= reduce(
+            filter_query &= reduce(
                 lambda a, b: a | b,
                 [Q(status=status) for status in statuses]
             )
-- 
GitLab