test_serializers.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from __future__ import unicode_literals
  2. import json
  3. from django.contrib.gis.geos import LinearRing, Point, Polygon
  4. from django.core import serializers
  5. from django.test import TestCase, mock, skipUnlessDBFeature
  6. from django.utils import six
  7. from .models import City, MultiFields, PennsylvaniaCity
  8. @skipUnlessDBFeature("gis_enabled")
  9. class GeoJSONSerializerTests(TestCase):
  10. fixtures = ['initial']
  11. def test_builtin_serializers(self):
  12. """
  13. 'geojson' should be listed in available serializers.
  14. """
  15. all_formats = set(serializers.get_serializer_formats())
  16. public_formats = set(serializers.get_public_serializer_formats())
  17. self.assertIn('geojson', all_formats),
  18. self.assertIn('geojson', public_formats)
  19. def test_serialization_base(self):
  20. geojson = serializers.serialize('geojson', City.objects.all().order_by('name'))
  21. try:
  22. geodata = json.loads(geojson)
  23. except Exception:
  24. self.fail("Serialized output is not valid JSON")
  25. self.assertEqual(len(geodata['features']), len(City.objects.all()))
  26. self.assertEqual(geodata['features'][0]['geometry']['type'], 'Point')
  27. self.assertEqual(geodata['features'][0]['properties']['name'], 'Chicago')
  28. def test_geometry_field_option(self):
  29. """
  30. When a model has several geometry fields, the 'geometry_field' option
  31. can be used to specify the field to use as the 'geometry' key.
  32. """
  33. MultiFields.objects.create(
  34. city=City.objects.first(), name='Name', point=Point(5, 23),
  35. poly=Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))))
  36. geojson = serializers.serialize('geojson', MultiFields.objects.all())
  37. geodata = json.loads(geojson)
  38. self.assertEqual(geodata['features'][0]['geometry']['type'], 'Point')
  39. geojson = serializers.serialize('geojson', MultiFields.objects.all(),
  40. geometry_field='poly')
  41. geodata = json.loads(geojson)
  42. self.assertEqual(geodata['features'][0]['geometry']['type'], 'Polygon')
  43. def test_fields_option(self):
  44. """
  45. The fields option allows to define a subset of fields to be present in
  46. the 'properties' of the generated output.
  47. """
  48. PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)')
  49. geojson = serializers.serialize('geojson', PennsylvaniaCity.objects.all(),
  50. fields=('county', 'point'))
  51. geodata = json.loads(geojson)
  52. self.assertIn('county', geodata['features'][0]['properties'])
  53. self.assertNotIn('founded', geodata['features'][0]['properties'])
  54. def test_srid_option(self):
  55. geojson = serializers.serialize('geojson', City.objects.all().order_by('name'), srid=2847)
  56. geodata = json.loads(geojson)
  57. self.assertEqual(
  58. [int(c) for c in geodata['features'][0]['geometry']['coordinates']],
  59. [1564802, 5613214])
  60. @mock.patch('django.contrib.gis.serializers.geojson.HAS_GDAL', False)
  61. def test_without_gdal(self):
  62. # Without coordinate transformation, the serialization should succeed:
  63. serializers.serialize('geojson', City.objects.all())
  64. with six.assertRaisesRegex(self, serializers.base.SerializationError, '.*GDAL is not installed'):
  65. # Coordinate transformations need GDAL
  66. serializers.serialize('geojson', City.objects.all(), srid=2847)
  67. def test_deserialization_exception(self):
  68. """
  69. GeoJSON cannot be deserialized.
  70. """
  71. with self.assertRaises(serializers.base.SerializerDoesNotExist):
  72. serializers.deserialize('geojson', '{}')