2012-09-24 04:11:46 +08:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2011-09-10 06:34:23 +08:00
|
|
|
from datetime import datetime
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.contrib.gis.tests.utils import no_mysql, no_spatialite
|
2009-09-13 02:35:08 +08:00
|
|
|
from django.contrib.gis.shortcuts import render_to_kmz
|
2012-12-26 19:45:41 +08:00
|
|
|
from django.db.models import Count, Min
|
2011-09-10 06:34:23 +08:00
|
|
|
from django.test import TestCase
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2012-03-03 06:32:22 +08:00
|
|
|
from .models import City, PennsylvaniaCity, State, Truth
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2009-03-09 08:03:03 +08:00
|
|
|
|
2011-09-10 06:34:23 +08:00
|
|
|
class GeoRegressionTests(TestCase):
|
2009-03-09 08:03:03 +08:00
|
|
|
|
2012-08-03 16:47:27 +08:00
|
|
|
def test_update(self):
|
2012-03-03 01:16:52 +08:00
|
|
|
"Testing GeoQuerySet.update(). See #10411."
|
2009-03-09 08:03:03 +08: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-13 02:35:08 +08:00
|
|
|
|
2012-08-03 16:47:27 +08:00
|
|
|
def test_kmz(self):
|
2012-03-03 01:16:52 +08:00
|
|
|
"Testing `render_to_kmz` with non-ASCII data. See #11624."
|
2012-09-24 04:11:46 +08:00
|
|
|
name = "Åland Islands"
|
2009-09-13 02:35:08 +08:00
|
|
|
places = [{'name' : name,
|
|
|
|
'description' : name,
|
|
|
|
'kml' : '<Point><coordinates>5.0,23.0</coordinates></Point>'
|
|
|
|
}]
|
|
|
|
kmz = render_to_kmz('gis/kml/placemarks.kml', {'places' : places})
|
|
|
|
|
2009-09-14 01:40:46 +08:00
|
|
|
@no_spatialite
|
2009-11-02 10:17:56 +08:00
|
|
|
@no_mysql
|
2012-08-03 16:47:27 +08:00
|
|
|
def test_extent(self):
|
2012-03-03 01:16:52 +08:00
|
|
|
"Testing `extent` on a table with a single point. See #11827."
|
2009-09-14 01:40:46 +08:00
|
|
|
pnt = City.objects.get(name='Pueblo').point
|
|
|
|
ref_ext = (pnt.x, pnt.y, pnt.x, pnt.y)
|
2009-11-10 11:51:05 +08:00
|
|
|
extent = City.objects.filter(name='Pueblo').extent()
|
|
|
|
for ref_val, val in zip(ref_ext, extent):
|
|
|
|
self.assertAlmostEqual(ref_val, val, 4)
|
2011-09-10 06:34:23 +08:00
|
|
|
|
2012-08-03 16:47:27 +08:00
|
|
|
def test_unicode_date(self):
|
2012-03-03 01:16:52 +08:00
|
|
|
"Testing dates are converted properly, even on SpatiaLite. See #16408."
|
2011-09-10 06:34:23 +08:00
|
|
|
founded = datetime(1857, 5, 23)
|
|
|
|
mansfield = PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)',
|
|
|
|
founded=founded)
|
|
|
|
self.assertEqual(founded, PennsylvaniaCity.objects.dates('founded', 'day')[0])
|
2012-12-26 19:45:41 +08:00
|
|
|
self.assertEqual(founded, PennsylvaniaCity.objects.aggregate(Min('founded'))['founded__min'])
|
2011-09-10 08:29:34 +08:00
|
|
|
|
2012-08-03 16:47:27 +08:00
|
|
|
def test_empty_count(self):
|
2012-03-03 01:16:52 +08:00
|
|
|
"Testing that PostGISAdapter.__eq__ does check empty strings. See #13670."
|
2011-09-10 08:29:34 +08:00
|
|
|
# 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)
|
|
|
|
|
|
|
|
# .count() should not throw TypeError in __eq__
|
|
|
|
self.assertEqual(cities_within_state.count(), 1)
|
2012-02-11 21:50:27 +08:00
|
|
|
|
2012-08-03 16:47:27 +08:00
|
|
|
def test_defer_or_only_with_annotate(self):
|
2012-03-03 01:16:52 +08:00
|
|
|
"Regression for #16409. Make sure defer() and only() work with annotate()"
|
2012-02-11 21:50:27 +08: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-03 06:32:22 +08:00
|
|
|
|
2012-08-03 16:47:27 +08:00
|
|
|
def test_boolean_conversion(self):
|
2012-03-03 06:32:22 +08:00
|
|
|
"Testing Boolean value conversion with the spatial backend, see #15169."
|
|
|
|
t1 = Truth.objects.create(val=True)
|
|
|
|
t2 = Truth.objects.create(val=False)
|
|
|
|
|
|
|
|
val1 = Truth.objects.get(pk=1).val
|
|
|
|
val2 = Truth.objects.get(pk=2).val
|
|
|
|
# verify types -- should't be 0/1
|
|
|
|
self.assertIsInstance(val1, bool)
|
|
|
|
self.assertIsInstance(val2, bool)
|
|
|
|
# verify values
|
|
|
|
self.assertEqual(val1, True)
|
|
|
|
self.assertEqual(val2, False)
|