Skip to content

Commit

Permalink
PostgreSQL provider: fix paging (#1594) (#1628)
Browse files Browse the repository at this point in the history
* PostgreSQL provider: fix paging (#1594)

* add test
  • Loading branch information
tomkralidis committed Apr 15, 2024
1 parent 21b8672 commit b4b7ee1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
16 changes: 6 additions & 10 deletions pygeoapi/provider/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,9 @@ def query(self, offset=0, limit=10, resulttype='results',
.filter(property_filters)
.filter(cql_filters)
.filter(bbox_filter)
.order_by(*order_by_clauses)
.options(selected_properties)
.offset(offset))
.options(selected_properties))

matched = results.count()
if limit < matched:
returned = limit
else:
returned = matched

LOGGER.debug(f'Found {matched} result(s)')

Expand All @@ -168,14 +162,16 @@ def query(self, offset=0, limit=10, resulttype='results',
'type': 'FeatureCollection',
'features': [],
'numberMatched': matched,
'numberReturned': returned
'numberReturned': 0
}

if resulttype == "hits" or not results:
response['numberReturned'] = 0
return response

crs_transform_out = self._get_crs_transform(crs_transform_spec)
for item in results.limit(limit):

for item in results.order_by(*order_by_clauses).offset(offset).limit(limit): # noqa
response['numberReturned'] += 1
response['features'].append(
self._sqlalchemy_to_feature(item, crs_transform_out)
)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ def test_query_with_property_filter(config):
assert feature_collection['numberReturned'] == 50


def test_query_with_paging(config):
"""Test query valid features with paging"""
p = PostgreSQLProvider(config)
feature_collection = p.query(limit=50)

assert feature_collection['numberMatched'] == 14776
assert feature_collection['numberReturned'] == 50

offset = feature_collection['numberMatched'] - 10

feature_collection = p.query(offset=offset)
assert feature_collection['numberMatched'] == 14776
assert feature_collection['numberReturned'] == 10


def test_query_with_config_properties(config):
"""
Test that query is restricted by properties in the config.
Expand Down

0 comments on commit b4b7ee1

Please sign in to comment.