test_serializers.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, skipUnlessDBFeature
  6. from .models import City, MultiFields, PennsylvaniaCity
  7. @skipUnlessDBFeature("gis_enabled")
  8. class GeoJSONSerializerTests(TestCase):
  9. fixtures = ['initial']
  10. def test_builtin_serializers(self):
  11. """
  12. 'geojson' should be listed in available serializers.
  13. """
  14. all_formats = set(serializers.get_serializer_formats())
  15. public_formats = set(serializers.get_public_serializer_formats())
  16. self.assertIn('geojson', all_formats),
  17. self.assertIn('geojson', public_formats)
  18. def test_serialization_base(self):
  19. geojson = serializers.serialize('geojson', City.objects.all().order_by('name'))
  20. try:
  21. geodata = json.loads(geojson)
  22. except Exception:
  23. self.fail("Serialized output is not valid JSON")
  24. self.assertEqual(len(geodata['features']), len(City.objects.all()))
  25. self.assertEqual(geodata['features'][0]['geometry']['type'], 'Point')
  26. self.assertEqual(geodata['features'][0]['properties']['name'], 'Chicago')
  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(), name='Name', point=Point(5, 23),
  34. poly=Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0))))
  35. geojson = serializers.serialize('geojson', MultiFields.objects.all())
  36. geodata = json.loads(geojson)
  37. self.assertEqual(geodata['features'][0]['geometry']['type'], 'Point')
  38. geojson = serializers.serialize('geojson', MultiFields.objects.all(),
  39. geometry_field='poly')
  40. geodata = json.loads(geojson)
  41. self.assertEqual(geodata['features'][0]['geometry']['type'], 'Polygon')
  42. def test_fields_option(self):
  43. """
  44. The fields option allows to define a subset of fields to be present in
  45. the 'properties' of the generated output.
  46. """
  47. PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)')
  48. geojson = serializers.serialize('geojson', PennsylvaniaCity.objects.all(),
  49. fields=('county', 'point'))
  50. geodata = json.loads(geojson)
  51. self.assertIn('county', geodata['features'][0]['properties'])
  52. self.assertNotIn('founded', geodata['features'][0]['properties'])
  53. def test_srid_option(self):
  54. geojson = serializers.serialize('geojson', City.objects.all().order_by('name'), srid=2847)
  55. geodata = json.loads(geojson)
  56. self.assertEqual(
  57. [int(c) for c in geodata['features'][0]['geometry']['coordinates']],
  58. [1564802, 5613214])
  59. def test_deserialization_exception(self):
  60. """
  61. GeoJSON cannot be deserialized.
  62. """
  63. with self.assertRaises(serializers.base.SerializerDoesNotExist):
  64. serializers.deserialize('geojson', '{}')