Skip to content

Commit

Permalink
Fix json utils tests after FID_NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jun 22, 2020
1 parent 0a42144 commit 6d2e352
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/core/qgsjsonutils.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgsapplication.h"
#include "qgsfeatureid.h"

#include <QJsonDocument>
#include <QJsonArray>
Expand Down Expand Up @@ -94,6 +95,10 @@ json QgsJsonExporter::exportFeatureToJsonObject( const QgsFeature &feature, cons
featureJson["id"] = id.toString().toStdString();
}
}
else if ( FID_IS_NULL( feature.id() ) )
{
featureJson["id"] = nullptr;
}
else
{
featureJson["id"] = feature.id();
Expand Down
6 changes: 4 additions & 2 deletions tests/src/core/testqgsjsonutils.cpp
Expand Up @@ -223,7 +223,7 @@ void TestQgsJsonUtils::testExportFeatureJson()
const auto expectedJson { QStringLiteral( "{\"bbox\":[1.12,1.12,5.45,5.33],\"geometry\":{\"coordinates\":"
"[[[1.12,1.34],[5.45,1.12],[5.34,5.33],[1.56,5.2],[1.12,1.34]],"
"[[2.0,2.0],[3.0,2.0],[3.0,3.0],[2.0,3.0],[2.0,2.0]]],\"type\":\"Polygon\"}"
",\"id\":0,\"properties\":{\"flddbl\":2.0,\"fldint\":1,\"fldtxt\":\"a value\"}"
",\"id\":null,\"properties\":{\"flddbl\":2.0,\"fldint\":1,\"fldtxt\":\"a value\"}"
",\"type\":\"Feature\"}" ) };

const auto j( exporter.exportFeatureToJsonObject( feature ) );
Expand All @@ -233,12 +233,14 @@ void TestQgsJsonUtils::testExportFeatureJson()

QgsJsonExporter exporterPrecision { &vl, 1 };


const auto expectedJsonPrecision { QStringLiteral( "{\"bbox\":[1.1,1.1,5.5,5.3],\"geometry\":{\"coordinates\":"
"[[[1.1,1.3],[5.5,1.1],[5.3,5.3],[1.6,5.2],[1.1,1.3]],"
"[[2.0,2.0],[3.0,2.0],[3.0,3.0],[2.0,3.0],[2.0,2.0]]],\"type\":\"Polygon\"}"
",\"id\":0,\"properties\":{\"flddbl\":2.0,\"fldint\":1,\"fldtxt\":\"a value\"}"
",\"id\":123,\"properties\":{\"flddbl\":2.0,\"fldint\":1,\"fldtxt\":\"a value\"}"
",\"type\":\"Feature\"}" ) };

feature.setId( 123 );
const auto jPrecision( exporterPrecision.exportFeatureToJsonObject( feature ) );
QCOMPARE( QString::fromStdString( jPrecision.dump() ), expectedJsonPrecision );
const auto jsonPrecision { exporterPrecision.exportFeature( feature ) };
Expand Down
25 changes: 13 additions & 12 deletions tests/src/python/test_qgsjsonutils.py
Expand Up @@ -502,7 +502,7 @@ def testExportFeatureFieldFormatter(self):
source = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"parent", "memory")
pr = source.dataProvider()
pf1 = QgsFeature()
pf1 = QgsFeature(123)
pf1.setFields(source.fields())
pf1.setAttributes(["test1", 1])
pf2 = QgsFeature()
Expand All @@ -518,7 +518,7 @@ def testExportFeatureFieldFormatter(self):

expected = """{
"geometry": null,
"id": 0,
"id": 123,
"properties": {
"fldint": "one",
"fldtxt": "test1"
Expand Down Expand Up @@ -578,10 +578,10 @@ def testExportFeatureRelations(self):
parent = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer&field=foreignkey:integer",
"parent", "memory")
pr = parent.dataProvider()
pf1 = QgsFeature()
pf1 = QgsFeature(432)
pf1.setFields(parent.fields())
pf1.setAttributes(["test1", 67, 123])
pf2 = QgsFeature()
pf2 = QgsFeature(876)
pf2.setFields(parent.fields())
pf2.setAttributes(["test2", 68, 124])
assert pr.addFeatures([pf1, pf2])
Expand Down Expand Up @@ -622,7 +622,7 @@ def testExportFeatureRelations(self):

expected = """{
"geometry": null,
"id": 0,
"id": 432,
"properties": {
"fldint": 67,
"fldtxt": "test1",
Expand All @@ -646,7 +646,7 @@ def testExportFeatureRelations(self):

expected = """{
"geometry": null,
"id": 0,
"id": 876,
"properties": {
"fldint": 68,
"fldtxt": "test2",
Expand All @@ -669,7 +669,7 @@ def testExportFeatureRelations(self):
child.setEditorWidgetSetup(1, setup)
expected = """{
"geometry": null,
"id": 0,
"id": 432,
"properties": {
"fldint": 67,
"fldtxt": "test1",
Expand Down Expand Up @@ -697,7 +697,7 @@ def testExportFeatureRelations(self):

expected = """{
"geometry": null,
"id": 0,
"id": 876,
"properties": {
"fldint": 68,
"fldtxt": "test2",
Expand All @@ -713,7 +713,7 @@ def testExportFeatureRelations(self):

expected = """{
"geometry": null,
"id": 0,
"id": 876,
"properties": {
"fldint": 68,
"fldtxt": "test2",
Expand Down Expand Up @@ -875,7 +875,8 @@ def testExportFieldAlias(self):
pf2 = QgsFeature()
pf2.setFields(source.fields())
pf2.setAttributes(["test2", 2])
assert pr.addFeatures([pf1, pf2])
result, features = pr.addFeatures([pf1, pf2])
self.assertTrue(result)

source.setFieldAlias(0, "alias_fldtxt")
source.setFieldAlias(1, "alias_fldint")
Expand All @@ -886,14 +887,14 @@ def testExportFieldAlias(self):

expected = """{
"geometry": null,
"id": 0,
"id": 1,
"properties": {
"alias_fldint": 1,
"alias_fldtxt": "test1"
},
"type": "Feature"
}"""
self.assertEqual(exporter.exportFeature(pf1, indent=2), expected)
self.assertEqual(exporter.exportFeature(features[0], indent=2), expected)


if __name__ == "__main__":
Expand Down

0 comments on commit 6d2e352

Please sign in to comment.