- Timestamp:
- 08/23/10 14:20:47 (21 months ago)
- Location:
- framework/server/trunk
- Files:
-
- 4 added
- 7 modified
-
docs/framework/upgrading.txt (modified) (3 diffs)
-
mapfish/decorators (added)
-
mapfish/decorators/__init__.py (added)
-
mapfish/lib/protocol.py (modified) (8 diffs)
-
mapfish/templates/controller.py_tmpl (modified) (3 diffs)
-
mapfish/tests/__init__.py (added)
-
mapfish/tests/test_decorators.py (added)
-
mapfish/tests/test_mysql.py (modified) (5 diffs)
-
mapfish/tests/test_oracle.py (modified) (6 diffs)
-
mapfish/tests/test_postgis.py (modified) (6 diffs)
-
mapfish/tests/test_spatialite.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
framework/server/trunk/docs/framework/upgrading.txt
r3580 r3601 10 10 MapFish 1.2 is based on Pylons 0.9.7, and MapFish 2.0 is based on Pylons 1.0. 11 11 So to upgrade an application from MapFish 1.2 to MapFish 2.0 it is first needed 12 to upgrade from Pylons 0.9.7 to Pylons 1.0. For this refer to the `Pylons 1.013 Upgrading page <http://pylonshq.com/docs/en/1.0/upgrading/>`_, and follow the 14 provided indications.12 to upgrade that application from Pylons 0.9.7 to Pylons 1.0. For this refer to 13 the `Pylons 1.0 Upgrading page <http://pylonshq.com/docs/en/1.0/upgrading/>`_, 14 and follow the provided indications. 15 15 16 16 .. note: … … 57 57 MapFish 2.0 58 58 ~~~~~~~~~~~ 59 60 GeoAlchemy 61 ^^^^^^^^^^ 59 62 60 63 MapFish 2.0 is based on `GeoAlchemy <http://geoalchemy.org>`_. GeoAlchemy … … 104 107 } 105 108 the_geom = GeometryColumn(Point(srid=4326)) 109 110 geojsonify 111 ^^^^^^^^^^ 112 113 MapFish 2.0 introduces the ``geojsonify`` decorator for generating GeoJSON. 114 115 With MapFish 1.2 the ``paster mf-layer`` command generated controller files 116 that looked like this:: 117 118 from pylons import request, response, session, tmpl_context as c 119 from pylons.controllers.util import abort, redirect_to 120 121 from myproject.lib.base import BaseController 122 from myproject.model.users import User 123 from myproject.model.meta import Session 124 from myproject.lib.decorators import geojsonify 125 126 from mapfish.lib.filters import * 127 from mapfish.lib.protocol import Protocol, create_default_filter 128 129 class UsersController(BaseController): 130 readonly = False # if set to True, only GET is supported 131 132 def __init__(self): 133 self.protocol = Protocol(Session, User, self.readonly) 134 135 def index(self, format='json'): 136 """GET /: return all features.""" 137 return self.protocol.index(request, response, format=format) 138 139 def show(self, id, format='json'): 140 """GET /id: Show a specific feature.""" 141 return self.protocol.show(request, response, id) 142 143 def create(self): 144 """POST /: Create a new feature.""" 145 return self.protocol.create(request, response) 146 147 def update(self, id): 148 """PUT /id: Update an existing feature.""" 149 return self.protocol.update(request, response, id) 150 151 def delete(self, id): 152 """DELETE /id: Delete an existing feature.""" 153 return self.protocol.delete(request, response, id) 154 155 With MapFish 2.0 the same ``user`` controller looks like this:: 156 157 from pylons import request, response, session, tmpl_context as c 158 from pylons.controllers.util import abort, redirect 159 160 from test.lib.base import BaseController 161 from test.model.users import User 162 from test.model.meta import Session 163 164 from mapfish.lib.filters import * 165 from mapfish.lib.protocol import Protocol, create_default_filter 166 from mapfish.decorators import geojsonify 167 168 class UsersController(BaseController): 169 readonly = False # if set to True, only GET is supported 170 171 def __init__(self): 172 self.protocol = Protocol(Session, User, self.readonly) 173 174 @geojsonify 175 def index(self, format='json'): 176 """GET /: return all features.""" 177 if format != 'json': 178 abort(404) 179 return self.protocol.read(request) 180 181 @geojsonify 182 def show(self, id, format='json'): 183 """GET /id: Show a specific feature.""" 184 if format != 'json': 185 abort(404) 186 return self.protocol.read(request, response, id=id) 187 188 @geojsonify 189 def create(self): 190 """POST /: Create a new feature.""" 191 return self.protocol.create(request, response) 192 193 @geojsonify 194 def update(self, id): 195 """PUT /id: Update an existing feature.""" 196 return self.protocol.update(request, response, id) 197 198 def delete(self, id): 199 """DELETE /id: Delete an existing feature.""" 200 return self.protocol.delete(request, response, id) 201 202 Several things to note here: 203 204 * Protocols no longer expose ``index`` and ``show`` methods. They expose a 205 single method for reading features: ``read``. So ``read`` is to be used 206 from both the ``index`` and ``show`` actions. 207 * The ``index``, ``show``, ``create``, and ``update`` methods no longer 208 return GeoJSON strings. Instead they return objects that can be 209 serialized into GeoJSON strings. This serialization is taken 210 care of by the ``geojsonify`` decorator. -
framework/server/trunk/mapfish/lib/protocol.py
r3569 r3601 21 21 log = logging.getLogger(__name__) 22 22 23 import decimal, datetime24 25 23 from pylons.controllers.util import abort 26 24 … … 31 29 from geoalchemy import WKBSpatialElement 32 30 33 from geojson import dumps as _dumps, loads, Feature, FeatureCollection, GeoJSON 34 from geojson.codec import PyGFPEncoder 31 from geojson import Feature, FeatureCollection, loads, GeoJSON 35 32 36 33 from mapfish.lib.filters import Filter … … 50 47 "ilike": Comparison.ILIKE 51 48 } 52 53 class MapFishJSONEncoder(PyGFPEncoder):54 # SQLAlchemy's Reflecting Tables mechanism uses decimal.Decimal55 # for numeric columns and datetime.date for dates. simplejson does56 # not know how to deal with objects of those types. This class provides57 # a simple encoder that can deal with these kinds of objects.58 59 def default(self, obj):60 if isinstance(obj, (decimal.Decimal, datetime.date, datetime.datetime)):61 return str(obj)62 return PyGFPEncoder.default(self, obj)63 64 def dumps(obj, cls=MapFishJSONEncoder, **kwargs):65 # Wrapper for geojson's dumps function.66 return _dumps(obj, cls=cls, **kwargs)67 49 68 50 def create_geom_filter(request, mapped_class, **kwargs): … … 227 209 self.before_delete = kwargs['before_delete'] 228 210 229 def _encode(self, objects, request, response):230 """ Return a GeoJSON representation of the passed objects. """231 if objects is not None:232 response.content_type = "application/json"233 if isinstance(objects, list):234 return dumps(235 FeatureCollection(236 [self._filter_attrs(o.toFeature(), request) for o in objects if o.geometry is not None]237 )238 )239 else:240 return dumps(self._filter_attrs(objects.toFeature(), request))241 242 211 def _filter_attrs(self, feature, request): 243 212 """ Remove some attributes from the feature and set the geometry to None … … 313 282 return query 314 283 315 def index(self, request, response, format='json', filter=None):316 """ Build a query based on the filter and the request317 params, send the query to the database, and return a318 GeoJSON representation of the query results. """319 320 # only json is supported321 if format != 'json':322 abort(404)323 324 return self._encode(self._query(request, filter), request, response)325 326 284 def count(self, request, filter=None): 327 285 """ Return the number of records matching the given filter. """ … … 332 290 return str(self.Session.query(self.mapped_class).filter(filter).count()) 333 291 334 def show(self, request, response, id, format='json'): 335 """ Build a query based on the id argument, send the query 336 to the database, and return a GeoJSON representation of the 337 result. """ 338 339 # only json is supported 340 if format != 'json': 341 abort(404) 342 343 obj = self.Session.query(self.mapped_class).get(id) 344 if obj is None: 345 abort(404) 346 347 return self._encode(obj, request, response) 292 def read(self, request, filter=None, id=None): 293 """ Build a query based on the filter or the idenfier, send the query 294 to the database, and return a Feature or a FeatureCollection. """ 295 ret = None 296 if id is not None: 297 o = self.Session.query(self.mapped_class).get(id) 298 if o is None: 299 abort(404) 300 ret = self._filter_attrs(o.toFeature(), request) 301 else: 302 objs = self._query(request, filter) 303 ret = FeatureCollection( 304 [self._filter_attrs(o.toFeature(), request) \ 305 for o in objs if o.geometry is not None]) 306 return ret 348 307 349 308 def create(self, request, response, execute=True): … … 376 335 response.status = 201 377 336 if len(objects) > 0: 378 return self._encode(objects, request, response) 337 features = [o.toFeature() for o in objects \ 338 if o.geometry is not None] 339 return FeatureCollection(features) 379 340 return 380 341 … … 397 358 self.Session.commit() 398 359 response.status = 201 399 return self._encode(obj, request, response)360 return obj.toFeature() 400 361 401 362 def delete(self, request, response, id): -
framework/server/trunk/mapfish/templates/controller.py_tmpl
r3580 r3601 8 8 from mapfish.lib.filters import * 9 9 from mapfish.lib.protocol import Protocol, create_default_filter 10 from mapfish.decorators import geojsonify 10 11 11 12 class {{contrClass}}Controller(BaseController): … … 15 16 self.protocol = Protocol(Session, {{modelClass}}, self.readonly) 16 17 18 @geojsonify 17 19 def index(self, format='json'): 18 20 """GET /: return all features.""" … … 56 58 # else: 57 59 # filter = compare_filter 58 # return self.protocol.index(request, response, format=format, filter=filter) 60 # return self.protocol.read(request, filter=filter) 61 if format != 'json': 62 abort(404) 63 return self.protocol.read(request) 59 64 60 return self.protocol.index(request, response, format=format) 61 65 @geojsonify 62 66 def show(self, id, format='json'): 63 67 """GET /id: Show a specific feature.""" 64 return self.protocol.show(request, response, id, format=format) 68 if format != 'json': 69 abort(404) 70 return self.protocol.read(request, response, id=id) 65 71 72 @geojsonify 66 73 def create(self): 67 74 """POST /: Create a new feature.""" 68 75 return self.protocol.create(request, response) 69 76 77 @geojsonify 70 78 def update(self, id): 71 79 """PUT /id: Update an existing feature.""" -
framework/server/trunk/mapfish/tests/test_mysql.py
r3569 r3601 16 16 from webob.exc import HTTPNotFound 17 17 from exceptions import Exception 18 from geojson import loads, FeatureCollection, GeoJSON18 from geojson import Feature, FeatureCollection, GeoJSON 19 19 20 20 … … 82 82 83 83 response = FakeResponse() 84 returned_geojson = proto.create(request, response) 85 eq_(returned_geojson, 86 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [45.0, 5.0]}, "id": 10, "type": "Feature", "bbox": [45.0, 5.0, 45.0, 5.0], "properties": {"spot_height": 12.0}}]}') 84 collection = proto.create(request, response) 87 85 eq_(response.status, 201) 88 86 eq_(len(collection.features), 1) 87 feature0 = collection.features[0] 88 eq_(feature0.id, 10) 89 eq_(feature0.geometry.coordinates, (45.0, 5.0)) 90 eq_(feature0.properties["spot_height"], 12) 91 89 92 new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 90 93 ok_(new_spot is not None) … … 105 108 106 109 response = FakeResponse() 107 returned_geojson = proto.create(request, response) 108 eq_(returned_geojson, 109 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [45.0, 5.0]}, "id": 10, "type": "Feature", "bbox": [45.0, 5.0, 45.0, 5.0], "properties": {"spot_height": 12.0}}, {"geometry": {"type": "Point", "coordinates": [1.0, 1.0]}, "id": 2, "type": "Feature", "bbox": [1.0, 1.0, 1.0, 1.0], "properties": {"spot_height": 102.34}}]}') 110 collection = proto.create(request, response) 110 111 eq_(response.status, 201) 112 eq_(len(collection.features), 2) 113 feature0 = collection.features[0] 114 eq_(feature0.id, 10) 115 eq_(feature0.geometry.coordinates, (45.0, 5.0)) 116 eq_(feature0.properties["spot_height"], 12) 117 feature1 = collection.features[1] 118 eq_(feature1.id, old_spot.spot_id) 119 eq_(feature1.geometry.coordinates, (1, 1)) 111 120 112 121 new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() … … 141 150 142 151 response = FakeResponse() 143 returned_geojson = proto.update(request, response, id) 144 eq_(returned_geojson, 145 '{"geometry": {"type": "Point", "coordinates": [1.0, 1.0]}, "id": 1, "type": "Feature", "bbox": [1.0, 1.0, 1.0, 1.0], "properties": {"spot_height": 420.39999999999998}}') 152 feature = proto.update(request, response, id) 146 153 eq_(response.status, 201) 147 154 eq_(feature.id, 1) 155 eq_(feature.geometry.coordinates, (1.0, 1.0)) 156 148 157 spot = session.query(Spot).get(id) 149 158 ok_(spot is not None) … … 290 299 291 300 eq_(proto.count(request, filter=filter), '1') 292 293 294 def test_protocol_show(self): 295 """Get a single point by its primary key""" 296 proto = Protocol(session, Spot) 297 298 request = FakeRequest({}) 299 response = FakeResponse() 300 301 returned_geojson = proto.show(request, response, 1) 302 eq_(returned_geojson, 303 '{"geometry": {"type": "Point", "coordinates": [0.0, 0.0]}, "id": 1, "type": "Feature", "bbox": [0.0, 0.0, 0.0, 0.0], "properties": {"spot_height": 420.39999999999998}}') 304 eq_(response.status, 0) # note that the return code is set later by Pylons 305 306 301 302 303 def test_protocol_read_all(self): 304 """Return all features""" 305 proto = Protocol(session, Spot) 306 307 collection = proto.read(FakeRequest({})) 308 ok_(collection is not None) 309 ok_(isinstance(collection, FeatureCollection)) 310 eq_(len(collection.features), 9) 311 312 313 def test_protocol_read_one(self): 314 """Return one feature""" 315 proto = Protocol(session, Spot) 316 317 feature = proto.read(FakeRequest({}), id=1) 318 ok_(feature is not None) 319 ok_(isinstance(feature, Feature)) 320 eq_(feature.id, 1) 321 eq_(feature.geometry.coordinates, (0.0, 0.0)) 322 eq_(feature.properties["spot_height"], 420.39999999999998) 323 324 307 325 @raises(HTTPNotFound) 308 def test_protocol_ show_fails(self):326 def test_protocol_read_one_fails(self): 309 327 """Try to get a single point with a wrong primary key""" 310 328 proto = Protocol(session, Spot) 311 312 proto.show(FakeRequest({}), FakeResponse(), -1) 313 314 315 def test_protocol_index(self): 316 """Return all features""" 317 proto = Protocol(session, Spot) 318 returned_geojson = proto.index(FakeRequest({}), FakeResponse()) 319 320 ok_(returned_geojson is not None) 321 322 factory = lambda ob: GeoJSON.to_instance(ob) 323 features = loads(returned_geojson, object_hook=factory) 324 325 ok_(isinstance(features, FeatureCollection)) 326 eq_(len(features.features), 9) 327 329 330 proto.read(FakeRequest({}), id=-1) 331 328 332 329 333 def test_within_distance(self): -
framework/server/trunk/mapfish/tests/test_oracle.py
r3569 r3601 17 17 from webob.exc import HTTPNotFound 18 18 from exceptions import Exception 19 from geojson import loads, FeatureCollection, GeoJSON19 from geojson import Feature, FeatureCollection, GeoJSON 20 20 import os 21 21 from sqlalchemy.schema import Sequence … … 29 29 30 30 #engine = create_engine('oracle://gis:gis@localhost:1521/gis', echo=True) 31 engine = create_engine('oracle://system:system@172.16.10 1.131:1521/gis', echo=True)31 engine = create_engine('oracle://system:system@172.16.103.134:1521/gis', echo=True) 32 32 33 33 metadata = MetaData(engine) … … 98 98 99 99 response = FakeResponse() 100 returned_geojson = proto.create(request, response) 101 eq_(returned_geojson, 102 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [45.0, 5.0]}, "id": 10, "type": "Feature", "bbox": [45.0, 5.0, 45.0, 5.0], "properties": {"spot_height": 12.0}}]}') 100 collection = proto.create(request, response) 103 101 eq_(response.status, 201) 104 102 eq_(len(collection.features), 1) 103 feature0 = collection.features[0] 104 eq_(feature0.id, 10) 105 eq_(feature0.geometry.coordinates, (45.0, 5.0)) 106 eq_(feature0.properties["spot_height"], 12) 107 105 108 new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 106 109 ok_(new_spot is not None) … … 121 124 122 125 response = FakeResponse() 123 returned_geojson = proto.create(request, response) 124 eq_(returned_geojson, 125 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [45.0, 5.0]}, "id": 10, "type": "Feature", "bbox": [45.0, 5.0, 45.0, 5.0], "properties": {"spot_height": 12.0}}, {"geometry": {"type": "Point", "coordinates": [1.0, 1.0]}, "id": 2, "type": "Feature", "bbox": [1.0, 1.0, 1.0, 1.0], "properties": {"spot_height": 102.34}}]}') 126 collection = proto.create(request, response) 126 127 eq_(response.status, 201) 127 128 eq_(len(collection.features), 2) 129 feature0 = collection.features[0] 130 eq_(feature0.id, 10) 131 eq_(feature0.geometry.coordinates, (45.0, 5.0)) 132 eq_(feature0.properties["spot_height"], 12) 133 feature1 = collection.features[1] 134 eq_(feature1.id, old_spot.spot_id) 135 eq_(feature1.geometry.coordinates, (1, 1)) 136 128 137 new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 129 138 ok_(new_spot is not None) … … 157 166 158 167 response = FakeResponse() 159 returned_geojson = proto.update(request, response, id) 160 eq_(returned_geojson, 161 '{"geometry": {"type": "Point", "coordinates": [1.0, 1.0]}, "id": 1, "type": "Feature", "bbox": [1.0, 1.0, 1.0, 1.0], "properties": {"spot_height": 420.39999999999998}}') 168 feature = proto.update(request, response, id) 162 169 eq_(response.status, 201) 170 eq_(feature.id, 1) 171 eq_(feature.geometry.coordinates, (1.0, 1.0)) 163 172 164 173 spot = session.query(Spot).get(id) … … 318 327 319 328 eq_(proto.count(request, filter=filter), '3') 320 321 322 def test_protocol_show(self): 323 """Get a single point by its primary key""" 324 proto = Protocol(session, Spot) 325 326 request = FakeRequest({}) 327 response = FakeResponse() 328 329 returned_geojson = proto.show(request, response, 1) 330 eq_(returned_geojson, 331 '{"geometry": {"type": "Point", "coordinates": [0.0, 0.0]}, "id": 1, "type": "Feature", "bbox": [0.0, 0.0, 0.0, 0.0], "properties": {"spot_height": 420.39999999999998}}') 332 eq_(response.status, 0) # note that the return code is set later by Pylons 333 334 329 330 331 def test_protocol_read_all(self): 332 """Return all features""" 333 proto = Protocol(session, Spot) 334 335 collection = proto.read(FakeRequest({})) 336 ok_(collection is not None) 337 ok_(isinstance(collection, FeatureCollection)) 338 eq_(len(collection.features), 9) 339 340 341 def test_protocol_read_one(self): 342 """Return one feature""" 343 proto = Protocol(session, Spot) 344 345 feature = proto.read(FakeRequest({}), id=1) 346 ok_(feature is not None) 347 ok_(isinstance(feature, Feature)) 348 eq_(feature.id, 1) 349 eq_(feature.geometry.coordinates, (0.0, 0.0)) 350 eq_(feature.properties["spot_height"], 420.39999999999998) 351 352 335 353 @raises(HTTPNotFound) 336 def test_protocol_ show_fails(self):354 def test_protocol_read_one_fails(self): 337 355 """Try to get a single point with a wrong primary key""" 338 356 proto = Protocol(session, Spot) 339 357 340 proto.show(FakeRequest({}), FakeResponse(), -1) 341 342 343 def test_protocol_index(self): 344 """Return all features""" 345 proto = Protocol(session, Spot) 346 returned_geojson = proto.index(FakeRequest({}), FakeResponse()) 347 348 ok_(returned_geojson is not None) 349 350 factory = lambda ob: GeoJSON.to_instance(ob) 351 features = loads(returned_geojson, object_hook=factory) 352 353 ok_(isinstance(features, FeatureCollection)) 354 eq_(len(features.features), 9) 355 358 proto.read(FakeRequest({}), id=-1) 359 356 360 357 361 def test_within_distance(self): -
framework/server/trunk/mapfish/tests/test_postgis.py
r3569 r3601 16 16 from webob.exc import HTTPNotFound 17 17 from exceptions import Exception 18 from geojson import loads, FeatureCollection, GeoJSON18 from geojson import Feature, FeatureCollection, GeoJSON 19 19 20 20 … … 82 82 83 83 response = FakeResponse() 84 returned_geojson = proto.create(request, response) 85 eq_(returned_geojson, 86 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [45.0, 5.0]}, "id": 10, "type": "Feature", "bbox": [45.0, 5.0, 45.0, 5.0], "properties": {"spot_height": 12.0}}]}') 84 collection = proto.create(request, response) 87 85 eq_(response.status, 201) 88 86 eq_(len(collection.features), 1) 87 feature0 = collection.features[0] 88 eq_(feature0.id, 10) 89 eq_(feature0.geometry.coordinates, (45.0, 5.0)) 90 eq_(feature0.properties["spot_height"], 12) 91 89 92 new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 90 93 ok_(new_spot is not None) … … 105 108 106 109 response = FakeResponse() 107 returned_geojson = proto.create(request, response) 108 eq_(returned_geojson, 109 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [45.0, 5.0]}, "id": 10, "type": "Feature", "bbox": [45.0, 5.0, 45.0, 5.0], "properties": {"spot_height": 12.0}}, {"geometry": {"type": "Point", "coordinates": [1.0, 1.0]}, "id": 2, "type": "Feature", "bbox": [1.0, 1.0, 1.0, 1.0], "properties": {"spot_height": 102.34}}]}') 110 collection = proto.create(request, response) 110 111 eq_(response.status, 201) 111 112 eq_(len(collection.features), 2) 113 feature0 = collection.features[0] 114 eq_(feature0.id, 10) 115 eq_(feature0.geometry.coordinates, (45.0, 5.0)) 116 eq_(feature0.properties["spot_height"], 12) 117 feature1 = collection.features[1] 118 eq_(feature1.id, old_spot.spot_id) 119 eq_(feature1.geometry.coordinates, (1, 1)) 120 112 121 new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 113 122 ok_(new_spot is not None) … … 141 150 142 151 response = FakeResponse() 143 returned_geojson = proto.update(request, response, id) 144 eq_(returned_geojson, 145 '{"geometry": {"type": "Point", "coordinates": [1.0, 1.0]}, "id": 1, "type": "Feature", "bbox": [1.0, 1.0, 1.0, 1.0], "properties": {"spot_height": 420.39999999999998}}') 152 feature = proto.update(request, response, id) 146 153 eq_(response.status, 201) 154 eq_(feature.id, 1) 155 eq_(feature.geometry.coordinates, (1.0, 1.0)) 147 156 148 157 spot = session.query(Spot).get(id) … … 177 186 spot = session.query(Spot).get(id) 178 187 ok_(spot is None) 179 188 180 189 181 190 @raises(HTTPNotFound) … … 282 291 283 292 eq_(proto.count(request, filter=filter), '1') 284 285 286 def test_protocol_show(self): 287 """Get a single point by its primary key""" 288 proto = Protocol(session, Spot) 289 290 request = FakeRequest({}) 291 response = FakeResponse() 292 293 returned_geojson = proto.show(request, response, 1) 294 eq_(returned_geojson, 295 '{"geometry": {"type": "Point", "coordinates": [0.0, 0.0]}, "id": 1, "type": "Feature", "bbox": [0.0, 0.0, 0.0, 0.0], "properties": {"spot_height": 420.39999999999998}}') 296 eq_(response.status, 0) # note that the return code is set later by Pylons 297 298 293 294 295 def test_protocol_read_all(self): 296 """Return all features""" 297 proto = Protocol(session, Spot) 298 299 collection = proto.read(FakeRequest({})) 300 ok_(collection is not None) 301 ok_(isinstance(collection, FeatureCollection)) 302 eq_(len(collection.features), 9) 303 304 305 def test_protocol_read_one(self): 306 """Return one feature""" 307 proto = Protocol(session, Spot) 308 309 feature = proto.read(FakeRequest({}), id=1) 310 ok_(feature is not None) 311 ok_(isinstance(feature, Feature)) 312 eq_(feature.id, 1) 313 eq_(feature.geometry.coordinates, (0.0, 0.0)) 314 eq_(feature.properties["spot_height"], 420.39999999999998) 315 316 299 317 @raises(HTTPNotFound) 300 def test_protocol_ show_fails(self):318 def test_protocol_read_one_fails(self): 301 319 """Try to get a single point with a wrong primary key""" 302 320 proto = Protocol(session, Spot) 303 321 304 proto.show(FakeRequest({}), FakeResponse(), -1) 305 306 307 def test_protocol_index(self): 308 """Return all features""" 309 proto = Protocol(session, Spot) 310 returned_geojson = proto.index(FakeRequest({}), FakeResponse()) 311 312 ok_(returned_geojson is not None) 313 314 factory = lambda ob: GeoJSON.to_instance(ob) 315 features = loads(returned_geojson, object_hook=factory) 316 317 ok_(isinstance(features, FeatureCollection)) 318 eq_(len(features.features), 9) 319 322 proto.read(FakeRequest({}), id=-1) 323 320 324 321 325 def test_within_distance(self): -
framework/server/trunk/mapfish/tests/test_spatialite.py
r3569 r3601 17 17 from webob.exc import HTTPNotFound 18 18 from exceptions import Exception 19 from geojson import loads, FeatureCollection, GeoJSON19 from geojson import Feature, FeatureCollection, GeoJSON 20 20 21 21 … … 101 101 102 102 response = FakeResponse() 103 returned_geojson = proto.create(request, response) 104 eq_(returned_geojson, 105 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [45.0, 5.0]}, "id": 10, "type": "Feature", "bbox": [45.0, 5.0, 45.0, 5.0], "properties": {"spot_height": 12}}]}') 103 collection = proto.create(request, response) 106 104 eq_(response.status, 201) 105 eq_(len(collection.features), 1) 106 feature0 = collection.features[0] 107 eq_(feature0.id, 10) 108 eq_(feature0.geometry.coordinates, (45.0, 5.0)) 109 eq_(feature0.properties["spot_height"], 12) 107 110 108 111 new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() … … 124 127 125 128 response = FakeResponse() 126 returned_geojson = proto.create(request, response) 127 eq_(returned_geojson, 128 '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [45.0, 5.0]}, "id": 10, "type": "Feature", "bbox": [45.0, 5.0, 45.0, 5.0], "properties": {"spot_height": 12}}, {"geometry": {"type": "Point", "coordinates": [1.0, 1.0]}, "id": 2, "type": "Feature", "bbox": [1.0, 1.0, 1.0, 1.0], "properties": {"spot_height": 102.34}}]}') 129 collection = proto.create(request, response) 129 130 eq_(response.status, 201) 130 131 eq_(len(collection.features), 2) 132 feature0 = collection.features[0] 133 eq_(feature0.id, 10) 134 eq_(feature0.geometry.coordinates, (45.0, 5.0)) 135 eq_(feature0.properties["spot_height"], 12) 136 feature1 = collection.features[1] 137 eq_(feature1.id, old_spot.spot_id) 138 eq_(feature1.geometry.coordinates, (1, 1)) 139 131 140 new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 132 141 ok_(new_spot is not None) … … 160 169 161 170 response = FakeResponse() 162 returned_geojson = proto.update(request, response, id) 163 eq_(returned_geojson, 164 '{"geometry": {"type": "Point", "coordinates": [1.0, 1.0]}, "id": 1, "type": "Feature", "bbox": [1.0, 1.0, 1.0, 1.0], "properties": {"spot_height": 420.39999999999998}}') 171 feature = proto.update(request, response, id) 165 172 eq_(response.status, 201) 166 173 eq_(feature.id, 1) 174 eq_(feature.geometry.coordinates, (1.0, 1.0)) 175 167 176 spot = session.query(Spot).get(id) 168 177 ok_(spot is not None) … … 265 274 eq_(proto.count(request), '3') 266 275 267 268 def test_protocol_show(self): 269 """Get a single point by its primary key""" 270 proto = Protocol(session, Spot) 271 272 request = FakeRequest({}) 273 response = FakeResponse() 274 275 returned_geojson = proto.show(request, response, 1) 276 eq_(returned_geojson, 277 '{"geometry": {"type": "Point", "coordinates": [0.0, 0.0]}, "id": 1, "type": "Feature", "bbox": [0.0, 0.0, 0.0, 0.0], "properties": {"spot_height": 420.39999999999998}}') 278 eq_(response.status, 0) # note that the return code is set later by Pylons 279 280 276 281 277 def test_protocol_count_custom_filter(self): 282 278 """Count all features that match a custom filter""" … … 314 310 315 311 eq_(proto.count(request, filter=filter), '1') 316 317 312 313 314 def test_protocol_read_all(self): 315 """Return all features""" 316 proto = Protocol(session, Spot) 317 318 collection = proto.read(FakeRequest({})) 319 ok_(collection is not None) 320 ok_(isinstance(collection, FeatureCollection)) 321 eq_(len(collection.features), 9) 322 323 324 def test_protocol_read_one(self): 325 """Return one feature""" 326 proto = Protocol(session, Spot) 327 328 feature = proto.read(FakeRequest({}), id=1) 329 ok_(feature is not None) 330 ok_(isinstance(feature, Feature)) 331 eq_(feature.id, 1) 332 eq_(feature.geometry.coordinates, (0.0, 0.0)) 333 eq_(feature.properties["spot_height"], 420.39999999999998) 334 proto = Protocol(session, Spot) 335 336 318 337 @raises(HTTPNotFound) 319 def test_protocol_ show_fails(self):338 def test_protocol_read_one_fails(self): 320 339 """Try to get a single point with a wrong primary key""" 321 340 proto = Protocol(session, Spot) 322 341 323 proto.show(FakeRequest({}), FakeResponse(), -1) 324 325 326 def test_protocol_index(self): 327 """Return all features""" 328 proto = Protocol(session, Spot) 329 returned_geojson = proto.index(FakeRequest({}), FakeResponse()) 330 331 ok_(returned_geojson is not None) 332 333 factory = lambda ob: GeoJSON.to_instance(ob) 334 features = loads(returned_geojson, object_hook=factory) 335 336 ok_(isinstance(features, FeatureCollection)) 337 eq_(len(features.features), 9) 338 342 proto.read(FakeRequest({}), id=-1) 343 339 344 340 345 def test_within_distance(self):
