Debian Codegraph @

tests/gis_tests/geoapp/test_serializers.py

raw
1 import json
2
3 from django.contrib.gis.geos import LinearRing, Point, Polygon
4 from django.core import serializers
5 from django.test import TestCase
6
7 from .models import City, MultiFields, PennsylvaniaCity
8
9
10 class GeoJSONSerializerTests(TestCase):
11 fixtures = ["initial"]
12
13 def test_builtin_serializers(self):
14 """
15 'geojson' should be listed in available serializers.
16 """
17 all_formats = set(serializers.get_serializer_formats())
18 public_formats = set(serializers.get_public_serializer_formats())
19
20 self.assertIn("geojson", all_formats)
21 self.assertIn("geojson", public_formats)
22
23 def test_serialization_base(self):
24 geojson = serializers.serialize("geojson", City.objects.order_by("name"))
25 geodata = json.loads(geojson)
26 self.assertEqual(list(geodata.keys()), ["type", "features"])
27 self.assertEqual(geodata["type"], "FeatureCollection")
28 self.assertEqual(len(geodata["features"]), len(City.objects.all()))
29 self.assertEqual(geodata["features"][0]["geometry"]["type"], "Point")
30 self.assertEqual(geodata["features"][0]["properties"]["name"], "Chicago")
31 first_city = City.objects.order_by("name").first()
32 self.assertEqual(geodata["features"][0]["id"], first_city.pk)
33 self.assertEqual(geodata["features"][0]["properties"]["pk"], str(first_city.pk))
34
35 def test_geometry_field_option(self):
36 """
37 When a model has several geometry fields, the 'geometry_field' option
38 can be used to specify the field to use as the 'geometry' key.
39 """
40 MultiFields.objects.create(
41 city=City.objects.first(),
42 name="Name",
43 point=Point(5, 23),
44 poly=Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))),
45 )
46
47 geojson = serializers.serialize("geojson", MultiFields.objects.all())
48 geodata = json.loads(geojson)
49 self.assertEqual(geodata["features"][0]["geometry"]["type"], "Point")
50
51 geojson = serializers.serialize(
52 "geojson", MultiFields.objects.all(), geometry_field="poly"
53 )
54 geodata = json.loads(geojson)
55 self.assertEqual(geodata["features"][0]["geometry"]["type"], "Polygon")
56
57 # geometry_field is considered even if not in fields (#26138).
58 geojson = serializers.serialize(
59 "geojson",
60 MultiFields.objects.all(),
61 geometry_field="poly",
62 fields=("city",),
63 )
64 geodata = json.loads(geojson)
65 self.assertEqual(geodata["features"][0]["geometry"]["type"], "Polygon")
66
67 def test_id_field_option(self):
68 """
69 By default Django uses the pk of the object as the id for a feature.
70 The 'id_field' option can be used to specify a different field to use
71 as the id.
72 """
73 cities = City.objects.order_by("name")
74 geojson = serializers.serialize("geojson", cities, id_field="name")
75 geodata = json.loads(geojson)
76 self.assertEqual(geodata["features"][0]["id"], cities[0].name)
77
78 def test_fields_option(self):
79 """
80 The fields option allows to define a subset of fields to be present in
81 the 'properties' of the generated output.
82 """
83 PennsylvaniaCity.objects.create(
84 name="Mansfield", county="Tioga", point="POINT(-77.071445 41.823881)"
85 )
86 geojson = serializers.serialize(
87 "geojson",
88 PennsylvaniaCity.objects.all(),
89 fields=("county", "point"),
90 )
91 geodata = json.loads(geojson)
92 self.assertIn("county", geodata["features"][0]["properties"])
93 self.assertNotIn("founded", geodata["features"][0]["properties"])
94 self.assertNotIn("pk", geodata["features"][0]["properties"])
95
96 def test_srid_option(self):
97 geojson = serializers.serialize(
98 "geojson", City.objects.order_by("name"), srid=2847
99 )
100 geodata = json.loads(geojson)
101 coordinates = geodata["features"][0]["geometry"]["coordinates"]
102 # Different PROJ versions use different transformations, all are
103 # correct as having a 1 meter accuracy.
104 self.assertAlmostEqual(coordinates[0], 1564802, -1)
105 self.assertAlmostEqual(coordinates[1], 5613214, -1)
106
107 def test_deserialization_exception(self):
108 """
109 GeoJSON cannot be deserialized.
110 """
111 with self.assertRaises(serializers.base.SerializerDoesNotExist):
112 serializers.deserialize("geojson", "{}")