Skip to content
Permalink
Browse files

Merge pull request #9668 from elpaso/bugfix-21714-filewriter-field-le…

…ngth-3_6_backport

Merge pull request #9661 from elpaso/bugfix-21714-filewriter-field-le…
  • Loading branch information
elpaso committed Apr 1, 2019
2 parents adf15e7 + f04ee09 commit edb557b050085ae4e8301177671b4e8d2859653d
Showing with 39 additions and 1 deletion.
  1. +1 −1 src/core/qgsvectorfilewriter.cpp
  2. +38 −0 tests/src/core/testqgsvectorfilewriter.cpp
@@ -542,7 +542,7 @@ void QgsVectorFileWriter::init( QString vectorFileName,
}
case QVariant::String:
ogrType = OFTString;
if ( ogrWidth <= 0 || ogrWidth > 255 )
if ( ( ogrWidth <= 0 || ogrWidth > 255 ) && mOgrDriverName == QLatin1String( "ESRI Shapefile" ) )
ogrWidth = 255;
break;

@@ -82,6 +82,8 @@ class TestQgsVectorFileWriter: public QObject
void regression1141();
//! Test prepareWriteAsVectorFormat
void prepareWriteAsVectorFormat();
//! Test regression #21714 (Exported GeoPackages have wrong field definitions)
void testTextFieldLength();

private:
// a little util fn used by all tests
@@ -491,5 +493,41 @@ void TestQgsVectorFileWriter::prepareWriteAsVectorFormat()
QCOMPARE( details.providerUriParams.value( "path" ).toString(), fileName );
}

void TestQgsVectorFileWriter::testTextFieldLength()
{
QTemporaryFile tmpFile( QDir::tempPath() + "/test_qgsvectorfilewriter2_XXXXXX.gpkg" );
tmpFile.open();
QString fileName( tmpFile.fileName( ) );
QgsVectorLayer vl( "Point?field=firstfield:string(1024)", "test", "memory" );
QCOMPARE( vl.fields().at( 0 ).length(), 1024 );
QgsVectorFileWriter w( fileName,
QStringLiteral( "UTF-8" ),
vl.fields(),
QgsWkbTypes::Point,
vl.crs() );
QgsFeature f { vl.fields() };
f.setAttribute( 0, QString( 1024, 'x' ) );
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "point(9 45)" ) ) );
QVERIFY( vl.startEditing() );
QVERIFY( vl.addFeature( f ) );
QgsVectorFileWriter::SaveVectorOptions options;
options.driverName = "GPKG";
options.layerName = "test";
QString errorMessage;
QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormat(
&vl,
fileName,
options,
&errorMessage ) );
QCOMPARE( error, QgsVectorFileWriter::WriterError::NoError );
QCOMPARE( errorMessage, fileName );
QgsVectorLayer vl2( QStringLiteral( "%1|layername=test" ).arg( fileName ), "src_test", "ogr" );
QVERIFY( vl2.isValid() );
QCOMPARE( vl2.featureCount(), 1L );
QCOMPARE( vl2.fields().at( 1 ).length(), 1024 );
QCOMPARE( vl2.getFeature( 1 ).attribute( 1 ).toString(), QString( 1024, 'x' ) );

}

QGSTEST_MAIN( TestQgsVectorFileWriter )
#include "testqgsvectorfilewriter.moc"

0 comments on commit edb557b

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