diff --git a/sizakat/mustahik/query.py b/sizakat/mustahik/query.py index e3e0d43df2fc7845a834688e8304dd579fd4b356..72667bc32d51d4d67a1eed925cd956094872978c 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,15 +24,22 @@ 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() if statuses and len(statuses) > 0: - filter_query |= reduce( + filter_query &= reduce( lambda a, b: a | b, [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) diff --git a/sizakat/mustahik/tests.py b/sizakat/mustahik/tests.py index 4aa61b96554fa57d8f9182ebb793f6a785074110..f82c5f69f757964a893bf44a7019836191091ffd 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( '''