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