Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for not null with default #122

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
},
),
]
5 changes: 5 additions & 0 deletions tests/django_oapif_tests/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
10 changes: 10 additions & 0 deletions tests/django_oapif_tests/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
}
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))
24 changes: 24 additions & 0 deletions tests/integration/test_integration_qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading