Skip to content

Commit

Permalink
location: add tests for graphql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfa committed Nov 20, 2018
1 parent 7caad6b commit c71967a
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 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())
3 changes: 3 additions & 0 deletions 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
Expand All @@ -22,6 +23,8 @@ class Meta:


class AddressType(DjangoObjectType):
country = graphene.String()

class Meta:
model = Address
only_fields = (
Expand Down
56 changes: 56 additions & 0 deletions 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)}]
112 changes: 112 additions & 0 deletions 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)},
}

0 comments on commit c71967a

Please sign in to comment.