2011-09-10 06:34:23 +08:00
|
|
|
from datetime import datetime
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2015-04-24 23:24:07 +08:00
|
|
|
from django.contrib.gis.db.models import Extent
|
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
|
2014-08-18 01:06:25 +08:00
|
|
|
from django.test import TestCase, skipUnlessDBFeature
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2019-03-04 02:33:48 +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):
|
2014-07-27 01:15:54 +08:00
|
|
|
fixtures = ['initial']
|
2009-03-09 08:03:03 +08:00
|
|
|
|
2012-08-03 16:47:27 +08:00
|
|
|
def test_update(self):
|
2016-11-07 20:00:40 +08:00
|
|
|
"Testing QuerySet.update() (#10411)."
|
2019-01-15 07:32:42 +08:00
|
|
|
pueblo = City.objects.get(name='Pueblo')
|
|
|
|
bak = pueblo.point.clone()
|
|
|
|
pueblo.point.y += 0.005
|
|
|
|
pueblo.point.x += 0.005
|
|
|
|
|
|
|
|
City.objects.filter(name='Pueblo').update(point=pueblo.point)
|
|
|
|
pueblo.refresh_from_db()
|
|
|
|
self.assertAlmostEqual(bak.y + 0.005, pueblo.point.y, 6)
|
|
|
|
self.assertAlmostEqual(bak.x + 0.005, pueblo.point.x, 6)
|
2009-03-09 08:03:03 +08:00
|
|
|
City.objects.filter(name='Pueblo').update(point=bak)
|
2019-01-15 07:32:42 +08:00
|
|
|
pueblo.refresh_from_db()
|
|
|
|
self.assertAlmostEqual(bak.y, pueblo.point.y, 6)
|
|
|
|
self.assertAlmostEqual(bak.x, pueblo.point.x, 6)
|
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"
|
2013-12-09 01:20:06 +08:00
|
|
|
places = [{
|
|
|
|
'name': name,
|
|
|
|
'description': name,
|
|
|
|
'kml': '<Point><coordinates>5.0,23.0</coordinates></Point>'
|
|
|
|
}]
|
2013-10-27 09:27:42 +08:00
|
|
|
render_to_kmz('gis/kml/placemarks.kml', {'places': places})
|
2009-09-13 02:35:08 +08:00
|
|
|
|
2014-08-20 00:47:23 +08:00
|
|
|
@skipUnlessDBFeature("supports_extent_aggr")
|
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)
|
2015-01-15 03:48:55 +08:00
|
|
|
extent = City.objects.filter(name='Pueblo').aggregate(Extent('point'))['point__extent']
|
2009-11-10 11:51:05 +08:00
|
|
|
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)
|
2013-09-08 23:05:16 +08:00
|
|
|
PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)',
|
|
|
|
founded=founded)
|
2013-02-10 23:15:49 +08:00
|
|
|
self.assertEqual(founded, PennsylvaniaCity.objects.datetimes('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):
|
2013-09-04 02:22:21 +08: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 08:29:34 +08:00
|
|
|
|
2013-09-04 02:22:21 +08:00
|
|
|
# .count() should not throw TypeError in __eq__
|
|
|
|
self.assertEqual(cities_within_state.count(), 1)
|
2012-02-11 21:50:27 +08:00
|
|
|
|
2020-09-08 03:45:56 +08:00
|
|
|
@skipUnlessDBFeature('allows_group_by_lob')
|
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)
|
|
|
|
|
2013-05-12 04:57:01 +08:00
|
|
|
val1 = Truth.objects.get(pk=t1.pk).val
|
|
|
|
val2 = Truth.objects.get(pk=t2.pk).val
|
2015-01-20 22:54:12 +08:00
|
|
|
# verify types -- shouldn't be 0/1
|
2012-03-03 06:32:22 +08:00
|
|
|
self.assertIsInstance(val1, bool)
|
|
|
|
self.assertIsInstance(val2, bool)
|
|
|
|
# verify values
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(val1, True)
|
|
|
|
self.assertIs(val2, False)
|