test_serializers.py 3.2 KB

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