test_serializers.py 4.4 KB

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