From 053da79e55874adde61888ea46b21d126e0ab410 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 17 May 2024 14:33:02 +0200 Subject: [PATCH] add test with null geom (#120) --- .pre-commit-config.yaml | 6 ++++++ django_oapif/crs_utils.py | 2 +- django_oapif/decorators.py | 6 +++--- tests/conformance/parse_report.py | 10 +++++----- ...ter_point_2056_10fields_local_geom_geom.py | 19 +++++++++++++++++++ tests/django_oapif_tests/tests/models.py | 2 +- tests/django_oapif_tests/tests/tests.py | 16 ++++++++++++++-- 7 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 tests/django_oapif_tests/tests/migrations/0002_alter_point_2056_10fields_local_geom_geom.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 77dd15ac..67f2137b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,3 +33,9 @@ repos: hooks: - id: black exclude: migrations/ + + - repo: https://github.com/asottile/pyupgrade + rev: v3.15.2 + hooks: + - id: pyupgrade + args: [--py39-plus] diff --git a/django_oapif/crs_utils.py b/django_oapif/crs_utils.py index cb632e5a..cdc189e6 100644 --- a/django_oapif/crs_utils.py +++ b/django_oapif/crs_utils.py @@ -11,7 +11,7 @@ "OGC", ] CRS_URI_PATTERN = re.compile( - (rf"^http://www.opengis\.net/def/crs/" rf"(?P{'|'.join(CRS_AUTHORITY)})/" rf"[\d|\.]+?/(?P\w+?)$") + rf"^http://www.opengis\.net/def/crs/" rf"(?P{'|'.join(CRS_AUTHORITY)})/" rf"[\d|\.]+?/(?P\w+?)$" ) diff --git a/django_oapif/decorators.py b/django_oapif/decorators.py index 428a9aca..1f119af0 100644 --- a/django_oapif/decorators.py +++ b/django_oapif/decorators.py @@ -1,5 +1,5 @@ import json -from typing import Any, Callable, Dict, Optional +from typing import Any, Callable, Optional from django.contrib.gis.geos import GEOSGeometry from django.db import models @@ -23,8 +23,8 @@ def register_oapif_viewset( serialize_geom_in_db: Optional[bool] = True, geom_field: [str] = "geom", crs: Optional[int] = None, - custom_serializer_attrs: Dict[str, Any] = None, - custom_viewset_attrs: Dict[str, Any] = None, + custom_serializer_attrs: dict[str, Any] = None, + custom_viewset_attrs: dict[str, Any] = None, ) -> Callable[[Any], models.Model]: """ This decorator takes care of all boilerplate code (creating a serializer, a viewset and registering it) to register diff --git a/tests/conformance/parse_report.py b/tests/conformance/parse_report.py index 8e47fd65..748580db 100755 --- a/tests/conformance/parse_report.py +++ b/tests/conformance/parse_report.py @@ -15,7 +15,7 @@ from itertools import islice from os import path from sys import argv, exit -from typing import List, NamedTuple +from typing import NamedTuple from lxml import etree @@ -36,13 +36,13 @@ def emoji(cls, cmp) -> str: class Result(NamedTuple): - passed: List[str] - skipped: List[str] - failed: List[str] + passed: list[str] + skipped: list[str] + failed: list[str] @classmethod def load(cls, _path) -> "Result": - with open(_path, "r") as fh: + with open(_path) as fh: results = json.load(fh) return cls(**results) diff --git a/tests/django_oapif_tests/tests/migrations/0002_alter_point_2056_10fields_local_geom_geom.py b/tests/django_oapif_tests/tests/migrations/0002_alter_point_2056_10fields_local_geom_geom.py new file mode 100644 index 00000000..043f8785 --- /dev/null +++ b/tests/django_oapif_tests/tests/migrations/0002_alter_point_2056_10fields_local_geom_geom.py @@ -0,0 +1,19 @@ +# Generated by Django 5.0.6 on 2024-05-17 12:06 + +import django.contrib.gis.db.models.fields +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('tests', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='point_2056_10fields_local_geom', + name='geom', + field=django.contrib.gis.db.models.fields.PointField(null=True, srid=2056, verbose_name='Geometry'), + ), + ] diff --git a/tests/django_oapif_tests/tests/models.py b/tests/django_oapif_tests/tests/models.py index 4491cb22..7d42f125 100644 --- a/tests/django_oapif_tests/tests/models.py +++ b/tests/django_oapif_tests/tests/models.py @@ -36,7 +36,7 @@ class Point_2056_10fields(BaseModelWithTenFields): @register_oapif_viewset(crs=2056, serialize_geom_in_db=False) class Point_2056_10fields_local_geom(BaseModelWithTenFields): - geom = models.PointField(srid=2056, verbose_name=_("Geometry")) + geom = models.PointField(srid=2056, verbose_name=_("Geometry"), null=True) @register_oapif_viewset(geom_field=None) diff --git a/tests/django_oapif_tests/tests/tests.py b/tests/django_oapif_tests/tests/tests.py index d5fe7107..6dbec71e 100644 --- a/tests/django_oapif_tests/tests/tests.py +++ b/tests/django_oapif_tests/tests/tests.py @@ -54,7 +54,7 @@ def test_anonymous_items_options(self): url = f"{collections_url}/{layer}/items" response = self.client.options(url) - allowed_headers = set(s.strip() for s in response.headers["Allow"].split(",")) + allowed_headers = {s.strip() for s in response.headers["Allow"].split(",")} allowed_body = set(response.json()["actions"].keys()) self.assertEqual(allowed_body, expected) @@ -68,8 +68,20 @@ def test_editor_items_options(self): url = f"{collections_url}/{layer}/items" response = self.client.options(url) - allowed_headers = set(s.strip() for s in response.headers["Allow"].split(",")) + allowed_headers = {s.strip() for s in response.headers["Allow"].split(",")} allowed_body = set(response.json()["actions"].keys()) self.assertEqual(allowed_body, expected) self.assertEqual(allowed_headers, allowed_body) + + def test_post_without_geometry(self): + self.client.force_authenticate(user=self.demo_editor) + data = { + "geometry": None, + "properties": {"field_str_0": "test123456"}, + } + + for layer in ("tests.point_2056_10fields_local_geom",): + url = f"{collections_url}/{layer}/items" + post_to_items = self.client.post(url, data, format="json") + self.assertIn(post_to_items.status_code, (200, 201), (url, data, post_to_items.data))