|
1 | 1 | from django.contrib.gis.geos import Point |
| 2 | +from django.contrib.gis.measure import Distance |
2 | 3 | from django.db import NotSupportedError |
3 | 4 | from django.test import TestCase, skipUnlessDBFeature |
4 | 5 |
|
5 | | -from .models import City |
| 6 | +from .models import City, Zipcode |
6 | 7 |
|
7 | 8 |
|
8 | 9 | @skipUnlessDBFeature("gis_enabled") |
9 | 10 | class LookupTests(TestCase): |
10 | | - def test_unsupported_lookups(self): |
| 11 | + fixtures = ["initial"] |
| 12 | + |
| 13 | + def test_unsupported(self): |
11 | 14 | msg = "MongoDB does not support the 'same_as' lookup." |
12 | 15 | with self.assertRaisesMessage(NotSupportedError, msg): |
13 | 16 | City.objects.get(point__same_as=Point(95, 30)) |
14 | 17 |
|
15 | | - def test_within_lookup(self): |
16 | | - city = City.objects.create(point=Point(95, 30)) |
17 | | - qs = City.objects.filter(point__within=Point(95, 30).buffer(10)) |
18 | | - self.assertIn(city, qs) |
| 18 | + def test_contains(self): |
| 19 | + houston = City.objects.get(name="Houston") |
| 20 | + qs = City.objects.filter(point__contains=Point(-95.363151, 29.763374)) |
| 21 | + self.assertEqual(qs.count(), 1) |
| 22 | + self.assertEqual(houston, qs.first()) |
19 | 23 |
|
20 | | - def test_intersects_lookup(self): |
21 | | - city = City.objects.create(point=Point(95, 30)) |
22 | | - qs = City.objects.filter(point__intersects=Point(95, 30).buffer(10)) |
23 | | - self.assertIn(city, qs) |
24 | | - |
25 | | - def test_disjoint_lookup(self): |
26 | | - city = City.objects.create(point=Point(50, 30)) |
| 24 | + def test_disjoint(self): |
| 25 | + houston = City.objects.get(name="Houston") |
27 | 26 | qs = City.objects.filter(point__disjoint=Point(100, 50)) |
28 | | - self.assertIn(city, qs) |
| 27 | + self.assertIn(houston, qs) |
29 | 28 |
|
30 | | - def test_contains_lookup(self): |
31 | | - city = City.objects.create(point=Point(95, 30)) |
32 | | - qs = City.objects.filter(point__contains=Point(95, 30)) |
33 | | - self.assertIn(city, qs) |
| 29 | + def test_distance_gt(self): |
| 30 | + houston = City.objects.get(name="Houston") |
| 31 | + dallas = City.objects.get(name="Dallas") # Roughly ~363 km from Houston |
| 32 | + qs = City.objects.filter(point__distance_gt=(houston.point, 362826)) |
| 33 | + self.assertEqual(qs.count(), 6) |
| 34 | + self.assertNotIn(dallas, list(qs)) |
34 | 35 |
|
35 | | - def test_distance_gt_lookup(self): |
36 | | - city = City.objects.create(point=Point(95, 30)) |
37 | | - qs = City.objects.filter(point__distance_gt=(Point(0, 0), 100)) |
38 | | - self.assertIn(city, qs) |
| 36 | + def test_distance_gte(self): |
| 37 | + houston = City.objects.get(name="Houston") |
| 38 | + dallas = City.objects.get(name="Dallas") # Roughly ~363 km from Houston |
| 39 | + qs = City.objects.filter(point__distance_gte=(houston.point, 362825)) |
| 40 | + self.assertEqual(qs.count(), 7) |
| 41 | + self.assertIn(dallas, list(qs)) |
39 | 42 |
|
40 | | - def test_distance_lt_lookup(self): |
41 | | - city = City.objects.create(point=Point(40.7589, -73.9851)) |
42 | | - qs = City.objects.filter(point__distance_lt=(Point(40.7670, -73.9820), 1000)) |
43 | | - self.assertIn(city, qs) |
| 43 | + def test_distance_lt(self): |
| 44 | + houston = City.objects.get(name="Houston") |
| 45 | + qs = City.objects.filter(point__distance_lt=(houston.point, 362825)) |
| 46 | + self.assertEqual(qs.count(), 1) |
| 47 | + self.assertEqual(houston, qs.first()) |
44 | 48 |
|
45 | | - def test_distance_gte_lookup(self): |
46 | | - city = City.objects.create(point=Point(95, 30)) |
47 | | - qs = City.objects.filter(point__distance_gt=(Point(0, 0), 100)) |
48 | | - self.assertIn(city, qs) |
| 49 | + def test_distance_lte(self): |
| 50 | + houston = City.objects.get(name="Houston") |
| 51 | + dallas = City.objects.get(name="Dallas") # Roughly ~363 km from Houston |
| 52 | + qs = City.objects.filter(point__distance_lte=(houston.point, 362826)) |
| 53 | + self.assertEqual(qs.count(), 2) |
| 54 | + self.assertEqual([houston, dallas], list(qs)) |
49 | 55 |
|
50 | | - def test_distance_lte_lookup(self): |
51 | | - city = City.objects.create(point=Point(40.7589, -73.9851)) |
52 | | - qs = City.objects.filter(point__distance_lt=(Point(40.7670, -73.9820), 1000)) |
53 | | - self.assertIn(city, qs) |
| 56 | + def test_distance_units(self): |
| 57 | + chicago = City.objects.get(name="Chicago") |
| 58 | + lawrence = City.objects.get(name="Lawrence") |
| 59 | + qs = City.objects.filter(point__distance_lt=(chicago.point, Distance(km=720))) |
| 60 | + self.assertEqual(qs.count(), 2) |
| 61 | + self.assertEqual([lawrence, chicago], list(qs)) |
| 62 | + qs = City.objects.filter(point__distance_lt=(chicago.point, Distance(mi=447))) |
| 63 | + self.assertEqual(qs.count(), 2) |
| 64 | + self.assertEqual([lawrence, chicago], list(qs)) |
54 | 65 |
|
55 | | - def test_dwithin_lookup(self): |
56 | | - city = City.objects.create(point=Point(40.7589, -73.9851)) |
57 | | - qs = City.objects.filter(point__dwithin=(Point(40.7670, -73.9820), 1000)) |
| 66 | + def test_dwithin(self): |
| 67 | + houston = City.objects.get(name="Houston") |
| 68 | + qs = City.objects.filter(point__dwithin=(houston.point, 0.2)) |
| 69 | + self.assertEqual(qs.count(), 5) |
| 70 | + |
| 71 | + def test_dwithin_unsupported_units(self): |
| 72 | + qs = City.objects.filter(point__dwithin=(Point(40.7670, -73.9820), Distance(km=1))) |
| 73 | + with self.assertRaisesMessage( |
| 74 | + ValueError, |
| 75 | + "Only numeric values of radian units are allowed on geodetic distance queries.", |
| 76 | + ): |
| 77 | + qs.first() |
| 78 | + |
| 79 | + def test_intersects(self): |
| 80 | + city = City.objects.create(point=Point(95, 30)) |
| 81 | + qs = City.objects.filter(point__intersects=Point(95, 30).buffer(10)) |
| 82 | + self.assertEqual(qs.count(), 1) |
58 | 83 | self.assertIn(city, qs) |
| 84 | + |
| 85 | + def test_within(self): |
| 86 | + zipcode = Zipcode.objects.get(code="77002") |
| 87 | + qs = City.objects.filter(point__within=zipcode.poly).values_list("name", flat=True) |
| 88 | + self.assertEqual(["Houston"], list(qs)) |
0 commit comments