File: test_features.py

package info (click to toggle)
geojson-pydantic 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 216 kB
  • sloc: python: 1,503; makefile: 3
file content (445 lines) | stat: -rw-r--r-- 13,629 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import json
from random import randint
from typing import Any, Dict
from uuid import uuid4

import pytest
from pydantic import BaseModel, ValidationError

from geojson_pydantic.features import Feature, FeatureCollection
from geojson_pydantic.geometries import (
    Geometry,
    GeometryCollection,
    MultiPolygon,
    Polygon,
)


class GenericProperties(BaseModel):
    id: str
    description: str
    size: int


properties: Dict[str, Any] = {
    "id": str(uuid4()),
    "description": str(uuid4()),
    "size": randint(0, 1000),
}

coordinates = [
    [
        [13.38272, 52.46385],
        [13.42786, 52.46385],
        [13.42786, 52.48445],
        [13.38272, 52.48445],
        [13.38272, 52.46385],
    ]
]

polygon: Dict[str, Any] = {
    "type": "Polygon",
    "coordinates": coordinates,
}

multipolygon: Dict[str, Any] = {
    "type": "MultiPolygon",
    "coordinates": [coordinates],
}

geom_collection: Dict[str, Any] = {
    "type": "GeometryCollection",
    "geometries": [polygon, multipolygon],
}

test_feature: Dict[str, Any] = {
    "type": "Feature",
    "geometry": polygon,
    "properties": properties,
    "bbox": [13.38272, 52.46385, 13.42786, 52.48445],
}

test_feature_geom_null: Dict[str, Any] = {
    "type": "Feature",
    "geometry": None,
    "properties": properties,
}

test_feature_geometry_collection: Dict[str, Any] = {
    "type": "Feature",
    "geometry": geom_collection,
    "properties": properties,
}


@pytest.mark.parametrize(
    "obj",
    [
        FeatureCollection,
        Feature,
    ],
)
def test_pydantic_schema(obj):
    """Test schema for Pydantic Object."""
    assert obj.model_json_schema()


def test_feature_collection_iteration():
    """test if feature collection is iterable"""
    gc = FeatureCollection(
        type="FeatureCollection", features=[test_feature, test_feature]
    )
    assert hasattr(gc, "__geo_interface__")
    assert list(iter(gc))
    assert len(list(gc.iter())) == 2
    assert dict(gc)


def test_geometry_collection_iteration():
    """test if feature collection is iterable"""
    gc = FeatureCollection(
        type="FeatureCollection", features=[test_feature_geometry_collection]
    )
    assert hasattr(gc, "__geo_interface__")
    assert list(iter(gc))
    assert len(list(gc.iter())) == 1
    assert dict(gc)


def test_generic_properties_is_dict():
    feature = Feature(**test_feature)
    assert hasattr(feature, "__geo_interface__")
    assert feature.properties["id"] == test_feature["properties"]["id"]
    assert isinstance(feature.properties, dict)
    assert not hasattr(feature.properties, "id")


def test_generic_properties_is_dict_collection():
    feature = Feature(**test_feature_geometry_collection)
    assert hasattr(feature, "__geo_interface__")
    assert (
        feature.properties["id"] == test_feature_geometry_collection["properties"]["id"]
    )
    assert isinstance(feature.properties, dict)
    assert not hasattr(feature.properties, "id")


def test_generic_properties_is_object():
    feature = Feature[Geometry, GenericProperties](**test_feature)
    assert feature.properties.id == test_feature["properties"]["id"]
    assert type(feature.properties) == GenericProperties
    assert hasattr(feature.properties, "id")


def test_generic_geometry():
    feature = Feature[Polygon, GenericProperties](**test_feature)
    assert feature.properties.id == test_feature_geometry_collection["properties"]["id"]
    assert type(feature.geometry) == Polygon
    assert type(feature.properties) == GenericProperties
    assert hasattr(feature.properties, "id")

    feature = Feature[Polygon, Dict](**test_feature)
    assert type(feature.geometry) == Polygon
    assert feature.properties["id"] == test_feature["properties"]["id"]
    assert isinstance(feature.properties, dict)
    assert not hasattr(feature.properties, "id")

    with pytest.raises(ValidationError):
        Feature[MultiPolygon, Dict](**({"type": "Feature", "geometry": polygon}))


def test_generic_geometry_collection():
    feature = Feature[GeometryCollection, GenericProperties](
        **test_feature_geometry_collection
    )
    assert feature.properties.id == test_feature_geometry_collection["properties"]["id"]
    assert type(feature.geometry) == GeometryCollection
    assert feature.geometry.wkt.startswith("GEOMETRYCOLLECTION (POLYGON ")
    assert type(feature.properties) == GenericProperties
    assert hasattr(feature.properties, "id")

    feature = Feature[GeometryCollection, Dict](**test_feature_geometry_collection)
    assert type(feature.geometry) == GeometryCollection
    assert (
        feature.properties["id"] == test_feature_geometry_collection["properties"]["id"]
    )
    assert isinstance(feature.properties, dict)
    assert not hasattr(feature.properties, "id")

    with pytest.raises(ValidationError):
        Feature[MultiPolygon, Dict](**({"type": "Feature", "geometry": polygon}))


def test_generic_properties_should_raise_for_string():
    with pytest.raises(ValidationError):
        Feature(
            **({"type": "Feature", "geometry": polygon, "properties": "should raise"})
        )


def test_feature_collection_generic():
    fc = FeatureCollection[Feature[Polygon, GenericProperties]](
        type="FeatureCollection", features=[test_feature, test_feature]
    )
    assert fc.length == 2
    assert len(list(fc.iter())) == 2
    assert type(fc.features[0].properties) == GenericProperties
    assert type(fc.features[0].geometry) == Polygon
    assert dict(fc)


def test_geo_interface_protocol():
    class Pointy:
        __geo_interface__ = {"type": "Point", "coordinates": (0.0, 0.0)}

    feat = Feature(type="Feature", geometry=Pointy(), properties={})
    assert feat.geometry.model_dump(exclude_unset=True) == Pointy.__geo_interface__


def test_feature_with_null_geometry():
    feature = Feature(**test_feature_geom_null)
    assert feature.geometry is None


def test_feature_geo_interface_with_null_geometry():
    feature = Feature(**test_feature_geom_null)
    assert "bbox" not in feature.__geo_interface__


def test_feature_collection_geo_interface_with_null_geometry():
    fc = FeatureCollection(
        type="FeatureCollection", features=[test_feature_geom_null, test_feature]
    )
    assert "bbox" not in fc.__geo_interface__
    assert "bbox" not in fc.__geo_interface__["features"][0]
    assert "bbox" in fc.__geo_interface__["features"][1]


@pytest.mark.parametrize("id", ["a", 1, "1"])
def test_feature_id(id):
    """Test if a string stays a string and if an int stays an int."""
    feature = Feature(**test_feature, id=id)
    assert feature.id == id


@pytest.mark.parametrize("id", [True, 1.0])
def test_bad_feature_id(id):
    """make sure it raises error."""
    with pytest.raises(ValidationError):
        Feature(**test_feature, id=id)


def test_feature_validation():
    """Test default."""
    assert Feature(type="Feature", properties=None, geometry=None)
    assert Feature(type="Feature", properties=None, geometry=None, bbox=None)

    with pytest.raises(ValidationError):
        # should be type=Feature
        Feature(type="feature", properties=None, geometry=None)

    with pytest.raises(ValidationError):
        # missing type
        Feature(properties=None, geometry=None)

    with pytest.raises(ValidationError):
        # missing properties
        Feature(type="Feature", geometry=None)

    with pytest.raises(ValidationError):
        # missing geometry
        Feature(type="Feature", properties=None)

    assert Feature(
        type="Feature", properties=None, bbox=(0, 0, 100, 100), geometry=None
    )
    assert Feature(
        type="Feature", properties=None, bbox=(0, 0, 0, 100, 100, 100), geometry=None
    )

    with pytest.raises(ValidationError):
        # bad bbox2d
        Feature(type="Feature", properties=None, bbox=(0, 100, 100, 0), geometry=None)

    with pytest.raises(ValidationError):
        # bad bbox3d
        Feature(
            type="Feature",
            properties=None,
            bbox=(0, 100, 100, 100, 0, 0),
            geometry=None,
        )

    # Antimeridian
    with pytest.warns(UserWarning):
        Feature(type="Feature", properties=None, bbox=(100, 0, 0, 100), geometry=None)

    with pytest.warns(UserWarning):
        Feature(
            type="Feature",
            properties=None,
            bbox=(100, 0, 0, 0, 100, 100),
            geometry=None,
        )


def test_bbox_validation():
    # Some attempts at generic validation did not validate the types within
    # bbox before passing them to the function and resulted in TypeErrors.
    # This test exists to ensure that doesn't happen in the future.
    with pytest.raises(ValidationError):
        Feature(
            type="Feature",
            properties=None,
            bbox=(0, "a", 0, 1, 1, 1),
            geometry=None,
        )


def test_feature_validation_error_count():
    # Tests that validation does not include irrelevant errors to make them
    # easier to read. The input below used to raise 18 errors.
    # See #93 for more details.
    with pytest.raises(ValidationError):
        try:
            Feature(
                type="Feature",
                geometry=Polygon(
                    type="Polygon",
                    coordinates=[
                        [
                            (-55.9947406591177, -9.26104045526505),
                            (-55.9976752102375, -9.266589696568962),
                            (-56.00200328975916, -9.264041751931352),
                            (-55.99899921566248, -9.257935213034594),
                            (-55.99477406591177, -9.26103945526505),
                        ]
                    ],
                ),
                properties={},
            )
        except ValidationError as e:
            assert e.error_count() == 1
            raise


def test_feature_serializer():
    f = Feature(
        **{
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": coordinates,
            },
            "properties": {},
            "id": "Yo",
            "bbox": [13.38272, 52.46385, 13.42786, 52.48445],
        }
    )
    assert "bbox" in f.model_dump()
    assert "id" in f.model_dump()

    # Exclude
    assert "bbox" not in f.model_dump(exclude={"bbox"})
    assert "bbox" not in list(json.loads(f.model_dump_json(exclude={"bbox"})).keys())

    # Include
    assert ["bbox"] == list(f.model_dump(include={"bbox"}).keys())
    assert ["bbox"] == list(json.loads(f.model_dump_json(include={"bbox"})).keys())

    feat_ser = json.loads(f.model_dump_json())
    assert "bbox" in feat_ser
    assert "id" in feat_ser
    assert "bbox" not in feat_ser["geometry"]

    f = Feature(
        **{
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": coordinates,
            },
            "properties": {},
        }
    )
    # BBOX Should'nt be present if `None`
    # https://github.com/developmentseed/geojson-pydantic/issues/125
    assert "bbox" in f.model_dump()

    feat_ser = json.loads(f.model_dump_json())
    assert "bbox" not in feat_ser
    assert "id" not in feat_ser
    assert "bbox" not in feat_ser["geometry"]

    f = Feature(
        **{
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": coordinates,
                "bbox": [13.38272, 52.46385, 13.42786, 52.48445],
            },
            "properties": {},
        }
    )
    feat_ser = json.loads(f.model_dump_json())
    assert "bbox" not in feat_ser
    assert "id" not in feat_ser
    assert "bbox" in feat_ser["geometry"]


def test_feature_collection_serializer():
    fc = FeatureCollection(
        **{
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": coordinates,
                        "bbox": [13.38272, 52.46385, 13.42786, 52.48445],
                    },
                    "properties": {},
                    "bbox": [13.38272, 52.46385, 13.42786, 52.48445],
                }
            ],
            "bbox": [13.38272, 52.46385, 13.42786, 52.48445],
        }
    )
    assert "bbox" in fc.model_dump()

    # Exclude
    assert "bbox" not in fc.model_dump(exclude={"bbox"})
    assert "bbox" not in list(json.loads(fc.model_dump_json(exclude={"bbox"})).keys())

    # Include
    assert ["bbox"] == list(fc.model_dump(include={"bbox"}).keys())
    assert ["bbox"] == list(json.loads(fc.model_dump_json(include={"bbox"})).keys())

    featcoll_ser = json.loads(fc.model_dump_json())
    assert "bbox" in featcoll_ser
    assert "bbox" in featcoll_ser["features"][0]
    assert "bbox" in featcoll_ser["features"][0]["geometry"]

    fc = FeatureCollection(
        **{
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": coordinates,
                    },
                    "properties": {},
                }
            ],
        }
    )
    assert "bbox" in fc.model_dump()

    featcoll_ser = json.loads(fc.model_dump_json())
    assert "bbox" not in featcoll_ser
    assert "bbox" not in featcoll_ser["features"][0]
    assert "bbox" not in featcoll_ser["features"][0]["geometry"]