Debian Codegraph @

tests/django_restframework_gis_tests/test_schema_generation.py

raw
1 from django.test import RequestFactory, TestCase
2 from packaging.version import parse as parse_version
3 from rest_framework import VERSION as DRF_VERSION
4 from rest_framework.generics import RetrieveAPIView
5 from rest_framework.request import Request
6 from rest_framework.schemas.openapi import SchemaGenerator
7
8 from rest_framework_gis.pagination import GeoJsonPagination
9 from rest_framework_gis.schema import GeoFeatureAutoSchema
10
11 from .serializers import (
12 BoxedLocationGeoFeatureWithBBoxGeoFieldSerializer,
13 ChildPointSerializer,
14 GeometrySerializer,
15 GeometrySerializerMethodFieldSerializer,
16 LineStringSerializer,
17 ListChildPointSerializer,
18 MultiLineStringSerializer,
19 MultiPointSerializer,
20 MultiPolygonSerializer,
21 PointSerializer,
22 )
23 from .views import (
24 GeojsonBoxedLocationDetails,
25 GeojsonLocationContainedInBBoxList,
26 GeojsonLocationContainedInTileList,
27 GeojsonLocationWithinDistanceOfPointList,
28 ModelViewWithPolygon,
29 geojson_location_list,
30 )
31
32
33 def create_request(path):
34 factory = RequestFactory()
35 request = Request(factory.get(path))
36 return request
37
38
39 def create_view(view_cls, method, request):
40 generator = SchemaGenerator()
41 view = generator.create_view(view_cls.as_view(), method, request)
42 return view
43
44
45 class TestSchemaGeneration(TestCase):
46 def test_point_field_outer_most_gis_serializer(self):
47 class TestPointFieldView(RetrieveAPIView):
48 serializer_class = PointSerializer
49
50 path = "/"
51 method = "GET"
52
53 view = create_view(TestPointFieldView, "POST", create_request("/"))
54 inspector = GeoFeatureAutoSchema()
55 inspector.view = view
56 serializer = inspector.get_serializer(path, method)
57 content = inspector.map_serializer(serializer)
58 content.pop("type", None)
59 content["properties"]["properties"].pop("type", None)
60 self.assertEqual(
61 content,
62 {
63 "properties": {
64 "type": {"type": "string", "enum": ["Feature"]},
65 "id": {"type": "integer", "readOnly": True},
66 "geometry": {
67 "type": "object",
68 "properties": {
69 "type": {"type": "string", "enum": ["Point"]},
70 "coordinates": {
71 "type": "array",
72 "items": {"type": "number", "format": "float"},
73 "example": [12.9721, 77.5933],
74 "minItems": 2,
75 "maxItems": 3,
76 },
77 },
78 },
79 "properties": {
80 "properties": {
81 "random_field1": {"type": "string", "maxLength": 32},
82 "random_field2": {
83 "type": "integer",
84 "maximum": 2147483647,
85 "minimum": -2147483648,
86 },
87 },
88 "required": ["random_field1", "random_field2", "location"],
89 },
90 }
91 },
92 )
93
94 def test_point_field_inner_geo_serializer(self):
95 class TestPointFieldView(RetrieveAPIView):
96 serializer_class = ChildPointSerializer
97
98 path = "/"
99 method = "GET"
100
101 view = create_view(TestPointFieldView, "POST", create_request("/"))
102 inspector = GeoFeatureAutoSchema()
103 inspector.view = view
104 serializer = inspector.get_serializer(path, method)
105 content = inspector.map_serializer(serializer)
106 content.pop("type", None)
107 content["properties"]["point"].pop("type", None)
108 content["properties"]["point"]["properties"]["properties"].pop("type", None)
109 self.assertEqual(
110 content,
111 {
112 "properties": {
113 "point": {
114 "properties": {
115 "type": {"type": "string", "enum": ["Feature"]},
116 "id": {"type": "integer", "readOnly": True},
117 "geometry": {
118 "type": "object",
119 "properties": {
120 "type": {"type": "string", "enum": ["Point"]},
121 "coordinates": {
122 "type": "array",
123 "items": {"type": "number", "format": "float"},
124 "example": [12.9721, 77.5933],
125 "minItems": 2,
126 "maxItems": 3,
127 },
128 },
129 },
130 "properties": {
131 "properties": {
132 "random_field1": {
133 "type": "string",
134 "maxLength": 32,
135 },
136 "random_field2": {
137 "type": "integer",
138 "maximum": 2147483647,
139 "minimum": -2147483648,
140 },
141 },
142 "required": [
143 "random_field1",
144 "random_field2",
145 "location",
146 ],
147 },
148 }
149 }
150 },
151 "required": ["point"],
152 },
153 )
154
155 def test_point_field_inner_geo_list_serializer(self):
156 class TestPointFieldView(RetrieveAPIView):
157 serializer_class = ListChildPointSerializer
158
159 path = "/"
160 method = "GET"
161
162 view = create_view(TestPointFieldView, "POST", create_request("/"))
163 inspector = GeoFeatureAutoSchema()
164 inspector.view = view
165 serializer = inspector.get_serializer(path, method)
166 content = inspector.map_serializer(serializer)
167 content.pop("type", None)
168 content["properties"]["points"].pop("type", None)
169 content["properties"]["points"]["properties"]["features"]["items"][
170 "properties"
171 ]["properties"].pop("type", None)
172 self.assertEqual(
173 content,
174 {
175 "properties": {
176 "points": {
177 "properties": {
178 "type": {"type": "string", "enum": ["FeatureCollection"]},
179 "features": {
180 "type": "array",
181 "items": {
182 "type": "object",
183 "properties": {
184 "type": {"type": "string", "enum": ["Feature"]},
185 "id": {"type": "integer", "readOnly": True},
186 "geometry": {
187 "type": "object",
188 "properties": {
189 "type": {
190 "type": "string",
191 "enum": ["Point"],
192 },
193 "coordinates": {
194 "type": "array",
195 "items": {
196 "type": "number",
197 "format": "float",
198 },
199 "example": [12.9721, 77.5933],
200 "minItems": 2,
201 "maxItems": 3,
202 },
203 },
204 },
205 "properties": {
206 "properties": {
207 "random_field1": {
208 "type": "string",
209 "maxLength": 32,
210 },
211 "random_field2": {
212 "type": "integer",
213 "maximum": 2147483647,
214 "minimum": -2147483648,
215 },
216 },
217 "required": [
218 "random_field1",
219 "random_field2",
220 "location",
221 ],
222 },
223 },
224 },
225 },
226 }
227 }
228 },
229 "required": ["points"],
230 },
231 )
232
233 def test_line_string_field(self):
234 class TestLineStringFieldView(RetrieveAPIView):
235 serializer_class = LineStringSerializer
236
237 path = "/"
238 method = "GET"
239
240 view = create_view(TestLineStringFieldView, "POST", create_request("/"))
241 inspector = GeoFeatureAutoSchema()
242 inspector.view = view
243 serializer = inspector.get_serializer(path, method)
244 content = inspector.map_serializer(serializer)
245 geometry_schema = content["properties"]["geometry"]
246 geometry_schema.pop("type", None)
247 self.assertEqual(
248 geometry_schema,
249 {
250 "properties": {
251 "type": {"type": "string", "enum": ["LineString"]},
252 "coordinates": {
253 "type": "array",
254 "items": {
255 "type": "array",
256 "items": {"type": "number", "format": "float"},
257 "example": [12.9721, 77.5933],
258 "minItems": 2,
259 "maxItems": 3,
260 },
261 "example": [[22.4707, 70.0577], [12.9721, 77.5933]],
262 "minItems": 2,
263 },
264 }
265 },
266 )
267
268 def test_multi_polygon(self):
269 class TestMultiPolygonFieldView(RetrieveAPIView):
270 serializer_class = MultiPolygonSerializer
271
272 path = "/"
273 method = "GET"
274
275 view = create_view(TestMultiPolygonFieldView, "POST", create_request("/"))
276 inspector = GeoFeatureAutoSchema()
277 inspector.view = view
278 serializer = inspector.get_serializer(path, method)
279 content = inspector.map_serializer(serializer)
280 geometry_schema = content["properties"]["geometry"]
281 geometry_schema.pop("type", None)
282 self.assertEqual(
283 geometry_schema,
284 {
285 "properties": {
286 "type": {"type": "string", "enum": ["MultiPolygon"]},
287 "coordinates": {
288 "type": "array",
289 "items": {
290 "type": "array",
291 "items": {
292 "type": "array",
293 "items": {
294 "type": "array",
295 "items": {"type": "number", "format": "float"},
296 "example": [12.9721, 77.5933],
297 "minItems": 2,
298 "maxItems": 3,
299 },
300 "example": [[22.4707, 70.0577], [12.9721, 77.5933]],
301 "minItems": 4,
302 },
303 "example": [
304 [0.0, 0.0],
305 [0.0, 50.0],
306 [50.0, 50.0],
307 [50.0, 0.0],
308 [0.0, 0.0],
309 ],
310 },
311 "example": [
312 [
313 [0.0, 0.0],
314 [0.0, 50.0],
315 [50.0, 50.0],
316 [50.0, 0.0],
317 [0.0, 0.0],
318 ]
319 ],
320 },
321 }
322 },
323 )
324
325 def test_multi_line_string_field(self):
326 class TestMultiLineStringFieldView(RetrieveAPIView):
327 serializer_class = MultiLineStringSerializer
328
329 path = "/"
330 method = "GET"
331
332 view = create_view(TestMultiLineStringFieldView, "POST", create_request("/"))
333 inspector = GeoFeatureAutoSchema()
334 inspector.view = view
335 serializer = inspector.get_serializer(path, method)
336 content = inspector.map_serializer(serializer)
337 geometry_schema = content["properties"]["geometry"]
338 geometry_schema.pop("type", None)
339 self.assertEqual(
340 geometry_schema,
341 {
342 "properties": {
343 "type": {"type": "string", "enum": ["MultiLineString"]},
344 "coordinates": {
345 "type": "array",
346 "items": {
347 "type": "array",
348 "items": {
349 "type": "array",
350 "items": {"type": "number", "format": "float"},
351 "example": [12.9721, 77.5933],
352 "minItems": 2,
353 "maxItems": 3,
354 },
355 "example": [[22.4707, 70.0577], [12.9721, 77.5933]],
356 "minItems": 2,
357 },
358 "example": [[[22.4707, 70.0577], [12.9721, 77.5933]]],
359 },
360 }
361 },
362 )
363
364 def test_multi_point(self):
365 class TestMultiLineStringFieldView(RetrieveAPIView):
366 serializer_class = MultiPointSerializer
367
368 path = "/"
369 method = "GET"
370
371 view = create_view(TestMultiLineStringFieldView, "POST", create_request("/"))
372 inspector = GeoFeatureAutoSchema()
373 inspector.view = view
374 serializer = inspector.get_serializer(path, method)
375 content = inspector.map_serializer(serializer)
376 geometry_schema = content["properties"]["geometry"]
377 geometry_schema.pop("type", None)
378 self.assertEqual(
379 geometry_schema,
380 {
381 "properties": {
382 "type": {"type": "string", "enum": ["MultiPoint"]},
383 "coordinates": {
384 "type": "array",
385 "items": {
386 "type": "array",
387 "items": {"type": "number", "format": "float"},
388 "example": [12.9721, 77.5933],
389 "minItems": 2,
390 "maxItems": 3,
391 },
392 "example": [[12.9721, 77.5933]],
393 },
394 }
395 },
396 )
397
398 def test_geometry_field(self):
399 class GeometrySerializerFieldView(RetrieveAPIView):
400 serializer_class = GeometrySerializer
401
402 path = "/"
403 method = "GET"
404
405 view = create_view(GeometrySerializerFieldView, "POST", create_request("/"))
406 inspector = GeoFeatureAutoSchema()
407 inspector.view = view
408 serializer = inspector.get_serializer(path, method)
409 content = inspector.map_serializer(serializer)
410 geometry_schema = content["properties"]["geometry"]
411 geometry_schema.pop("type", None)
412 geometry_schema["properties"].pop("coordinates", None)
413 self.assertEqual(geometry_schema, {"properties": {"type": {"type": "string"}}})
414
415 def check_bbox_schema(self):
416 class TestMultiLineStringFieldView(RetrieveAPIView):
417 serializer_class = MultiPointSerializer
418
419 path = "/"
420 method = "GET"
421
422 view = create_view(TestMultiLineStringFieldView, "POST", create_request("/"))
423 inspector = GeoFeatureAutoSchema()
424 inspector.view = view
425 serializer = inspector.get_serializer(path, method)
426 content = inspector.map_serializer(serializer)
427 bbox_schema = content["properties"]["bbox"]
428 self.assertEqual(
429 bbox_schema,
430 {
431 "type": "array",
432 "items": {"type": "number"},
433 "minItems": 4,
434 "maxItems": 4,
435 "example": [12.9721, 77.5933, 12.9721, 77.5933],
436 },
437 )
438
439 def test_warning_for_geometry_serializer_method_field(self):
440 class TestGeometrySerializerMethodField(RetrieveAPIView):
441 serializer_class = GeometrySerializerMethodFieldSerializer
442
443 path = "/"
444 method = "GET"
445
446 view = create_view(
447 TestGeometrySerializerMethodField, "POST", create_request("/")
448 )
449 inspector = GeoFeatureAutoSchema()
450 inspector.view = view
451 serializer = inspector.get_serializer(path, method)
452 with self.assertWarns(Warning):
453 inspector.map_serializer(serializer)
454
455 def test_schema_for_bbox_geo_field(self):
456 path = "/"
457 method = "GET"
458
459 class GeojsonBoxedLocationDetailsWithBBoxGeoFieldView(
460 GeojsonBoxedLocationDetails
461 ):
462 serializer_class = BoxedLocationGeoFeatureWithBBoxGeoFieldSerializer
463
464 view = create_view(
465 GeojsonBoxedLocationDetailsWithBBoxGeoFieldView, "GET", create_request("/")
466 )
467 inspector = GeoFeatureAutoSchema()
468 inspector.view = view
469 serializer = inspector.get_serializer(path, method)
470 content = inspector.map_serializer(serializer)
471 bbox_schema = content["properties"]["bbox"]
472 self.assertNotIn(
473 "bbox_geometry", content["properties"]["properties"]["properties"]
474 )
475 self.assertEqual(
476 bbox_schema,
477 {
478 "type": "array",
479 "items": {"type": "number"},
480 "minItems": 4,
481 "maxItems": 4,
482 "example": [12.9721, 77.5933, 12.9721, 77.5933],
483 },
484 )
485
486
487 class TestPaginationSchemaGeneration(TestCase):
488 def test_geo_json_pagination_schema(self):
489 generated_schema = GeoJsonPagination().get_paginated_response_schema(
490 geojson_location_list
491 )
492 self.assertIn("features", generated_schema["properties"])
493 generated_schema["properties"].pop("features")
494 expected_schema = {
495 "type": "object",
496 "properties": {
497 "type": {"type": "string", "enum": ["FeatureCollection"]},
498 "count": {"type": "integer", "example": 123},
499 "next": {
500 "type": "string",
501 "nullable": True,
502 "format": "uri",
503 "example": "http://api.example.org/accounts/?page=4",
504 },
505 "previous": {
506 "type": "string",
507 "nullable": True,
508 "format": "uri",
509 "example": "http://api.example.org/accounts/?page=2",
510 },
511 },
512 }
513 if parse_version(DRF_VERSION) >= parse_version("3.15"):
514 expected_schema["required"] = ["count", "results"]
515 self.assertDictEqual(generated_schema, expected_schema)
516
517
518 class TestRestFrameworkGisFiltersSchema(TestCase):
519 def test_in_BBox_filter_schema(self):
520 path = "/"
521 method = "GET"
522 view = create_view(
523 GeojsonLocationContainedInBBoxList, "GET", create_request("/")
524 )
525 inspector = GeoFeatureAutoSchema()
526 inspector.view = view
527 generated_schema = inspector.get_filter_parameters(path, method)
528 self.assertDictEqual(
529 generated_schema[0],
530 {
531 "name": "in_bbox",
532 "required": False,
533 "in": "query",
534 "description": "Specify a bounding box as filter: in_bbox=min_lon,min_lat,max_lon,max_lat",
535 "schema": {
536 "type": "array",
537 "items": {"type": "number", "format": "float"},
538 "minItems": 4,
539 "maxItems": 4,
540 "example": [0, 0, 10, 10],
541 },
542 "style": "form",
543 "explode": False,
544 },
545 )
546
547 def test_in_TMS_filter_schema(self):
548 path = "/"
549 method = "GET"
550 view = create_view(
551 GeojsonLocationContainedInTileList, "GET", create_request("/")
552 )
553 inspector = GeoFeatureAutoSchema()
554 inspector.view = view
555 generated_schema = inspector.get_filter_parameters(path, method)
556 self.assertDictEqual(
557 generated_schema[0],
558 {
559 "name": "tile",
560 "required": False,
561 "in": "query",
562 "description": "Specify a bounding box filter defined by a TMS tile address: tile=Z/X/Y",
563 "schema": {"type": "string", "example": "12/56/34"},
564 },
565 )
566
567 def test_distance_to_point_filter(self):
568 path = "/"
569 method = "GET"
570 view = create_view(
571 GeojsonLocationWithinDistanceOfPointList, "GET", create_request("/")
572 )
573 inspector = GeoFeatureAutoSchema()
574 inspector.view = view
575 generated_schema = inspector.get_filter_parameters(path, method)
576 self.assertListEqual(
577 generated_schema,
578 [
579 {
580 "name": "dist",
581 "required": False,
582 "in": "query",
583 "schema": {"type": "number", "format": "float", "default": 1000},
584 "description": "Represents **Distance** in **Distance to point** filter. "
585 "Default value is used only if ***point*** is passed.",
586 },
587 {
588 "name": "point",
589 "required": False,
590 "in": "query",
591 "description": "Point represented in **x,y** format. "
592 "Represents **point** in **Distance to point filter**",
593 "schema": {
594 "type": "array",
595 "items": {"type": "number", "format": "float"},
596 "minItems": 2,
597 "maxItems": 2,
598 "example": [0, 10],
599 },
600 "style": "form",
601 "explode": False,
602 },
603 ],
604 )
605
606 def test_geometry_field(self):
607 path = "/"
608 method = "GET"
609 view = create_view(ModelViewWithPolygon, "GET", create_request("/"))
610 inspector = GeoFeatureAutoSchema()
611 inspector.view = view
612 serializer = inspector.get_serializer(path, method)
613 content = inspector.map_serializer(serializer)
614
615 self.assertEqual(
616 content,
617 {
618 "type": "object",
619 "properties": {
620 "id": {"type": "integer", "readOnly": True},
621 "polygon": {
622 "properties": {
623 "coordinates": {
624 "example": [
625 [0.0, 0.0],
626 [0.0, 50.0],
627 [50.0, 50.0],
628 [50.0, 0.0],
629 [0.0, 0.0],
630 ],
631 "items": {
632 "example": [[22.4707, 70.0577], [12.9721, 77.5933]],
633 "items": {
634 "example": [12.9721, 77.5933],
635 "items": {"format": "float", "type": "number"},
636 "maxItems": 3,
637 "minItems": 2,
638 "type": "array",
639 },
640 "minItems": 4,
641 "type": "array",
642 },
643 "type": "array",
644 },
645 "type": {"enum": ["Polygon"], "type": "string"},
646 },
647 "type": "object",
648 },
649 "random_field1": {"type": "string", "maxLength": 32},
650 "random_field2": {
651 "type": "integer",
652 "maximum": 2147483647,
653 "minimum": -2147483648,
654 },
655 },
656 "required": ["random_field1", "random_field2", "polygon"],
657 },
658 )