Skip to content

Commit 6e91349

Browse files
committed
json gpkg tests
1 parent 627ac66 commit 6e91349

File tree

2 files changed

+124
-86
lines changed

2 files changed

+124
-86
lines changed

tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp

Lines changed: 124 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -488,92 +488,130 @@ void TestQgsValueRelationWidgetWrapper::testWithJsonInPostgres()
488488

489489
void TestQgsValueRelationWidgetWrapper::testWithJsonInGPKG()
490490
{
491-
/* prepared - functionality needs to be integrated first
492-
// create ogr gpkg layers
493-
QString myFileName( TEST_DATA_DIR ); //defined in CmakeLists.txt
494-
QString myTempDirName = tempDir.path();
495-
QFile::copy( myFileName + "/provider/test_json.gpkg", myTempDirName + "/test_json.gpkg" );
496-
QString myTempFileName = myTempDirName + "/test_json.gpkg";
497-
QFileInfo myMapFileInfo( myTempFileName );
498-
QgsVectorLayer *vl_json = new QgsVectorLayer( myMapFileInfo.filePath()+"|layername=reader", "test", QStringLiteral( "ogr" ) );
499-
QgsVectorLayer *vl_authors = new QgsVectorLayer( myMapFileInfo.filePath()+"|layername=author", "test", QStringLiteral( "ogr" ) );
500-
QVERIFY( vl_json->isValid() );
501-
QVERIFY( vl_authors->isValid() );
502-
503-
QgsProject::instance()->addMapLayer( vl_json, false, false );
504-
QgsProject::instance()->addMapLayer( vl_authors, false, false );
505-
506-
// build a value relation widget wrapper for bikes
507-
QgsValueRelationWidgetWrapper w_favoriteauthors( vl_json, vl_json->fields().indexOf( QStringLiteral( "json_content" ) ), nullptr, nullptr );
508-
QVariantMap cfg_favoriteautors;
509-
cfg_favoriteautors.insert( QStringLiteral( "Layer" ), vl_authors->id() );
510-
cfg_favoriteautors.insert( QStringLiteral( "Key" ), QStringLiteral( "fid" ) );
511-
cfg_favoriteautors.insert( QStringLiteral( "Value" ), QStringLiteral( "NAME" ) );
512-
cfg_favoriteautors.insert( QStringLiteral( "AllowMulti" ), true );
513-
cfg_favoriteautors.insert( QStringLiteral( "NofColumns" ), 1 );
514-
cfg_favoriteautors.insert( QStringLiteral( "AllowNull" ), false );
515-
cfg_favoriteautors.insert( QStringLiteral( "OrderByValue" ), false );
516-
cfg_favoriteautors.insert( QStringLiteral( "UseCompleter" ), false );
517-
w_favoriteauthors.setConfig( cfg_favoriteautors );
518-
w_favoriteauthors.widget();
519-
w_favoriteauthors.setEnabled( true );
520-
521-
//check if set up nice
522-
QCOMPARE( w_favoriteauthors.mTableWidget->rowCount(), 6 );
523-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
524-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "1" ) );
525-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Richard Helm" ) );
526-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "2" ) );
527-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->text(), QStringLiteral( "Ralph Johnson" ) );
528-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "3" ) );
529-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->text(), QStringLiteral( "John Vlissides" ) );
530-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "4" ) );
531-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->text(), QStringLiteral( "Douglas Adams" ) );
532-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "5" ) );
533-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->text(), QStringLiteral( "Ken Follett" ) );
534-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "6" ) );
535-
536-
//check if first feature checked correctly [4]
537-
w_favoriteauthors.setFeature( vl_json->getFeature( 1 ) );
538-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
539-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
540-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Richard Helm" ) );
541-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
542-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->text(), QStringLiteral( "Ralph Johnson" ) );
543-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Unchecked );
544-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->text(), QStringLiteral( "John Vlissides" ) );
545-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
546-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->text(), QStringLiteral( "Douglas Adams" ) );
547-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Checked );
548-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->text(), QStringLiteral( "Ken Follett" ) );
549-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
550-
551-
//check other authors
552-
w_favoriteauthors.mTableWidget->item( 0, 0 )->setCheckState( Qt::Checked);
553-
w_favoriteauthors.mTableWidget->item( 2, 0 )->setCheckState( Qt::Checked);
554-
w_favoriteauthors.mTableWidget->item( 5, 0 )->setCheckState( Qt::Checked);
555-
556-
//check if first feature checked correctly 0, 2, 4, 5
557-
w_favoriteauthors.setFeature( vl_json->getFeature( 1 ) );
558-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
559-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Checked );
560-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Richard Helm" ) );
561-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
562-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->text(), QStringLiteral( "Ralph Johnson" ) );
563-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Checked );
564-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->text(), QStringLiteral( "John Vlissides" ) );
565-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
566-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->text(), QStringLiteral( "Douglas Adams" ) );
567-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Checked );
568-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->text(), QStringLiteral( "Ken Follett" ) );
569-
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Checked );
570-
571-
// check if stored correctly
572-
vl_json->commitChanges();
573-
QVariantList expected_vl;
574-
expected_vl << 0 << 2 << 4 << 5;
575-
QCOMPARE( vl_json->getFeature( 1 ).attribute( QStringLiteral( "json_content" ) ), expected_vl );
576-
*/
491+
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
492+
// create ogr gpkg layers
493+
QString myFileName( TEST_DATA_DIR ); //defined in CmakeLists.txt
494+
QString myTempDirName = tempDir.path();
495+
QFile::copy( myFileName + "/provider/test_json.gpkg", myTempDirName + "/test_json.gpkg" );
496+
QString myTempFileName = myTempDirName + "/test_json.gpkg";
497+
QFileInfo myMapFileInfo( myTempFileName );
498+
QgsVectorLayer *vl_json = new QgsVectorLayer( myMapFileInfo.filePath() + "|layername=foo", "test", QStringLiteral( "ogr" ) );
499+
QgsVectorLayer *vl_authors = new QgsVectorLayer( myMapFileInfo.filePath() + "|layername=author", "test", QStringLiteral( "ogr" ) );
500+
QVERIFY( vl_json->isValid() );
501+
QVERIFY( vl_authors->isValid() );
502+
503+
QgsProject::instance()->addMapLayer( vl_json, false, false );
504+
QgsProject::instance()->addMapLayer( vl_authors, false, false );
505+
vl_json->startEditing();
506+
507+
// build a value relation widget wrapper for authors
508+
QgsValueRelationWidgetWrapper w_favoriteauthors( vl_json, vl_json->fields().indexOf( QStringLiteral( "json_content" ) ), nullptr, nullptr );
509+
QVariantMap cfg_favoriteautors;
510+
cfg_favoriteautors.insert( QStringLiteral( "Layer" ), vl_authors->id() );
511+
cfg_favoriteautors.insert( QStringLiteral( "Key" ), QStringLiteral( "fid" ) );
512+
cfg_favoriteautors.insert( QStringLiteral( "Value" ), QStringLiteral( "NAME" ) );
513+
cfg_favoriteautors.insert( QStringLiteral( "AllowMulti" ), true );
514+
cfg_favoriteautors.insert( QStringLiteral( "NofColumns" ), 1 );
515+
cfg_favoriteautors.insert( QStringLiteral( "AllowNull" ), false );
516+
cfg_favoriteautors.insert( QStringLiteral( "OrderByValue" ), false );
517+
cfg_favoriteautors.insert( QStringLiteral( "UseCompleter" ), false );
518+
w_favoriteauthors.setConfig( cfg_favoriteautors );
519+
w_favoriteauthors.widget();
520+
w_favoriteauthors.setEnabled( true );
521+
522+
//check if set up nice
523+
QCOMPARE( w_favoriteauthors.mTableWidget->rowCount(), 6 );
524+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
525+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "1" ) );
526+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Richard Helm" ) );
527+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "2" ) );
528+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->text(), QStringLiteral( "Ralph Johnson" ) );
529+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "3" ) );
530+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->text(), QStringLiteral( "John Vlissides" ) );
531+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "4" ) );
532+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->text(), QStringLiteral( "Douglas Adams" ) );
533+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "5" ) );
534+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->text(), QStringLiteral( "Ken Follett" ) );
535+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "6" ) );
536+
537+
w_favoriteauthors.setFeature( vl_json->getFeature( 1 ) );
538+
539+
//check if first feature checked correctly (none)
540+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
541+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
542+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Richard Helm" ) );
543+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
544+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->text(), QStringLiteral( "Ralph Johnson" ) );
545+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Unchecked );
546+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->text(), QStringLiteral( "John Vlissides" ) );
547+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
548+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->text(), QStringLiteral( "Douglas Adams" ) );
549+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
550+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->text(), QStringLiteral( "Ken Follett" ) );
551+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
552+
553+
//check other authors
554+
w_favoriteauthors.mTableWidget->item( 0, 0 )->setCheckState( Qt::Checked );
555+
w_favoriteauthors.mTableWidget->item( 2, 0 )->setCheckState( Qt::Checked );
556+
w_favoriteauthors.mTableWidget->item( 4, 0 )->setCheckState( Qt::Checked );
557+
558+
//check if first feature checked correctly 0, 2, 4 (means the fids 1, 3, 5
559+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
560+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Checked );
561+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Richard Helm" ) );
562+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
563+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->text(), QStringLiteral( "Ralph Johnson" ) );
564+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Checked );
565+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->text(), QStringLiteral( "John Vlissides" ) );
566+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
567+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->text(), QStringLiteral( "Douglas Adams" ) );
568+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Checked );
569+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->text(), QStringLiteral( "Ken Follett" ) );
570+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
571+
572+
//we do jump over the part with QgsAttributeForm::saveEdits
573+
vl_json->changeAttributeValue( 1, 4, w_favoriteauthors.value() );
574+
575+
w_favoriteauthors.setFeature( vl_json->getFeature( 2 ) );
576+
//check if second feature checked correctly (none)
577+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
578+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
579+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Richard Helm" ) );
580+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
581+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->text(), QStringLiteral( "Ralph Johnson" ) );
582+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Unchecked );
583+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->text(), QStringLiteral( "John Vlissides" ) );
584+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
585+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->text(), QStringLiteral( "Douglas Adams" ) );
586+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Unchecked );
587+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->text(), QStringLiteral( "Ken Follett" ) );
588+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
589+
590+
w_favoriteauthors.setFeature( vl_json->getFeature( 1 ) );
591+
//check if first feature checked correctly 0, 2, 4
592+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
593+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->checkState(), Qt::Checked );
594+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Richard Helm" ) );
595+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->checkState(), Qt::Unchecked );
596+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->text(), QStringLiteral( "Ralph Johnson" ) );
597+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 2, 0 )->checkState(), Qt::Checked );
598+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->text(), QStringLiteral( "John Vlissides" ) );
599+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 3, 0 )->checkState(), Qt::Unchecked );
600+
QCOMPARE( w_favoriteauthors.mTableWidget->isavtem( 4, 0 )->text(), QStringLiteral( "Douglas Adams" ) );
601+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 4, 0 )->checkState(), Qt::Checked );
602+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->text(), QStringLiteral( "Ken Follett" ) );
603+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 5, 0 )->checkState(), Qt::Unchecked );
604+
605+
// check if stored correctly
606+
vl_json->commitChanges();
607+
QVariantList expected_vl;
608+
expected_vl << "1" << "3" << "5";
609+
610+
QgsFeature f = vl_json->getFeature( 1 );
611+
QVariant attribute = f.attribute( QStringLiteral( "json_content" ) );
612+
QList<QVariant> value = attribute.toList();
613+
QCOMPARE( value, expected_vl );
614+
#endif
577615
}
578616

579617

-139 KB
Binary file not shown.

0 commit comments

Comments
 (0)