-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(geo): (WIP!) add e2e integration tests for geo features
- Loading branch information
1 parent
983fbd1
commit 5121575
Showing
10 changed files
with
189 additions
and
3 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
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
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,7 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ODataQueryConfig(AppConfig): | ||
name = "tests.integration.django_geo" | ||
verbose_name = "OData Query GeoDjango test app" | ||
default = True |
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,37 @@ | ||
import urllib.request as req | ||
from pathlib import Path | ||
from zipfile import ZipFile | ||
|
||
import pytest | ||
from django.core import management | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def django_db(): | ||
management.call_command("migrate", "--run-syncdb") | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def world_borders_dataset(data_dir: Path): | ||
target_dir = data_dir / "world_borders" | ||
|
||
if target_dir.exists(): | ||
return target_dir | ||
|
||
filename_zip = target_dir.with_suffix(".zip") | ||
|
||
if not filename_zip.exists(): | ||
breakpoint() | ||
opener = req.build_opener() | ||
opener.addheaders = [("Accept", "application/zip")] | ||
req.install_opener(opener) | ||
req.urlretrieve( | ||
"https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip", | ||
filename_zip, | ||
) | ||
assert filename_zip.exists() | ||
|
||
with ZipFile(filename_zip, "r") as z: | ||
z.extractall(target_dir) | ||
|
||
return target_dir |
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,25 @@ | ||
# https://docs.djangoproject.com/en/4.2/ref/contrib/gis/tutorial/#geographic-models | ||
|
||
from django.contrib.gis.db import models | ||
|
||
|
||
class WorldBorder(models.Model): | ||
# Regular Django fields corresponding to the attributes in the | ||
# world borders shapefile. | ||
name = models.CharField(max_length=50) | ||
area = models.IntegerField() | ||
pop2005 = models.IntegerField("Population 2005") | ||
fips = models.CharField("FIPS Code", max_length=2, null=True) | ||
iso2 = models.CharField("2 Digit ISO", max_length=2) | ||
iso3 = models.CharField("3 Digit ISO", max_length=3) | ||
un = models.IntegerField("United Nations Code") | ||
region = models.IntegerField("Region Code") | ||
subregion = models.IntegerField("Sub-Region Code") | ||
lon = models.FloatField() | ||
lat = models.FloatField() | ||
|
||
# GeoDjango-specific: a geometry field (MultiPolygonField) | ||
mpoly = models.MultiPolygonField() | ||
|
||
def __str__(self): | ||
return self.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from pathlib import Path | ||
from typing import Type | ||
|
||
import pytest | ||
from django.contrib.gis.db import models | ||
from django.contrib.gis.utils import LayerMapping | ||
|
||
from odata_query.django import apply_odata_query | ||
|
||
from .models import WorldBorder | ||
|
||
# The default spatial reference system for geometry fields is WGS84 | ||
# (meaning the SRID is 4326) | ||
SRID = "SRID=4326" | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def sample_data_sess(django_db, world_borders_dataset: Path): | ||
world_mapping = { | ||
"fips": "FIPS", | ||
"iso2": "ISO2", | ||
"iso3": "ISO3", | ||
"un": "UN", | ||
"name": "NAME", | ||
"area": "AREA", | ||
"pop2005": "POP2005", | ||
"region": "REGION", | ||
"subregion": "SUBREGION", | ||
"lon": "LON", | ||
"lat": "LAT", | ||
"mpoly": "MULTIPOLYGON", | ||
} | ||
|
||
world_shp = world_borders_dataset / "TM_WORLD_BORDERS-0.3.shp" | ||
lm = LayerMapping(WorldBorder, world_shp, world_mapping, transform=False) | ||
lm.save(strict=True, verbose=True) | ||
yield | ||
WorldBorder.objects.all().delete() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"model, query, exp_results", | ||
[ | ||
( | ||
WorldBorder, | ||
"geo.length(mpoly) gt 1000000", | ||
154, | ||
), | ||
( | ||
WorldBorder, | ||
f"geo.intersects(mpoly, geography'{SRID};Point(-95.3385 29.7245)')", | ||
1, | ||
), | ||
], | ||
) | ||
def test_query_with_odata( | ||
model: Type[models.Model], | ||
query: str, | ||
exp_results: int, | ||
sample_data_sess, | ||
): | ||
q = apply_odata_query(model.objects, query) | ||
results = q.all() | ||
assert len(results) == exp_results |
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