test_regress.py 3.8 KB

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