mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Refs #33476 -- Reformatted code with Black.
This commit is contained in:
committed by
Mariusz Felisiak
parent
f68fa8b45d
commit
9c19aff7c7
@@ -15,7 +15,7 @@ class City(NamedModel):
|
||||
point = models.PointField(geography=True)
|
||||
|
||||
class Meta:
|
||||
app_label = 'geogapp'
|
||||
app_label = "geogapp"
|
||||
|
||||
|
||||
class Zipcode(NamedModel):
|
||||
@@ -28,7 +28,7 @@ class County(NamedModel):
|
||||
mpoly = models.MultiPolygonField(geography=True)
|
||||
|
||||
class Meta:
|
||||
app_label = 'geogapp'
|
||||
app_label = "geogapp"
|
||||
|
||||
def __str__(self):
|
||||
return ' County, '.join([self.name, self.state])
|
||||
return " County, ".join([self.name, self.state])
|
||||
|
||||
@@ -15,7 +15,7 @@ from .models import City, County, Zipcode
|
||||
|
||||
|
||||
class GeographyTest(TestCase):
|
||||
fixtures = ['initial']
|
||||
fixtures = ["initial"]
|
||||
|
||||
def test01_fixture_load(self):
|
||||
"Ensure geography features loaded properly."
|
||||
@@ -24,26 +24,28 @@ class GeographyTest(TestCase):
|
||||
@skipUnlessDBFeature("supports_distances_lookups", "supports_distance_geodetic")
|
||||
def test02_distance_lookup(self):
|
||||
"Testing distance lookup support on non-point geography fields."
|
||||
z = Zipcode.objects.get(code='77002')
|
||||
cities1 = list(City.objects
|
||||
.filter(point__distance_lte=(z.poly, D(mi=500)))
|
||||
.order_by('name')
|
||||
.values_list('name', flat=True))
|
||||
cities2 = list(City.objects
|
||||
.filter(point__dwithin=(z.poly, D(mi=500)))
|
||||
.order_by('name')
|
||||
.values_list('name', flat=True))
|
||||
z = Zipcode.objects.get(code="77002")
|
||||
cities1 = list(
|
||||
City.objects.filter(point__distance_lte=(z.poly, D(mi=500)))
|
||||
.order_by("name")
|
||||
.values_list("name", flat=True)
|
||||
)
|
||||
cities2 = list(
|
||||
City.objects.filter(point__dwithin=(z.poly, D(mi=500)))
|
||||
.order_by("name")
|
||||
.values_list("name", flat=True)
|
||||
)
|
||||
for cities in [cities1, cities2]:
|
||||
self.assertEqual(['Dallas', 'Houston', 'Oklahoma City'], cities)
|
||||
self.assertEqual(["Dallas", "Houston", "Oklahoma City"], cities)
|
||||
|
||||
def test04_invalid_operators_functions(self):
|
||||
"Ensuring exceptions are raised for operators & functions invalid on geography fields."
|
||||
if not connection.ops.postgis:
|
||||
self.skipTest('This is a PostGIS-specific test.')
|
||||
self.skipTest("This is a PostGIS-specific test.")
|
||||
# Only a subset of the geometry functions & operator are available
|
||||
# to PostGIS geography types. For more information, visit:
|
||||
# http://postgis.refractions.net/documentation/manual-1.5/ch08.html#PostGIS_GeographyFunctions
|
||||
z = Zipcode.objects.get(code='77002')
|
||||
z = Zipcode.objects.get(code="77002")
|
||||
# ST_Within not available.
|
||||
with self.assertRaises(ValueError):
|
||||
City.objects.filter(point__within=z.poly).count()
|
||||
@@ -52,7 +54,7 @@ class GeographyTest(TestCase):
|
||||
City.objects.filter(point__contained=z.poly).count()
|
||||
|
||||
# Regression test for #14060, `~=` was never really implemented for PostGIS.
|
||||
htown = City.objects.get(name='Houston')
|
||||
htown = City.objects.get(name="Houston")
|
||||
with self.assertRaises(ValueError):
|
||||
City.objects.get(point__exact=htown.point)
|
||||
|
||||
@@ -63,22 +65,26 @@ class GeographyTest(TestCase):
|
||||
from django.contrib.gis.utils import LayerMapping
|
||||
|
||||
# Getting the shapefile and mapping dictionary.
|
||||
shp_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'data'))
|
||||
co_shp = os.path.join(shp_path, 'counties', 'counties.shp')
|
||||
shp_path = os.path.realpath(
|
||||
os.path.join(os.path.dirname(__file__), "..", "data")
|
||||
)
|
||||
co_shp = os.path.join(shp_path, "counties", "counties.shp")
|
||||
co_mapping = {
|
||||
'name': 'Name',
|
||||
'state': 'State',
|
||||
'mpoly': 'MULTIPOLYGON',
|
||||
"name": "Name",
|
||||
"state": "State",
|
||||
"mpoly": "MULTIPOLYGON",
|
||||
}
|
||||
# Reference county names, number of polygons, and state names.
|
||||
names = ['Bexar', 'Galveston', 'Harris', 'Honolulu', 'Pueblo']
|
||||
names = ["Bexar", "Galveston", "Harris", "Honolulu", "Pueblo"]
|
||||
num_polys = [1, 2, 1, 19, 1] # Number of polygons for each.
|
||||
st_names = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado']
|
||||
st_names = ["Texas", "Texas", "Texas", "Hawaii", "Colorado"]
|
||||
|
||||
lm = LayerMapping(County, co_shp, co_mapping, source_srs=4269, unique='name')
|
||||
lm = LayerMapping(County, co_shp, co_mapping, source_srs=4269, unique="name")
|
||||
lm.save(silent=True, strict=True)
|
||||
|
||||
for c, name, num_poly, state in zip(County.objects.order_by('name'), names, num_polys, st_names):
|
||||
for c, name, num_poly, state in zip(
|
||||
County.objects.order_by("name"), names, num_polys, st_names
|
||||
):
|
||||
self.assertEqual(4326, c.mpoly.srid)
|
||||
self.assertEqual(num_poly, len(c.mpoly))
|
||||
self.assertEqual(name, c.name)
|
||||
@@ -86,7 +92,7 @@ class GeographyTest(TestCase):
|
||||
|
||||
|
||||
class GeographyFunctionTests(FuncTestMixin, TestCase):
|
||||
fixtures = ['initial']
|
||||
fixtures = ["initial"]
|
||||
|
||||
@skipUnlessDBFeature("supports_extent_aggr")
|
||||
def test_cast_aggregate(self):
|
||||
@@ -96,11 +102,16 @@ class GeographyFunctionTests(FuncTestMixin, TestCase):
|
||||
"""
|
||||
if not connection.features.supports_geography:
|
||||
self.skipTest("This test needs geography support")
|
||||
expected = (-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)
|
||||
res = City.objects.filter(
|
||||
name__in=('Houston', 'Dallas')
|
||||
).aggregate(extent=models.Extent(Cast('point', models.PointField())))
|
||||
for val, exp in zip(res['extent'], expected):
|
||||
expected = (
|
||||
-96.8016128540039,
|
||||
29.7633724212646,
|
||||
-95.3631439208984,
|
||||
32.782058715820,
|
||||
)
|
||||
res = City.objects.filter(name__in=("Houston", "Dallas")).aggregate(
|
||||
extent=models.Extent(Cast("point", models.PointField()))
|
||||
)
|
||||
for val, exp in zip(res["extent"], expected):
|
||||
self.assertAlmostEqual(exp, val, 4)
|
||||
|
||||
@skipUnlessDBFeature("has_Distance_function", "supports_distance_geodetic")
|
||||
@@ -119,10 +130,10 @@ class GeographyFunctionTests(FuncTestMixin, TestCase):
|
||||
ref_dists = [0, 4899.68, 8081.30, 9115.15]
|
||||
else:
|
||||
ref_dists = [0, 4891.20, 8071.64, 9123.95]
|
||||
htown = City.objects.get(name='Houston')
|
||||
htown = City.objects.get(name="Houston")
|
||||
qs = Zipcode.objects.annotate(
|
||||
distance=Distance('poly', htown.point),
|
||||
distance2=Distance(htown.point, 'poly'),
|
||||
distance=Distance("poly", htown.point),
|
||||
distance2=Distance(htown.point, "poly"),
|
||||
)
|
||||
for z, ref in zip(qs, ref_dists):
|
||||
self.assertAlmostEqual(z.distance.m, ref, 2)
|
||||
@@ -135,7 +146,7 @@ class GeographyFunctionTests(FuncTestMixin, TestCase):
|
||||
|
||||
if not connection.ops.spatialite:
|
||||
# Distance function combined with a lookup.
|
||||
hzip = Zipcode.objects.get(code='77002')
|
||||
hzip = Zipcode.objects.get(code="77002")
|
||||
self.assertEqual(qs.get(distance__lte=0), hzip)
|
||||
|
||||
@skipUnlessDBFeature("has_Area_function", "supports_area_geodetic")
|
||||
@@ -144,7 +155,7 @@ class GeographyFunctionTests(FuncTestMixin, TestCase):
|
||||
Testing that Area calculations work on geography columns.
|
||||
"""
|
||||
# SELECT ST_Area(poly) FROM geogapp_zipcode WHERE code='77002';
|
||||
z = Zipcode.objects.annotate(area=Area('poly')).get(code='77002')
|
||||
z = Zipcode.objects.annotate(area=Area("poly")).get(code="77002")
|
||||
# Round to the nearest thousand as possible values (depending on
|
||||
# the database and geolib) include 5439084, 5439100, 5439101.
|
||||
rounded_value = z.area.sq_m
|
||||
@@ -154,5 +165,7 @@ class GeographyFunctionTests(FuncTestMixin, TestCase):
|
||||
@skipUnlessDBFeature("has_Area_function")
|
||||
@skipIfDBFeature("supports_area_geodetic")
|
||||
def test_geodetic_area_raises_if_not_supported(self):
|
||||
with self.assertRaisesMessage(NotSupportedError, 'Area on geodetic coordinate systems not supported.'):
|
||||
Zipcode.objects.annotate(area=Area('poly')).get(code='77002')
|
||||
with self.assertRaisesMessage(
|
||||
NotSupportedError, "Area on geodetic coordinate systems not supported."
|
||||
):
|
||||
Zipcode.objects.annotate(area=Area("poly")).get(code="77002")
|
||||
|
||||
Reference in New Issue
Block a user