| 1 |
""" |
| 2 |
unit tests for restframework_gis |
| 3 |
""" |
| 4 |
|
| 5 |
import json |
| 6 |
import pickle |
| 7 |
from html import escape |
| 8 |
|
| 9 |
from django.contrib.gis.geos import GEOSGeometry, Point |
| 10 |
from django.core.exceptions import ImproperlyConfigured |
| 11 |
from django.test import TestCase |
| 12 |
from django.urls import reverse |
| 13 |
|
| 14 |
from rest_framework_gis import serializers as gis_serializers |
| 15 |
from rest_framework_gis.fields import GeoJsonDict |
| 16 |
|
| 17 |
from .models import LocatedFile, Location, Nullable, OtherSridLocation |
| 18 |
from .serializers import LocationGeoSerializer |
| 19 |
|
| 20 |
|
| 21 |
class TestRestFrameworkGis(TestCase): |
| 22 |
def setUp(self): |
| 23 |
self.location_list_url = reverse("api_location_list") |
| 24 |
self.geojson_location_list_url = reverse("api_geojson_location_list") |
| 25 |
self.geos_error_message = ( |
| 26 |
"Invalid format: string or unicode input unrecognized as GeoJSON," |
| 27 |
" WKT EWKT or HEXEWKB." |
| 28 |
) |
| 29 |
self.gdal_error_message = ( |
| 30 |
"Unable to convert to python object:" |
| 31 |
' Invalid geometry pointer returned from "OGR_G_CreateGeometryFromJson".' |
| 32 |
) |
| 33 |
self.value_error_message = ( |
| 34 |
"Unable to convert to python object:" |
| 35 |
" String input unrecognized as WKT EWKT, and HEXEWKB." |
| 36 |
) |
| 37 |
self.type_error_message = ( |
| 38 |
"Unable to convert to python object: Improper geometry input type:" |
| 39 |
) |
| 40 |
|
| 41 |
def _create_locations(self): |
| 42 |
self.l1 = Location.objects.create( |
| 43 |
id=1, |
| 44 |
name="l1", |
| 45 |
slug="l1", |
| 46 |
geometry="POINT (13.0078125000020002 42.4234565179379999)", |
| 47 |
) |
| 48 |
self.l2 = Location.objects.create( |
| 49 |
id=2, |
| 50 |
name="l2", |
| 51 |
slug="l2", |
| 52 |
geometry="POINT (12.0078125000020002 43.4234565179379999)", |
| 53 |
) |
| 54 |
|
| 55 |
def test_get_location_list(self): |
| 56 |
response = self.client.get(self.location_list_url) |
| 57 |
self.assertEqual(response.status_code, 200) |
| 58 |
|
| 59 |
def test_post_location_list_geojson(self): |
| 60 |
self.assertEqual(Location.objects.count(), 0) |
| 61 |
data = { |
| 62 |
"name": "geojson input test", |
| 63 |
"geometry": { |
| 64 |
"type": "GeometryCollection", |
| 65 |
"geometries": [ |
| 66 |
{"type": "Point", "coordinates": [12.492324113849, 41.890307434153]} |
| 67 |
], |
| 68 |
}, |
| 69 |
} |
| 70 |
response = self.client.post( |
| 71 |
self.location_list_url, |
| 72 |
data=json.dumps(data), |
| 73 |
content_type="application/json", |
| 74 |
) |
| 75 |
self.assertEqual(response.status_code, 201) |
| 76 |
self.assertEqual(Location.objects.count(), 1) |
| 77 |
data = { |
| 78 |
"name": "geojson input test2", |
| 79 |
"geometry": { |
| 80 |
"type": "Point", |
| 81 |
"coordinates": [12.492324113849, 41.890307434153], |
| 82 |
}, |
| 83 |
} |
| 84 |
response = self.client.post( |
| 85 |
self.location_list_url, |
| 86 |
data=json.dumps(data), |
| 87 |
content_type="application/json", |
| 88 |
) |
| 89 |
self.assertEqual(response.status_code, 201) |
| 90 |
self.assertEqual(Location.objects.count(), 2) |
| 91 |
|
| 92 |
def test_post_location_list_geojson_as_multipartformdata(self): |
| 93 |
"""emulate sending geojson string in webform""" |
| 94 |
self.assertEqual(Location.objects.count(), 0) |
| 95 |
data = { |
| 96 |
"name": "geojson input test", |
| 97 |
"geometry": json.dumps( |
| 98 |
{ |
| 99 |
"type": "GeometryCollection", |
| 100 |
"geometries": [ |
| 101 |
{ |
| 102 |
"type": "Point", |
| 103 |
"coordinates": [12.492324113849, 41.890307434153], |
| 104 |
} |
| 105 |
], |
| 106 |
} |
| 107 |
), |
| 108 |
} |
| 109 |
response = self.client.post(self.location_list_url, data) |
| 110 |
self.assertEqual(response.status_code, 201) |
| 111 |
self.assertEqual(Location.objects.count(), 1) |
| 112 |
|
| 113 |
def test_post_HTML_browsable_api(self): |
| 114 |
self.assertEqual(Location.objects.count(), 0) |
| 115 |
data = { |
| 116 |
"name": "geojson input test2", |
| 117 |
"slug": "prova", |
| 118 |
"geometry": json.dumps( |
| 119 |
{ |
| 120 |
"type": "GeometryCollection", |
| 121 |
"geometries": [ |
| 122 |
{ |
| 123 |
"type": "Point", |
| 124 |
"coordinates": [12.492324113849, 41.890307434153], |
| 125 |
} |
| 126 |
], |
| 127 |
} |
| 128 |
), |
| 129 |
} |
| 130 |
response = self.client.post( |
| 131 |
self.location_list_url, data, headers={"accept": "text/html"} |
| 132 |
) |
| 133 |
self.assertEqual(response.status_code, 201) |
| 134 |
self.assertEqual(Location.objects.count(), 1) |
| 135 |
location = Location.objects.all()[0] |
| 136 |
self.assertEqual(location.name, "geojson input test2") |
| 137 |
self.assertEqual(location.slug, "prova") |
| 138 |
|
| 139 |
def test_post_location_list_WKT(self): |
| 140 |
self.assertEqual(Location.objects.count(), 0) |
| 141 |
data = { |
| 142 |
"name": "WKT input test", |
| 143 |
"geometry": "POINT (12.492324113849 41.890307434153)", |
| 144 |
} |
| 145 |
response = self.client.post(self.location_list_url, data) |
| 146 |
self.assertEqual(response.status_code, 201) |
| 147 |
self.assertEqual(Location.objects.count(), 1) |
| 148 |
|
| 149 |
def test_post_location_list_EWKT(self): |
| 150 |
self.assertEqual(Location.objects.count(), 0) |
| 151 |
data = { |
| 152 |
"name": "EWKT input test", |
| 153 |
"geometry": "SRID=28992;POINT(221160 600204)", |
| 154 |
} |
| 155 |
response = self.client.post(self.location_list_url, data) |
| 156 |
expected_coords = (6.381495826183805, 53.384066927384985) |
| 157 |
self.assertEqual(response.status_code, 201) |
| 158 |
self.assertEqual(Location.objects.count(), 1) |
| 159 |
for lat, lon in zip( |
| 160 |
Location.objects.get(name="EWKT input test").geometry.coords, |
| 161 |
expected_coords, |
| 162 |
): |
| 163 |
self.assertAlmostEqual(lat, lon, places=5) |
| 164 |
|
| 165 |
def test_post_location_list_WKT_as_json(self): |
| 166 |
self.assertEqual(Location.objects.count(), 0) |
| 167 |
data = { |
| 168 |
"name": "WKT input test", |
| 169 |
"geometry": "POINT (12.492324113849 41.890307434153)", |
| 170 |
} |
| 171 |
response = self.client.post( |
| 172 |
self.location_list_url, |
| 173 |
data=json.dumps(data), |
| 174 |
content_type="application/json", |
| 175 |
) |
| 176 |
self.assertEqual(response.status_code, 201) |
| 177 |
self.assertEqual(Location.objects.count(), 1) |
| 178 |
|
| 179 |
def test_post_location_list_empty_geometry(self): |
| 180 |
data = {"name": "empty input test"} |
| 181 |
response = self.client.post(self.location_list_url, data) |
| 182 |
self.assertEqual(response.data["geometry"][0], "This field is required.") |
| 183 |
data = {"name": "empty input test", "geometry": ""} |
| 184 |
response = self.client.post(self.location_list_url, data) |
| 185 |
self.assertEqual(response.data["geometry"][0], "This field is required.") |
| 186 |
data = {"name": "empty input test"} |
| 187 |
response = self.client.post( |
| 188 |
self.location_list_url, |
| 189 |
data=json.dumps(data), |
| 190 |
content_type="application/json", |
| 191 |
) |
| 192 |
self.assertEqual(response.data["geometry"][0], "This field is required.") |
| 193 |
data = {"name": "empty input test", "geometry": ""} |
| 194 |
response = self.client.post( |
| 195 |
self.location_list_url, |
| 196 |
data=json.dumps(data), |
| 197 |
content_type="application/json", |
| 198 |
) |
| 199 |
self.assertEqual(response.data["geometry"][0], "This field is required.") |
| 200 |
|
| 201 |
def test_post_location_list_invalid_WKT(self): |
| 202 |
data = {"name": "WKT wrong input test", "geometry": "I AM OBVIOUSLY WRONG"} |
| 203 |
response = self.client.post( |
| 204 |
self.location_list_url, |
| 205 |
data=json.dumps(data), |
| 206 |
content_type="application/json", |
| 207 |
) |
| 208 |
self.assertEqual(response.status_code, 400) |
| 209 |
self.assertEqual(Location.objects.count(), 0) |
| 210 |
self.assertEqual(response.data["geometry"][0], self.value_error_message) |
| 211 |
# repeat as multipart form data |
| 212 |
response = self.client.post(self.location_list_url, data) |
| 213 |
self.assertEqual(response.data["geometry"][0], self.value_error_message) |
| 214 |
data = { |
| 215 |
"name": "I AM MODERATELY WRONG", |
| 216 |
"geometry": "POINT (12.492324113849, 41.890307434153)", |
| 217 |
} |
| 218 |
response = self.client.post( |
| 219 |
self.location_list_url, |
| 220 |
data=json.dumps(data), |
| 221 |
content_type="application/json", |
| 222 |
) |
| 223 |
self.assertEqual(response.data["geometry"][0], self.geos_error_message) |
| 224 |
# repeat as multipart form data |
| 225 |
response = self.client.post(self.location_list_url, data) |
| 226 |
self.assertEqual(response.data["geometry"][0], self.geos_error_message) |
| 227 |
|
| 228 |
def test_post_location_list_invalid_geojson(self): |
| 229 |
data = { |
| 230 |
"name": "quite wrong", |
| 231 |
"geometry": { |
| 232 |
"type": "ARRRR", |
| 233 |
"dasdas": [{"STtype": "PTUAMAoint", "NNAare": "rgon"}], |
| 234 |
}, |
| 235 |
} |
| 236 |
response = self.client.post( |
| 237 |
self.location_list_url, |
| 238 |
data=json.dumps(data), |
| 239 |
content_type="application/json", |
| 240 |
) |
| 241 |
self.assertEqual(response.data["geometry"][0], self.gdal_error_message) |
| 242 |
data = {"name": "very wrong", "geometry": ["a", "b", "c"]} |
| 243 |
response = self.client.post( |
| 244 |
self.location_list_url, |
| 245 |
data=json.dumps(data), |
| 246 |
content_type="application/json", |
| 247 |
) |
| 248 |
self.assertEqual(response.data["geometry"][0][0:65], self.type_error_message) |
| 249 |
data = {"name": "very wrong", "geometry": False} |
| 250 |
response = self.client.post( |
| 251 |
self.location_list_url, |
| 252 |
data=json.dumps(data), |
| 253 |
content_type="application/json", |
| 254 |
) |
| 255 |
self.assertEqual(response.data["geometry"][0][0:65], self.type_error_message) |
| 256 |
data = {"name": "very wrong", "geometry": {"value": {"nested": ["yo"]}}} |
| 257 |
response = self.client.post( |
| 258 |
self.location_list_url, |
| 259 |
data=json.dumps(data), |
| 260 |
content_type="application/json", |
| 261 |
) |
| 262 |
self.assertEqual(response.data["geometry"][0], self.gdal_error_message) |
| 263 |
|
| 264 |
def test_geojson_format(self): |
| 265 |
"""test geojson format""" |
| 266 |
location = Location.objects.create( |
| 267 |
name="geojson test", geometry="POINT (135.0 45.0)" |
| 268 |
) |
| 269 |
url = reverse("api_geojson_location_details", args=[location.id]) |
| 270 |
expected = { |
| 271 |
"id": location.id, |
| 272 |
"type": "Feature", |
| 273 |
"properties": { |
| 274 |
"details": "http://testserver/geojson/%s/" % location.id, |
| 275 |
"name": "geojson test", |
| 276 |
"fancy_name": "Kool geojson test", |
| 277 |
"timestamp": None, |
| 278 |
"slug": "geojson-test", |
| 279 |
}, |
| 280 |
"geometry": {"type": "Point", "coordinates": [135.0, 45.0]}, |
| 281 |
} |
| 282 |
response = self.client.get(url) |
| 283 |
self.assertCountEqual(json.dumps(response.data), json.dumps(expected)) |
| 284 |
response = self.client.get(url, headers={"accept": "text/html"}) |
| 285 |
self.assertContains(response, "Kool geojson test") |
| 286 |
|
| 287 |
def test_geojson_id_attribute(self): |
| 288 |
location = Location.objects.create( |
| 289 |
name="geojson test", geometry="POINT (10.1 10.1)" |
| 290 |
) |
| 291 |
url = reverse("api_geojson_location_details", args=[location.id]) |
| 292 |
response = self.client.get(url) |
| 293 |
self.assertEqual(response.data["id"], location.id) |
| 294 |
|
| 295 |
def test_geojson_id_attribute_slug(self): |
| 296 |
location = Location.objects.create( |
| 297 |
name="geojson test", geometry="POINT (10.1 10.1)" |
| 298 |
) |
| 299 |
url = reverse("api_geojson_location_slug_details", args=[location.slug]) |
| 300 |
response = self.client.get(url) |
| 301 |
self.assertEqual(response.data["id"], location.slug) |
| 302 |
|
| 303 |
def test_geojson_false_id_attribute_slug(self): |
| 304 |
location = Location.objects.create( |
| 305 |
name="falseid test", geometry="POINT (10.1 10.1)" |
| 306 |
) |
| 307 |
url = reverse("api_geojson_location_falseid_details", args=[location.id]) |
| 308 |
response = self.client.get(url) |
| 309 |
self.assertEqual(response.data["properties"]["name"], "falseid test") |
| 310 |
with self.assertRaises(KeyError): |
| 311 |
response.data["id"] |
| 312 |
|
| 313 |
def test_geojson_srid_transforms_to_wgs84(self): |
| 314 |
location = OtherSridLocation.objects.create( |
| 315 |
name="other SRID location", |
| 316 |
geometry="POINT(625826.2376404074 483198.2074507246)", |
| 317 |
) |
| 318 |
url = reverse("api_other_srid_location_details", args=[location.id]) |
| 319 |
response = self.client.get(url) |
| 320 |
expected_coords = (16.372500007573713, 48.20833306345481) |
| 321 |
expected_coords_bbox = ( |
| 322 |
16.372500007573713, |
| 323 |
48.20833306345481, |
| 324 |
16.372500007573713, |
| 325 |
48.20833306345481, |
| 326 |
) |
| 327 |
self.assertEqual(response.data["properties"]["name"], "other SRID location") |
| 328 |
for received, expected in zip( |
| 329 |
response.data["geometry"]["coordinates"], |
| 330 |
expected_coords, |
| 331 |
): |
| 332 |
self.assertAlmostEqual(received, expected, places=5) |
| 333 |
|
| 334 |
for received, expected in zip( |
| 335 |
response.data["geometry"]["bbox"], |
| 336 |
expected_coords_bbox, |
| 337 |
): |
| 338 |
self.assertAlmostEqual(received, expected, places=5) |
| 339 |
|
| 340 |
def test_post_geojson_id_attribute(self): |
| 341 |
self.assertEqual(Location.objects.count(), 0) |
| 342 |
data = { |
| 343 |
"type": "Feature", |
| 344 |
"id": "42", |
| 345 |
"properties": {"name": "point"}, |
| 346 |
"geometry": {"type": "Point", "coordinates": [10.1, 10.1]}, |
| 347 |
} |
| 348 |
url = reverse("api_geojson_location_writable_id_list") |
| 349 |
response = self.client.post( |
| 350 |
url, |
| 351 |
data=json.dumps(data), |
| 352 |
content_type="application/json", |
| 353 |
) |
| 354 |
self.assertEqual(response.status_code, 201) |
| 355 |
self.assertEqual(Location.objects.count(), 1) |
| 356 |
self.assertEqual(Location.objects.first().id, 42) |
| 357 |
|
| 358 |
def test_geojson_no_id_attribute_slug(self): |
| 359 |
location = Location.objects.create( |
| 360 |
name="noid test", geometry="POINT (10.1 10.1)" |
| 361 |
) |
| 362 |
url = reverse("api_geojson_location_noid_details", args=[location.id]) |
| 363 |
response = self.client.get(url) |
| 364 |
self.assertEqual(response.data["properties"]["name"], "noid test") |
| 365 |
with self.assertRaises(KeyError): |
| 366 |
response.data["id"] |
| 367 |
|
| 368 |
def test_geojson_filefield_attribute(self): |
| 369 |
located_file = LocatedFile.objects.create( |
| 370 |
name="geojson filefield test", geometry="POINT (10.1 10.1)" |
| 371 |
) |
| 372 |
url = reverse("api_geojson_located_file_details", args=[located_file.id]) |
| 373 |
response = self.client.get(url) |
| 374 |
self.assertEqual(response.data["properties"]["file"], None) |
| 375 |
|
| 376 |
def test_post_geojson_location_list(self): |
| 377 |
self.assertEqual(Location.objects.count(), 0) |
| 378 |
data = { |
| 379 |
"type": "Feature", |
| 380 |
"properties": {"name": "point?", "details": "ignore this"}, |
| 381 |
"geometry": {"type": "Point", "coordinates": [10.1, 10.1]}, |
| 382 |
} |
| 383 |
response = self.client.post( |
| 384 |
self.geojson_location_list_url, |
| 385 |
data=json.dumps(data), |
| 386 |
content_type="application/json", |
| 387 |
) |
| 388 |
self.assertEqual(response.status_code, 201) |
| 389 |
self.assertEqual(Location.objects.count(), 1) |
| 390 |
url = reverse( |
| 391 |
"api_geojson_location_details", |
| 392 |
args=[Location.objects.order_by("-id")[0].id], |
| 393 |
) |
| 394 |
response = self.client.get(url) |
| 395 |
self.assertEqual(response.data["properties"]["name"], "point?") |
| 396 |
self.assertEqual(response.data["geometry"]["type"], "Point") |
| 397 |
self.assertEqual( |
| 398 |
json.dumps(response.data["geometry"]["coordinates"]), "[10.1, 10.1]" |
| 399 |
) |
| 400 |
self.assertNotEqual(response.data["properties"]["details"], "ignore this") |
| 401 |
|
| 402 |
def test_post_geojson_location_list_HTML(self): |
| 403 |
self.assertEqual(Location.objects.count(), 0) |
| 404 |
data = { |
| 405 |
"type": "Feature", |
| 406 |
"properties": {"name": "point?", "details": "ignore this"}, |
| 407 |
"geometry": {"type": "Point", "coordinates": [10.1, 10.1]}, |
| 408 |
} |
| 409 |
response = self.client.post( |
| 410 |
self.geojson_location_list_url, |
| 411 |
data=json.dumps(data), |
| 412 |
content_type="application/json", |
| 413 |
headers={"accept": "text/html"}, |
| 414 |
) |
| 415 |
self.assertEqual(response.status_code, 201) |
| 416 |
self.assertEqual(Location.objects.count(), 1) |
| 417 |
url = reverse( |
| 418 |
"api_geojson_location_details", |
| 419 |
args=[Location.objects.order_by("-id")[0].id], |
| 420 |
) |
| 421 |
response = self.client.get(url) |
| 422 |
self.assertEqual(response.data["properties"]["name"], "point?") |
| 423 |
self.assertEqual(response.data["geometry"]["type"], "Point") |
| 424 |
self.assertEqual( |
| 425 |
json.dumps(response.data["geometry"]["coordinates"]), "[10.1, 10.1]" |
| 426 |
) |
| 427 |
self.assertNotEqual(response.data["properties"]["details"], "ignore this") |
| 428 |
|
| 429 |
def test_post_invalid_geojson_location_list(self): |
| 430 |
data = { |
| 431 |
"type": "Feature", |
| 432 |
"properties": {"details": "ignore this"}, |
| 433 |
"geometry": {"type": "Point", "coordinates": [10.1, 10.1]}, |
| 434 |
} |
| 435 |
response = self.client.post( |
| 436 |
self.geojson_location_list_url, |
| 437 |
data=json.dumps(data), |
| 438 |
content_type="application/json", |
| 439 |
) |
| 440 |
self.assertEqual(response.status_code, 400) |
| 441 |
self.assertEqual(Location.objects.count(), 0) |
| 442 |
self.assertEqual(response.data["name"][0], "This field is required.") |
| 443 |
data = { |
| 444 |
"type": "Feature", |
| 445 |
"properties": {"name": "point?"}, |
| 446 |
"geometry": {"type": "Point", "WRONG": {}}, |
| 447 |
} |
| 448 |
response = self.client.post( |
| 449 |
self.geojson_location_list_url, |
| 450 |
data=json.dumps(data), |
| 451 |
content_type="application/json", |
| 452 |
) |
| 453 |
self.assertEqual(response.status_code, 400) |
| 454 |
self.assertEqual(Location.objects.count(), 0) |
| 455 |
self.assertEqual(response.data["geometry"][0], self.gdal_error_message) |
| 456 |
|
| 457 |
def test_post_geojson_location_list_WKT(self): |
| 458 |
self.assertEqual(Location.objects.count(), 0) |
| 459 |
data = { |
| 460 |
"type": "Feature", |
| 461 |
"properties": {"name": "point?"}, |
| 462 |
"geometry": "POINT (10.1 10.1)", |
| 463 |
} |
| 464 |
response = self.client.post( |
| 465 |
self.geojson_location_list_url, |
| 466 |
data=json.dumps(data), |
| 467 |
content_type="application/json", |
| 468 |
) |
| 469 |
self.assertEqual(response.status_code, 201) |
| 470 |
self.assertEqual(Location.objects.count(), 1) |
| 471 |
url = reverse( |
| 472 |
"api_geojson_location_details", |
| 473 |
args=[Location.objects.order_by("-id")[0].id], |
| 474 |
) |
| 475 |
response = self.client.get(url) |
| 476 |
self.assertEqual(response.data["properties"]["name"], "point?") |
| 477 |
self.assertEqual(response.data["geometry"]["type"], "Point") |
| 478 |
self.assertEqual( |
| 479 |
json.dumps(response.data["geometry"]["coordinates"]), "[10.1, 10.1]" |
| 480 |
) |
| 481 |
|
| 482 |
def test_geofeatured_model_serializer_compatible_with_geomodel_serializer(self): |
| 483 |
self.assertEqual(Location.objects.count(), 0) |
| 484 |
data = { |
| 485 |
"name": "geojson input test", |
| 486 |
"geometry": { |
| 487 |
"type": "GeometryCollection", |
| 488 |
"geometries": [ |
| 489 |
{"type": "Point", "coordinates": [12.492324113849, 41.890307434153]} |
| 490 |
], |
| 491 |
}, |
| 492 |
} |
| 493 |
response = self.client.post( |
| 494 |
self.geojson_location_list_url, |
| 495 |
data=json.dumps(data), |
| 496 |
content_type="application/json", |
| 497 |
) |
| 498 |
self.assertEqual(response.status_code, 201) |
| 499 |
self.assertEqual(Location.objects.count(), 1) |
| 500 |
|
| 501 |
def test_geofeatured_model_post_as_multipartformdata(self): |
| 502 |
"""emulate sending geojson string in webform""" |
| 503 |
self.assertEqual(Location.objects.count(), 0) |
| 504 |
data = { |
| 505 |
"name": "geojson input test", |
| 506 |
"geometry": json.dumps( |
| 507 |
{"type": "Point", "coordinates": [12.492324113849, 41.890307434153]} |
| 508 |
), |
| 509 |
} |
| 510 |
response = self.client.post(self.location_list_url, data) |
| 511 |
self.assertEqual(response.status_code, 201) |
| 512 |
self.assertEqual(Location.objects.count(), 1) |
| 513 |
self.assertEqual(response.data["geometry"]["type"], "Point") |
| 514 |
|
| 515 |
def test_HTML_browsable_geojson_location_list(self): |
| 516 |
response = self.client.get( |
| 517 |
self.geojson_location_list_url, headers={"accept": "text/html"} |
| 518 |
) |
| 519 |
self.assertEqual(response.status_code, 200) |
| 520 |
self._create_locations() |
| 521 |
response = self.client.get( |
| 522 |
self.geojson_location_list_url, headers={"accept": "text/html"} |
| 523 |
) |
| 524 |
self.assertContains(response, "l1") |
| 525 |
self.assertContains(response, "l2") |
| 526 |
|
| 527 |
def test_post_geojson_location_list_HTML_web_form(self): |
| 528 |
self.assertEqual(Location.objects.count(), 0) |
| 529 |
data = { |
| 530 |
"name": "HTML test", |
| 531 |
"geometry": json.dumps({"type": "Point", "coordinates": [10.1, 10.1]}), |
| 532 |
} |
| 533 |
response = self.client.post( |
| 534 |
self.geojson_location_list_url, data, headers={"accept": "text/html"} |
| 535 |
) |
| 536 |
self.assertEqual(response.status_code, 201) |
| 537 |
self.assertEqual(Location.objects.count(), 1) |
| 538 |
location = Location.objects.all()[0] |
| 539 |
self.assertEqual(location.name, "HTML test") |
| 540 |
self.assertEqual(location.geometry.geom_type, "Point") |
| 541 |
|
| 542 |
def test_post_geojson_location_list_HTML_web_form_WKT(self): |
| 543 |
self.assertEqual(Location.objects.count(), 0) |
| 544 |
data = {"name": "HTML test WKT", "geometry": "POINT (10.1 10.1)"} |
| 545 |
response = self.client.post( |
| 546 |
self.geojson_location_list_url, data, headers={"accept": "text/html"} |
| 547 |
) |
| 548 |
self.assertEqual(response.status_code, 201) |
| 549 |
self.assertEqual(Location.objects.count(), 1) |
| 550 |
location = Location.objects.all()[0] |
| 551 |
self.assertEqual(location.name, "HTML test WKT") |
| 552 |
|
| 553 |
def test_geojson_HTML_widget_value(self): |
| 554 |
self._create_locations() |
| 555 |
response = self.client.get( |
| 556 |
self.geojson_location_list_url, headers={"accept": "text/html"} |
| 557 |
) |
| 558 |
self.assertContains(response, '<textarea name="geometry"') |
| 559 |
self.assertContains(response, escape('"type": "Point"')) |
| 560 |
self.assertContains(response, escape('"coordinates": [')) |
| 561 |
|
| 562 |
def test_patch_geojson_location(self): |
| 563 |
location = Location.objects.create( |
| 564 |
name="geojson patch test", geometry="POINT (135.0 45.0)" |
| 565 |
) |
| 566 |
url = reverse("api_geojson_location_details", args=[location.id]) |
| 567 |
data = { |
| 568 |
"properties": {"name": "geojson successful patch test"}, |
| 569 |
"geometry": {"type": "Point", "coordinates": [10.1, 10.1]}, |
| 570 |
} |
| 571 |
response = self.client.generic( |
| 572 |
"PATCH", url, json.dumps(data), content_type="application/json" |
| 573 |
) |
| 574 |
self.assertEqual(response.status_code, 200) |
| 575 |
location_reloaded = Location.objects.get(pk=location.id) |
| 576 |
self.assertEqual(location_reloaded.name, "geojson successful patch test") |
| 577 |
self.assertEqual( |
| 578 |
location_reloaded.geometry, Point(10.1, 10.1, srid=location.geometry.srid) |
| 579 |
) |
| 580 |
|
| 581 |
def test_patch_geojson_location_wo_changing_geometry(self): |
| 582 |
location = Location.objects.create( |
| 583 |
name="geojson patch test", geometry="POINT (135.0 45.0)" |
| 584 |
) |
| 585 |
url = reverse("api_geojson_location_details", args=[location.id]) |
| 586 |
data = {"properties": {"name": "geojson successful patch test"}} |
| 587 |
response = self.client.generic( |
| 588 |
"PATCH", url, json.dumps(data), content_type="application/json" |
| 589 |
) |
| 590 |
self.assertEqual(response.status_code, 200) |
| 591 |
location_reloaded = Location.objects.get(pk=location.id) |
| 592 |
self.assertEqual(location_reloaded.name, "geojson successful patch test") |
| 593 |
self.assertEqual( |
| 594 |
location_reloaded.geometry, Point(135.0, 45.0, srid=location.geometry.srid) |
| 595 |
) |
| 596 |
|
| 597 |
def test_geometry_serializer_method_field(self): |
| 598 |
location = Location.objects.create( |
| 599 |
name="geometry serializer method test", geometry="POINT (135.0 45.0)" |
| 600 |
) |
| 601 |
location_loaded = Location.objects.get(pk=location.id) |
| 602 |
self.assertEqual(location_loaded.name, "geometry serializer method test") |
| 603 |
self.assertEqual( |
| 604 |
location_loaded.geometry, Point(135.0, 45.0, srid=location.geometry.srid) |
| 605 |
) |
| 606 |
url = reverse("api_geojson_location_details_hidden", args=[location.id]) |
| 607 |
data = {"properties": {"name": "hidden geometry"}} |
| 608 |
response = self.client.generic( |
| 609 |
"PATCH", url, json.dumps(data), content_type="application/json" |
| 610 |
) |
| 611 |
self.assertEqual(response.status_code, 200) |
| 612 |
self.assertEqual(response.data["properties"]["name"], "hidden geometry") |
| 613 |
self.assertEqual(response.data["geometry"]["type"], "Point") |
| 614 |
self.assertEqual(response.data["geometry"]["coordinates"], [0.0, 0.0]) |
| 615 |
|
| 616 |
def test_geometry_serializer_method_field_none(self): |
| 617 |
location = Location.objects.create( |
| 618 |
name="None value", geometry="POINT (135.0 45.0)" |
| 619 |
) |
| 620 |
location_loaded = Location.objects.get(pk=location.id) |
| 621 |
self.assertEqual( |
| 622 |
location_loaded.geometry, Point(135.0, 45.0, srid=location.geometry.srid) |
| 623 |
) |
| 624 |
url = reverse("api_geojson_location_details_none", args=[location.id]) |
| 625 |
response = self.client.generic("GET", url, content_type="application/json") |
| 626 |
self.assertEqual(response.status_code, 200) |
| 627 |
self.assertEqual(response.data["properties"]["name"], "None value") |
| 628 |
self.assertEqual(response.data["geometry"], None) |
| 629 |
|
| 630 |
def test_geometry_serializer_method_field_nogeo(self): |
| 631 |
nullable = Nullable.objects.create(name="No geometry value") |
| 632 |
nullable_loaded = Nullable.objects.get(pk=nullable.id) |
| 633 |
self.assertEqual(nullable_loaded.name, "No geometry value") |
| 634 |
url = reverse("api_geojson_nullable_details_nogeo", args=[nullable.id]) |
| 635 |
response = self.client.generic("GET", url, content_type="application/json") |
| 636 |
self.assertEqual(response.status_code, 200) |
| 637 |
self.assertEqual(response.data["properties"]["name"], "No geometry value") |
| 638 |
self.assertEqual(response.data["geometry"], None) |
| 639 |
|
| 640 |
def test_nullable_empty_geometry(self): |
| 641 |
empty = Nullable(name="empty", geometry="POINT EMPTY") |
| 642 |
empty.full_clean() |
| 643 |
empty.save() |
| 644 |
url = reverse("api_geojson_nullable_details", args=[empty.id]) |
| 645 |
response = self.client.generic("GET", url, content_type="application/json") |
| 646 |
self.assertIsNotNone(response.data["geometry"]) |
| 647 |
self.assertEqual(response.data["geometry"]["coordinates"], []) |
| 648 |
|
| 649 |
def test_nullable_null_geometry(self): |
| 650 |
empty = Nullable(name="empty", geometry=None) |
| 651 |
empty.full_clean() |
| 652 |
empty.save() |
| 653 |
url = reverse("api_geojson_nullable_details", args=[empty.id]) |
| 654 |
response = self.client.generic("GET", url, content_type="application/json") |
| 655 |
self.assertIsNone(response.data["geometry"]) |
| 656 |
|
| 657 |
def test_geometry_field_to_representation_none(self): |
| 658 |
self._create_locations() |
| 659 |
f = LocationGeoSerializer(instance=self.l1).fields["geometry"] |
| 660 |
self.assertIsNone(f.to_representation(None)) |
| 661 |
|
| 662 |
def test_geometry_empty_representation(self): |
| 663 |
self._create_locations() |
| 664 |
f = LocationGeoSerializer(instance=self.l1).fields["geometry"] |
| 665 |
geom_types = ( |
| 666 |
"POINT", |
| 667 |
"LINESTRING", |
| 668 |
"LINEARRING", |
| 669 |
"POLYGON", |
| 670 |
"MULTIPOINT", |
| 671 |
"MULTILINESTRING", |
| 672 |
"MULTIPOLYGON", |
| 673 |
) |
| 674 |
for geom_type in geom_types: |
| 675 |
with self.subTest(geom_type=geom_type): |
| 676 |
value = f.to_representation(GEOSGeometry(f"{geom_type} EMPTY")) |
| 677 |
self.assertIsNotNone(value) |
| 678 |
if geom_type == "LINEARRING": |
| 679 |
geom_type = "LINESTRING" |
| 680 |
self.assertEqual(value["type"].upper(), geom_type) |
| 681 |
self.assertEqual(value["coordinates"], []) |
| 682 |
# GEOMETRYCOLLECTION needs different handling |
| 683 |
value = f.to_representation(GEOSGeometry("GEOMETRYCOLLECTION EMPTY")) |
| 684 |
self.assertIsNotNone(value) |
| 685 |
self.assertEqual(value["type"].upper(), "GEOMETRYCOLLECTION") |
| 686 |
self.assertEqual(value["geometries"], []) |
| 687 |
|
| 688 |
def test_no_geo_field_improperly_configured(self): |
| 689 |
class LocationGeoFeatureSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 690 |
class Meta: |
| 691 |
model = Location |
| 692 |
|
| 693 |
with self.assertRaises(ImproperlyConfigured): |
| 694 |
LocationGeoFeatureSerializer() |
| 695 |
|
| 696 |
def test_exclude_geo_field_improperly_configured(self): |
| 697 |
self._create_locations() |
| 698 |
|
| 699 |
class LocationGeoFeatureSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 700 |
class Meta: |
| 701 |
model = Location |
| 702 |
geo_field = "geometry" |
| 703 |
exclude = ("geometry",) |
| 704 |
|
| 705 |
with self.assertRaises(ImproperlyConfigured): |
| 706 |
LocationGeoFeatureSerializer(instance=self.l1) |
| 707 |
|
| 708 |
def test_geojson_pagination(self): |
| 709 |
self._create_locations() |
| 710 |
response = self.client.get(self.geojson_location_list_url) |
| 711 |
self.assertEqual(response.data["type"], "FeatureCollection") |
| 712 |
self.assertEqual(len(response.data["features"]), 2) |
| 713 |
response = self.client.get(f"{self.geojson_location_list_url}?page_size=1") |
| 714 |
self.assertEqual(response.data["type"], "FeatureCollection") |
| 715 |
self.assertEqual(len(response.data["features"]), 1) |
| 716 |
self.assertIn("next", response.data) |
| 717 |
self.assertIn("previous", response.data) |
| 718 |
|
| 719 |
def test_pickle(self): |
| 720 |
geometry = GEOSGeometry("POINT (30 10)") |
| 721 |
geojsondict = GeoJsonDict( |
| 722 |
( |
| 723 |
("type", geometry.geom_type), |
| 724 |
("coordinates", geometry.coords), |
| 725 |
) |
| 726 |
) |
| 727 |
pickled = pickle.dumps(geojsondict) |
| 728 |
restored = pickle.loads(pickled) |
| 729 |
self.assertEqual(restored, geojsondict) |
| 730 |
|
| 731 |
def test_geometrycollection_geojson(self): |
| 732 |
"""test geometry collection geojson behaviour""" |
| 733 |
location = Location.objects.create( |
| 734 |
name="geometry collection geojson test", |
| 735 |
geometry="GEOMETRYCOLLECTION (" |
| 736 |
"POINT (135.0 45.0)," |
| 737 |
"LINESTRING (135.0 45.0,140.0 50.0,145.0 55.0)," |
| 738 |
"POLYGON ((135.0 45.0,140.0 50.0,145.0 55.0,135.0 45.0)))", |
| 739 |
) |
| 740 |
url = reverse("api_geojson_location_details", args=[location.id]) |
| 741 |
expected = { |
| 742 |
"id": location.id, |
| 743 |
"type": "Feature", |
| 744 |
"properties": { |
| 745 |
"details": "http://testserver/geojson/%s/" % location.id, |
| 746 |
"name": "geometry collection geojson test", |
| 747 |
"fancy_name": "Kool geometry collection geojson test", |
| 748 |
"timestamp": None, |
| 749 |
"slug": "geometry-collection-geojson-test", |
| 750 |
}, |
| 751 |
"geometry": { |
| 752 |
"type": "GeometryCollection", |
| 753 |
"geometries": [ |
| 754 |
{"type": "Point", "coordinates": [135.0, 45.0]}, |
| 755 |
{ |
| 756 |
"type": "LineString", |
| 757 |
"coordinates": [[135.0, 45.0], [140.0, 50.0], [145.0, 55.0]], |
| 758 |
}, |
| 759 |
{ |
| 760 |
"type": "Polygon", |
| 761 |
"coordinates": [ |
| 762 |
[ |
| 763 |
[135.0, 45.0], |
| 764 |
[140.0, 50.0], |
| 765 |
[145.0, 55.0], |
| 766 |
[135.0, 45.0], |
| 767 |
] |
| 768 |
], |
| 769 |
}, |
| 770 |
], |
| 771 |
}, |
| 772 |
} |
| 773 |
response = self.client.get(url) |
| 774 |
self.assertCountEqual(json.dumps(response.data), json.dumps(expected)) |
| 775 |
self.assertContains(response, "Kool geometry collection geojson test") |