Debian Codegraph @

tests/django_restframework_gis_tests/test_bbox.py

raw
1 import json
2
3 from django.core.exceptions import ImproperlyConfigured
4 from django.test import TestCase
5 from django.urls import reverse
6
7 from rest_framework_gis import serializers as gis_serializers
8
9 from .models import BoxedLocation, Location
10
11
12 class TestRestFrameworkGisBBox(TestCase):
13 """
14 unit tests for bbox support in restframework_gis
15 """
16
17 def setUp(self):
18 self.geojson_boxedlocation_list_url = reverse("api_geojson_boxedlocation_list")
19 self.geojson_location_bbox_list_url = reverse("api_geojson_location_bbox_list")
20
21 def _create_locations(self):
22 self.bl1 = BoxedLocation.objects.create(
23 id=1,
24 name="l1",
25 slug="l1",
26 geometry="POINT (13.007 42.423)",
27 bbox_geometry="POLYGON((12.997 42.413,12.997 42.433,13.017 42.433,13.017 42.413,12.997 42.413))",
28 )
29 self.bl2 = BoxedLocation.objects.create(
30 id=2,
31 name="l2",
32 slug="l2",
33 geometry="POINT (12.007 43.423)",
34 bbox_geometry="POLYGON((11.997 43.413,11.997 43.433,12.017 43.433,12.017 43.413,11.997 43.413))",
35 )
36 self.l1 = Location.objects.create(
37 id=1,
38 name="l1",
39 slug="l1",
40 geometry="POLYGON((12.997 42.413,12.997 42.433,13.017 42.433,13.017 42.413,12.997 42.413))",
41 )
42
43 def test_list(self):
44 self._create_locations()
45 response = self.client.get(self.geojson_boxedlocation_list_url)
46 self.assertEqual(response.status_code, 200)
47 self.assertEqual(len(response.data["features"]), 2)
48 for feature in response.data["features"]:
49 self.assertIn("bbox", feature)
50 fid = feature["id"]
51 if fid == 1:
52 self.assertEqual(feature["bbox"], self.bl1.bbox_geometry.extent)
53 elif fid == 2:
54 self.assertEqual(feature["bbox"], self.bl2.bbox_geometry.extent)
55 else:
56 self.fail(f"Unexpected id: {fid}")
57 BoxedLocation.objects.all().delete()
58
59 def test_post_location_list_geojson(self):
60 self.assertEqual(BoxedLocation.objects.count(), 0)
61 data = {
62 "properties": {"name": "geojson input test"},
63 "geometry": {"type": "Point", "coordinates": [12.49, 41.89]},
64 "bbox": [11.0, 40.0, 13.0, 42.0],
65 }
66 response = self.client.post(
67 self.geojson_boxedlocation_list_url,
68 data=json.dumps(data),
69 content_type="application/json",
70 )
71 self.assertEqual(response.status_code, 201)
72 self.assertEqual(BoxedLocation.objects.count(), 1)
73 self.assertEqual(
74 BoxedLocation.objects.all()[0].bbox_geometry.extent,
75 (11.0, 40.0, 13.0, 42.0),
76 )
77
78 def test_get_autogenerated_location_bbox_geojson(self):
79 self._create_locations()
80 response = self.client.get(self.geojson_location_bbox_list_url)
81 self.assertEqual(response.status_code, 200)
82 self.assertEqual(len(response.data["features"]), 1)
83 self.assertEqual(response.data["features"][0]["bbox"], self.l1.geometry.extent)
84
85 def test_bbox_improperly_configured(self):
86 self._create_locations()
87
88 class LocationGeoFeatureSerializer(gis_serializers.GeoFeatureModelSerializer):
89 class Meta:
90 model = Location
91 geo_field = "geometry"
92 bbox_geo_field = "geometry"
93 auto_bbox = True
94
95 with self.assertRaises(ImproperlyConfigured):
96 LocationGeoFeatureSerializer(instance=self.l1)