def test_create_update(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest from geojson import Feature, FeatureCollection from shapely.geometry import Point MappedClass = self._get_mapped_class() # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return {'a': mapped_class(Feature(id='a')), 'b': mapped_class(Feature(id='b'))} def flush(self): pass proto = Protocol(MockSession, MappedClass, 'geom') # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'POST' request.body = '{"type": "FeatureCollection", "features": [{"type": "Feature", "id": "a", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}, {"type": "Feature", "id": "b", "properties": {"text": "bar"}, "geometry": {"type": "Point", "coordinates": [46, 6]}}]}' # NOQA features = proto.create(request) self.assertTrue(isinstance(features, FeatureCollection)) self.assertEqual(len(features.features), 2) self.assertEqual(features.features[0].id, 'a') self.assertEqual(features.features[0].text, 'foo') self.assertTrue(features.features[0].geom.shape.equals(Point(45, 5))) self.assertEqual(features.features[1].id, 'b') self.assertEqual(features.features[1].text, 'bar') self.assertTrue(features.features[1].geom.shape.equals(Point(46, 6)))
def test_delete(self): from papyrus.protocol import Protocol from geojson import Feature from pyramid.response import Response MappedClass = self._get_mapped_class() # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return {'a': mapped_class(Feature())} def delete(self, obj): pass # a before_update callback def before_delete(request, obj): request._log = dict(obj=obj) proto = Protocol(MockSession, MappedClass, "geom", before_delete=before_delete) request = testing.DummyRequest() response = proto.delete(request, 'a') self.assertTrue(isinstance(response, Response)) self.assertEqual(response.status_int, 204) # test before_delete self.assertTrue(hasattr(request, '_log')) self.assertTrue(isinstance(request._log["obj"], MappedClass))
def test_create_update(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest from geojson import Feature, FeatureCollection from shapely.geometry import Point MappedClass = self._get_mapped_class() # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return { 'a': mapped_class(Feature(id='a')), 'b': mapped_class(Feature(id='b')) } def flush(self): pass proto = Protocol(MockSession, MappedClass, 'geom') # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'POST' request.body = '{"type": "FeatureCollection", "features": [{"type": "Feature", "id": "a", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}, {"type": "Feature", "id": "b", "properties": {"text": "bar"}, "geometry": {"type": "Point", "coordinates": [46, 6]}}]}' features = proto.create(request) self.assertTrue(isinstance(features, FeatureCollection)) self.assertEqual(len(features.features), 2) self.assertEqual(features.features[0].id, 'a') self.assertEqual(features.features[0].text, 'foo') self.assertTrue(features.features[0].geom.shape.equals(Point(45, 5))) self.assertEqual(features.features[1].id, 'b') self.assertEqual(features.features[1].text, 'bar') self.assertTrue(features.features[1].geom.shape.equals(Point(46, 6)))
def test___query(self): from papyrus.protocol import Protocol, create_attr_filter from mock import patch engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") request = testing.DummyRequest() with patch('sqlalchemy.orm.query.Query.all', lambda q : q): query = proto._query(request) self.assertTrue("SELECT" in query_to_str(query, engine)) request = testing.DummyRequest(params={"queryable": "id", "id__eq": "1"}) with patch('sqlalchemy.orm.query.Query.all', lambda q : q): query = proto._query(request) self.assertTrue("WHERE" in query_to_str(query, engine)) request = testing.DummyRequest(params={"queryable": "id", "id__eq": "1"}) with patch('sqlalchemy.orm.query.Query.all', lambda q : q): filter = create_attr_filter(request, MappedClass) query = proto._query(testing.DummyRequest(), filter=filter) self.assertTrue("WHERE" in query_to_str(query, engine)) request = testing.DummyRequest(params={"limit": "2"}) with patch('sqlalchemy.orm.query.Query.all', lambda q : q): query = proto._query(request) self.assertTrue("LIMIT" in query_to_str(query, engine)) request = testing.DummyRequest(params={"maxfeatures": "2"}) with patch('sqlalchemy.orm.query.Query.all', lambda q : q): query = proto._query(request) self.assertTrue("LIMIT" in query_to_str(query, engine)) request = testing.DummyRequest(params={"limit": "2", "offset": "10"}) with patch('sqlalchemy.orm.query.Query.all', lambda q : q): query = proto._query(request) self.assertTrue("OFFSET" in query_to_str(query, engine)) request = testing.DummyRequest(params={"order_by": "text"}) with patch('sqlalchemy.orm.query.Query.all', lambda q : q): query = proto._query(request) self.assertTrue("ORDER BY" in query_to_str(query, engine)) self.assertTrue("ASC" in query_to_str(query, engine)) request = testing.DummyRequest(params={"sort": "text"}) with patch('sqlalchemy.orm.query.Query.all', lambda q : q): query = proto._query(request) self.assertTrue("ORDER BY" in query_to_str(query, engine)) self.assertTrue("ASC" in query_to_str(query, engine)) request = testing.DummyRequest(params={"order_by": "text", "dir": "DESC"}) with patch('sqlalchemy.orm.query.Query.all', lambda q : q): query = proto._query(request) self.assertTrue("ORDER BY" in query_to_str(query, engine)) self.assertTrue("DESC" in query_to_str(query, engine))
def test_delete_forbidden(self): from papyrus.protocol import Protocol engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom", readonly=True) request = testing.DummyRequest() response = proto.delete(request, 1) self.assertTrue(response.headers.get('Allow') == "GET, HEAD") self.assertEqual(response.status_int, 405)
def get_property(request): if 'id' not in request.params: return HTTPBadRequest(detail='Please add a valid id in your request') id_ = request.params['id'] proto = Protocol(DBSession, Property, 'geom') filter = "id IN ('" + id_ + "')" return proto.read(request, filter=filter)
def test_delete_notfound(self): from papyrus.protocol import Protocol # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return {} proto = Protocol(MockSession, self._get_mapped_class(), "geom") request = testing.DummyRequest() response = proto.delete(request, 1) self.assertEqual(response.status_int, 404)
def test_read_notfound(self): from papyrus.protocol import Protocol from pyramid.httpexceptions import HTTPNotFound class Session(object): def query(self, mapped_class): return {'a': None} proto = Protocol(Session, self._get_mapped_class(), 'geom') request = testing.DummyRequest() resp = proto.read(request, id='a') self.assertTrue(isinstance(resp, HTTPNotFound))
def test_create(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest from pyramid.response import Response engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() class MockSession(object): def add(self, o): Session.add(o) def flush(self): pass # a before_update callback def before_create(request, feature, obj): if not hasattr(request, '_log'): request._log = [] request._log.append(dict(feature=feature, obj=obj)) proto = Protocol(MockSession, MappedClass, "geom", before_create=before_create) # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'POST' request.body = '{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}, {"type": "Feature", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}]}' proto.create(request) self.assertEqual(len(Session.new), 2) for obj in Session.new: self.assertEqual(obj.text, 'foo') self.assertEqual(obj.geom.shape.x, 45) self.assertEqual(obj.geom.shape.y, 5) Session.rollback() # test before_create self.assertTrue(hasattr(request, '_log')) self.assertEqual(len(request._log), 2) self.assertEqual(request._log[0]['feature'].properties['text'], 'foo') self.assertEqual(request._log[0]['obj'], None) self.assertEqual(request._log[1]['feature'].properties['text'], 'foo') self.assertEqual(request._log[1]['obj'], None) # test response status self.assertEqual(request.response.status_int, 201)
def test_create(self): from papyrus.protocol import Protocol from pyramid.request import Request from pyramid.response import Response engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() class MockSession(object): def add(self, o): Session.add(o) def flush(self): pass # a before_update callback def before_create(request, feature, obj): if not hasattr(request, '_log'): request._log = [] request._log.append(dict(feature=feature, obj=obj)) proto = Protocol(MockSession, MappedClass, "geom", before_create=before_create) # we need an actual Request object here, for body to do its job request = Request({}) request.method = 'POST' request.body = '{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}, {"type": "Feature", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}]}' proto.create(request) self.assertEqual(len(request.response_callbacks), 1) self.assertEqual(len(Session.new), 2) for obj in Session.new: self.assertEqual(obj.text, 'foo') self.assertEqual(obj.geom.shape.x, 45) self.assertEqual(obj.geom.shape.y, 5) Session.rollback() # test before_create self.assertTrue(hasattr(request, '_log')) self.assertEqual(len(request._log), 2) self.assertEqual(request._log[0]['feature'].properties['text'], 'foo') self.assertEqual(request._log[0]['obj'], None) self.assertEqual(request._log[1]['feature'].properties['text'], 'foo') self.assertEqual(request._log[1]['obj'], None) # test response status response = Response(status_int=400) request._process_response_callbacks(response) self.assertEqual(response.status_int, 201)
def test_create_badrequest(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'POST' request.body = '{"type": "Feature", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}' # NOQA response = proto.create(request) self.assertEqual(response.status_int, 400)
def test__filter_attrs(self): from papyrus.protocol import Protocol, create_attr_filter from geojson import Feature engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") feature = Feature(properties={'foo': 'foo', 'bar': 'bar', 'foobar': 'foobar'}) request = testing.DummyRequest(params={'attrs': 'bar,foo'}) feature = proto._filter_attrs(feature, request) self.assertEqual(feature.properties, {'foo': 'foo', 'bar': 'bar'})
def test_create_badrequest(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'POST' request.body = '{"type": "Feature", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}' response = proto.create(request) self.assertEqual(response.status_int, 400)
def test_update_forbidden(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom", readonly=True) # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'PUT' request.body = '{"type": "Feature", "id": 1, "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}' # NOQA response = proto.update(request, 1) self.assertTrue(response.headers.get('Allow') == "GET, HEAD") self.assertEqual(response.status_int, 405)
def test_create_empty(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'POST' request.body = '{"type": "FeatureCollection", "features": []}' resp = proto.create(request) self.assertEqual(resp, None)
def test__filter_attrs(self): from papyrus.protocol import Protocol from geojson import Feature engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") feature = Feature(properties={'foo': 'foo', 'bar': 'bar', 'foobar': 'foobar'}) request = testing.DummyRequest(params={'attrs': 'bar,foo'}) feature = proto._filter_attrs(feature, request) self.assertEqual(feature.properties, {'foo': 'foo', 'bar': 'bar'})
def test_count(self): from papyrus.protocol import Protocol from mock import patch engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") # We make Query.count return the query and just check it includes # "SELECT". Yes, not so good! request = testing.DummyRequest() with patch('sqlalchemy.orm.query.Query.count', lambda q : q): query = proto.count(request) self.assertTrue("SELECT" in query_to_str(query, engine))
def test_create_forbidden(self): from papyrus.protocol import Protocol from pyramid.request import Request from StringIO import StringIO engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom", readonly=True) # we need an actual Request object here, for body_file to do its job request = Request({}) request.body_file = StringIO('{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}, {"type": "Feature", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}]}') response = proto.create(request) self.assertTrue(response.headers.get('Allow') == "GET, HEAD") self.assertEqual(response.status_int, 405)
def test_count(self): from papyrus.protocol import Protocol from mock import patch engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") # We make Query.count return the query and just check it includes # "SELECT". Yes, not so good! request = testing.DummyRequest() with patch('sqlalchemy.orm.query.Query.count', lambda q: q): query = proto.count(request) self.assertTrue(b"SELECT" in query_to_str(query, engine))
def test_update_forbidden(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom", readonly=True) # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'PUT' request.body = '{"type": "Feature", "id": 1, "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}' response = proto.update(request, 1) self.assertTrue(response.headers.get('Allow') == "GET, HEAD") self.assertEqual(response.status_int, 405)
def test_create_empty(self): from papyrus.protocol import Protocol from pyramid.request import Request from pyramid.response import Response from StringIO import StringIO engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") # we need an actual Request object here, for body_file to do its job request = Request({}) request.body_file = StringIO('{"type": "FeatureCollection", "features": []}') resp = proto.create(request) self.assertEqual(resp, None)
def test_read_id(self): from papyrus.protocol import Protocol from shapely.geometry import Point from geojson import Feature class Session(object): def query(self, mapped_class): feature = Feature(id='a', geometry=Point(1, 2), properties=dict(text='foo')) return {'a': mapped_class(feature)} proto = Protocol(Session, self._get_mapped_class(), 'geom') request = testing.DummyRequest() feature = proto.read(request, id='a') self.assertEqual(feature.id, 'a') self.assertEqual(feature.properties['text'], 'foo')
def test_update_notfound(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest MappedClass = self._get_mapped_class() # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return {} proto = Protocol(MockSession, MappedClass, "geom") # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'PUT' request.body = '{"type": "Feature", "id": 1, "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}' # NOQA response = proto.update(request, 1) self.assertEqual(response.status_int, 404)
def test_update_badrequest(self): from papyrus.protocol import Protocol from pyramid.testing import DummyRequest # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return {'a': {}} proto = Protocol(MockSession, self._get_mapped_class(), "geom") # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'PUT' request.body = '{"type": "Point", "coordinates": [45, 5]}' response = proto.update(request, 'a') self.assertEqual(response.status_int, 400)
def test__filter_attrs_no_geom(self): from papyrus.protocol import Protocol from geojson import Feature from shapely.geometry import Point engine = self._get_engine() Session = self._get_session(engine) Session.bind = engine MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") feature = Feature(geometry=Point(1.0, 2.0)) request = testing.DummyRequest(params={'no_geom': 'true'}) feature = proto._filter_attrs(feature, request) self.assertEqual(feature.geometry, None)
def test__filter_attrs_no_geom(self): from papyrus.protocol import Protocol, create_attr_filter from geojson import Feature from shapely.geometry import Point engine = self._get_engine() Session = self._get_session(engine) Session.bind = engine MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") feature = Feature(geometry=Point(1.0, 2.0)) request = testing.DummyRequest(params={'no_geom': 'true'}) feature = proto._filter_attrs(feature, request) self.assertEqual(feature.geometry, None)
def test_update(self): from papyrus.protocol import Protocol from geojson import Feature from pyramid.testing import DummyRequest from pyramid.response import Response from geoalchemy2.elements import WKBElement MappedClass = self._get_mapped_class() # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return {'a': MappedClass(Feature(id='a'))} def flush(self): pass # a before_update callback def before_update(request, feature, obj): request._log = dict(feature=feature, obj=obj) proto = Protocol(MockSession, MappedClass, "geom", before_update=before_update) # we need an actual Request object here, for body to do its job request = DummyRequest({}) request.method = 'PUT' request.body = '{"type": "Feature", "id": "a", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}' obj = proto.update(request, "a") self.assertTrue(isinstance(obj, MappedClass)) self.assertTrue(isinstance(obj.geom, WKBElement)) self.assertEqual(obj.text, "foo") # test before_update self.assertTrue(hasattr(request, '_log')) self.assertEqual(request._log["feature"].id, 'a') self.assertEqual(request._log["feature"].properties['text'], 'foo') self.assertTrue(isinstance(request._log["obj"], MappedClass)) # test response status self.assertEqual(request.response.status_int, 201)
def test_update(self): from papyrus.protocol import Protocol from geojson import Feature from pyramid.request import Request from pyramid.response import Response from geoalchemy import WKBSpatialElement MappedClass = self._get_mapped_class() # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return {'a': MappedClass(Feature(id='a'))} def flush(self): pass # a before_update callback def before_update(request, feature, obj): request._log = dict(feature=feature, obj=obj) proto = Protocol(MockSession, MappedClass, "geom", before_update=before_update) # we need an actual Request object here, for body to do its job request = Request({}) request.method = 'PUT' request.body = '{"type": "Feature", "id": "a", "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}' obj = proto.update(request, "a") self.assertEqual(len(request.response_callbacks), 1) self.assertTrue(isinstance(obj, MappedClass)) self.assertTrue(isinstance(obj.geom, WKBSpatialElement)) self.assertEqual(obj.text, "foo") # test before_update self.assertTrue(hasattr(request, '_log')) self.assertEqual(request._log["feature"].id, 'a') self.assertEqual(request._log["feature"].properties['text'], 'foo') self.assertTrue(isinstance(request._log["obj"], MappedClass)) # test response status response = Response(status_int=400) request._process_response_callbacks(response) self.assertEqual(response.status_int, 201)
def test_update_notfound(self): from papyrus.protocol import Protocol from pyramid.request import Request from StringIO import StringIO engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() # a mock session specific to this test class MockSession(object): def query(self, mapped_class): return {} proto = Protocol(MockSession, MappedClass, "geom") # we need an actual Request object here, for body_file to do its job request = Request({}) request.body_file = StringIO('{"type": "Feature", "id": 1, "properties": {"text": "foo"}, "geometry": {"type": "Point", "coordinates": [45, 5]}}') response = proto.update(request, 1) self.assertEqual(response.status_int, 404)
def test_read_many(self): from papyrus.protocol import Protocol from shapely.geometry import Point from geojson import Feature, FeatureCollection engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, 'geom') def _query(request, filter): f1 = Feature(geometry=Point(1, 2)) f2 = Feature(geometry=Point(2, 3)) return [MappedClass(f1), MappedClass(f2)] proto._query = _query features = proto.read(testing.DummyRequest()) self.assertTrue(isinstance(features, FeatureCollection)) self.assertEqual(len(features.features), 2)
def _get_protocol_for_layer(layer, **kwargs): """ Returns a papyrus ``Protocol`` for the ``Layer`` object. """ cls = get_class(str(layer.geoTable)) geom_attr = _get_geom_col_info(layer)[0] return Protocol(DBSession, cls, geom_attr, **kwargs)
def test___query(self): from papyrus.protocol import Protocol, create_attr_filter from mock import patch engine = self._get_engine() Session = self._get_session(engine) MappedClass = self._get_mapped_class() proto = Protocol(Session, MappedClass, "geom") request = testing.DummyRequest() with patch('sqlalchemy.orm.query.Query.all', lambda q: q): query = proto._query(request) self.assertTrue(b"SELECT" in query_to_str(query, engine)) request = testing.DummyRequest(params={ "queryable": "id", "id__eq": "1" }) with patch('sqlalchemy.orm.query.Query.all', lambda q: q): query = proto._query(request) self.assertTrue(b"WHERE" in query_to_str(query, engine)) request = testing.DummyRequest(params={ "queryable": "id", "id__eq": "1" }) with patch('sqlalchemy.orm.query.Query.all', lambda q: q): filter = create_attr_filter(request, MappedClass) query = proto._query(testing.DummyRequest(), filter=filter) self.assertTrue(b"WHERE" in query_to_str(query, engine)) request = testing.DummyRequest(params={"limit": "2"}) with patch('sqlalchemy.orm.query.Query.all', lambda q: q): query = proto._query(request) self.assertTrue(b"LIMIT" in query_to_str(query, engine)) request = testing.DummyRequest(params={"maxfeatures": "2"}) with patch('sqlalchemy.orm.query.Query.all', lambda q: q): query = proto._query(request) self.assertTrue(b"LIMIT" in query_to_str(query, engine)) request = testing.DummyRequest(params={"limit": "2", "offset": "10"}) with patch('sqlalchemy.orm.query.Query.all', lambda q: q): query = proto._query(request) self.assertTrue(b"OFFSET" in query_to_str(query, engine)) request = testing.DummyRequest(params={"order_by": "text"}) with patch('sqlalchemy.orm.query.Query.all', lambda q: q): query = proto._query(request) self.assertTrue(b"ORDER BY" in query_to_str(query, engine)) self.assertTrue(b"ASC" in query_to_str(query, engine)) request = testing.DummyRequest(params={"sort": "text"}) with patch('sqlalchemy.orm.query.Query.all', lambda q: q): query = proto._query(request) self.assertTrue(b"ORDER BY" in query_to_str(query, engine)) self.assertTrue(b"ASC" in query_to_str(query, engine)) request = testing.DummyRequest(params={ "order_by": "text", "dir": "DESC" }) with patch('sqlalchemy.orm.query.Query.all', lambda q: q): query = proto._query(request) self.assertTrue(b"ORDER BY" in query_to_str(query, engine)) self.assertTrue(b"DESC" in query_to_str(query, engine))
def _get_protocol_for_layer(self, layer, **kwargs): """ Returns a papyrus ``Protocol`` for the ``Layer`` object. """ cls = get_layer_class(layer) geom_attr = self._get_geom_col_info(layer)[0] return Protocol(models.DBSession, cls, geom_attr, **kwargs)