Skip to content
Permalink
Browse files

Fix json utils tests after FID_NULL

  • Loading branch information
elpaso committed Jun 22, 2020
1 parent 0a42144 commit 6d2e352d5ebd169fc2e64accccbe9541b546a907
Showing with 22 additions and 14 deletions.
  1. +5 −0 src/core/qgsjsonutils.cpp
  2. +4 −2 tests/src/core/testqgsjsonutils.cpp
  3. +13 −12 tests/src/python/test_qgsjsonutils.py
@@ -26,6 +26,7 @@
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgsapplication.h"
#include "qgsfeatureid.h"

#include <QJsonDocument>
#include <QJsonArray>
@@ -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();
@@ -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 ) );
@@ -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 ) };
@@ -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()
@@ -518,7 +518,7 @@ def testExportFeatureFieldFormatter(self):

expected = """{
"geometry": null,
"id": 0,
"id": 123,
"properties": {
"fldint": "one",
"fldtxt": "test1"
@@ -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])
@@ -622,7 +622,7 @@ def testExportFeatureRelations(self):

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

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

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

expected = """{
"geometry": null,
"id": 0,
"id": 876,
"properties": {
"fldint": 68,
"fldtxt": "test2",
@@ -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")
@@ -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__":

0 comments on commit 6d2e352

Please sign in to comment.
You can’t perform that action at this time.