| 1 |
from datetime import datetime |
| 2 |
|
| 3 |
from django.contrib.gis.db.models import Extent |
| 4 |
from django.contrib.gis.shortcuts import render_to_kmz |
| 5 |
from django.db.models import Count, Min |
| 6 |
from django.test import TestCase, skipUnlessDBFeature |
| 7 |
|
| 8 |
from .models import City, PennsylvaniaCity, State, Truth |
| 9 |
|
| 10 |
|
| 11 |
class GeoRegressionTests(TestCase): |
| 12 |
fixtures = ["initial"] |
| 13 |
|
| 14 |
def test_update(self): |
| 15 |
"Testing QuerySet.update() (#10411)." |
| 16 |
pueblo = City.objects.get(name="Pueblo") |
| 17 |
bak = pueblo.point.clone() |
| 18 |
pueblo.point.y += 0.005 |
| 19 |
pueblo.point.x += 0.005 |
| 20 |
|
| 21 |
City.objects.filter(name="Pueblo").update(point=pueblo.point) |
| 22 |
pueblo.refresh_from_db() |
| 23 |
self.assertAlmostEqual(bak.y + 0.005, pueblo.point.y, 6) |
| 24 |
self.assertAlmostEqual(bak.x + 0.005, pueblo.point.x, 6) |
| 25 |
City.objects.filter(name="Pueblo").update(point=bak) |
| 26 |
pueblo.refresh_from_db() |
| 27 |
self.assertAlmostEqual(bak.y, pueblo.point.y, 6) |
| 28 |
self.assertAlmostEqual(bak.x, pueblo.point.x, 6) |
| 29 |
|
| 30 |
def test_kmz(self): |
| 31 |
"Testing `render_to_kmz` with non-ASCII data. See #11624." |
| 32 |
name = "Ă…land Islands" |
| 33 |
places = [ |
| 34 |
{ |
| 35 |
"name": name, |
| 36 |
"description": name, |
| 37 |
"kml": "<Point><coordinates>5.0,23.0</coordinates></Point>", |
| 38 |
} |
| 39 |
] |
| 40 |
render_to_kmz("gis/kml/placemarks.kml", {"places": places}) |
| 41 |
|
| 42 |
@skipUnlessDBFeature("supports_extent_aggr") |
| 43 |
def test_extent(self): |
| 44 |
"Testing `extent` on a table with a single point. See #11827." |
| 45 |
pnt = City.objects.get(name="Pueblo").point |
| 46 |
ref_ext = (pnt.x, pnt.y, pnt.x, pnt.y) |
| 47 |
extent = City.objects.filter(name="Pueblo").aggregate(Extent("point"))[ |
| 48 |
"point__extent" |
| 49 |
] |
| 50 |
for ref_val, val in zip(ref_ext, extent): |
| 51 |
self.assertAlmostEqual(ref_val, val, 4) |
| 52 |
|
| 53 |
def test_unicode_date(self): |
| 54 |
"Testing dates are converted properly, even on SpatiaLite. See #16408." |
| 55 |
founded = datetime(1857, 5, 23) |
| 56 |
PennsylvaniaCity.objects.create( |
| 57 |
name="Mansfield", |
| 58 |
county="Tioga", |
| 59 |
point="POINT(-77.071445 41.823881)", |
| 60 |
founded=founded, |
| 61 |
) |
| 62 |
self.assertEqual( |
| 63 |
founded, PennsylvaniaCity.objects.datetimes("founded", "day")[0] |
| 64 |
) |
| 65 |
self.assertEqual( |
| 66 |
founded, PennsylvaniaCity.objects.aggregate(Min("founded"))["founded__min"] |
| 67 |
) |
| 68 |
|
| 69 |
def test_empty_count(self): |
| 70 |
"Testing that PostGISAdapter.__eq__ does check empty strings. See #13670." |
| 71 |
# contrived example, but need a geo lookup paired with an id__in lookup |
| 72 |
pueblo = City.objects.get(name="Pueblo") |
| 73 |
state = State.objects.filter(poly__contains=pueblo.point) |
| 74 |
cities_within_state = City.objects.filter(id__in=state) |
| 75 |
|
| 76 |
# .count() should not throw TypeError in __eq__ |
| 77 |
self.assertEqual(cities_within_state.count(), 1) |
| 78 |
|
| 79 |
@skipUnlessDBFeature("allows_group_by_lob") |
| 80 |
def test_defer_or_only_with_annotate(self): |
| 81 |
"Regression for #16409. Make sure defer() and only() work with annotate()" |
| 82 |
self.assertIsInstance( |
| 83 |
list(City.objects.annotate(Count("point")).defer("name")), list |
| 84 |
) |
| 85 |
self.assertIsInstance( |
| 86 |
list(City.objects.annotate(Count("point")).only("name")), list |
| 87 |
) |
| 88 |
|
| 89 |
def test_boolean_conversion(self): |
| 90 |
"Testing Boolean value conversion with the spatial backend, see #15169." |
| 91 |
t1 = Truth.objects.create(val=True) |
| 92 |
t2 = Truth.objects.create(val=False) |
| 93 |
|
| 94 |
val1 = Truth.objects.get(pk=t1.pk).val |
| 95 |
val2 = Truth.objects.get(pk=t2.pk).val |
| 96 |
# verify types -- shouldn't be 0/1 |
| 97 |
self.assertIsInstance(val1, bool) |
| 98 |
self.assertIsInstance(val2, bool) |
| 99 |
# verify values |
| 100 |
self.assertIs(val1, True) |
| 101 |
self.assertIs(val2, False) |