From 9a10b5a34f84be8600abf0c1ce8e8002a5ff2771 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Thu, 23 May 2024 11:49:51 +0200 Subject: [PATCH 1/6] add test for not null with default --- tests/django_oapif_tests/tests/models.py | 5 +++++ tests/django_oapif_tests/tests/tests.py | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/tests/django_oapif_tests/tests/models.py b/tests/django_oapif_tests/tests/models.py index 7d42f12..c68c8a7 100644 --- a/tests/django_oapif_tests/tests/models.py +++ b/tests/django_oapif_tests/tests/models.py @@ -29,6 +29,11 @@ class Meta: field_str_9 = models.CharField(max_length=255, verbose_name=_("Field 9"), null=True, blank=True) +@register_oapif_viewset(geom_field=None) +class Non_Null_Field_With_Default(BaseModelWithTenFields): + field_non_null_with_default = models.IntegerField(null=False, blank=False, default=8) + + @register_oapif_viewset(crs=2056) class Point_2056_10fields(BaseModelWithTenFields): geom = models.PointField(srid=2056, verbose_name=_("Geometry")) diff --git a/tests/django_oapif_tests/tests/tests.py b/tests/django_oapif_tests/tests/tests.py index 4d63552..325fac3 100644 --- a/tests/django_oapif_tests/tests/tests.py +++ b/tests/django_oapif_tests/tests/tests.py @@ -136,3 +136,13 @@ def test_delete(self): fid = re.match(r"^.*([0-9a-f\-]{36})$", location).group(1) delete_from_items = self.client.delete(f"{url}/{fid}") self.assertIn(delete_from_items.status_code, (200, 204), f"{url}/{fid}") + + def test_non_null_with_default(self): + self.client.force_authenticate(user=self.demo_editor) + data = { + "geometry": None, + "properties": None, + } + url = f"{collections_url}/tests.non_null_field_with_default/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)) From c50b6c336eac40df8180c898591496870974ea8c Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 24 May 2024 11:19:42 +0200 Subject: [PATCH 2/6] migrate --- .../0003_non_null_field_with_default.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/django_oapif_tests/tests/migrations/0003_non_null_field_with_default.py diff --git a/tests/django_oapif_tests/tests/migrations/0003_non_null_field_with_default.py b/tests/django_oapif_tests/tests/migrations/0003_non_null_field_with_default.py new file mode 100644 index 0000000..9856bbc --- /dev/null +++ b/tests/django_oapif_tests/tests/migrations/0003_non_null_field_with_default.py @@ -0,0 +1,37 @@ +# Generated by Django 5.0.6 on 2024-05-24 09:19 + +import uuid + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('tests', '0002_alter_point_2056_10fields_local_geom_geom'), + ] + + operations = [ + migrations.CreateModel( + name='Non_Null_Field_With_Default', + fields=[ + ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('field_bool', models.BooleanField(default=True)), + ('field_int', models.IntegerField(blank=True, null=True)), + ('field_str_0', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 0')), + ('field_str_1', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 1')), + ('field_str_2', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 2')), + ('field_str_3', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 3')), + ('field_str_4', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 4')), + ('field_str_5', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 5')), + ('field_str_6', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 6')), + ('field_str_7', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 7')), + ('field_str_8', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 8')), + ('field_str_9', models.CharField(blank=True, max_length=255, null=True, verbose_name='Field 9')), + ('field_non_null_with_default', models.IntegerField(default=8)), + ], + options={ + 'abstract': False, + }, + ), + ] From 74052b58ccc44d3b8dbc8b22076ad2254784c236 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 24 May 2024 11:25:22 +0200 Subject: [PATCH 3/6] add permission2 --- .../tests/management/commands/populate_users.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/django_oapif_tests/tests/management/commands/populate_users.py b/tests/django_oapif_tests/tests/management/commands/populate_users.py index f3cf7b7..cf0a964 100644 --- a/tests/django_oapif_tests/tests/management/commands/populate_users.py +++ b/tests/django_oapif_tests/tests/management/commands/populate_users.py @@ -15,6 +15,7 @@ def handle(self, *args, **options): deleting = [] for model in ( + "non_null_field_with_default", "point_2056_10fields", "point_2056_10fields_local_geom", "nogeom_10fields", From 3f4ec9c5bf38e97985fb14fb43d80ffdbaaffcfc Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 24 May 2024 11:28:34 +0200 Subject: [PATCH 4/6] fix dict --- tests/django_oapif_tests/tests/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/django_oapif_tests/tests/tests.py b/tests/django_oapif_tests/tests/tests.py index 325fac3..a70c825 100644 --- a/tests/django_oapif_tests/tests/tests.py +++ b/tests/django_oapif_tests/tests/tests.py @@ -141,7 +141,7 @@ def test_non_null_with_default(self): self.client.force_authenticate(user=self.demo_editor) data = { "geometry": None, - "properties": None, + "properties": {}, } url = f"{collections_url}/tests.non_null_field_with_default/items" post_to_items = self.client.post(url, data, format="json") From fd64c6531ddc62604f0dc6371e2df8885a3a37dd Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 24 May 2024 11:38:38 +0200 Subject: [PATCH 5/6] add integration test --- tests/integration/test_integration_qgis.py | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/integration/test_integration_qgis.py b/tests/integration/test_integration_qgis.py index 52741d4..b339a6a 100644 --- a/tests/integration/test_integration_qgis.py +++ b/tests/integration/test_integration_qgis.py @@ -99,3 +99,27 @@ def test_load_and_edit_with_basic_auth(self): f = next(layer.getFeatures("field_str_0='Super Green'")) self.assertIsInstance(f, QgsFeature) self.assertEqual(geom.asWkt(), f.geometry().asWkt()) + + def test_non_null_default(self): + layer = "tests.non_null_field_with_default" + uri = QgsDataSourceUri() + uri.setParam("service", "wfs") + uri.setParam("typename", layer) + uri.setParam("url", ROOT_URL) + uri.setPassword(self.password) + uri.setUsername(self.user) + + layer = QgsVectorLayer(uri.uri(), layer, "OAPIF") + self.assertTrue(layer.isValid()) + layer = self.project.addMapLayer(layer) + self.assertIsNotNone(layer) + + self.assertTrue(bool(layer.dataProvider().capabilities() & QgsVectorDataProvider.Capability.AddFeatures)) + + f = QgsFeature(layer.fields()) + self.assertIsNone(f["field_non_null_with_default"]) + with edit(layer): + layer.addFeature(f) + f = next(layer.getFeatures()) + self.assertIsInstance(f, QgsFeature) + self.assertEqual(f["field_non_null_with_default"], 8) From 369c5539c7fbd6f2e338b72f96249eaf5c00346d Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 24 May 2024 12:09:38 +0200 Subject: [PATCH 6/6] populate --- .../tests/management/commands/populate_data.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/django_oapif_tests/tests/management/commands/populate_data.py b/tests/django_oapif_tests/tests/management/commands/populate_data.py index 56d0359..a62a13f 100644 --- a/tests/django_oapif_tests/tests/management/commands/populate_data.py +++ b/tests/django_oapif_tests/tests/management/commands/populate_data.py @@ -11,6 +11,7 @@ Line_2056_10fields_local_geom, NoGeom_10fields, NoGeom_100fields, + Non_Null_Field_With_Default, Point_2056_10fields, Point_2056_10fields_local_geom, SecretLayer, @@ -26,6 +27,8 @@ def add_arguments(self, parser): @transaction.atomic def handle(self, *args, **options): """Populate db with testdata""" + Non_Null_Field_With_Default.objects.create() + size = options["size"] x_start = 2508500 y_start = 1152000