forked from coderholic/django-cities
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* graphql support types * Update Readme * Support for 3.12 on tests and update some libraries * python 3.12 on job * making the test pass * Linting problems --------- Co-authored-by: marianoeramirez <Sosinformatico1990>
- Loading branch information
1 parent
bd0743e
commit 3eaaa87
Showing
11 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[pytest] | ||
FAIL_INVALID_TEMPLATE_VARS = True | ||
django_debug_mode = true | ||
DJANGO_SETTINGS_MODULE = test_project.settings | ||
addopts = --cov cities_light --create-db --strict -v --no-migrations | ||
python_files = tests.py test_*.py *_tests.py |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from graphene import ObjectType, String, Int, Field, Float | ||
|
||
|
||
class BaseType(ObjectType): | ||
name = String(description="Name.") | ||
name_ascii = String(description="Name ascii.") | ||
slug = String(description="Slug.") | ||
geoname_id = Int(description="Geoname id.") | ||
alternate_names = String(description="Alternate names.") | ||
|
||
|
||
class Country(BaseType): | ||
code2 = String(description="Country code 2 letters.") | ||
code3 = String(description="Country code 3 letters.") | ||
continent = String(description="Country continent.") | ||
tld = String(description="Country top level domain.") | ||
phone = String(description="Country phone code.") | ||
|
||
|
||
class Region(BaseType): | ||
display_name = String(description="display name") | ||
geoname_code = String(description="Geoname code") | ||
country = Field(Country, description="Country.") | ||
|
||
|
||
class SubRegion(BaseType): | ||
display_name = String(description="display name.") | ||
geoname_code = String(description="Geoname code") | ||
country = Field(Country, description="Country") | ||
region = Field(Region, description="Region") | ||
|
||
|
||
class City(BaseType): | ||
display_name = String(description="display name") | ||
search_names = String() | ||
latitude = Float() | ||
longitude = Float() | ||
population = Int() | ||
feature_code = String() | ||
timezone = String() | ||
country = Field(Country, description="Country") | ||
region = Field(Region, description="Region") | ||
subregion = Field(SubRegion, description="Region") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import graphene # type: ignore | ||
import graphene_django # type: ignore | ||
|
||
from cities_light.graphql.types import Country as CountryType | ||
from cities_light.graphql.types import Region as RegionType | ||
from cities_light.graphql.types import City as CityType | ||
from cities_light.graphql.types import SubRegion as SubRegionType | ||
|
||
from ..models import Person as PersonModel | ||
|
||
class Person(graphene_django.DjangoObjectType): | ||
country = graphene.Field(CountryType) | ||
region = graphene.Field(RegionType) | ||
subregion = graphene.Field(SubRegionType) | ||
city = graphene.Field(CityType) | ||
|
||
class Meta: | ||
model = PersonModel | ||
fields = ["name", "country", "region", "subregion", "city"] | ||
|
||
|
||
class Query(graphene.ObjectType): | ||
people = graphene.List(Person) | ||
|
||
@staticmethod | ||
def resolve_people(parent, info): | ||
return PersonModel.objects.all() | ||
|
||
|
||
schema = graphene.Schema(query=Query) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from graphene.test import Client # type: ignore | ||
import pytest | ||
|
||
from cities_light.models import Country, Region, SubRegion, City | ||
from cities_light.tests.graphql.schema import schema | ||
from cities_light.tests.models import Person | ||
|
||
@pytest.fixture | ||
def country_fixture(): | ||
return Country.objects.create(name='France') | ||
@pytest.fixture | ||
def region_fixture(country_fixture): | ||
return Region.objects.create(name='Normandy', country=country_fixture) | ||
@pytest.fixture | ||
def subregion_fixture(country_fixture, region_fixture): | ||
return SubRegion.objects.create(name='Upper Normandy', country=country_fixture, region=region_fixture) | ||
@pytest.fixture | ||
def city_fixture(country_fixture, region_fixture, subregion_fixture): | ||
return City.objects.create(name='Caen', country=country_fixture, region=region_fixture, subregion=subregion_fixture) | ||
def test_country_type(db, country_fixture): | ||
Person.objects.create(name="Skippy", country=country_fixture) | ||
client = Client(schema) | ||
executed = client.execute("""{ people { name, country {name} } }""") | ||
returned_person = executed["data"]["people"][0] | ||
assert returned_person == {"name": "Skippy", "country": {"name": "France"}} | ||
|
||
def test_region_type(db, country_fixture, region_fixture): | ||
Person.objects.create(name="Skippy", country=country_fixture, region=region_fixture) | ||
client = Client(schema) | ||
executed = client.execute("""{ people { name, region {name, country{ name}} } }""") | ||
returned_person = executed["data"]["people"][0] | ||
assert returned_person == {"name": "Skippy", "region": {"name": "Normandy", 'country': {'name': 'France'},}} | ||
|
||
def test_subregion_type(db, country_fixture, subregion_fixture): | ||
Person.objects.create(name="Skippy", country=country_fixture, subregion=subregion_fixture) | ||
client = Client(schema) | ||
executed = client.execute("""{ people { name, subregion {name, region{name}, country{ name}} } }""") | ||
returned_person = executed["data"]["people"][0] | ||
assert returned_person == {"name": "Skippy", "subregion": {"name": "Upper Normandy", 'region': {'name': 'Normandy'}, 'country': {'name': 'France'},}} | ||
|
||
def test_city_type(db, country_fixture, city_fixture): | ||
Person.objects.create(name="Skippy", country=country_fixture, city=city_fixture) | ||
client = Client(schema) | ||
executed = client.execute("""{ people { name, city{name, subregion {name, region{name}, country{ name}} } }}""") | ||
returned_person = executed["data"]["people"][0] | ||
assert returned_person == {"name": "Skippy", "city": {"name": "Caen", 'subregion': {'name': 'Upper Normandy', | ||
'region': {'name': 'Normandy'}, | ||
'country': {'name': 'France'},}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.db import models | ||
|
||
from cities_light.models import Country, Region, SubRegion, City | ||
|
||
|
||
class Person(models.Model): | ||
name = models.CharField(max_length=50) | ||
country = models.ForeignKey(Country, models.CASCADE) | ||
region = models.ForeignKey(Region, models.CASCADE, blank=True, null=True) | ||
subregion = models.ForeignKey(SubRegion, models.CASCADE, blank=True, null=True) | ||
city = models.ForeignKey(City, models.CASCADE, blank=True, null=True) | ||
|
||
class Meta: | ||
ordering = ("name",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters