Skip to content

Commit 1c8c461

Browse files
committed
[afs] Add unit test for non OBJECTID named id fields
1 parent e3f55dc commit 1c8c461

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

src/providers/arcgisrest/qgsafsprovider.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ QgsAfsProvider::QgsAfsProvider( const QString &uri )
151151
appendError( QgsErrorMessage( tr( "Failed to determine objectIdFieldName and/or objectIds" ), QStringLiteral( "AFSProvider" ) ) );
152152
return;
153153
}
154-
objectIdFieldName = objectIdData[QStringLiteral( "objectIdFieldName" )].toString();
154+
mSharedData->mObjectIdFieldName = objectIdData[QStringLiteral( "objectIdFieldName" )].toString();
155155
for ( int idx = 0, nIdx = mSharedData->mFields.count(); idx < nIdx; ++idx )
156156
{
157-
if ( mSharedData->mFields.at( idx ).name() == objectIdFieldName )
157+
if ( mSharedData->mFields.at( idx ).name() == mSharedData->mObjectIdFieldName )
158158
{
159159
mObjectIdFieldIdx = idx;
160160

src/providers/arcgisrest/qgsafsshareddata.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ QgsFeatureIds QgsAfsSharedData::getFeatureIdsInExtent( const QgsRectangle &exten
148148

149149

150150
const QList<quint32> featuresInRect = QgsArcGisRestUtils::getObjectIdsByExtent( mDataSource.param( QStringLiteral( "url" ) ),
151+
mObjectIdFieldName,
151152
extent, errorTitle, errorText );
152153

153154
QgsFeatureIds ids;

src/providers/arcgisrest/qgsafsshareddata.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class QgsAfsSharedData : public QObject
4646
QgsRectangle mExtent;
4747
QgsWkbTypes::Type mGeometryType = QgsWkbTypes::Unknown;
4848
QgsFields mFields;
49+
QString mObjectIdFieldName;
4950
QList<quint32> mObjectIds;
5051
QMap<QgsFeatureId, QgsFeature> mCache;
5152
QgsCoordinateReferenceSystem mSourceCRS;

src/providers/arcgisrest/qgsarcgisrestutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ QVariantMap QgsArcGisRestUtils::getObjects( const QString &layerurl, const QList
407407
return queryServiceJSON( queryUrl, errorTitle, errorText );
408408
}
409409

410-
QList<quint32> QgsArcGisRestUtils::getObjectIdsByExtent( const QString &layerurl, const QgsRectangle &filterRect, QString &errorTitle, QString &errorText )
410+
QList<quint32> QgsArcGisRestUtils::getObjectIdsByExtent( const QString &layerurl, const QString &objectIdField, const QgsRectangle &filterRect, QString &errorTitle, QString &errorText )
411411
{
412412
QUrl queryUrl( layerurl + "/query" );
413413
queryUrl.addQueryItem( QStringLiteral( "f" ), QStringLiteral( "json" ) );
414-
queryUrl.addQueryItem( QStringLiteral( "where" ), QStringLiteral( "objectid=objectid" ) );
414+
queryUrl.addQueryItem( QStringLiteral( "where" ), QStringLiteral( "%1=%1" ).arg( objectIdField ) );
415415
queryUrl.addQueryItem( QStringLiteral( "returnIdsOnly" ), QStringLiteral( "true" ) );
416416
queryUrl.addQueryItem( QStringLiteral( "geometry" ), QStringLiteral( "%1,%2,%3,%4" )
417417
.arg( filterRect.xMinimum(), 0, 'f', -1 ).arg( filterRect.yMinimum(), 0, 'f', -1 )

src/providers/arcgisrest/qgsarcgisrestutils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class QgsArcGisRestUtils
4141
static QVariantMap getObjects( const QString &layerurl, const QList<quint32> &objectIds, const QString &crs,
4242
bool fetchGeometry, const QStringList &fetchAttributes, bool fetchM, bool fetchZ,
4343
const QgsRectangle &filterRect, QString &errorTitle, QString &errorText );
44-
static QList<quint32> getObjectIdsByExtent( const QString &layerurl, const QgsRectangle &filterRect, QString &errorTitle, QString &errorText );
44+
static QList<quint32> getObjectIdsByExtent( const QString &layerurl, const QString &objectIdField, const QgsRectangle &filterRect, QString &errorTitle, QString &errorText );
4545
static QByteArray queryService( const QUrl &url, QString &errorTitle, QString &errorText );
4646
static QVariantMap queryServiceJSON( const QUrl &url, QString &errorTitle, QString &errorText );
4747

tests/src/python/test_provider_afs.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def setUpClass(cls):
111111
"supportsAdvancedQueries":true,"supportedQueryFormats":"JSON, AMF",
112112
"ownershipBasedAccessControlForFeatures":{"allowOthersToQuery":true},"useStandardizedQueries":true}""".encode('UTF-8'))
113113

114-
with open(sanitize(endpoint, '/query?f=json_where=objectid=objectid_returnIdsOnly=true'), 'wb') as f:
114+
with open(sanitize(endpoint, '/query?f=json_where=OBJECTID=OBJECTID_returnIdsOnly=true'), 'wb') as f:
115115
f.write("""
116116
{
117117
"objectIdFieldName": "OBJECTID",
@@ -312,7 +312,7 @@ def setUpClass(cls):
312312
]
313313
}""".encode('UTF-8'))
314314

315-
with open(sanitize(endpoint, '/query?f=json&where=objectid=objectid&returnIdsOnly=true&geometry=-70.000000,67.000000,-60.000000,80.000000&geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelEnvelopeIntersects'), 'wb') as f:
315+
with open(sanitize(endpoint, '/query?f=json&where=OBJECTID=OBJECTID&returnIdsOnly=true&geometry=-70.000000,67.000000,-60.000000,80.000000&geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelEnvelopeIntersects'), 'wb') as f:
316316
f.write("""
317317
{
318318
"objectIdFieldName": "OBJECTID",
@@ -323,7 +323,7 @@ def setUpClass(cls):
323323
}
324324
""".encode('UTF-8'))
325325

326-
with open(sanitize(endpoint, '/query?f=json&where=objectid=objectid&returnIdsOnly=true&geometry=-73.000000,70.000000,-63.000000,80.000000&geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelEnvelopeIntersects'), 'wb') as f:
326+
with open(sanitize(endpoint, '/query?f=json&where=OBJECTID=OBJECTID&returnIdsOnly=true&geometry=-73.000000,70.000000,-63.000000,80.000000&geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelEnvelopeIntersects'), 'wb') as f:
327327
f.write("""
328328
{
329329
"objectIdFieldName": "OBJECTID",
@@ -334,7 +334,7 @@ def setUpClass(cls):
334334
}
335335
""".encode('UTF-8'))
336336

337-
with open(sanitize(endpoint, '/query?f=json&where=objectid=objectid&returnIdsOnly=true&geometry=-68.721119,68.177676,-64.678700,79.123755&geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelEnvelopeIntersects'), 'wb') as f:
337+
with open(sanitize(endpoint, '/query?f=json&where=OBJECTID=OBJECTID&returnIdsOnly=true&geometry=-68.721119,68.177676,-64.678700,79.123755&geometryType=esriGeometryEnvelope&spatialRel=esriSpatialRelEnvelopeIntersects'), 'wb') as f:
338338
f.write("""
339339
{
340340
"objectIdFieldName": "OBJECTID",
@@ -366,6 +366,47 @@ def testGetFeaturesNoGeometry(self):
366366
"""
367367
pass
368368

369+
def testObjectIdDifferentName(self):
370+
""" Test that object id fields not named OBJECTID work correctly """
371+
372+
endpoint = self.basetestpath + '/oid_fake_qgis_http_endpoint'
373+
with open(sanitize(endpoint, '?f=json'), 'wb') as f:
374+
f.write("""
375+
{"currentVersion":10.22,"id":1,"name":"QGIS Test","type":"Feature Layer","description":
376+
"QGIS Provider Test Layer.\n","geometryType":"esriGeometryPoint","copyrightText":"","parentLayer":{"id":0,"name":"QGIS Tests"},"subLayers":[],
377+
"minScale":72225,"maxScale":0,
378+
"defaultVisibility":true,
379+
"extent":{"xmin":-71.123,"ymin":66.33,"xmax":-65.32,"ymax":78.3,
380+
"spatialReference":{"wkid":4326,"latestWkid":4326}},
381+
"hasAttachments":false,"htmlPopupType":"esriServerHTMLPopupTypeAsHTMLText",
382+
"displayField":"LABEL","typeIdField":null,
383+
"fields":[{"name":"OBJECTID1","type":"esriFieldTypeOID","alias":"OBJECTID","domain":null},
384+
{"name":"pk","type":"esriFieldTypeInteger","alias":"pk","domain":null},
385+
{"name":"cnt","type":"esriFieldTypeInteger","alias":"cnt","domain":null}],
386+
"relationships":[],"canModifyLayer":false,"canScaleSymbols":false,"hasLabels":false,
387+
"capabilities":"Map,Query,Data","maxRecordCount":1000,"supportsStatistics":true,
388+
"supportsAdvancedQueries":true,"supportedQueryFormats":"JSON, AMF",
389+
"ownershipBasedAccessControlForFeatures":{"allowOthersToQuery":true},"useStandardizedQueries":true}""".encode(
390+
'UTF-8'))
391+
392+
with open(sanitize(endpoint, '/query?f=json_where=OBJECTID1=OBJECTID1_returnIdsOnly=true'), 'wb') as f:
393+
f.write("""
394+
{
395+
"objectIdFieldName": "OBJECTID1",
396+
"objectIds": [
397+
5,
398+
3,
399+
1,
400+
2,
401+
4
402+
]
403+
}
404+
""".encode('UTF-8'))
405+
406+
# Create test layer
407+
vl = QgsVectorLayer("url='http://" + endpoint + "' crs='epsg:4326'", 'test', 'arcgisfeatureserver')
408+
assert vl.isValid()
409+
369410

370411
if __name__ == '__main__':
371412
unittest.main()

0 commit comments

Comments
 (0)