test_regress.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- encoding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from datetime import datetime
  4. from unittest import skipUnless
  5. from django.contrib.gis.geos import HAS_GEOS
  6. from django.contrib.gis.tests.utils import no_mysql, no_spatialite
  7. from django.contrib.gis.shortcuts import render_to_kmz
  8. from django.contrib.gis.tests.utils import HAS_SPATIAL_DB
  9. from django.db.models import Count, Min
  10. from django.test import TestCase
  11. if HAS_GEOS:
  12. from .models import City, PennsylvaniaCity, State, Truth
  13. @skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.")
  14. class GeoRegressionTests(TestCase):
  15. def test_update(self):
  16. "Testing GeoQuerySet.update(). See #10411."
  17. pnt = City.objects.get(name='Pueblo').point
  18. bak = pnt.clone()
  19. pnt.y += 0.005
  20. pnt.x += 0.005
  21. City.objects.filter(name='Pueblo').update(point=pnt)
  22. self.assertEqual(pnt, City.objects.get(name='Pueblo').point)
  23. City.objects.filter(name='Pueblo').update(point=bak)
  24. self.assertEqual(bak, City.objects.get(name='Pueblo').point)
  25. def test_kmz(self):
  26. "Testing `render_to_kmz` with non-ASCII data. See #11624."
  27. name = "Åland Islands"
  28. places = [{
  29. 'name': name,
  30. 'description': name,
  31. 'kml': '<Point><coordinates>5.0,23.0</coordinates></Point>'
  32. }]
  33. render_to_kmz('gis/kml/placemarks.kml', {'places': places})
  34. @no_spatialite
  35. @no_mysql
  36. def test_extent(self):
  37. "Testing `extent` on a table with a single point. See #11827."
  38. pnt = City.objects.get(name='Pueblo').point
  39. ref_ext = (pnt.x, pnt.y, pnt.x, pnt.y)
  40. extent = City.objects.filter(name='Pueblo').extent()
  41. for ref_val, val in zip(ref_ext, extent):
  42. self.assertAlmostEqual(ref_val, val, 4)
  43. def test_unicode_date(self):
  44. "Testing dates are converted properly, even on SpatiaLite. See #16408."
  45. founded = datetime(1857, 5, 23)
  46. PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)',
  47. founded=founded)
  48. self.assertEqual(founded, PennsylvaniaCity.objects.datetimes('founded', 'day')[0])
  49. self.assertEqual(founded, PennsylvaniaCity.objects.aggregate(Min('founded'))['founded__min'])
  50. def test_empty_count(self):
  51. "Testing that PostGISAdapter.__eq__ does check empty strings. See #13670."
  52. # contrived example, but need a geo lookup paired with an id__in lookup
  53. pueblo = City.objects.get(name='Pueblo')
  54. state = State.objects.filter(poly__contains=pueblo.point)
  55. cities_within_state = City.objects.filter(id__in=state)
  56. # .count() should not throw TypeError in __eq__
  57. self.assertEqual(cities_within_state.count(), 1)
  58. def test_defer_or_only_with_annotate(self):
  59. "Regression for #16409. Make sure defer() and only() work with annotate()"
  60. self.assertIsInstance(list(City.objects.annotate(Count('point')).defer('name')), list)
  61. self.assertIsInstance(list(City.objects.annotate(Count('point')).only('name')), list)
  62. def test_boolean_conversion(self):
  63. "Testing Boolean value conversion with the spatial backend, see #15169."
  64. t1 = Truth.objects.create(val=True)
  65. t2 = Truth.objects.create(val=False)
  66. val1 = Truth.objects.get(pk=t1.pk).val
  67. val2 = Truth.objects.get(pk=t2.pk).val
  68. # verify types -- should't be 0/1
  69. self.assertIsInstance(val1, bool)
  70. self.assertIsInstance(val2, bool)
  71. # verify values
  72. self.assertEqual(val1, True)
  73. self.assertEqual(val2, False)