-
Notifications
You must be signed in to change notification settings - Fork 70
/
tests.py
621 lines (518 loc) · 26.2 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
import json
from django.test import TestCase
from django.conf import settings
from django.core import serializers
from django.core.exceptions import ValidationError
from django.contrib.gis.db import models
from django.contrib.gis.geos import LineString, Point, GeometryCollection
from django.utils.encoding import smart_text
from .templatetags.geojson_tags import geojsonfeature
from .serializers import Serializer
from .views import GeoJSONLayerView, TiledGeoJSONLayerView
from .fields import GeoJSONField, GeoJSONFormField, GeoJSONValidator
settings.SERIALIZATION_MODULES = {'geojson': 'djgeojson.serializers'}
class PictureMixin(object):
@property
def picture(self):
return 'image.png'
class Route(PictureMixin, models.Model):
name = models.CharField(max_length=20)
geom = models.LineStringField(spatial_index=False, srid=4326)
countries = models.ManyToManyField('Country')
def natural_key(self):
return self.name
@property
def upper_name(self):
return self.name.upper()
objects = models.GeoManager()
class Sign(models.Model):
label = models.CharField(max_length=20)
route = models.ForeignKey(Route, related_name='signs')
def natural_key(self):
return self.label
@property
def geom(self):
return self.route.geom.centroid
class Country(models.Model):
label = models.CharField(max_length=20)
geom = models.PolygonField(spatial_index=False, srid=4326)
objects = models.GeoManager()
def natural_key(self):
return self.label
class GeoJsonDeSerializerTest(TestCase):
def test_basic(self):
input_geojson = """
{"type": "FeatureCollection",
"features": [
{ "type": "Feature",
"properties": {"model": "djgeojson.route", "name": "green", "upper_name": "RED"},
"id": 1,
"geometry": {
"type": "LineString",
"coordinates": [
[0.0, 0.0],
[1.0, 1.0]
]
}
},
{ "type": "Feature",
"properties": {"model": "djgeojson.route", "name": "blue"},
"id": 2,
"geometry": {
"type": "LineString",
"coordinates": [
[0.0, 0.0],
[1.0, 1.0]
]
}
}
]}"""
# Deserialize into a list of objects
objects = list(serializers.deserialize('geojson', input_geojson))
# Were three objects deserialized?
self.assertEqual(len(objects), 2)
# Did the objects deserialize correctly?
self.assertEqual(objects[1].object.name, "blue")
self.assertEqual(objects[0].object.upper_name, "GREEN")
self.assertEqual(objects[0].object.geom,
LineString((0.0, 0.0), (1.0, 1.0)))
def test_with_model_name_passed_as_argument(self):
input_geojson = """
{"type": "FeatureCollection",
"features": [
{ "type": "Feature",
"properties": {"name": "bleh"},
"id": 24,
"geometry": {
"type": "LineString",
"coordinates": [
[1, 2],
[42, 3]
]
}
}
]}"""
my_object = list(serializers.deserialize(
'geojson', input_geojson, model_name='djgeojson.route'))[0].object
self.assertEqual(my_object.name, "bleh")
class GeoJsonSerializerTest(TestCase):
def test_basic(self):
# Stuff to serialize
route1 = Route.objects.create(
name='green', geom="LINESTRING (0 0, 1 1)")
route2 = Route.objects.create(
name='blue', geom="LINESTRING (0 0, 1 1)")
route3 = Route.objects.create(name='red', geom="LINESTRING (0 0, 1 1)")
actual_geojson = json.loads(serializers.serialize(
'geojson', Route.objects.all(), properties=['name']))
self.assertEqual(
actual_geojson, {"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "name": "green"}, "id": route1.pk}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "name": "blue"}, "id": route2.pk}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "name": "red"}, "id": route3.pk}]})
actual_geojson_with_prop = json.loads(
serializers.serialize(
'geojson', Route.objects.all(),
properties=['name', 'upper_name', 'picture']))
self.assertEqual(actual_geojson_with_prop,
{"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"picture": "image.png", "model": "djgeojson.route", "upper_name": "GREEN", "name": "green"}, "id": route1.pk}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"picture": "image.png", "model": "djgeojson.route", "upper_name": "BLUE", "name": "blue"}, "id": route2.pk}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"picture": "image.png", "model": "djgeojson.route", "upper_name": "RED", "name": "red"}, "id": route3.pk}]})
def test_precision(self):
serializer = Serializer()
features = json.loads(serializer.serialize(
[{'geom': 'SRID=2154;POINT (1 1)'}], precision=2, crs=False))
self.assertEqual(
features, {"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [-1.36, -5.98]}, "type": "Feature", "properties": {}}]})
def test_simplify(self):
serializer = Serializer()
features = json.loads(serializer.serialize(
[{'geom': 'SRID=4326;LINESTRING (1 1, 1.5 1, 2 3, 3 3)'}], simplify=0.5, crs=False))
self.assertEqual(
features, {"type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[1.0, 1.0], [2.0, 3.0], [3.0, 3.0]]}, "type": "Feature", "properties": {}}]})
def test_force2d(self):
serializer = Serializer()
features2d = json.loads(serializer.serialize(
[{'geom': 'SRID=4326;POINT Z (1 2 3)'}],
force2d=True, crs=False))
self.assertEqual(
features2d, {"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [1.0, 2.0]}, "type": "Feature", "properties": {}}]})
def test_pk_property(self):
route = Route.objects.create(name='red', geom="LINESTRING (0 0, 1 1)")
serializer = Serializer()
features2d = json.loads(serializer.serialize(
Route.objects.all(), properties=['id'], crs=False))
self.assertEqual(
features2d, {"type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "id": route.pk}, "id": route.pk}]})
def test_geometry_property(self):
class Basket(models.Model):
@property
def geom(self):
return GeometryCollection(LineString((3, 4, 5), (6, 7, 8)), Point(1, 2, 3), srid=4326)
serializer = Serializer()
features = json.loads(
serializer.serialize([Basket()], crs=False, force2d=True))
expected_content = {"type": "FeatureCollection", "features": [{"geometry": {"type": "GeometryCollection", "geometries": [{"type": "LineString", "coordinates": [[3.0, 4.0], [6.0, 7.0]]}, {"type": "Point", "coordinates": [1.0, 2.0]}]}, "type": "Feature", "properties": {"id": None}}]}
self.assertEqual(features, expected_content)
def test_none_geometry(self):
class Empty(models.Model):
geom = None
serializer = Serializer()
features = json.loads(serializer.serialize([Empty()], crs=False))
self.assertEqual(
features, {
"type": "FeatureCollection",
"features": [{
"geometry": None,
"type": "Feature",
"properties": {"id": None}}]
})
def test_bbox_auto(self):
serializer = Serializer()
features = json.loads(serializer.serialize([{'geom': 'SRID=4326;LINESTRING (1 1, 3 3)'}],
bbox_auto=True, crs=False))
self.assertEqual(
features, {
"type": "FeatureCollection",
"features": [{
"geometry": {"type": "LineString", "coordinates": [[1.0, 1.0], [3.0, 3.0]]},
"type": "Feature",
"properties": {},
"bbox": [1.0, 1.0, 3.0, 3.0]
}]
})
class ForeignKeyTest(TestCase):
def setUp(self):
self.route = Route.objects.create(
name='green', geom="LINESTRING (0 0, 1 1)")
Sign(label='A', route=self.route).save()
def test_serialize_foreign(self):
serializer = Serializer()
features = json.loads(serializer.serialize(Sign.objects.all(), properties=['route']))
self.assertEqual(
features, {"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [0.5, 0.5]}, "type": "Feature", "properties": {"route": 1, "model": "djgeojson.sign"}, "id": self.route.pk}]})
def test_serialize_foreign_natural(self):
serializer = Serializer()
features = json.loads(serializer.serialize(
Sign.objects.all(), use_natural_keys=True, properties=['route']))
self.assertEqual(
features, {"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [0.5, 0.5]}, "type": "Feature", "properties": {"route": "green", "model": "djgeojson.sign"}, "id": self.route.pk}]})
class ManyToManyTest(TestCase):
def setUp(self):
country1 = Country(label='C1', geom="POLYGON ((0 0,1 1,0 2,0 0))")
country1.save()
country2 = Country(label='C2', geom="POLYGON ((0 0,1 1,0 2,0 0))")
country2.save()
self.route1 = Route.objects.create(
name='green', geom="LINESTRING (0 0, 1 1)")
self.route2 = Route.objects.create(
name='blue', geom="LINESTRING (0 0, 1 1)")
self.route2.countries.add(country1)
self.route3 = Route.objects.create(
name='red', geom="LINESTRING (0 0, 1 1)")
self.route3.countries.add(country1)
self.route3.countries.add(country2)
def test_serialize_manytomany(self):
serializer = Serializer()
features = json.loads(serializer.serialize(
Route.objects.all(), properties=['countries']))
self.assertEqual(
features, {"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "countries": []}, "id": self.route1.pk}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "countries": [1]}, "id": self.route2.pk}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "countries": [1, 2]}, "id": self.route3.pk}]})
def test_serialize_manytomany_natural(self):
serializer = Serializer()
features = json.loads(serializer.serialize(
Route.objects.all(), use_natural_keys=True, properties=['countries']))
self.assertEqual(
features, {"crs": {"type": "link", "properties": {"href": "http://spatialreference.org/ref/epsg/4326/", "type": "proj4"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "countries": []}, "id": self.route1.pk}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "countries": ["C1"]}, "id": self.route2.pk}, {"geometry": {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0]]}, "type": "Feature", "properties": {"model": "djgeojson.route", "countries": ["C1", "C2"]}, "id": self.route3.pk}]})
class ReverseForeignkeyTest(TestCase):
def setUp(self):
self.route = Route(name='green', geom="LINESTRING (0 0, 1 1)")
self.route.save()
self.sign1 = Sign.objects.create(label='A', route=self.route)
self.sign2 = Sign.objects.create(label='B', route=self.route)
self.sign3 = Sign.objects.create(label='C', route=self.route)
def test_relation_set(self):
self.assertEqual(len(self.route.signs.all()), 3)
def test_serialize_reverse(self):
serializer = Serializer()
features = json.loads(serializer.serialize(
Route.objects.all(), properties=['signs']))
self.assertEqual(
features, {
"crs": {
"type": "link", "properties": {
"href": "http://spatialreference.org/ref/epsg/4326/",
"type": "proj4"
}
},
"type": "FeatureCollection",
"features": [{
"geometry": {
"type": "LineString",
"coordinates": [[0.0, 0.0], [1.0, 1.0]]
},
"type": "Feature",
"properties": {
"model": "djgeojson.route",
"signs": [
self.sign1.pk,
self.sign2.pk,
self.sign3.pk]},
"id": self.route.pk
}]
})
def test_serialize_reverse_natural(self):
serializer = Serializer()
features = json.loads(serializer.serialize(
Route.objects.all(), use_natural_keys=True, properties=['signs']))
self.assertEqual(
features, {
"crs": {
"type": "link",
"properties": {
"href": "http://spatialreference.org/ref/epsg/4326/",
"type": "proj4"
}
},
"type": "FeatureCollection",
"features": [{
"geometry": {
"type": "LineString",
"coordinates": [[0.0, 0.0], [1.0, 1.0]]},
"type": "Feature",
"properties": {
"model": "djgeojson.route",
"signs": ["A", "B", "C"]},
"id": self.route.pk
}]
})
class GeoJsonTemplateTagTest(TestCase):
def test_single(self):
r = Route(name='red', geom="LINESTRING (0 0, 1 1)")
feature = json.loads(geojsonfeature(r))
self.assertEqual(
feature, {
"crs": {
"type": "link",
"properties": {
"href": "http://spatialreference.org/ref/epsg/4326/",
"type": "proj4"
}
},
"type": "FeatureCollection",
"features": [{
"geometry": {
"type": "LineString",
"coordinates": [[0.0, 0.0], [1.0, 1.0]]},
"type": "Feature", "properties": {}}]
})
def test_queryset(self):
route1 = Route.objects.create(
name='green', geom="LINESTRING (0 0, 1 1)")
route2 = Route.objects.create(
name='blue', geom="LINESTRING (0 0, 1 1)")
route3 = Route.objects.create(name='red', geom="LINESTRING (0 0, 1 1)")
feature = json.loads(geojsonfeature(Route.objects.all()))
self.assertEqual(
feature, {
"crs": {
"type": "link", "properties": {
"href": "http://spatialreference.org/ref/epsg/4326/",
"type": "proj4"
}
},
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "LineString",
"coordinates": [[0.0, 0.0], [1.0, 1.0]]
},
"type": "Feature",
"properties": {
"model": "djgeojson.route"
},
"id": route1.pk
},
{
"geometry": {
"type": "LineString",
"coordinates": [[0.0, 0.0], [1.0, 1.0]]
},
"type": "Feature",
"properties": {"model": "djgeojson.route"},
"id": route2.pk
},
{
"geometry": {"type": "LineString",
"coordinates": [[0.0, 0.0], [1.0, 1.0]]},
"type": "Feature",
"properties": {"model": "djgeojson.route"},
"id": route3.pk
}
]
})
def test_feature(self):
r = Route(name='red', geom="LINESTRING (0 0, 1 1)")
feature = json.loads(geojsonfeature(r.geom))
self.assertEqual(
feature, {
"geometry": {"type": "LineString",
"coordinates": [[0.0, 0.0], [1.0, 1.0]]},
"type": "Feature", "properties": {}
})
class ViewsTest(TestCase):
def setUp(self):
self.route = Route(name='green', geom="LINESTRING (0 0, 1 1)")
self.route.save()
def test_view_default_options(self):
view = GeoJSONLayerView(model=Route)
view.object_list = []
response = view.render_to_response(context={})
geojson = json.loads(smart_text(response.content))
self.assertEqual(geojson['features'][0]['geometry']['coordinates'],
[[0.0, 0.0], [1.0, 1.0]])
def test_view_can_control_properties(self):
klass = type('FullGeoJSON', (GeoJSONLayerView,),
{'properties': ['name']})
view = klass(model=Route)
view.object_list = []
response = view.render_to_response(context={})
geojson = json.loads(smart_text(response.content))
self.assertEqual(geojson['features'][0]['properties']['name'],
'green')
class TileEnvelopTest(TestCase):
def setUp(self):
self.view = TiledGeoJSONLayerView()
def test_raises_error_if_not_spherical_mercator(self):
self.view.tile_srid = 2154
self.assertRaises(AssertionError, self.view.tile_coord, 0, 0, 0)
def test_origin_is_north_west_for_tile_0(self):
self.assertEqual((-180.0, 85.0511287798066),
self.view.tile_coord(0, 0, 0))
def test_origin_is_center_for_middle_tile(self):
self.assertEqual((0, 0), self.view.tile_coord(8, 8, 4))
class TiledGeoJSONViewTest(TestCase):
def setUp(self):
self.view = TiledGeoJSONLayerView(model=Route)
self.r1 = Route.objects.create(geom=LineString((0, 1), (10, 1)))
self.r2 = Route.objects.create(geom=LineString((0, -1), (-10, -1)))
def test_view_is_serialized_as_geojson(self):
self.view.args = [4, 8, 7]
response = self.view.render_to_response(context={})
geojson = json.loads(smart_text(response.content))
self.assertEqual(geojson['features'][0]['geometry']['coordinates'],
[[0.0, 1.0], [10.0, 1.0]])
def test_view_trims_to_geometries_boundaries(self):
self.view.args = [8, 128, 127]
response = self.view.render_to_response(context={})
geojson = json.loads(smart_text(response.content))
self.assertEqual(geojson['features'][0]['geometry']['coordinates'],
[[0.0, 1.0], [1.40625, 1.0]])
def test_geometries_trim_can_be_disabled(self):
self.view.args = [8, 128, 127]
self.view.trim_to_boundary = False
response = self.view.render_to_response(context={})
geojson = json.loads(smart_text(response.content))
self.assertEqual(geojson['features'][0]['geometry']['coordinates'],
[[0.0, 1.0], [10.0, 1.0]])
def test_url_parameters_are_converted_to_int(self):
self.view.args = ['0', '0', '0']
self.assertEqual(2, len(self.view.get_queryset()))
def test_zoom_0_queryset_contains_all(self):
self.view.args = [0, 0, 0]
self.assertEqual(2, len(self.view.get_queryset()))
def test_zoom_4_filters_by_tile_extent(self):
self.view.args = [4, 8, 7]
self.assertEqual([self.r1], list(self.view.get_queryset()))
def test_some_tiles_have_empty_queryset(self):
self.view.args = [4, 6, 8]
self.assertEqual(0, len(self.view.get_queryset()))
def test_simplification_depends_on_zoom_level(self):
self.view.simplifications = {6: 100}
self.view.args = [6, 8, 4]
self.view.get_queryset()
self.assertEqual(self.view.simplify, 100)
def test_simplification_is_default_if_not_specified(self):
self.view.simplifications = {}
self.view.args = [0, 8, 4]
self.view.get_queryset()
self.assertEqual(self.view.simplify, None)
def test_simplification_takes_the_closest_upper_level(self):
self.view.simplifications = {3: 100, 6: 200}
self.view.args = [4, 8, 4]
self.view.get_queryset()
self.assertEqual(self.view.simplify, 200)
class Address(models.Model):
geom = GeoJSONField()
class ModelFieldTest(TestCase):
def setUp(self):
self.address = Address()
self.address.geom = {'type': 'Point', 'coordinates': [0, 0]}
self.address.save()
def test_models_can_have_geojson_fields(self):
saved = Address.objects.get(id=self.address.id)
self.assertDictEqual(saved.geom, self.address.geom)
def test_default_form_field_is_geojsonfield(self):
field = self.address._meta.get_field('geom').formfield()
self.assertTrue(isinstance(field, GeoJSONFormField))
def test_default_form_field_has_geojson_validator(self):
field = self.address._meta.get_field('geom').formfield()
validator = field.validators[0]
self.assertTrue(isinstance(validator, GeoJSONValidator))
def test_form_field_raises_if_invalid_type(self):
field = self.address._meta.get_field('geom').formfield()
self.assertRaises(ValidationError, field.clean,
{'type': 'FeatureCollection', 'foo': 'bar'})
def test_form_field_raises_if_type_missing(self):
field = self.address._meta.get_field('geom').formfield()
self.assertRaises(ValidationError, field.clean,
{'foo': 'bar'})
def test_field_can_be_serialized(self):
serializer = Serializer()
geojson = serializer.serialize(Address.objects.all(), crs=False)
features = json.loads(geojson)
self.assertEqual(
features, {
u'type': u'FeatureCollection',
u'features': [{
u'id': self.address.id,
u'type': u'Feature',
u'geometry': {u'type': u'Point', u'coordinates': [0, 0]},
u'properties': {
u'model': u'djgeojson.address'
}
}]
})
def test_field_can_be_deserialized(self):
input_geojson = """
{"type": "FeatureCollection",
"features": [
{ "type": "Feature",
"properties": {"model": "djgeojson.address"},
"id": 1,
"geometry": {
"type": "Point",
"coordinates": [0.0, 0.0]
}
}
]}"""
objects = list(serializers.deserialize('geojson', input_geojson))
self.assertEqual(objects[0].object.geom,
{'type': 'Point', 'coordinates': [0, 0]})
class GeoJSONValidatorTest(TestCase):
def test_validator_raises_if_missing_type(self):
validator = GeoJSONValidator('GEOMETRY')
self.assertRaises(ValidationError, validator, {'foo': 'bar'})
def test_validator_raises_if_type_is_wrong(self):
validator = GeoJSONValidator('GEOMETRY')
self.assertRaises(ValidationError, validator,
{'type': 'FeatureCollection',
'features': []})
def test_validator_succeeds_if_type_matches(self):
validator = GeoJSONValidator('POINT')
self.assertIsNone(validator({'type': 'Point', 'coords': [0, 0]}))
def test_validator_succeeds_if_type_is_generic(self):
validator = GeoJSONValidator('GEOMETRY')
self.assertIsNone(validator({'type': 'Point', 'coords': [0, 0]}))
self.assertIsNone(validator({'type': 'LineString', 'coords': [0, 0]}))
self.assertIsNone(validator({'type': 'Polygon', 'coords': [0, 0]}))
def test_validator_fails_if_type_does_not_match(self):
validator = GeoJSONValidator('POINT')
self.assertRaises(ValidationError, validator,
{'type': 'LineString', 'coords': [0, 0]})