def test_mapping_mutation(self): file_id = self.dummy_file.id input_json = { 'metadata_id': file_id, 'mapping_dict': { "indicator": ["Indicator"], "value_format": ["Unit"], "geolocation": ["Area"], "value": ["Data Value"], "date": ["Time Period"], "comment": ["Source"], "filters": ["Subgroup"] }, 'filter_headings': { "Subgroup": "Subgroup" }, } input_json_str = json.dumps(input_json) query_input = {"input": {"data": input_json_str}} query = """ mutation mapping($input: MappingMutationInput!) { mapping(input: $input) { id data } }""" schema.execute(query, variable_values=query_input) self.assertTrue(Mapping.objects.filter(data=input_json).exists())
def test_mutation_file_source(self): name = 'something' query = """ mutation fileSource { fileSource(input:{name:"%s"}) { name } } """ % name schema.execute(query) self.assertTrue(FileSource.objects.filter(name=name).exists())
def test_filter_entryId_In_countries(self): country = Country.objects.all()[:2] country_id_one = str(country[0].id) country_id_two = str(country[1].id) query = """ { allCountries(entryId_In:"%s,%s") { edges { cursor node { id name } } } } """ % (country_id_one, country_id_two) result = schema.execute(query) self.assertEqual( result.data['allCountries']['edges'][0]['node']['name'], country[0].name) self.assertEqual( result.data['allCountries']['edges'][1]['node']['name'], country[1].name)
def test_filter_iso2_In_countries(self): country = Country.objects.filter(iso2__in=['al', 'bs']) query = """ { allCountries(iso2_In:"al,bs") { edges { cursor node { id iso2 } } } } """ result = schema.execute(query) country_list = [country[0].iso2, country[1].iso2].sort() result_list = [ result.data['allCountries']['edges'][0]['node']['iso2'], result.data['allCountries']['edges'][1]['node']['iso2'] ].sort() self.assertTrue(country_list == result_list)
def test_filter_name_In_countries(self): country = Country.objects.filter(name__in=["Albania", "Andora"]) query = """ { allCountries(name_In:"Albania,Andora") { edges { cursor node { id name } } } } """ result = schema.execute(query) self.assertEqual( result.data['allCountries']['edges'][0]['node']['name'], country[0].name) self.assertEqual( result.data['allCountries']['edges'][1]['node']['name'], country[1].name)
def test_countryCenterLongLat(self): # TODO: please make the test for the field polygons # The test code is the same logic with the bellow code. # ---------------------- # Get country record country = Country.objects.first() coordinates = [coord for coord in country.center_longlat] # GraphQL Query query = """ {allCountries {edges {node { id entryId name centerLonglat } } } } """ result = schema.execute(query) # Check if center_longlat at the current record has # the same coordinates with the GraphQL query center_longlat = json.loads( result.data['allCountries']['edges'][0]['node']['centerLonglat']) # The centerLonglat is JSONString, so it is needed to convert again # to the Python dictionary center_longlat = json.loads(center_longlat) self.assertEqual(coordinates, center_longlat['coordinates'])
def test_geolocation_pointbase(self): query = """{ allGeolocations(type_In:"pointbased") { edges { node { pointBased { id name centerLonglat } } } } }""" result = schema.execute(query) self.assertIsNone(result.errors) geolocations = Geolocation.objects.filter(type__in=["pointbased"]) i = 0 for a in geolocations: point_coords = PointBased.objects.get( id=a.object_id).center_longlat.json result_coords = json.loads(result.data["allGeolocations"]["edges"][i][ "node"]["pointBased"]["centerLonglat"]) self.assertEqual(point_coords, result_coords) i += 1
def test_countryPolygons(self): # Get country record country = Country.objects.first() polygons = country.polygons polygons_in_json = polygons.geojson # GraphQL Query query = """ {allCountries {edges {node { id entryId name polygons } } } } """ result = schema.execute(query) # Check if center_longlat at the current record has # the same coordinates with the GraphQL query result_polygons = json.loads( result.data['allCountries']['edges'][0]['node']['polygons']) result_polygons_in_dict = json.loads(result_polygons) # The centerLonglat is JSONString, so it is needed to convert again # to the Python dictionary polygons_in_dict = json.loads(polygons_in_json) self.assertEqual(polygons_in_dict['coordinates'], result_polygons_in_dict['coordinates'])
def test_filter_type_In_geolocations(self): geolocation = Geolocation.objects.filter(type__in=['country', 'city']) query = """ { allGeolocations(type_In:"city,country") { edges { cursor node { id tag objectId } } } } """ result = schema.execute(query) self.assertEqual( result.data['allGeolocations']['edges'][0]['node']['tag'], geolocation[0].tag) self.assertEqual( result.data['allGeolocations']['edges'][1]['node']['tag'], geolocation[1].tag) self.assertEqual( result.data['allGeolocations']['edges'][2]['node']['tag'], geolocation[2].tag) self.assertEqual( result.data['allGeolocations']['edges'][3]['node']['tag'], geolocation[3].tag)
def test_datapoints_aggregation_geolocationObjectId_In(self): query = """{ datapointsAggregation(groupBy:["indicatorName", "geolocationObjectId"],orderBy:["indicatorName"],aggregation:[ "Sum(value)"],geolocationObjectId_In: ["126","149"] ) { indicatorName value geolocationObjectId } }""" result = schema.execute(query) self.assertIsNone(result.errors) countries = Datapoints.objects.values('indicator__name', 'geolocation__object_id').annotate( Sum('value')).filter( geolocation__object_id__in=['126', '149']) self.assertEqual(len(countries), len(result.data['datapointsAggregation'])) i = 0 while i < len(countries): self.assertEqual(result.data['datapointsAggregation'][i][ 'geolocationObjectId'], countries[i]['geolocation__object_id']) i += 1
def test_datapoints_aggregation_geolocationIso3_In(self): query = """{ datapointsAggregation(groupBy:["indicatorName", "geolocationIso3"],orderBy:["indicatorName"],aggregation:[ "Sum(value)"], geolocationIso3_In: ["mng","lso"] ) { indicatorName value geolocationIso3 } }""" result = schema.execute(query) self.assertIsNone(result.errors) countries = Datapoints.objects.values( 'indicator__name', 'geolocation__iso3').annotate(Sum('value')).filter( geolocation__iso3__in=['mng', 'lso']).order_by( 'indicator__name') self.assertEqual(len(countries), len(result.data['datapointsAggregation'])) i = 0 while i < len(countries): self.assertEqual( result.data['datapointsAggregation'][i]['geolocationIso3'], countries[i]['geolocation__iso3']) i += 1
def test_datapoints_aggregation_geolocationType_In(self): query = """{ datapointsAggregation(groupBy:["indicatorName", "geolocationType"],orderBy:["indicatorName"],aggregation:[ "Sum(value)"],geolocationType_In: ["subnational", "country"] ) { indicatorName value geolocationType } }""" result = schema.execute(query) self.assertIsNone(result.errors) countries = Datapoints.objects.values('indicator__name', 'geolocation__type').annotate( Sum('value')).filter( geolocation__type__in=['subnational', 'country']) self.assertEqual(len(countries), len(result.data['datapointsAggregation'])) i = 0 while i < len(countries): self.assertEqual(result.data['datapointsAggregation'][i][ 'geolocationType'], countries[i]['geolocation__type']) i += 1
def test_filter_entryId_In_geolocations(self): geolocation = Geolocation.objects.all()[:2] id_one = str(geolocation[0].id) id_two = str(geolocation[1].id) query = """ { allGeolocations(entryId_In:"%s,%s") { edges { cursor node { id tag objectId } } } } """ % (id_one, id_two) result = schema.execute(query) self.assertEqual( result.data['allGeolocations']['edges'][0]['node']['tag'], geolocation[0].tag) self.assertEqual( result.data['allGeolocations']['edges'][1]['node']['tag'], geolocation[1].tag)
def test_region_centerlonglat(self): # Get country record region = Region.objects.first() center_longlat = region.center_longlat center_longlat_in_json = center_longlat.geojson # GraphQL Query query = """ {allGeolocations(tag:"asia") {edges {node { entryId tag region{ centerLonglat} } } } } """ result = schema.execute(query) # Check if center_longlat at the current record has # the same coordinates with the GraphQL query result_center_longlat = json.loads( result.data['allGeolocations']['edges'][0]['node']['region'][ 'centerLonglat'] ) # The centerLonglat is JSONString, so it is needed to convert again # to the Python dictionary result_center_longlat_in_dict = json.loads(result_center_longlat) center_longlat_in_dict = json.loads(center_longlat_in_json) self.assertEqual(center_longlat_in_dict['coordinates'], result_center_longlat_in_dict['coordinates'])
def test_filter_objectId_In_geolocations(self): geolocation = Geolocation.objects.filter( object_id__in=[self.obj_id_1, self.obj_id_2]) query = """ query geolocations($object_id_str: String!){ allGeolocations(objectId_In: $object_id_str) { edges { cursor node { id tag objectId } } } } """ object_id_str = { "object_id_str": str(self.obj_id_1) + ',' + str(self.obj_id_2) } result = schema.execute(query, variable_values=object_id_str) self.assertEqual( result.data['allGeolocations']['edges'][0]['node']['objectId'], geolocation[0].object_id) self.assertEqual( result.data['allGeolocations']['edges'][1]['node']['objectId'], geolocation[1].object_id)
def test_mapping_mutation(self): file_id = self.dummy_file.id EXTRA_INFORMATION = { 'empty_entries': { 'empty_indicator': 'Test Region', 'empty_geolocation': { 'value': '', 'type': '' }, 'empty_filter': 'Default', 'empty_value_format': { 'value format': 'Numeric' }, 'empty_date': '2016' }, 'multi_mapped': { 'column_heading': {}, 'column_values': {}, }, 'point_based_info': { 'coord': { 'lat': '', 'lon': '' }, 'subnational': '', 'country': '', 'type': '', } } input_json = { 'metadata_id': file_id, 'mapping_dict': { 'indicator': [], 'geolocation': ["Subnational"], 'date': [], 'value_format': [], 'value': ["new infections"], 'comment': [], }, "filter_headings": { "filters": "filters" }, 'extra_information': EXTRA_INFORMATION } input_json_str = json.dumps(input_json) query_input = {"input": {"data": input_json_str}} query = """ mutation mapping($input: MappingMutationInput!) { mapping(input: $input) { id data } }""" result = schema.execute(query, variable_values=query_input) self.assertEqual(result.errors, None)
def test_datapoints_aggregation_parm_orderBy_missing(self): query = """{ datapointsAggregation(groupBy: ["indicatorName"],aggregation: ["Sum(value)"]) { indicatorName value } } """ result = schema.execute(query) self.assertEqual(result.errors[0].message, "'orderBy'")
def test_datapoints_aggregation_parm_pass(self): query = """{ datapointsAggregation(groupBy: ["indicatorName"],orderBy:[ "indicatorName"], aggregation:["Sum(value)"]) { indicatorName value } } """ result = schema.execute(query) self.assertIsNone(result.errors)
def test_mutation_file(self): location = self.dummy_geolocation.id source = self.dummy_file_source.id query_input = """ input: { title: "test", description: "test", containsSubnationalData: true, organisation: "test", maintainer: "test", dateOfDataset: "2009-01-01", methodology: "test", defineMethodology: "test", updateFrequency: "test", comments: "test", accessibility: "p", dataQuality: "test", numberOfRows: 1, fileTypes: "csv", location: "%s", source: "%s", file: "AIDSinfotest.csv" }""" % (location, source) query = """ mutation file { file(%s) { id description containsSubnationalData organisation maintainer dateOfDataset methodology defineMethodology updateFrequency comments accessibility dataQuality numberOfRows fileTypes location source file } }""" % query_input result = schema.execute(query) self.assertEqual(result.errors, None)
async def parse(self, request: Answering, settings: dict) -> Dict[str, Any]: query = request.question.querystring self.logger.info("Executing Schema") await self.progress("Querying Schema") result = schema.execute(query) resultdict = result.to_dict() datapackages = [] data = resultdict["data"] await self.progress("Creating Answers") for key in data.keys(): dataframe = json_normalize(data[key]) datapackages.append((key, dataframe)) return {"datapackage": datapackages}
def test_allCountries(self): country = Country.objects.first() query = """ {allCountries {edges {node { id entryId name } } } } """ result = schema.execute(query) self.assertEqual(result.data['allCountries']['edges'][0]['node'] ['name'], country.name)
def test_mapping_query(self): self.test_mapping_mutation() # we need to have a data in the database query = """{ allMappings { edges { node { id entryId data } } } }""" result = schema.execute(query) result_mapping_object = Mapping.objects.get( id=result.data['allMappings']['edges'][0]['node']['entryId']) self.assertTrue(result_mapping_object)
def test_filter_tag_geolocations(self): geolocation = Geolocation.objects.filter(tag='Albania') query = """ { allGeolocations(tag:"Albania") { edges { cursor node { id tag } } } } """ result = schema.execute(query) self.assertEqual( result.data['allGeolocations']['edges'][0]['node']['tag'], geolocation[0].tag)
def test_filter_last_geolocations(self): geolocation = Geolocation.objects.last() query = """ { allGeolocations(last:1) { edges { cursor node { id tag } } } } """ result = schema.execute(query) self.assertEqual( result.data['allGeolocations']['edges'][0]['node']['tag'], geolocation.tag)
def test_filter_tag_icontains_geolocations(self): geolocation = Geolocation.objects.filter(tag__contains="lb") query = """ { allGeolocations(tag_Icontains:"lb") { edges { cursor node { id tag } } } } """ result = schema.execute(query) self.assertEqual( result.data['allGeolocations']['edges'][0]['node']['tag'], geolocation[0].tag)
def test_filter_tag_istartswith_countries(self): country = Country.objects.filter(name__startswith="Al") query = """ { allCountries(name_Istartswith:"Al") { edges { cursor node { id name } } } } """ result = schema.execute(query) self.assertEqual( result.data['allCountries']['edges'][0]['node']['name'], country[0].name)
def test_filter_iso3_istartswith_countries(self): country = Country.objects.filter(iso2__istartswith='al') query = """ { allCountries(iso3_Istartswith:"al") { edges { cursor node { id iso3 } } } } """ result = schema.execute(query) self.assertEqual( result.data['allCountries']['edges'][0]['node']['iso3'], country[0].iso3)
def test_filter_last_countries(self): country = Country.objects.last() query = """ { allCountries(last:1) { edges { cursor node { id name } } } } """ result = schema.execute(query) self.assertEqual( result.data['allCountries']['edges'][0]['node']['name'], country.name)
def test_filter_tag_istartswith_geolocations(self): geolocation = Geolocation.objects.filter(tag__startswith="al") query = """ { allGeolocations(tag_Istartswith:"al") { edges { cursor node { id tag } } } } """ result = schema.execute(query) self.assertEqual( result.data['allGeolocations']['edges'][0]['node']['tag'], geolocation[0].tag)
def test_filter_name_icontains_countries(self): country = Country.objects.filter(name__contains="lb") query = """ { allCountries(name_Icontains:"lb") { edges { cursor node { id name } } } } """ result = schema.execute(query) self.assertEqual( result.data['allCountries']['edges'][0]['node']['name'], country[0].name)