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