app/django/contrib/gis/tests/relatedapp/tests.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 import os, unittest
       
     2 from django.contrib.gis.geos import *
       
     3 from django.contrib.gis.tests.utils import no_mysql, postgis
       
     4 from django.conf import settings
       
     5 from models import City, Location
       
     6 
       
     7 cities = (('Aurora', 'TX', -97.516111, 33.058333),
       
     8           ('Roswell', 'NM', -104.528056, 33.387222),
       
     9           ('Kecksburg', 'PA',  -79.460734, 40.18476),
       
    10            )
       
    11 
       
    12 class RelatedGeoModelTest(unittest.TestCase):
       
    13     
       
    14     def test01_setup(self):
       
    15         "Setting up for related model tests."
       
    16         for name, state, lon, lat in cities:
       
    17             loc = Location(point=Point(lon, lat))
       
    18             loc.save()
       
    19             c = City(name=name, state=state, location=loc)
       
    20             c.save()
       
    21             
       
    22     def test02_select_related(self):
       
    23         "Testing `select_related` on geographic models (see #7126)."
       
    24         qs1 = City.objects.all()
       
    25         qs2 = City.objects.select_related()
       
    26         qs3 = City.objects.select_related('location')
       
    27 
       
    28         for qs in (qs1, qs2, qs3):
       
    29             for ref, c in zip(cities, qs):
       
    30                 nm, st, lon, lat = ref
       
    31                 self.assertEqual(nm, c.name)
       
    32                 self.assertEqual(st, c.state)
       
    33                 self.assertEqual(Point(lon, lat), c.location.point)
       
    34         
       
    35     @no_mysql
       
    36     def test03_transform_related(self):
       
    37         "Testing the `transform` GeoQuerySet method on related geographic models."
       
    38         # All the transformations are to state plane coordinate systems using
       
    39         # US Survey Feet (thus a tolerance of 0 implies error w/in 1 survey foot).
       
    40         if postgis:
       
    41             tol = 3
       
    42             nqueries = 4 # +1 for `postgis_lib_version`
       
    43         else:
       
    44             tol = 0
       
    45             nqueries = 3
       
    46             
       
    47         def check_pnt(ref, pnt):
       
    48             self.assertAlmostEqual(ref.x, pnt.x, tol)
       
    49             self.assertAlmostEqual(ref.y, pnt.y, tol)
       
    50             self.assertEqual(ref.srid, pnt.srid)
       
    51 
       
    52         # Turning on debug so we can manually verify the number of SQL queries issued.
       
    53         # DISABLED: the number of queries count testing mechanism is way too brittle.
       
    54         #dbg = settings.DEBUG
       
    55         #settings.DEBUG = True
       
    56         from django.db import connection
       
    57 
       
    58         # Each city transformed to the SRID of their state plane coordinate system.
       
    59         transformed = (('Kecksburg', 2272, 'POINT(1490553.98959621 314792.131023984)'),
       
    60                        ('Roswell', 2257, 'POINT(481902.189077221 868477.766629735)'),
       
    61                        ('Aurora', 2276, 'POINT(2269923.2484839 7069381.28722222)'),
       
    62                        )
       
    63 
       
    64         for name, srid, wkt in transformed:
       
    65             # Doing this implicitly sets `select_related` select the location.
       
    66             qs = list(City.objects.filter(name=name).transform(srid, field_name='location__point'))
       
    67             check_pnt(GEOSGeometry(wkt, srid), qs[0].location.point) 
       
    68         #settings.DEBUG= dbg
       
    69 
       
    70         # Verifying the number of issued SQL queries.
       
    71         #self.assertEqual(nqueries, len(connection.queries))
       
    72 
       
    73     @no_mysql
       
    74     def test04_related_aggregate(self):
       
    75         "Testing the `extent` and `unionagg` GeoQuerySet aggregates on related geographic models."
       
    76         if postgis:
       
    77             # One for all locations, one that excludes Roswell.
       
    78             all_extent = (-104.528060913086, 33.0583305358887,-79.4607315063477, 40.1847610473633)
       
    79             txpa_extent = (-97.51611328125, 33.0583305358887,-79.4607315063477, 40.1847610473633)
       
    80             e1 = City.objects.extent(field_name='location__point')
       
    81             e2 = City.objects.exclude(name='Roswell').extent(field_name='location__point')
       
    82             for ref, e in [(all_extent, e1), (txpa_extent, e2)]:
       
    83                 for ref_val, e_val in zip(ref, e): self.assertAlmostEqual(ref_val, e_val)
       
    84 
       
    85         # The second union is for a query that has something in the WHERE clause.
       
    86         ref_u1 = GEOSGeometry('MULTIPOINT(-104.528056 33.387222,-97.516111 33.058333,-79.460734 40.18476)', 4326)
       
    87         ref_u2 = GEOSGeometry('MULTIPOINT(-97.516111 33.058333,-79.460734 40.18476)', 4326)
       
    88         u1 = City.objects.unionagg(field_name='location__point')
       
    89         u2 = City.objects.exclude(name='Roswell').unionagg(field_name='location__point')
       
    90         self.assertEqual(ref_u1, u1)
       
    91         self.assertEqual(ref_u2, u2)
       
    92 
       
    93     # TODO: Related tests for KML, GML, and distance lookups.
       
    94         
       
    95 def suite():
       
    96     s = unittest.TestSuite()
       
    97     s.addTest(unittest.makeSuite(RelatedGeoModelTest))
       
    98     return s