Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #30096 from elpaso/bugfix-gh30062-gpkg-copy-fid
createFeatures check unique constraints on unfiltered layer
  • Loading branch information
elpaso committed Jun 7, 2019
2 parents 613f304 + 91332ae commit 1b5fb70
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion mac/development.md
Expand Up @@ -212,7 +212,7 @@ brew unlink pyqt
brew unlink txt2tags

# Install and verify GDAL/OGR with decent driver support
# Do NOT install `gdal` (1.11.x) formula, unless you truely need it otherwise
# Do NOT install `gdal` (1.11.x) formula, unless you truly need it otherwise
# NOTE: keg-only, e.g. only available from HOMEBREW_PREFIX/opt/gdal2 prefix
brew install osgeo/osgeo4mac/gdal2 --with-complete --with-libkml
brew test osgeo/osgeo4mac/gdal2
Expand Down
2 changes: 1 addition & 1 deletion scripts/spell_check/.agignore
Expand Up @@ -65,4 +65,4 @@ tests/testdata/qgis_server/ets-wms13/project.qgs
tests/testdata/layouts/sample_project.qgs
tests/testdata/layouts/2x_template_attributetable.qpt
tests/testdata/plugindependencies_data.json

mac/development.md
14 changes: 13 additions & 1 deletion src/core/qgsvectorlayerutils.cpp
Expand Up @@ -491,7 +491,19 @@ QgsFeatureList QgsVectorLayerUtils::createFeatures( const QgsVectorLayer *layer,

// Cache unique values
if ( hasUniqueConstraint && ! uniqueValueCaches.contains( idx ) )
uniqueValueCaches[ idx ] = layer->uniqueValues( idx );
{
// If the layer is filtered, get unique values from an unfiltered clone
if ( ! layer->subsetString().isEmpty() )
{
std::unique_ptr<QgsVectorLayer> unfilteredClone { layer->clone( ) };
unfilteredClone->setSubsetString( QString( ) );
uniqueValueCaches[ idx ] = unfilteredClone->uniqueValues( idx );
}
else
{
uniqueValueCaches[ idx ] = layer->uniqueValues( idx );
}
}

// 2. client side default expression
// note - deliberately not using else if!
Expand Down
8 changes: 4 additions & 4 deletions tests/src/core/testqgsjsonutils.cpp
Expand Up @@ -184,10 +184,10 @@ void TestQgsJsonUtils::testExportFeatureJson()
QgsJsonExporter exporter { &vl };

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\"}"
",\"type\":\"Feature\"}" ) };
"[[[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\"}"
",\"type\":\"Feature\"}" ) };

const auto j( exporter.exportFeatureToJsonObject( feature ) );
QCOMPARE( QString::fromStdString( j.dump() ), expectedJson );
Expand Down
26 changes: 25 additions & 1 deletion tests/src/python/test_qgsvectorlayerutils.py
Expand Up @@ -12,6 +12,8 @@

import os
import qgis # NOQA
import shutil
import tempfile

from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsProject,
Expand All @@ -32,7 +34,7 @@
NULL
)
from qgis.testing import start_app, unittest

from utilities import unitTestDataPath

start_app()

Expand Down Expand Up @@ -614,6 +616,28 @@ def test_create_nulls_and_defaults(self):
for f in features:
self.assertEqual(f.attributes()[0], 'my_default', f.id())

def test_unique_pk_when_subset(self):
"""Test unique values on filtered layer GH #30062"""

src = unitTestDataPath('points_gpkg.gpkg')
dest = tempfile.mktemp() + '.gpkg'
shutil.copy(src, dest)
vl = QgsVectorLayer(dest, 'vl', 'ogr')
self.assertTrue(vl.isValid())
features_data = []
it = vl.getFeatures()
for _ in range(3):
f = next(it)
features_data.append(QgsVectorLayerUtils.QgsFeatureData(f.geometry(), dict(zip(range(f.fields().count()), f.attributes()))))
# Set a filter
vl.setSubsetString('"fid" in (4,5,6)')
self.assertTrue(vl.isValid())
context = vl.createExpressionContext()
features = QgsVectorLayerUtils.createFeatures(vl, features_data, context)
self.assertTrue(vl.startEditing())
vl.addFeatures(features)
self.assertTrue(vl.commitChanges())


if __name__ == '__main__':
unittest.main()
Binary file modified tests/testdata/humanbeings.gpkg
Binary file not shown.
Binary file modified tests/testdata/points_gpkg.gpkg
Binary file not shown.

0 comments on commit 1b5fb70

Please sign in to comment.