Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Short circuit if query yields no results #82

Merged
merged 3 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion esridump/dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def get_feature_count(self):
response = self._request('GET', url, params=query_args, headers=headers)
count_json = self._handle_esri_errors(response, "Could not retrieve row count")
count = count_json.get('count')
if not count:
if count is None:
raise EsriDownloadError("Server doesn't support returnCountOnly")
return count_json['count']

Expand Down Expand Up @@ -310,6 +310,11 @@ def __iter__(self):
except EsriDownloadError:
self._logger.info("Source does not support feature count")

# If there are no records matching the query, short circuit and return an empty generator.
if row_count == 0:
return
yield

page_args = []

if not self._paginate_oid and row_count is not None and (metadata.get('supportsPagination') or \
Expand Down
18 changes: 18 additions & 0 deletions tests/download_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,21 @@ def test_geo_queries_when_oid_enumeration_doesnt_work(self):
# bounding boxes.

self.assertEqual(2, len(data))

def test_empty_result_set_short_circuits(self):
self.add_fixture_response(
'.*/\?f=json.*',
'us-il-chicago/metadata.json',
method='GET',
)

self.add_fixture_response(
'.*returnCountOnly=true.*',
'us-il-chicago/count-only.json',
method='GET',
)

dump = EsriDumper(self.fake_url)
data = list(dump)

self.assertEqual(0, len(data))
1 change: 1 addition & 0 deletions tests/fixtures/us-il-chicago/count-only.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"count":0}
1 change: 1 addition & 0 deletions tests/fixtures/us-il-chicago/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"currentVersion":10.81,"id":28,"name":"Census Designated Places","type":"Feature Layer","description":"Census Designated Places; ACS 2017 - January 1, 2017 vintage","geometryType":"esriGeometryPolygon","sourceSpatialReference":{"wkid":102100,"latestWkid":3857},"copyrightText":"Source: U.S. Census Bureau","parentLayer":null,"subLayers":[],"minScale":1400000,"maxScale":100.00000000673367,"drawingInfo":{"renderer":{"type":"simple","symbol":{"type":"esriSFS","style":"esriSFSSolid","color":[0,0,0,0],"outline":{"type":"esriSLS","style":"esriSLSSolid","color":[230,161,138,255],"width":0.75}},"label":"","description":""},"transparency":15,"labelingInfo":null},"defaultVisibility":false,"extent":{"xmin":-19707613.1476,"ymin":-1633572.792,"xmax":19352664.3864,"ymax":11569901.304,"spatialReference":{"wkid":102100,"latestWkid":3857}},"hasAttachments":false,"htmlPopupType":"esriServerHTMLPopupTypeNone","displayField":"BASENAME","typeIdField":null,"subtypeFieldName":null,"subtypeField":null,"defaultSubtypeCode":null,"fields":[{"name":"MTFCC","type":"esriFieldTypeString","alias":"MTFCC","length":5,"domain":null},{"name":"OID","type":"esriFieldTypeString","alias":"OID","length":22,"domain":null},{"name":"GEOID","type":"esriFieldTypeString","alias":"GEOID","length":7,"domain":null},{"name":"STATE","type":"esriFieldTypeString","alias":"STATE","length":2,"domain":null},{"name":"PLACE","type":"esriFieldTypeString","alias":"PLACE","length":5,"domain":null},{"name":"BASENAME","type":"esriFieldTypeString","alias":"BASENAME","length":100,"domain":null},{"name":"NAME","type":"esriFieldTypeString","alias":"NAME","length":100,"domain":null},{"name":"LSADC","type":"esriFieldTypeString","alias":"LSADC","length":2,"domain":null},{"name":"FUNCSTAT","type":"esriFieldTypeString","alias":"FUNCSTAT","length":1,"domain":null},{"name":"PLACECC","type":"esriFieldTypeString","alias":"PLACECC","length":2,"domain":null},{"name":"AREALAND","type":"esriFieldTypeDouble","alias":"AREALAND","domain":null},{"name":"AREAWATER","type":"esriFieldTypeDouble","alias":"AREAWATER","domain":null},{"name":"CBSAPCI","type":"esriFieldTypeString","alias":"CBSAPCI","length":1,"domain":null},{"name":"NECTAPCI","type":"esriFieldTypeString","alias":"NECTAPCI","length":1,"domain":null},{"name":"STGEOMETRY","type":"esriFieldTypeGeometry","alias":"STGEOMETRY","domain":null},{"name":"CENTLAT","type":"esriFieldTypeString","alias":"CENTLAT","length":11,"domain":null},{"name":"CENTLON","type":"esriFieldTypeString","alias":"CENTLON","length":12,"domain":null},{"name":"INTPTLAT","type":"esriFieldTypeString","alias":"INTPTLAT","length":11,"domain":null},{"name":"INTPTLON","type":"esriFieldTypeString","alias":"INTPTLON","length":12,"domain":null},{"name":"PLACENS","type":"esriFieldTypeString","alias":"PLACENS","length":8,"domain":null},{"name":"OBJECTID","type":"esriFieldTypeOID","alias":"OBJECTID","domain":null}],"geometryField":{"name":"STGEOMETRY","type":"esriFieldTypeGeometry","alias":"STGEOMETRY"},"indexes":[{"name":"PLACE_57_BASENAME","fields":"BASENAME","isAscending":true,"isUnique":false,"description":""},{"name":"PLACE_57_GEOID","fields":"GEOID","isAscending":true,"isUnique":false,"description":""},{"name":"PLACE_57_LSADC","fields":"LSADC","isAscending":true,"isUnique":false,"description":""},{"name":"PLACE_57_MTFCC","fields":"MTFCC","isAscending":true,"isUnique":false,"description":""},{"name":"PLACE_57_OID","fields":"OID","isAscending":true,"isUnique":false,"description":""},{"name":"PLACE_57_PK","fields":"STATE,PLACE","isAscending":true,"isUnique":false,"description":""},{"name":"R1728765_SDE_ROWID_UK","fields":"OBJECTID","isAscending":true,"isUnique":true,"description":""},{"name":"A124382_IX1","fields":"STGEOMETRY","isAscending":true,"isUnique":true,"description":""}],"subtypes":[],"relationships":[],"canModifyLayer":false,"canScaleSymbols":false,"hasLabels":false,"capabilities":"Map,Data,Query","maxRecordCount":100000,"supportsStatistics":true,"supportsAdvancedQueries":true,"supportedQueryFormats":"JSON, geoJSON","isDataVersioned":false,"ownershipBasedAccessControlForFeatures":{"allowOthersToQuery":true},"useStandardizedQueries":true,"advancedQueryCapabilities":{"useStandardizedQueries":true,"supportsStatistics":true,"supportsHavingClause":true,"supportsCountDistinct":true,"supportsOrderBy":true,"supportsDistinct":true,"supportsPagination":true,"supportsTrueCurve":true,"supportsReturningQueryExtent":true,"supportsQueryWithDistance":true,"supportsSqlExpression":true},"supportsDatumTransformation":true,"supportsCoordinatesQuantization":true}