2016-03-28 23:19:25 +08:00
|
|
|
from django.contrib.gis.db.models import Collect, Count, Extent, F, Union
|
2015-04-24 23:24:07 +08:00
|
|
|
from django.contrib.gis.geometry.backend import Geometry
|
|
|
|
from django.contrib.gis.geos import GEOSGeometry, MultiPoint, Point
|
2014-08-22 00:47:57 +08:00
|
|
|
from django.db import connection
|
2015-09-03 07:55:55 +08:00
|
|
|
from django.test import TestCase, skipUnlessDBFeature
|
2014-08-12 20:08:40 +08:00
|
|
|
from django.test.utils import override_settings
|
|
|
|
from django.utils import timezone
|
2011-10-18 02:45:22 +08:00
|
|
|
|
2015-02-10 23:07:44 +08:00
|
|
|
from ..utils import no_oracle
|
2015-04-24 23:24:07 +08:00
|
|
|
from .models import (
|
|
|
|
Article, Author, Book, City, DirectoryEntry, Event, Location, Parcel,
|
|
|
|
)
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2010-12-02 13:58:21 +08:00
|
|
|
class RelatedGeoModelTest(TestCase):
|
2014-07-27 01:15:54 +08:00
|
|
|
fixtures = ['initial']
|
2009-03-04 06:10:15 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def test02_select_related(self):
|
|
|
|
"Testing `select_related` on geographic models (see #7126)."
|
2014-09-29 20:50:36 +08:00
|
|
|
qs1 = City.objects.order_by('id')
|
|
|
|
qs2 = City.objects.order_by('id').select_related()
|
|
|
|
qs3 = City.objects.order_by('id').select_related('location')
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2010-12-02 13:58:21 +08:00
|
|
|
# Reference data for what's in the fixtures.
|
|
|
|
cities = (
|
|
|
|
('Aurora', 'TX', -97.516111, 33.058333),
|
|
|
|
('Roswell', 'NM', -104.528056, 33.387222),
|
2013-11-03 03:37:48 +08:00
|
|
|
('Kecksburg', 'PA', -79.460734, 40.18476),
|
2010-12-02 13:58:21 +08:00
|
|
|
)
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
for qs in (qs1, qs2, qs3):
|
|
|
|
for ref, c in zip(cities, qs):
|
|
|
|
nm, st, lon, lat = ref
|
|
|
|
self.assertEqual(nm, c.name)
|
|
|
|
self.assertEqual(st, c.state)
|
2016-11-23 16:23:06 +08:00
|
|
|
self.assertEqual(Point(lon, lat, srid=c.location.point.srid), c.location.point)
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2014-08-20 00:47:23 +08:00
|
|
|
@skipUnlessDBFeature("supports_extent_aggr")
|
2015-01-15 03:48:55 +08:00
|
|
|
def test_related_extent_aggregate(self):
|
2015-09-03 07:55:55 +08:00
|
|
|
"Testing the `Extent` aggregate on related geographic models."
|
2009-03-04 06:10:15 +08:00
|
|
|
# This combines the Extent and Union aggregates into one query
|
2009-03-31 01:15:49 +08:00
|
|
|
aggs = City.objects.aggregate(Extent('location__point'))
|
2009-03-04 06:10:15 +08:00
|
|
|
|
2010-12-02 13:58:21 +08:00
|
|
|
# One for all locations, one that excludes New Mexico (Roswell).
|
|
|
|
all_extent = (-104.528056, 29.763374, -79.460734, 40.18476)
|
|
|
|
txpa_extent = (-97.516111, 29.763374, -79.460734, 40.18476)
|
2015-09-03 07:55:55 +08:00
|
|
|
e1 = City.objects.aggregate(Extent('location__point'))['location__point__extent']
|
|
|
|
e2 = City.objects.exclude(state='NM').aggregate(Extent('location__point'))['location__point__extent']
|
2009-03-04 06:10:15 +08:00
|
|
|
e3 = aggs['location__point__extent']
|
|
|
|
|
|
|
|
# The tolerance value is to four decimal places because of differences
|
|
|
|
# between the Oracle and PostGIS spatial backends on the extent calculation.
|
|
|
|
tol = 4
|
|
|
|
for ref, e in [(all_extent, e1), (txpa_extent, e2), (all_extent, e3)]:
|
2013-10-17 16:17:41 +08:00
|
|
|
for ref_val, e_val in zip(ref, e):
|
|
|
|
self.assertAlmostEqual(ref_val, e_val, tol)
|
2009-03-04 06:10:15 +08:00
|
|
|
|
2015-01-15 03:48:55 +08:00
|
|
|
@skipUnlessDBFeature("supports_extent_aggr")
|
|
|
|
def test_related_extent_annotate(self):
|
|
|
|
"""
|
|
|
|
Test annotation with Extent GeoAggregate.
|
|
|
|
"""
|
|
|
|
cities = City.objects.annotate(points_extent=Extent('location__point')).order_by('name')
|
|
|
|
tol = 4
|
|
|
|
self.assertAlmostEqual(
|
|
|
|
cities[0].points_extent,
|
|
|
|
(-97.516111, 33.058333, -97.516111, 33.058333),
|
|
|
|
tol
|
|
|
|
)
|
|
|
|
|
2017-01-03 22:49:00 +08:00
|
|
|
@skipUnlessDBFeature('supports_union_aggr')
|
2015-01-15 03:48:55 +08:00
|
|
|
def test_related_union_aggregate(self):
|
2015-09-03 07:55:55 +08:00
|
|
|
"Testing the `Union` aggregate on related geographic models."
|
2009-03-31 01:15:49 +08:00
|
|
|
# This combines the Extent and Union aggregates into one query
|
|
|
|
aggs = City.objects.aggregate(Union('location__point'))
|
|
|
|
|
2009-03-04 06:10:15 +08:00
|
|
|
# These are the points that are components of the aggregate geographic
|
2010-12-02 13:58:21 +08:00
|
|
|
# union that is returned. Each point # corresponds to City PK.
|
2009-03-04 06:10:15 +08:00
|
|
|
p1 = Point(-104.528056, 33.387222)
|
|
|
|
p2 = Point(-97.516111, 33.058333)
|
|
|
|
p3 = Point(-79.460734, 40.18476)
|
2010-12-02 13:58:21 +08:00
|
|
|
p4 = Point(-96.801611, 32.782057)
|
|
|
|
p5 = Point(-95.363151, 29.763374)
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2014-03-28 04:17:10 +08:00
|
|
|
# The second union aggregate is for a union
|
2009-03-04 06:10:15 +08:00
|
|
|
# query that includes limiting information in the WHERE clause (in other
|
2015-09-03 07:55:55 +08:00
|
|
|
# words a `.filter()` precedes the call to `.aggregate(Union()`).
|
2014-03-28 04:17:10 +08:00
|
|
|
ref_u1 = MultiPoint(p1, p2, p4, p5, p3, srid=4326)
|
|
|
|
ref_u2 = MultiPoint(p2, p3, srid=4326)
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2015-09-03 07:55:55 +08:00
|
|
|
u1 = City.objects.aggregate(Union('location__point'))['location__point__union']
|
2014-09-04 20:15:09 +08:00
|
|
|
u2 = City.objects.exclude(
|
|
|
|
name__in=('Roswell', 'Houston', 'Dallas', 'Fort Worth'),
|
2015-09-03 07:55:55 +08:00
|
|
|
).aggregate(Union('location__point'))['location__point__union']
|
2009-03-04 06:10:15 +08:00
|
|
|
u3 = aggs['location__point__union']
|
2014-03-28 04:17:10 +08:00
|
|
|
self.assertEqual(type(u1), MultiPoint)
|
|
|
|
self.assertEqual(type(u3), MultiPoint)
|
|
|
|
|
|
|
|
# Ordering of points in the result of the union is not defined and
|
|
|
|
# implementation-dependent (DB backend, GEOS version)
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual({p.ewkt for p in ref_u1}, {p.ewkt for p in u1})
|
|
|
|
self.assertEqual({p.ewkt for p in ref_u2}, {p.ewkt for p in u2})
|
|
|
|
self.assertEqual({p.ewkt for p in ref_u1}, {p.ewkt for p in u3})
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2008-12-06 09:52:14 +08:00
|
|
|
def test05_select_related_fk_to_subclass(self):
|
|
|
|
"Testing that calling select_related on a query over a model with an FK to a model subclass works"
|
|
|
|
# Regression test for #9752.
|
2013-09-08 23:05:16 +08:00
|
|
|
list(DirectoryEntry.objects.all().select_related())
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-04-02 00:01:50 +08:00
|
|
|
def test06_f_expressions(self):
|
2009-03-04 06:10:15 +08:00
|
|
|
"Testing F() expressions on GeometryFields."
|
|
|
|
# Constructing a dummy parcel border and getting the City instance for
|
|
|
|
# assigning the FK.
|
2014-09-04 20:15:09 +08:00
|
|
|
b1 = GEOSGeometry(
|
|
|
|
'POLYGON((-97.501205 33.052520,-97.501205 33.052576,'
|
|
|
|
'-97.501150 33.052576,-97.501150 33.052520,-97.501205 33.052520))',
|
|
|
|
srid=4326
|
|
|
|
)
|
2009-03-04 06:10:15 +08:00
|
|
|
pcity = City.objects.get(name='Aurora')
|
|
|
|
|
|
|
|
# First parcel has incorrect center point that is equal to the City;
|
|
|
|
# it also has a second border that is different from the first as a
|
|
|
|
# 100ft buffer around the City.
|
|
|
|
c1 = pcity.location.point
|
|
|
|
c2 = c1.transform(2276, clone=True)
|
|
|
|
b2 = c2.buffer(100)
|
2013-09-08 23:05:16 +08:00
|
|
|
Parcel.objects.create(name='P1', city=pcity, center1=c1, center2=c2, border1=b1, border2=b2)
|
2009-03-04 06:10:15 +08:00
|
|
|
|
|
|
|
# Now creating a second Parcel where the borders are the same, just
|
|
|
|
# in different coordinate systems. The center points are also the
|
2011-08-12 22:14:15 +08:00
|
|
|
# same (but in different coordinate systems), and this time they
|
2009-03-04 06:10:15 +08:00
|
|
|
# actually correspond to the centroid of the border.
|
|
|
|
c1 = b1.centroid
|
|
|
|
c2 = c1.transform(2276, clone=True)
|
2013-09-08 23:05:16 +08:00
|
|
|
Parcel.objects.create(name='P2', city=pcity, center1=c1, center2=c2, border1=b1, border2=b1)
|
2009-03-04 06:10:15 +08:00
|
|
|
|
|
|
|
# Should return the second Parcel, which has the center within the
|
|
|
|
# border.
|
|
|
|
qs = Parcel.objects.filter(center1__within=F('border1'))
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual('P2', qs[0].name)
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2014-08-22 00:47:57 +08:00
|
|
|
if connection.features.supports_transform:
|
2009-03-04 06:10:15 +08:00
|
|
|
# This time center2 is in a different coordinate system and needs
|
|
|
|
# to be wrapped in transformation SQL.
|
|
|
|
qs = Parcel.objects.filter(center2__within=F('border1'))
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual('P2', qs[0].name)
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2009-03-04 06:10:15 +08:00
|
|
|
# Should return the first Parcel, which has the center point equal
|
|
|
|
# to the point in the City ForeignKey.
|
|
|
|
qs = Parcel.objects.filter(center1=F('city__location__point'))
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual('P1', qs[0].name)
|
|
|
|
|
2014-08-22 00:47:57 +08:00
|
|
|
if connection.features.supports_transform:
|
2009-03-04 06:10:15 +08:00
|
|
|
# This time the city column should be wrapped in transformation SQL.
|
|
|
|
qs = Parcel.objects.filter(border2__contains=F('city__location__point'))
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual('P1', qs[0].name)
|
|
|
|
|
2009-04-02 00:01:50 +08:00
|
|
|
def test07_values(self):
|
2016-11-07 20:00:40 +08:00
|
|
|
"Testing values() and values_list()."
|
2009-04-02 00:01:50 +08:00
|
|
|
gqs = Location.objects.all()
|
|
|
|
gvqs = Location.objects.values()
|
|
|
|
gvlqs = Location.objects.values_list()
|
|
|
|
|
|
|
|
# Incrementing through each of the models, dictionaries, and tuples
|
2016-11-07 20:00:40 +08:00
|
|
|
# returned by each QuerySet.
|
2009-04-02 00:01:50 +08:00
|
|
|
for m, d, t in zip(gqs, gvqs, gvlqs):
|
|
|
|
# The values should be Geometry objects and not raw strings returned
|
|
|
|
# by the spatial database.
|
2014-04-10 04:20:22 +08:00
|
|
|
self.assertIsInstance(d['point'], Geometry)
|
|
|
|
self.assertIsInstance(t[1], Geometry)
|
2009-04-02 00:01:50 +08:00
|
|
|
self.assertEqual(m.point, d['point'])
|
|
|
|
self.assertEqual(m.point, t[1])
|
|
|
|
|
2014-08-12 20:08:40 +08:00
|
|
|
@override_settings(USE_TZ=True)
|
|
|
|
def test_07b_values(self):
|
|
|
|
"Testing values() and values_list() with aware datetime. See #21565."
|
|
|
|
Event.objects.create(name="foo", when=timezone.now())
|
|
|
|
list(Event.objects.values_list('when'))
|
|
|
|
|
2009-04-04 11:21:31 +08:00
|
|
|
def test08_defer_only(self):
|
|
|
|
"Testing defer() and only() on Geographic models."
|
|
|
|
qs = Location.objects.all()
|
|
|
|
def_qs = Location.objects.defer('point')
|
|
|
|
for loc, def_loc in zip(qs, def_qs):
|
|
|
|
self.assertEqual(loc.point, def_loc.point)
|
2009-04-02 00:01:50 +08:00
|
|
|
|
2009-04-08 05:18:41 +08:00
|
|
|
def test09_pk_relations(self):
|
|
|
|
"Ensuring correct primary key column is selected across relations. See #10757."
|
|
|
|
# The expected ID values -- notice the last two location IDs
|
2010-12-02 13:58:21 +08:00
|
|
|
# are out of order. Dallas and Houston have location IDs that differ
|
|
|
|
# from their PKs -- this is done to ensure that the related location
|
|
|
|
# ID column is selected instead of ID column for the city.
|
2009-04-08 05:18:41 +08:00
|
|
|
city_ids = (1, 2, 3, 4, 5)
|
|
|
|
loc_ids = (1, 2, 3, 5, 4)
|
|
|
|
ids_qs = City.objects.order_by('id').values('id', 'location__id')
|
|
|
|
for val_dict, c_id, l_id in zip(ids_qs, city_ids, loc_ids):
|
|
|
|
self.assertEqual(val_dict['id'], c_id)
|
|
|
|
self.assertEqual(val_dict['location__id'], l_id)
|
|
|
|
|
2014-09-20 03:03:55 +08:00
|
|
|
# TODO: fix on Oracle -- qs2 returns an empty result for an unknown reason
|
|
|
|
@no_oracle
|
2009-04-15 03:14:32 +08:00
|
|
|
def test10_combine(self):
|
2016-11-07 20:00:40 +08:00
|
|
|
"Testing the combination of two QuerySets (#10807)."
|
2009-04-15 03:14:32 +08:00
|
|
|
buf1 = City.objects.get(name='Aurora').location.point.buffer(0.1)
|
|
|
|
buf2 = City.objects.get(name='Kecksburg').location.point.buffer(0.1)
|
|
|
|
qs1 = City.objects.filter(location__point__within=buf1)
|
|
|
|
qs2 = City.objects.filter(location__point__within=buf2)
|
|
|
|
combined = qs1 | qs2
|
|
|
|
names = [c.name for c in combined]
|
|
|
|
self.assertEqual(2, len(names))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('Aurora', names)
|
|
|
|
self.assertIn('Kecksburg', names)
|
2009-04-15 03:14:32 +08:00
|
|
|
|
2009-07-17 06:38:22 +08: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
|
|
|
|
def test12a_count(self):
|
2016-03-12 01:53:12 +08:00
|
|
|
"Testing `Count` aggregate on geo-fields."
|
2010-12-02 13:58:21 +08:00
|
|
|
# The City, 'Fort Worth' uses the same location as Dallas.
|
2009-06-03 12:49:16 +08:00
|
|
|
dallas = City.objects.get(name='Dallas')
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2009-06-03 12:49:16 +08:00
|
|
|
# Count annotation should be 2 for the Dallas location now.
|
|
|
|
loc = Location.objects.annotate(num_cities=Count('city')).get(id=dallas.location.id)
|
|
|
|
self.assertEqual(2, loc.num_cities)
|
|
|
|
|
2009-07-17 06:38:22 +08:00
|
|
|
def test12b_count(self):
|
2016-03-12 01:53:12 +08:00
|
|
|
"Testing `Count` aggregate on non geo-fields."
|
2009-06-03 12:49:16 +08:00
|
|
|
# Should only be one author (Trevor Paglen) returned by this query, and
|
2010-12-02 13:58:21 +08:00
|
|
|
# the annotation should have 3 for the number of books, see #11087.
|
2015-01-30 15:26:13 +08:00
|
|
|
# Also testing with a values(), see #11489.
|
2009-06-03 12:49:16 +08:00
|
|
|
qs = Author.objects.annotate(num_books=Count('books')).filter(num_books__gt=1)
|
2009-07-17 06:38:22 +08:00
|
|
|
vqs = Author.objects.values('name').annotate(num_books=Count('books')).filter(num_books__gt=1)
|
2009-06-03 12:49:16 +08:00
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual(3, qs[0].num_books)
|
2009-07-17 06:38:22 +08:00
|
|
|
self.assertEqual(1, len(vqs))
|
|
|
|
self.assertEqual(3, vqs[0]['num_books'])
|
2009-06-03 12:49:16 +08:00
|
|
|
|
2014-09-18 03:33:50 +08: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
|
2011-09-11 08:00:15 +08:00
|
|
|
def test13c_count(self):
|
|
|
|
"Testing `Count` aggregate with `.values()`. See #15305."
|
|
|
|
qs = Location.objects.filter(id=5).annotate(num_cities=Count('city')).values('id', 'point', 'num_cities')
|
|
|
|
self.assertEqual(1, len(qs))
|
|
|
|
self.assertEqual(2, qs[0]['num_cities'])
|
2014-04-10 04:20:22 +08:00
|
|
|
self.assertIsInstance(qs[0]['point'], GEOSGeometry)
|
2011-09-11 08:00:15 +08:00
|
|
|
|
2009-07-17 06:38:22 +08:00
|
|
|
# TODO: The phantom model does appear on Oracle.
|
|
|
|
@no_oracle
|
2009-06-30 00:31:21 +08:00
|
|
|
def test13_select_related_null_fk(self):
|
2016-03-12 01:53:12 +08:00
|
|
|
"Testing `select_related` on a nullable ForeignKey."
|
2013-09-08 23:05:16 +08:00
|
|
|
Book.objects.create(title='Without Author')
|
2009-06-30 00:31:21 +08:00
|
|
|
b = Book.objects.select_related('author').get(title='Without Author')
|
|
|
|
# Should be `None`, and not a 'dummy' model.
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertIsNone(b.author)
|
2009-06-30 00:31:21 +08:00
|
|
|
|
2014-08-20 00:47:23 +08:00
|
|
|
@skipUnlessDBFeature("supports_collect_aggr")
|
2015-01-15 03:48:55 +08:00
|
|
|
def test_collect(self):
|
|
|
|
"""
|
2015-09-03 07:55:55 +08:00
|
|
|
Testing the `Collect` aggregate.
|
2015-01-15 03:48:55 +08:00
|
|
|
"""
|
2009-07-12 04:34:27 +08:00
|
|
|
# Reference query:
|
2009-12-22 23:18:51 +08:00
|
|
|
# SELECT AsText(ST_Collect("relatedapp_location"."point")) FROM "relatedapp_city" LEFT OUTER JOIN
|
|
|
|
# "relatedapp_location" ON ("relatedapp_city"."location_id" = "relatedapp_location"."id")
|
2009-07-12 04:34:27 +08:00
|
|
|
# WHERE "relatedapp_city"."state" = 'TX';
|
2014-09-04 20:15:09 +08:00
|
|
|
ref_geom = GEOSGeometry(
|
|
|
|
'MULTIPOINT(-97.516111 33.058333,-96.801611 32.782057,'
|
|
|
|
'-95.363151 29.763374,-96.801611 32.782057)'
|
|
|
|
)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2015-09-03 07:55:55 +08:00
|
|
|
coll = City.objects.filter(state='TX').aggregate(Collect('location__point'))['location__point__collect']
|
|
|
|
# Even though Dallas and Ft. Worth share same point, Collect doesn't
|
|
|
|
# consolidate -- that's why 4 points in MultiPoint.
|
|
|
|
self.assertEqual(4, len(coll))
|
|
|
|
self.assertTrue(ref_geom.equals(coll))
|
2009-07-12 04:34:27 +08:00
|
|
|
|
2010-07-21 03:05:46 +08:00
|
|
|
def test15_invalid_select_related(self):
|
|
|
|
"Testing doing select_related on the related name manager of a unique FK. See #13934."
|
|
|
|
qs = Article.objects.select_related('author__article')
|
|
|
|
# This triggers TypeError when `get_default_columns` has no `local_only`
|
|
|
|
# keyword. The TypeError is swallowed if QuerySet is actually
|
|
|
|
# evaluated as list generation swallows TypeError in CPython.
|
2013-09-08 23:05:16 +08:00
|
|
|
str(qs.query)
|
2009-03-04 06:10:15 +08:00
|
|
|
|
2011-09-11 06:53:26 +08:00
|
|
|
def test16_annotated_date_queryset(self):
|
|
|
|
"Ensure annotated date querysets work if spatial backend is used. See #14648."
|
2011-09-11 08:00:15 +08:00
|
|
|
birth_years = [dt.year for dt in
|
2011-09-11 06:53:26 +08:00
|
|
|
list(Author.objects.annotate(num_books=Count('books')).dates('dob', 'year'))]
|
|
|
|
birth_years.sort()
|
|
|
|
self.assertEqual([1950, 1974], birth_years)
|
|
|
|
|
2010-12-02 13:58:21 +08:00
|
|
|
# TODO: Related tests for KML, GML, and distance lookups.
|