diff --git a/graphy/conftest.py b/graphy/conftest.py index f460fae..6c0fd92 100644 --- a/graphy/conftest.py +++ b/graphy/conftest.py @@ -1,8 +1,26 @@ +from graphene.test import Client as GraphQLClient + import pytest from rest_framework import test +from graphy.schema import schema + @pytest.fixture def drf_client(db): return test.APIClient() + + +@pytest.fixture +def gql_client(): + class Request: + def __init__(self): + self.META = {} + + class Context: + def __init__(self): + self._request = Request() + self.META = {} + + return GraphQLClient(schema, context=Context()) diff --git a/graphy/location/gql_types.py b/graphy/location/gql_types.py index cb62c7d..a71bad9 100644 --- a/graphy/location/gql_types.py +++ b/graphy/location/gql_types.py @@ -1,3 +1,4 @@ +import graphene from graphene_django import DjangoObjectType from graphy.location.models import Address, County, Municipality, ZipCode @@ -22,6 +23,8 @@ class Meta: class AddressType(DjangoObjectType): + country = graphene.String() + class Meta: model = Address only_fields = ( diff --git a/graphy/location/tests/test_graphql_actions.py b/graphy/location/tests/test_graphql_actions.py new file mode 100644 index 0000000..301b3c8 --- /dev/null +++ b/graphy/location/tests/test_graphql_actions.py @@ -0,0 +1,56 @@ +def test_address(gql_client, address): + query = """ + query($id: String!) { + address(id: $id) { + id + } + } + """ + params = {'id': str(address.id)} + + result = gql_client.execute(query, variables=params) + + assert result['data']['address'] == {'id': str(address.id)} + + +def test_addresses(gql_client, address): + query = """ + query { + addresses { + id + } + } + """ + params = {'id': str(address.id)} + + result = gql_client.execute(query, variables=params) + + assert result['data']['addresses'] == [{'id': str(address.id)}] + + +def test_county(gql_client, county): + query = """ + query($id: String!) { + county(id: $id) { + id + } + } + """ + + params = {'id': str(county.id)} + result = gql_client.execute(query, variables=params) + assert result['data']['county'] == {'id': str(county.id)} + + +def test_counties(gql_client, county): + query = """ + query { + counties { + id + } + } + """ + + params = {'id': str(county.id)} + result = gql_client.execute(query, variables=params) + assert result['data']['counties'] == [{'id': str(county.id)}] diff --git a/graphy/location/tests/test_graphql_types.py b/graphy/location/tests/test_graphql_types.py new file mode 100644 index 0000000..ca08da3 --- /dev/null +++ b/graphy/location/tests/test_graphql_types.py @@ -0,0 +1,112 @@ +def test_address_type(gql_client, address): + query = """ + query($id: String!) { + address(id: $id) { + id + country + streetAddress + latitude + longitude + zipCode { + id + } + } + } + """ + params = {'id': str(address.id)} + + result = gql_client.execute(query, variables=params) + + address_dict = result['data']['address'] + assert address_dict == { + 'id': str(address.id), + 'streetAddress': address.street_address, + 'latitude': address.latitude, + 'longitude': address.longitude, + 'country': str(address.country), + 'zipCode': {'id': str(address.zip_code.id)}, + } + + +def test_county_type(gql_client, county): + query = """ + query($id: String!) { + county(id: $id) { + id + name + code + country + } + } + """ + + params = {'id': str(county.id)} + result = gql_client.execute(query, variables=params) + county_dict = result['data']['county'] + assert county_dict == { + 'id': str(county.id), + 'name': county.name, + 'code': county.code, + 'country': 'NO', + } + + +def test_municipality_type(gql_client, address): + query = """ + query($id: String!) { + address(id: $id) { + zipCode { + municipality { + id + name + county { + id + } + } + } + } + } + """ + params = {'id': str(address.id)} + + result = gql_client.execute(query, variables=params) + + municipality = address.zip_code.municipality + municipality_dict = result['data']['address']['zipCode']['municipality'] + assert municipality_dict == { + 'id': str(municipality.id), + 'name': municipality.name, + 'county': {'id': str(municipality.county.id)}, + } + + +def test_zipcode_type(gql_client, address): + query = """ + query($id: String!) { + address(id: $id) { + zipCode { + id + code + name + municipality { + id + } + county { + id + } + } + } + } + """ + params = {'id': str(address.id)} + + result = gql_client.execute(query, variables=params) + + zip_code_dict = result['data']['address']['zipCode'] + assert zip_code_dict == { + 'id': str(address.zip_code.id), + 'name': address.zip_code.name, + 'code': address.zip_code.code, + 'municipality': {'id': str(address.zip_code.municipality.id)}, + 'county': {'id': str(address.zip_code.county.id)}, + }