Changeset 3601 for framework

Show
Ignore:
Timestamp:
08/23/10 14:20:47 (21 months ago)
Author:
elemoine
Message:

add a geojsonify decorator, r=bbinet (closes #587)

Location:
framework/server/trunk
Files:
4 added
7 modified

Legend:

Unmodified
Added
Removed
  • framework/server/trunk/docs/framework/upgrading.txt

    r3580 r3601  
    1010MapFish 1.2 is based on Pylons 0.9.7, and MapFish 2.0 is based on Pylons 1.0. 
    1111So 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.0 
    13 Upgrading page <http://pylonshq.com/docs/en/1.0/upgrading/>`_, and follow the 
    14 provided indications. 
     12to upgrade that application from Pylons 0.9.7 to Pylons 1.0. For this refer to 
     13the `Pylons 1.0 Upgrading page <http://pylonshq.com/docs/en/1.0/upgrading/>`_, 
     14and follow the provided indications. 
    1515 
    1616.. note: 
     
    5757MapFish 2.0 
    5858~~~~~~~~~~~ 
     59 
     60GeoAlchemy 
     61^^^^^^^^^^ 
    5962 
    6063MapFish 2.0 is based on `GeoAlchemy <http://geoalchemy.org>`_. GeoAlchemy 
     
    104107        } 
    105108        the_geom = GeometryColumn(Point(srid=4326)) 
     109 
     110geojsonify 
     111^^^^^^^^^^ 
     112 
     113MapFish 2.0 introduces the ``geojsonify`` decorator for generating GeoJSON. 
     114 
     115With MapFish 1.2 the ``paster mf-layer`` command generated controller files 
     116that 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 
     155With 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 
     202Several 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  
    2121log = logging.getLogger(__name__) 
    2222 
    23 import decimal, datetime 
    24  
    2523from pylons.controllers.util import abort 
    2624 
     
    3129from geoalchemy import WKBSpatialElement 
    3230 
    33 from geojson import dumps as _dumps, loads, Feature, FeatureCollection, GeoJSON 
    34 from geojson.codec import PyGFPEncoder 
     31from geojson import Feature, FeatureCollection, loads, GeoJSON 
    3532 
    3633from mapfish.lib.filters import Filter 
     
    5047    "ilike": Comparison.ILIKE 
    5148} 
    52  
    53 class MapFishJSONEncoder(PyGFPEncoder): 
    54     # SQLAlchemy's Reflecting Tables mechanism uses decimal.Decimal 
    55     # for numeric columns and datetime.date for dates. simplejson does 
    56     # not know how to deal with objects of those types. This class provides 
    57     # 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) 
    6749 
    6850def create_geom_filter(request, mapped_class, **kwargs): 
     
    227209            self.before_delete = kwargs['before_delete'] 
    228210 
    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  
    242211    def _filter_attrs(self, feature, request): 
    243212        """ Remove some attributes from the feature and set the geometry to None 
     
    313282            return query 
    314283 
    315     def index(self, request, response, format='json', filter=None): 
    316         """ Build a query based on the filter and the request 
    317         params, send the query to the database, and return a 
    318         GeoJSON representation of the query results. """ 
    319  
    320         # only json is supported 
    321         if format != 'json': 
    322             abort(404) 
    323  
    324         return self._encode(self._query(request, filter), request, response) 
    325  
    326284    def count(self, request, filter=None): 
    327285        """ Return the number of records matching the given filter. """ 
     
    332290        return str(self.Session.query(self.mapped_class).filter(filter).count()) 
    333291 
    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 
    348307 
    349308    def create(self, request, response, execute=True): 
     
    376335        response.status = 201 
    377336        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) 
    379340        return 
    380341 
     
    397358        self.Session.commit() 
    398359        response.status = 201 
    399         return self._encode(obj, request, response) 
     360        return obj.toFeature() 
    400361 
    401362    def delete(self, request, response, id): 
  • framework/server/trunk/mapfish/templates/controller.py_tmpl

    r3580 r3601  
    88from mapfish.lib.filters import * 
    99from mapfish.lib.protocol import Protocol, create_default_filter 
     10from mapfish.decorators import geojsonify 
    1011 
    1112class {{contrClass}}Controller(BaseController): 
     
    1516        self.protocol = Protocol(Session, {{modelClass}}, self.readonly) 
    1617 
     18    @geojsonify 
    1719    def index(self, format='json'): 
    1820        """GET /: return all features.""" 
     
    5658        # else: 
    5759        #     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) 
    5964 
    60         return self.protocol.index(request, response, format=format) 
    61  
     65    @geojsonify 
    6266    def show(self, id, format='json'): 
    6367        """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) 
    6571 
     72    @geojsonify 
    6673    def create(self): 
    6774        """POST /: Create a new feature.""" 
    6875        return self.protocol.create(request, response) 
    6976 
     77    @geojsonify 
    7078    def update(self, id): 
    7179        """PUT /id: Update an existing feature.""" 
  • framework/server/trunk/mapfish/tests/test_mysql.py

    r3569 r3601  
    1616from webob.exc import HTTPNotFound 
    1717from exceptions import Exception 
    18 from geojson import loads, FeatureCollection, GeoJSON 
     18from geojson import Feature, FeatureCollection, GeoJSON 
    1919 
    2020 
     
    8282         
    8383        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) 
    8785        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 
    8992        new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 
    9093        ok_(new_spot is not None) 
     
    105108         
    106109        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) 
    110111        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)) 
    111120         
    112121        new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 
     
    141150         
    142151        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) 
    146153        eq_(response.status, 201) 
    147          
     154        eq_(feature.id, 1) 
     155        eq_(feature.geometry.coordinates, (1.0, 1.0)) 
     156 
    148157        spot = session.query(Spot).get(id) 
    149158        ok_(spot is not None) 
     
    290299             
    291300        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 
    307325    @raises(HTTPNotFound) 
    308     def test_protocol_show_fails(self): 
     326    def test_protocol_read_one_fails(self): 
    309327        """Try to get a single point with a wrong primary key""" 
    310328        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 
    328332 
    329333    def test_within_distance(self): 
  • framework/server/trunk/mapfish/tests/test_oracle.py

    r3569 r3601  
    1717from webob.exc import HTTPNotFound 
    1818from exceptions import Exception 
    19 from geojson import loads, FeatureCollection, GeoJSON 
     19from geojson import Feature, FeatureCollection, GeoJSON 
    2020import os 
    2121from sqlalchemy.schema import Sequence 
     
    2929 
    3030#engine = create_engine('oracle://gis:gis@localhost:1521/gis', echo=True) 
    31 engine = create_engine('oracle://system:system@172.16.101.131:1521/gis', echo=True) 
     31engine = create_engine('oracle://system:system@172.16.103.134:1521/gis', echo=True) 
    3232 
    3333metadata = MetaData(engine) 
     
    9898         
    9999        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) 
    103101        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 
    105108        new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 
    106109        ok_(new_spot is not None) 
     
    121124         
    122125        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) 
    126127        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 
    128137        new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 
    129138        ok_(new_spot is not None) 
     
    157166         
    158167        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) 
    162169        eq_(response.status, 201) 
     170        eq_(feature.id, 1) 
     171        eq_(feature.geometry.coordinates, (1.0, 1.0)) 
    163172         
    164173        spot = session.query(Spot).get(id) 
     
    318327             
    319328        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 
    335353    @raises(HTTPNotFound) 
    336     def test_protocol_show_fails(self): 
     354    def test_protocol_read_one_fails(self): 
    337355        """Try to get a single point with a wrong primary key""" 
    338356        proto = Protocol(session, Spot) 
    339357         
    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 
    356360 
    357361    def test_within_distance(self): 
  • framework/server/trunk/mapfish/tests/test_postgis.py

    r3569 r3601  
    1616from webob.exc import HTTPNotFound 
    1717from exceptions import Exception 
    18 from geojson import loads, FeatureCollection, GeoJSON 
     18from geojson import Feature, FeatureCollection, GeoJSON 
    1919 
    2020 
     
    8282         
    8383        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) 
    8785        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 
    8992        new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 
    9093        ok_(new_spot is not None) 
     
    105108         
    106109        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) 
    110111        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 
    112121        new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 
    113122        ok_(new_spot is not None) 
     
    141150         
    142151        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) 
    146153        eq_(response.status, 201) 
     154        eq_(feature.id, 1) 
     155        eq_(feature.geometry.coordinates, (1.0, 1.0)) 
    147156         
    148157        spot = session.query(Spot).get(id) 
     
    177186        spot = session.query(Spot).get(id) 
    178187        ok_(spot is None) 
    179          
     188 
    180189 
    181190    @raises(HTTPNotFound) 
     
    282291             
    283292        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 
    299317    @raises(HTTPNotFound) 
    300     def test_protocol_show_fails(self): 
     318    def test_protocol_read_one_fails(self): 
    301319        """Try to get a single point with a wrong primary key""" 
    302320        proto = Protocol(session, Spot) 
    303321         
    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 
    320324 
    321325    def test_within_distance(self): 
  • framework/server/trunk/mapfish/tests/test_spatialite.py

    r3569 r3601  
    1717from webob.exc import HTTPNotFound 
    1818from exceptions import Exception 
    19 from geojson import loads, FeatureCollection, GeoJSON 
     19from geojson import Feature, FeatureCollection, GeoJSON 
    2020 
    2121 
     
    101101         
    102102        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) 
    106104        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) 
    107110         
    108111        new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 
     
    124127         
    125128        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) 
    129130        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 
    131140        new_spot = session.query(Spot).filter(Spot.spot_height==12.0).one() 
    132141        ok_(new_spot is not None) 
     
    160169         
    161170        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) 
    165172        eq_(response.status, 201) 
    166          
     173        eq_(feature.id, 1) 
     174        eq_(feature.geometry.coordinates, (1.0, 1.0)) 
     175 
    167176        spot = session.query(Spot).get(id) 
    168177        ok_(spot is not None) 
     
    265274        eq_(proto.count(request), '3') 
    266275         
    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 
    281277    def test_protocol_count_custom_filter(self): 
    282278        """Count all features that match a custom filter""" 
     
    314310             
    315311        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 
    318337    @raises(HTTPNotFound) 
    319     def test_protocol_show_fails(self): 
     338    def test_protocol_read_one_fails(self): 
    320339        """Try to get a single point with a wrong primary key""" 
    321340        proto = Protocol(session, Spot) 
    322341         
    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 
    339344 
    340345    def test_within_distance(self):