| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879 |
- /***************************************************************************
- test_deltafilewrapper.h
- -----------------------
- begin : Apr 2020
- copyright : (C) 2020 by Ivan Ivanov
- email : ivan@opengis.ch
- ***************************************************************************/
- /***************************************************************************
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, or *
- * (at your option) any later version. *
- * *
- ***************************************************************************/
- #define QFIELDTEST_MAIN
- #include "deltafilewrapper.h"
- #include "qfield.h"
- #include "utils/fileutils.h"
- #include "utils/qfieldcloudutils.h"
- #include "qgsvectorlayerjoininfo.h"
- #include "qgssettings.h"
- #include "qgsapplication.h"
- #include "catch2.h"
- #include <qgsproject.h>
- #include <QFileInfo>
- QT_BEGIN_NAMESPACE
- std::ostream &operator << ( std::ostream &os, const QJsonDocument &value )
- {
- os << value.toJson().constData();
- return os;
- }
- QT_END_NAMESPACE
- QJsonArray normalizeDeltasSchema( const QJsonArray &deltasJson )
- {
- QStringList localLayerIds;
- QStringList sourceLayerIds;
- QJsonArray deltas;
- // normalize layerIds
- for ( const QJsonValue &v : deltasJson )
- {
- QJsonObject deltaItem = v.toObject();
- const QString localLayerId = deltaItem.value( QStringLiteral( "localLayerId" ) ).toString();
- const QString sourceLayerId = deltaItem.value( QStringLiteral( "sourceLayerId" ) ).toString();
- if ( !localLayerIds.contains( localLayerId ) )
- localLayerIds.append( localLayerId );
- if ( !sourceLayerIds.contains( sourceLayerId ) )
- sourceLayerIds.append( sourceLayerId );
- int localLayerIdx = localLayerIds.indexOf( localLayerId ) + 1;
- int sourceLayerIdx = sourceLayerIds.indexOf( sourceLayerId ) + 1;
- deltaItem.insert( QStringLiteral( "localLayerId" ), QStringLiteral( "dummyLayerIdL%1" ).arg( localLayerIdx ) );
- deltaItem.insert( QStringLiteral( "sourceLayerId" ), QStringLiteral( "dummyLayerIdS%1" ).arg( sourceLayerIdx ) );
- deltaItem.insert( QStringLiteral( "uuid" ), QStringLiteral( "11111111-1111-1111-1111-111111111111" ) );
- deltas.append( deltaItem );
- }
- return deltas;
- }
- QJsonArray normalizeFilesSchema( const QJsonArray &filesJson )
- {
- QJsonArray files;
- // normalize file names
- int i = 0;
- while ( i < filesJson.size() )
- files.append( QStringLiteral( "file%1.jpg" ).arg( i++ ) );
- return files;
- }
- /**
- * Normalized the random part of the delta file JSON schema to static values.
- * "id" - "11111111-1111-1111-1111-111111111111"
- * "project" - "projectId"
- *
- * @param json - JSON string
- * @return QJsonDocument normalized JSON document. NULL document if the input is invalid.
- */
- QJsonDocument normalizeSchema( const QString &json )
- {
- QJsonDocument doc = QJsonDocument::fromJson( json.toUtf8() );
- if ( doc.isNull() )
- return doc;
- QJsonObject o = doc.object();
- QJsonArray deltas;
- if ( o.value( QStringLiteral( "version" ) ).toString() != DeltaFormatVersion )
- return QJsonDocument();
- if ( o.value( QStringLiteral( "project" ) ).toString().size() == 0 )
- return QJsonDocument();
- if ( QUuid::fromString( o.value( QStringLiteral( "id" ) ).toString() ).isNull() )
- return QJsonDocument();
- if ( !o.value( QStringLiteral( "deltas" ) ).isArray() )
- return QJsonDocument();
- if ( !o.value( QStringLiteral( "files" ) ).isArray() )
- return QJsonDocument();
- // normalize non-constant values
- o.insert( QStringLiteral( "id" ), QStringLiteral( "11111111-1111-1111-1111-111111111111" ) );
- o.insert( QStringLiteral( "project" ), QStringLiteral( "projectId" ) );
- o.insert( QStringLiteral( "deltas" ), normalizeDeltasSchema( o.value( QStringLiteral( "deltas" ) ).toArray() ) );
- o.insert( QStringLiteral( "files" ), normalizeFilesSchema( o.value( QStringLiteral( "files" ) ).toArray() ) );
- return QJsonDocument( o );
- }
- QJsonArray getDeltasArray( const QString &json )
- {
- return normalizeSchema( json.toUtf8() )
- .object()
- .value( QStringLiteral( "deltas" ) )
- .toArray();
- }
- TEST_CASE( "Delta File Wrapper" )
- {
- QgsProject *project = QgsProject::instance();
- QTemporaryDir settingsDir;
- QTemporaryFile tmpDeltaFile;
- QTemporaryDir workDir;
- REQUIRE( settingsDir.isValid() );
- REQUIRE( tmpDeltaFile.open() );
- REQUIRE( QDir( settingsDir.path() ).mkpath( QStringLiteral( "cloud_projects/TEST_PROJECT_ID" ) ) );
- QDir projectDir( QStringLiteral( "%1/cloud_projects/TEST_PROJECT_ID" ).arg( settingsDir.path() ) );
- QFieldCloudUtils::setLocalCloudDirectory( settingsDir.path() );
- QFile projectFile( QStringLiteral( "%1/%2" ).arg( projectDir.path(), QStringLiteral( "project.qgs" ) ) );
- QFile attachmentFile( QStringLiteral( "%1/%2" ).arg( projectDir.path(), QStringLiteral( "attachment.jpg" ) ) );
- REQUIRE( projectFile.open( QIODevice::WriteOnly ) );
- REQUIRE( projectFile.flush() );
- project->setFileName( projectFile.fileName() );
- const char *fileContents = "кирилица"; // SHA 256 71055d022f50027387eae32426a1857d6e2fa2d416d64753b63470db7f00f239
- REQUIRE( attachmentFile.open( QIODevice::ReadWrite ) );
- REQUIRE( attachmentFile.write( fileContents ) );
- REQUIRE( attachmentFile.flush() );
- QString attachmentFileName = attachmentFile.fileName();
- QString attachmentFileChecksum = FileUtils::fileChecksum( attachmentFileName ).toHex();
- std::unique_ptr<QgsVectorLayer> layer = std::make_unique<QgsVectorLayer>( QStringLiteral( "Point?crs=EPSG:3857&field=fid:integer&field=int:integer&field=dbl:double&field=str:string&field=attachment:string" ), QStringLiteral( "layer_name" ), QStringLiteral( "memory" ) );
- layer->setEditorWidgetSetup( layer->fields().indexFromName( QStringLiteral( "attachment" ) ), QgsEditorWidgetSetup( QStringLiteral( "ExternalResource" ), QVariantMap() ) );
- std::unique_ptr<QgsVectorLayer> joinedLayer = std::make_unique<QgsVectorLayer>( QStringLiteral( "NoGeometry?field=fid:integer&field=number_field:integer" ), QStringLiteral( "joined_layer" ), QStringLiteral( "memory" ) );
- REQUIRE( layer->isValid() );
- REQUIRE( joinedLayer->isValid() );
- REQUIRE( project->addMapLayer( layer.get(), false, false ) );
- REQUIRE( project->addMapLayer( joinedLayer.get(), false, false ) );
- QFieldCloudUtils::setProjectSetting( QStringLiteral( "TEST_PROJECT_ID" ), QStringLiteral( "lastLocalExportId" ), QStringLiteral( "22222222-2222-2222-2222-222222222222" ) );
- QFieldCloudUtils::setProjectSetting( QStringLiteral( "TEST_PROJECT_ID" ), QStringLiteral( "lastExportId" ), QStringLiteral( "33333333-3333-3333-3333-333333333333" ) );
- QgsFeature f( layer->fields(), 1 );
- f.setAttribute( QStringLiteral( "fid" ), 1 );
- f.setAttribute( QStringLiteral( "dbl" ), 3.14 );
- f.setAttribute( QStringLiteral( "int" ), 42 );
- f.setAttribute( QStringLiteral( "str" ), QStringLiteral( "stringy" ) );
- f.setAttribute( QStringLiteral( "attachment" ), attachmentFileName );
- REQUIRE( layer->startEditing() );
- REQUIRE( layer->addFeature( f ) );
- REQUIRE( layer->commitChanges() );
- QgsFeature jf1( joinedLayer->fields() );
- jf1.setAttribute( QStringLiteral( "fid" ), 1 );
- jf1.setAttribute( QStringLiteral( "number_field" ), 100 );
- REQUIRE( joinedLayer->startEditing() );
- REQUIRE( joinedLayer->addFeature( jf1 ) );
- REQUIRE( joinedLayer->commitChanges() );
- #if 0
- // TODO enable this code once we have a single delta pointer stored per project and passed to the layer observer.
- // Now both the qfieldcloudprojects model (Read only) and the layer observer (Read/Write) create their pointers to the deltafilewrapper
- SECTION( "NoMoreThanOneInstance" )
- {
- QString fileName( wrappedDeltaFilePath );
- DeltaFileWrapper dfw1( project, fileName );
- REQUIRE( dfw1.errorType() == DeltaFileWrapper::ErrorTypes::NoError );
- DeltaFileWrapper dfw2( project, fileName );
- REQUIRE( dfw2.errorType() == DeltaFileWrapper::ErrorTypes::LockError );
- }
- #endif
- SECTION( "No error with existing file" )
- {
- QString correctExistingContents = QStringLiteral( R""""(
- {
- "deltas":[],
- "files":[],
- "id":"11111111-1111-1111-1111-111111111111",
- "project":"projectId",
- "version":"1.0"
- }
- )"""" );
- REQUIRE( tmpDeltaFile.write( correctExistingContents.toUtf8() ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper correctExistingDfw( project, tmpDeltaFile.fileName() );
- REQUIRE( correctExistingDfw.errorType() == DeltaFileWrapper::ErrorTypes::NoError );
- QJsonDocument correctExistingDoc = normalizeSchema( correctExistingDfw.toString() );
- REQUIRE( !correctExistingDoc.isNull() );
- REQUIRE( correctExistingDoc == QJsonDocument::fromJson( correctExistingContents.toUtf8() ) );
- }
- SECTION( "NoErrorNonExistingFile" )
- {
- QString fileName( workDir.filePath( QUuid::createUuid().toString() ) );
- DeltaFileWrapper dfw( project, fileName );
- REQUIRE( dfw.errorType() == DeltaFileWrapper::ErrorTypes::NoError );
- REQUIRE( QFileInfo::exists( fileName ) );
- DeltaFileWrapper validNonexistingFileCheckDfw( project, fileName );
- QFile deltaFile( fileName );
- REQUIRE( deltaFile.open( QIODevice::ReadOnly ) );
- QJsonDocument fileContents = normalizeSchema( deltaFile.readAll() );
- REQUIRE( !fileContents.isNull() );
- QJsonDocument expectedDoc = QJsonDocument::fromJson( R""""(
- {
- "deltas": [],
- "files":[],
- "id":"11111111-1111-1111-1111-111111111111",
- "project": "projectId",
- "version": "1.0"
- }
- )"""" );
- REQUIRE( fileContents == expectedDoc );
- }
- SECTION( "ErrorInvalidName" )
- {
- DeltaFileWrapper dfw( project, "" );
- REQUIRE( dfw.errorType() == DeltaFileWrapper::ErrorTypes::IOError );
- }
- SECTION( "ErrorInvalidJsonParse" )
- {
- REQUIRE( tmpDeltaFile.write( R""""( asd )"""" ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper dfw( project, tmpDeltaFile.fileName() );
- REQUIRE( dfw.errorType() == DeltaFileWrapper::ErrorTypes::JsonParseError );
- }
- SECTION( "ErrorJsonFormatVersionType" )
- {
- REQUIRE( tmpDeltaFile.write( R""""({"version":5,"files":[],"id":"11111111-1111-1111-1111-111111111111","project":"projectId","deltas":[]})"""" ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper dfw( project, tmpDeltaFile.fileName() );
- REQUIRE( dfw.errorType() == DeltaFileWrapper::ErrorTypes::JsonFormatVersionError );
- }
- SECTION( "ErrorJsonFormatVersionEmpty" )
- {
- REQUIRE( tmpDeltaFile.write( R""""({"version":"","files":[],"id":"11111111-1111-1111-1111-111111111111","project":"projectId","deltas":[]})"""" ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper emptyVersionDfw( project, tmpDeltaFile.fileName() );
- REQUIRE( emptyVersionDfw.errorType() == DeltaFileWrapper::ErrorTypes::JsonFormatVersionError );
- }
- SECTION( "ErrorJsonFormatVersionValue" )
- {
- REQUIRE( tmpDeltaFile.write( R""""({"version":"2.0","files":[],"id":"11111111-1111-1111-1111-111111111111","project":"projectId","deltas":[]})"""" ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper wrongVersionNumberDfw( project, tmpDeltaFile.fileName() );
- REQUIRE( wrongVersionNumberDfw.errorType() == DeltaFileWrapper::ErrorTypes::JsonIncompatibleVersionError );
- }
- SECTION( "ErrorJsonFormatIdType" )
- {
- REQUIRE( tmpDeltaFile.write( R""""({"version":"2.0","files":[],"id": 5,"project":"projectId","deltas":[]})"""" ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper wrongIdTypeDfw( project, tmpDeltaFile.fileName() );
- REQUIRE( wrongIdTypeDfw.errorType() == DeltaFileWrapper::ErrorTypes::JsonFormatIdError );
- }
- SECTION( "ErrorJsonFormatIdEmpty" )
- {
- REQUIRE( tmpDeltaFile.write( R""""({"version":"2.0","files":[],"id": "","project":"projectId","deltas":[]})"""" ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper emptyIdDfw( project, tmpDeltaFile.fileName() );
- REQUIRE( emptyIdDfw.errorType() == DeltaFileWrapper::ErrorTypes::JsonFormatIdError );
- }
- SECTION( "ErrorJsonFormatProjectIdType" )
- {
- REQUIRE( tmpDeltaFile.write( R""""({"version":"2.0","files":[],"id": "11111111-1111-1111-1111-111111111111","project":5,"deltas":[]})"""" ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper wrongProjectIdTypeDfw( project, tmpDeltaFile.fileName() );
- REQUIRE( wrongProjectIdTypeDfw.errorType() == DeltaFileWrapper::ErrorTypes::JsonFormatProjectIdError );
- }
- SECTION( "ErrorJsonFormatProjectIdEmpty" )
- {
- REQUIRE( tmpDeltaFile.write( R""""({"version":"2.0","files":[],"id": "11111111-1111-1111-1111-111111111111","project":"","deltas":[]})"""" ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper emptyProjectIdDfw( project, tmpDeltaFile.fileName() );
- REQUIRE( emptyProjectIdDfw.errorType() == DeltaFileWrapper::ErrorTypes::JsonFormatProjectIdError );
- }
- SECTION( "ErrorJsonFormatDeltasType" )
- {
- REQUIRE( tmpDeltaFile.write( R""""({"version":"2.0","files":[],"id": "11111111-1111-1111-1111-111111111111","project":"projectId","deltas":{}})"""" ) );
- tmpDeltaFile.flush();
- DeltaFileWrapper wrongDeltasTypeDfw( project, tmpDeltaFile.fileName() );
- REQUIRE( wrongDeltasTypeDfw.errorType() == DeltaFileWrapper::ErrorTypes::JsonFormatDeltasError );
- }
- SECTION( "FileName" )
- {
- QString fileName( QFileInfo( workDir.filePath( QUuid::createUuid().toString() ) ).absoluteFilePath() );
- DeltaFileWrapper dfw( project, fileName );
- REQUIRE( dfw.fileName() == fileName );
- }
- SECTION( "Id" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- REQUIRE( !QUuid::fromString( dfw.id() ).isNull() );
- }
- SECTION( "Reset" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), QgsFeature() );
- REQUIRE( getDeltasArray( dfw.toString() ).size() == 1 );
- dfw.reset();
- REQUIRE( getDeltasArray( dfw.toString() ).size() == 0 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), QgsFeature() );
- REQUIRE( getDeltasArray( dfw.toString() ).size() == 1 );
- }
- SECTION( "ResetId" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- REQUIRE( getDeltasArray( dfw.toString() ).size() == 0 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), QgsFeature() );
- const QString dfwId = dfw.id();
- dfw.resetId();
- REQUIRE( getDeltasArray( dfw.toString() ).size() == 1 );
- REQUIRE( dfwId != dfw.id() );
- }
- SECTION( "ToString" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFields fields;
- fields.append( QgsField( "fid", QVariant::Int, "integer" ) );
- QgsFeature f1( fields, 100 );
- f1.setAttribute( "fid", 100 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f1 );
- QgsFeature f2( fields, 101 );
- f2.setAttribute( "fid", 101 );
- dfw.addDelete( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f2 );
- QJsonDocument doc = normalizeSchema( dfw.toString() );
- REQUIRE( !doc.isNull() );
- QJsonDocument expectedDoc = QJsonDocument::fromJson( R""""(
- {
- "deltas": [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "fid": 100
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "101",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "101",
- "method": "delete",
- "old": {
- "attributes": {
- "fid": 101
- },
- "geometry": null
- }
- }
- ],
- "files":[],
- "id": "11111111-1111-1111-1111-111111111111",
- "project": "projectId",
- "version": "1.0"
- }
- )"""" );
- REQUIRE( doc == expectedDoc );
- }
- SECTION( "ToJson" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFields fields;
- fields.append( QgsField( "fid", QVariant::Int, "integer" ) );
- QgsFeature f1( fields, 100 );
- f1.setAttribute( "fid", 100 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f1 );
- QgsFeature f2( fields, 101 );
- f2.setAttribute( "fid", 101 );
- dfw.addDelete( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f2 );
- QJsonDocument doc = normalizeSchema( QString( dfw.toJson() ) );
- REQUIRE( !doc.isNull() );
- QJsonDocument expectedDoc = QJsonDocument::fromJson( R""""(
- {
- "deltas": [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "fid": 100
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "101",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "101",
- "method": "delete",
- "old": {
- "attributes": {
- "fid": 101
- },
- "geometry": null
- }
- }
- ],
- "files":[],
- "id": "11111111-1111-1111-1111-111111111111",
- "project": "projectId",
- "version": "1.0"
- }
- )"""" );
- REQUIRE( doc == expectedDoc );
- }
- SECTION( "ProjectId" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- REQUIRE( dfw.projectId() == QStringLiteral( "TEST_PROJECT_ID" ) );
- }
- SECTION( "IsDirty" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- REQUIRE( dfw.isDirty() == false );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), QgsFeature() );
- REQUIRE( dfw.isDirty() == true );
- REQUIRE( dfw.toFile() );
- REQUIRE( dfw.isDirty() == false );
- dfw.reset();
- REQUIRE( dfw.isDirty() == true );
- }
- SECTION( "Count" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- REQUIRE( dfw.count() == 0 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), QgsFeature() );
- REQUIRE( dfw.count() == 1 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), QgsFeature() );
- REQUIRE( dfw.count() == 2 );
- }
- SECTION( "Deltas" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- REQUIRE( QJsonDocument( dfw.deltas() ) == QJsonDocument::fromJson( "[]" ) );
- QgsFields fields;
- fields.append( QgsField( "fid", QVariant::Int, "integer" ) );
- QgsFeature f1( fields, 100 );
- f1.setAttribute( "fid", 100 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f1 );
- QJsonDocument expectedDoc = QJsonDocument::fromJson( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "fid": 100
- },
- "geometry": null
- }
- }
- ]
- )"""" );
- REQUIRE( QJsonDocument( normalizeDeltasSchema( dfw.deltas() ) ) == expectedDoc );
- QgsFeature f2( fields, 101 );
- f2.setAttribute( "fid", 101 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f2 );
- expectedDoc = QJsonDocument::fromJson( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "fid": 100
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "101",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "101",
- "method": "create",
- "new": {
- "attributes": {
- "fid": 101
- },
- "geometry": null
- }
- }
- ]
- )"""" );
- REQUIRE( QJsonDocument( normalizeDeltasSchema( dfw.deltas() ) ) == expectedDoc );
- }
- SECTION( "ToFile" )
- {
- QString fileName = workDir.filePath( QUuid::createUuid().toString() );
- DeltaFileWrapper dfw1( project, fileName );
- dfw1.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), QgsFeature() );
- REQUIRE( !dfw1.hasError() );
- REQUIRE( getDeltasArray( dfw1.toString() ).size() == 1 );
- REQUIRE( dfw1.toFile() );
- REQUIRE( getDeltasArray( dfw1.toString() ).size() == 1 );
- QFile deltaFile( fileName );
- REQUIRE( deltaFile.open( QIODevice::ReadOnly ) );
- REQUIRE( getDeltasArray( deltaFile.readAll() ).size() == 1 );
- }
- SECTION( "Append" )
- {
- DeltaFileWrapper dfw1( project, workDir.filePath( QUuid::createUuid().toString() ) );
- DeltaFileWrapper dfw2( project, workDir.filePath( QUuid::createUuid().toString() ) );
- dfw1.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), QgsFeature( QgsFields(), 100 ) );
- dfw2.append( &dfw1 );
- REQUIRE( dfw2.count() == 1 );
- }
- SECTION( "AttachmentFieldNames" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QStringList attachmentFields = dfw.attachmentFieldNames( project, layer->id() );
- REQUIRE( attachmentFields == QStringList( { QStringLiteral( "attachment" ) } ) );
- }
- SECTION( "AttachmentFileNames" )
- {
- QTemporaryFile deltaFile;
- REQUIRE( deltaFile.open() );
- REQUIRE( deltaFile.write( QStringLiteral( R""""(
- {
- "deltas": [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "100",
- "sourceLayerId": "%1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "FILE1.jpg",
- "dbl": 3.14,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "FILE1.jpg": null
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "102",
- "sourceLayerId": "%1",
- "sourcePk": "102",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "FILE2.jpg",
- "dbl": null,
- "int": null,
- "str": null
- },
- "files_sha256": {
- "FILE2.jpg": null
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "102",
- "sourceLayerId": "%1",
- "sourcePk": "102",
- "method": "patch",
- "new": {
- "attributes": {
- "attachment": "FILE3.jpg"
- },
- "files_sha256": {
- "FILE3.jpg": null
- },
- "geometry": null
- },
- "old": {
- "attributes": {
- "attachment": "FILE2.jpg"
- },
- "files_sha256": {
- "FILE2.jpg": null
- },
- "geometry": null
- }
- }
- ],
- "files": [],
- "id": "11111111-1111-1111-1111-111111111111",
- "project": "projectId",
- "version": "1.0"
- }
- )"""" )
- .arg( layer->id() )
- .toUtf8() ) );
- REQUIRE( deltaFile.flush() );
- DeltaFileWrapper dfw( project, deltaFile.fileName() );
- REQUIRE( !dfw.hasError() );
- QMap<QString, QString> attachmentFileNames = dfw.attachmentFileNames();
- QMap<QString, QString> expectedAttachmentFileNames(
- {
- { "FILE1.jpg", "" },
- { "FILE3.jpg", "" } } );
- REQUIRE( attachmentFileNames == expectedAttachmentFileNames );
- }
- SECTION( "AddCreate" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFeature f( layer->fields(), 100 );
- f.setAttribute( QStringLiteral( "fid" ), 100 );
- f.setAttribute( QStringLiteral( "dbl" ), 3.14 );
- f.setAttribute( QStringLiteral( "int" ), 42 );
- f.setAttribute( QStringLiteral( "str" ), QStringLiteral( "stringy" ) );
- f.setAttribute( QStringLiteral( "attachment" ), attachmentFileName );
- // Check if creates delta of a feature with a geometry and existing attachment
- f.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f );
- QJsonDocument expectedDoc = QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "%1",
- "dbl": 3.14,
- "fid": 100,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "%1": "%2"
- },
- "geometry": "Point (25.96569999999999823 43.83559999999999945)"
- }
- }
- ]
- )"""" )
- .arg( attachmentFileName, attachmentFileChecksum )
- .toUtf8() );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == expectedDoc );
- // Check if creates delta of a feature with a NULL geometry and non existant attachment.
- // NOTE this is the same as calling f clearGeometry()
- dfw.reset();
- f.setGeometry( QgsGeometry() );
- f.setAttribute( QStringLiteral( "attachment" ), workDir.filePath( QUuid::createUuid().toString() ) );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f );
- expectedDoc = QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "%1",
- "dbl": 3.14,
- "fid": 100,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "%1": null
- },
- "geometry": null
- }
- }
- ]
- )"""" )
- .arg( f.attribute( QStringLiteral( "attachment" ) ).toString() )
- .toUtf8() );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == expectedDoc );
- // Check if creates delta of a feature without attributes
- dfw.reset();
- QgsFields fields;
- fields.append( QgsField( QStringLiteral( "fid" ), QVariant::Int ) );
- QgsFeature f1( fields, 101 );
- f1.setAttribute( QStringLiteral( "fid" ), 101 );
- f1.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f1 );
- expectedDoc = QJsonDocument::fromJson( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "101",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "101",
- "method": "create",
- "new": {
- "attributes":{
- "fid": 101
- },
- "geometry": "Point (25.96569999999999823 43.83559999999999945)"
- }
- }
- ]
- )"""" );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == expectedDoc );
- }
- SECTION( "AddPatch" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFeature oldFeature( layer->fields(), 100 );
- oldFeature.setAttribute( QStringLiteral( "dbl" ), 3.14 );
- oldFeature.setAttribute( QStringLiteral( "int" ), 42 );
- oldFeature.setAttribute( QStringLiteral( "fid" ), 100 );
- oldFeature.setAttribute( QStringLiteral( "str" ), QStringLiteral( "stringy" ) );
- oldFeature.setAttribute( QStringLiteral( "attachment" ), QString() );
- QgsFeature newFeature( layer->fields(), 100 );
- newFeature.setAttribute( QStringLiteral( "dbl" ), 9.81 );
- newFeature.setAttribute( QStringLiteral( "int" ), 680 );
- newFeature.setAttribute( QStringLiteral( "fid" ), 100 );
- newFeature.setAttribute( QStringLiteral( "str" ), QStringLiteral( "pingy" ) );
- newFeature.setAttribute( QStringLiteral( "attachment" ), attachmentFileName );
- // Patch both the attributes with existing attachment and the geometry
- oldFeature.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- newFeature.setGeometry( QgsGeometry( new QgsPoint( 23.398819, 41.7672147 ) ) );
- dfw.addPatch( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), oldFeature, newFeature );
- QJsonDocument expectedDoc = QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "patch",
- "new": {
- "attributes": {
- "attachment": "%1",
- "dbl": 9.81,
- "int": 680,
- "str": "pingy"
- },
- "files_sha256": {
- "%1": "%2"
- },
- "geometry": "Point (23.39881899999999959 41.7672146999999967)"
- },
- "old": {
- "attributes": {
- "attachment": null,
- "dbl": 3.14,
- "int": 42,
- "str": "stringy"
- },
- "geometry": "Point (25.96569999999999823 43.83559999999999945)"
- }
- }
- ]
- )"""" )
- .arg( attachmentFileName, attachmentFileChecksum )
- .toUtf8() ) ;
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == expectedDoc );
- // Patch attributes only with non existing attachnment
- dfw.reset();
- newFeature.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- newFeature.setAttribute( QStringLiteral( "attachment" ), workDir.filePath( QUuid::createUuid().toString() ) );
- oldFeature.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- dfw.addPatch( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), oldFeature, newFeature );
- expectedDoc = QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "patch",
- "new": {
- "attributes": {
- "attachment": "%1",
- "dbl": 9.81,
- "int": 680,
- "str": "pingy"
- },
- "files_sha256": {
- "%1": null
- }
- },
- "old": {
- "attributes": {
- "attachment": null,
- "dbl": 3.14,
- "int": 42,
- "str": "stringy"
- }
- }
- }
- ]
- )"""" )
- .arg( newFeature.attribute( "attachment" ).toString() )
- .toUtf8() ) ;
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == expectedDoc );
- // Patch feature without geometry on attributes only with non existant attachment
- dfw.reset();
- newFeature.setGeometry( QgsGeometry() );
- oldFeature.setGeometry( QgsGeometry() );
- dfw.addPatch( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), oldFeature, newFeature );
- // Patch geometry only
- dfw.reset();
- newFeature.setAttribute( QStringLiteral( "dbl" ), 3.14 );
- newFeature.setAttribute( QStringLiteral( "int" ), 42 );
- newFeature.setAttribute( QStringLiteral( "fid" ), 100 );
- newFeature.setAttribute( QStringLiteral( "str" ), QStringLiteral( "stringy" ) );
- newFeature.setAttribute( QStringLiteral( "attachment" ), QVariant() );
- newFeature.setGeometry( QgsGeometry( new QgsPoint( 23.398819, 41.7672147 ) ) );
- oldFeature.setAttribute( QStringLiteral( "attachment" ), QVariant() );
- oldFeature.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- dfw.addPatch( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), oldFeature, newFeature );
- expectedDoc = QJsonDocument::fromJson( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "patch",
- "new": {
- "geometry": "Point (23.39881899999999959 41.7672146999999967)"
- },
- "old": {
- "geometry": "Point (25.96569999999999823 43.83559999999999945)"
- }
- }
- ]
- )"""" );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == expectedDoc );
- // Do not patch equal features
- dfw.reset();
- oldFeature.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- newFeature.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- dfw.addPatch( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), oldFeature, newFeature );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == QJsonDocument::fromJson( "[]" ) );
- }
- SECTION( "AddDeleteWithStringPk" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFeature f( layer->fields(), 100 );
- f.setAttribute( QStringLiteral( "fid" ), 100 );
- f.setAttribute( QStringLiteral( "dbl" ), 3.14 );
- f.setAttribute( QStringLiteral( "int" ), 42 );
- f.setAttribute( QStringLiteral( "str" ), QStringLiteral( "stringy" ) );
- f.setAttribute( QStringLiteral( "attachment" ), workDir.filePath( QUuid::createUuid().toString() ) );
- f.setGeometry( QgsGeometry() );
- dfw.addDelete( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "str" ), f );
- QJsonDocument expectedDoc = QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "stringy",
- "method": "delete",
- "old": {
- "attributes": {
- "attachment": "%1",
- "dbl": 3.14,
- "fid": 100,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "%1": null
- },
- "geometry": null
- }
- }
- ]
- )"""" )
- .arg( f.attribute( QStringLiteral( "attachment" ) ).toString() )
- .toUtf8() ) ;
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == expectedDoc );
- }
- SECTION( "AddDelete" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFeature f( layer->fields(), 100 );
- f.setAttribute( QStringLiteral( "fid" ), 100 );
- f.setAttribute( QStringLiteral( "dbl" ), 3.14 );
- f.setAttribute( QStringLiteral( "int" ), 42 );
- f.setAttribute( QStringLiteral( "str" ), QStringLiteral( "stringy" ) );
- f.setAttribute( QStringLiteral( "attachment" ), attachmentFileName );
- // Check if creates delta of a feature with a geometry and existant attachment.
- f.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- // ? why this is not working, as QgsPoint is QgsAbstractGeometry and there is example in the docs? https://qgis.org/api/classQgsFeature.html#a14dcfc99b476b613c21b8c35840ff388
- // f.setGeometry( QgsPoint( 25.9657, 43.8356 ) );
- dfw.addDelete( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f );
- QJsonDocument expectedDoc = QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "delete",
- "old": {
- "attributes": {
- "attachment": "%1",
- "dbl": 3.14,
- "fid": 100,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "%1": "%2"
- },
- "geometry": "Point (25.96569999999999823 43.83559999999999945)"
- }
- }
- ]
- )"""" )
- .arg( attachmentFileName, attachmentFileChecksum )
- .toUtf8() );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == expectedDoc );
- // Check if creates delta of a feature with a NULL geometry and non existant attachment.
- // NOTE this is the same as calling f clearGeometry()
- dfw.reset();
- f.setGeometry( QgsGeometry() );
- f.setAttribute( QStringLiteral( "attachment" ), workDir.filePath( QUuid::createUuid().toString() ) );
- dfw.addDelete( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "delete",
- "old": {
- "attributes": {
- "attachment": "%1",
- "dbl": 3.14,
- "fid": 100,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "%1": null
- },
- "geometry": null
- }
- }
- ]
- )"""" )
- .arg( f.attribute( QStringLiteral( "attachment" ) ).toString() )
- .toUtf8() ) );
- // Check if creates delta of a feature without attributes
- dfw.reset();
- QgsFields fields;
- fields.append( QgsField( QStringLiteral( "fid" ), QVariant::Int ) );
- QgsFeature f1( fields, 101 );
- f1.setAttribute( QStringLiteral( "fid" ), 101 );
- f1.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- dfw.addDelete( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f1 );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == QJsonDocument::fromJson( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "101",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "101",
- "method": "delete",
- "old": {
- "attributes": {
- "fid": 101
- },
- "geometry": "Point (25.96569999999999823 43.83559999999999945)"
- }
- }
- ]
- )"""" ) );
- }
- SECTION( "MultipleDeltaAdd" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFields fields;
- fields.append( QgsField( "dbl", QVariant::Double, "double" ) );
- fields.append( QgsField( "int", QVariant::Int, "integer" ) );
- fields.append( QgsField( "fid", QVariant::Int, "integer" ) );
- fields.append( QgsField( "str", QVariant::String, "text" ) );
- QgsFeature f1( fields, 100 );
- f1.setAttribute( QStringLiteral( "dbl" ), 3.14 );
- f1.setAttribute( QStringLiteral( "int" ), 42 );
- f1.setAttribute( QStringLiteral( "fid" ), 100 );
- f1.setAttribute( QStringLiteral( "str" ), QStringLiteral( "stringy" ) );
- QgsFields fields2;
- fields2.append( QgsField( "fid", QVariant::Int, "integer" ) );
- QgsFeature f2( fields2, 101 );
- f2.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- f2.setAttribute( QStringLiteral( "fid" ), 101 );
- QgsFeature f3( fields, 102 );
- f3.setAttribute( QStringLiteral( "fid" ), 102 );
- dfw.addCreate( QStringLiteral( "dummyLayerId1" ), QStringLiteral( "dummyLayerId1" ), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f1 );
- dfw.addDelete( QStringLiteral( "dummyLayerId2" ), QStringLiteral( "dummyLayerId2" ), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f2 );
- dfw.addDelete( QStringLiteral( "dummyLayerId1" ), QStringLiteral( "dummyLayerId1" ), QStringLiteral( "fid" ), QStringLiteral( "fid" ), f3 );
- QJsonDocument doc = normalizeSchema( dfw.toString() );
- REQUIRE( !doc.isNull() );
- REQUIRE( doc == QJsonDocument::fromJson( R""""(
- {
- "deltas": [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "100",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "dbl": 3.14,
- "fid": 100,
- "int": 42,
- "str": "stringy"
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL2",
- "localPk": "101",
- "sourceLayerId": "dummyLayerIdS2",
- "sourcePk": "101",
- "method": "delete",
- "old": {
- "attributes": {
- "fid": 101
- },
- "geometry": "Point (25.96569999999999823 43.83559999999999945)"
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "102",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "102",
- "method": "delete",
- "old": {
- "attributes": {
- "dbl": null,
- "fid": 102,
- "int": null,
- "str": null
- },
- "geometry": null
- }
- }
- ],
- "files":[],
- "id": "11111111-1111-1111-1111-111111111111",
- "project": "projectId",
- "version": "1.0"
- }
- )"""" ) );
- }
- SECTION( "Apply" )
- {
- QTemporaryFile deltaFile;
- REQUIRE( deltaFile.open() );
- REQUIRE( deltaFile.write( QStringLiteral( R""""(
- {
- "deltas": [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "100",
- "sourceLayerId": "%1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "FILE1.jpg",
- "dbl": 3.14,
- "fid": 100,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "FILE1.jpg": null
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "102",
- "sourceLayerId": "%1",
- "sourcePk": "102",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "FILE2.jpg",
- "dbl": null,
- "fid": 102,
- "int": null,
- "str": null
- },
- "files_sha256": {
- "FILE2.jpg": null
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "102",
- "sourceLayerId": "%1",
- "sourcePk": "102",
- "method": "patch",
- "new": {
- "attributes": {
- "attachment": "FILE3.jpg"
- },
- "files_sha256": {
- "FILE3.jpg": null
- },
- "geometry": null
- },
- "old": {
- "attributes": {
- "attachment": "FILE2.jpg"
- },
- "files_sha256": {
- "FILE2.jpg": null
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "1",
- "sourceLayerId": "%1",
- "sourcePk": "1",
- "method": "delete",
- "old": {
- "attachment": "%2",
- "dbl": 3.14,
- "fid": 1,
- "int": 42,
- "str": "stringy"
- }
- }
- ],
- "files": [],
- "id": "11111111-1111-1111-1111-111111111111",
- "project": "projectId",
- "version": "1.0"
- }
- )"""" )
- .arg( layer->id(), attachmentFileName )
- .toUtf8() ) );
- REQUIRE( deltaFile.flush() );
- DeltaFileWrapper dfw( project, deltaFile.fileName() );
- // make sure there is a single feature with id 1
- QgsFeature f0;
- qApp->processEvents();
- QgsFeatureIterator it0 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 1 " ) ) );
- REQUIRE( it0.nextFeature( f0 ) );
- REQUIRE( layer->featureCount() == 1 );
- REQUIRE( dfw.apply() );
- REQUIRE( layer->featureCount() == 2 );
- QgsFeature f1;
- QgsFeatureIterator it1 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 100 " ) ) );
- REQUIRE( it1.nextFeature( f1 ) );
- REQUIRE( f1.isValid() );
- REQUIRE( f1.attribute( QStringLiteral( "int" ) ) == 42 );
- REQUIRE( f1.attribute( QStringLiteral( "dbl" ) ) == 3.14 );
- REQUIRE( f1.attribute( QStringLiteral( "str" ) ) == QStringLiteral( "stringy" ) );
- REQUIRE( f1.attribute( QStringLiteral( "attachment" ) ) == QStringLiteral( "FILE1.jpg" ) );
- REQUIRE( !it1.nextFeature( f1 ) );
- QgsFeature f2;
- QgsFeatureIterator it2 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 102 " ) ) );
- REQUIRE( it2.nextFeature( f2 ) );
- REQUIRE( f2.isValid() );
- REQUIRE( f2.attribute( QStringLiteral( "attachment" ) ).toString() == QStringLiteral( "FILE3.jpg" ) );
- REQUIRE( !it2.nextFeature( f2 ) );
- QgsFeature f3;
- QgsFeatureIterator it3 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 1 " ) ) );
- REQUIRE( !it3.nextFeature( f3 ) );
- }
- SECTION( "ApplyReversed" )
- {
- QTemporaryFile deltaFile;
- REQUIRE( deltaFile.open() );
- REQUIRE( deltaFile.write( QStringLiteral( R""""(
- {
- "deltas": [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "100",
- "sourceLayerId": "%1",
- "sourcePk": "100",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "FILE1.jpg",
- "dbl": 3.14,
- "fid": 100,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "FILE1.jpg": null
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "102",
- "sourceLayerId": "%1",
- "sourcePk": "102",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "FILE2.jpg",
- "dbl": null,
- "fid": 102,
- "int": null,
- "str": null
- },
- "files_sha256": {
- "FILE2.jpg": null
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "102",
- "sourceLayerId": "%1",
- "sourcePk": "102",
- "method": "patch",
- "new": {
- "attributes": {
- "attachment": "FILE3.jpg"
- },
- "files_sha256": {
- "FILE3.jpg": null
- },
- "geometry": null
- },
- "old": {
- "attributes": {
- "attachment": "FILE2.jpg"
- },
- "files_sha256": {
- "FILE2.jpg": null
- },
- "geometry": null
- }
- },
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "%1",
- "localPk": "1",
- "sourceLayerId": "%1",
- "sourcePk": "1",
- "method": "delete",
- "old": {
- "attributes": {
- "attachment": "%2",
- "dbl": 3.14,
- "fid": 1,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "%2": "%3"
- }
- }
- }
- ],
- "files": [],
- "id": "11111111-1111-1111-1111-111111111111",
- "project": "projectId",
- "version": "1.0"
- }
- )"""" )
- .arg( layer->id(), attachmentFileName, attachmentFileChecksum )
- .toUtf8() ) );
- REQUIRE( deltaFile.flush() );
- DeltaFileWrapper dfw( project, deltaFile.fileName() );
- // make sure there is a single feature with id 1
- QgsFeature f0;
- QgsFeatureIterator it0 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 1 " ) ) );
- REQUIRE( it0.nextFeature( f0 ) );
- REQUIRE( layer->featureCount() == 1 );
- REQUIRE( dfw.apply() );
- REQUIRE( layer->featureCount() == 2 );
- QgsFeature f1;
- QgsFeatureIterator it1 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 100 " ) ) );
- REQUIRE( it1.nextFeature( f1 ) );
- REQUIRE( f1.isValid() );
- REQUIRE( f1.attribute( QStringLiteral( "int" ) ) == 42 );
- REQUIRE( f1.attribute( QStringLiteral( "dbl" ) ) == 3.14 );
- REQUIRE( f1.attribute( QStringLiteral( "str" ) ) == QStringLiteral( "stringy" ) );
- REQUIRE( f1.attribute( QStringLiteral( "attachment" ) ) == QStringLiteral( "FILE1.jpg" ) );
- REQUIRE( !it1.nextFeature( f1 ) );
- QgsFeature f2;
- QgsFeatureIterator it2 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 102 " ) ) );
- REQUIRE( it2.nextFeature( f2 ) );
- REQUIRE( f2.isValid() );
- REQUIRE( f2.attribute( QStringLiteral( "attachment" ) ).toString() == QStringLiteral( "FILE3.jpg" ) );
- REQUIRE( !it2.nextFeature( f2 ) );
- QgsFeature f3;
- QgsFeatureIterator it3 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 1 " ) ) );
- REQUIRE( !it3.nextFeature( f3 ) );
- // ^^^ the same as apply above
- dfw.applyReversed();
- QgsFeature f4;
- QgsFeatureIterator it4 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 100 OR fid = 102 " ) ) );
- REQUIRE( !it4.nextFeature( f4 ) );
- QgsFeature f5;
- QgsFeatureIterator it5 = layer->getFeatures( QgsFeatureRequest( QgsExpression( " fid = 1 " ) ) );
- REQUIRE( it5.nextFeature( f5 ) );
- REQUIRE( f5.isValid() );
- REQUIRE( f5.attribute( QStringLiteral( "fid" ) ) == 1 );
- REQUIRE( f5.attribute( QStringLiteral( "int" ) ) == 42 );
- REQUIRE( f5.attribute( QStringLiteral( "dbl" ) ) == 3.14 );
- REQUIRE( f5.attribute( QStringLiteral( "str" ) ) == QStringLiteral( "stringy" ) );
- REQUIRE( f5.attribute( QStringLiteral( "attachment" ) ) == attachmentFileName );
- REQUIRE( !it5.nextFeature( f5 ) );
- }
- SECTION( "AddCreateWithJoinedLayer" )
- {
- QgsVectorLayerJoinInfo ji;
- ji.setTargetFieldName( QStringLiteral( "fid" ) );
- ji.setJoinLayer( joinedLayer.get() );
- ji.setJoinFieldName( QStringLiteral( "fid" ) );
- ji.setPrefix( QString( "" ) );
- ji.setEditable( false );
- REQUIRE( layer->addJoin( ji ) );
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFeature f( layer->fields(), 2 );
- f.setAttribute( QStringLiteral( "fid" ), 2 );
- f.setAttribute( QStringLiteral( "dbl" ), 3.14 );
- f.setAttribute( QStringLiteral( "int" ), 42 );
- f.setAttribute( QStringLiteral( "str" ), QStringLiteral( "stringy" ) );
- f.setAttribute( QStringLiteral( "attachment" ), attachmentFileName );
- REQUIRE( f.isValid() );
- REQUIRE( layer->startEditing() );
- REQUIRE( layer->addFeature( f ) );
- REQUIRE( layer->commitChanges() );
- QgsFeature savedFeat = layer->getFeature( 2 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), savedFeat );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "2",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "2",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "%1",
- "dbl": 3.14,
- "fid": 2,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "%1": "%2"
- },
- "geometry": null
- }
- }
- ]
- )"""" )
- .arg( attachmentFileName, attachmentFileChecksum )
- .toUtf8() ) );
- }
- SECTION( "AddCreateWithExpressionField" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFields fields = layer->fields();
- QgsField expressionField( QStringLiteral( "expression" ), QVariant::String, QStringLiteral( "text" ) );
- QgsFeature f( fields, 2 );
- f.setAttribute( QStringLiteral( "fid" ), 2 );
- f.setAttribute( QStringLiteral( "dbl" ), 3.14 );
- f.setAttribute( QStringLiteral( "int" ), 42 );
- f.setAttribute( QStringLiteral( "str" ), QStringLiteral( "stringy" ) );
- f.setAttribute( QStringLiteral( "attachment" ), attachmentFileName );
- REQUIRE( f.isValid() );
- REQUIRE( layer->startEditing() );
- REQUIRE( layer->addFeature( f ) );
- REQUIRE( layer->commitChanges() );
- REQUIRE( layer->addExpressionField( QStringLiteral( " UPPER( str ) " ), expressionField ) >= 0 );
- QgsFeature savedFeat = layer->getFeature( 2 );
- dfw.addCreate( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), savedFeat );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "2",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "2",
- "method": "create",
- "new": {
- "attributes": {
- "attachment": "%1",
- "dbl": 3.14,
- "fid": 2,
- "int": 42,
- "str": "stringy"
- },
- "files_sha256": {
- "%1": "%2"
- },
- "geometry": null
- }
- }
- ]
- )"""" )
- .arg( attachmentFileName, attachmentFileChecksum )
- .toUtf8() ) );
- }
- SECTION( "AddPatchWithJoinedLayer" )
- {
- QgsVectorLayerJoinInfo ji;
- ji.setTargetFieldName( QStringLiteral( "fid" ) );
- ji.setJoinLayer( joinedLayer.get() );
- ji.setJoinFieldName( QStringLiteral( "fid" ) );
- ji.setPrefix( QString( "" ) );
- ji.setEditable( false );
- REQUIRE( layer->addJoin( ji ) );
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFields fields = layer->fields();
- QgsField expressionField( QStringLiteral( "expression" ), QVariant::String, QStringLiteral( "text" ) );
- QgsFeature oldFeature = layer->getFeature( 1 );
- QgsFeature newFeature = layer->getFeature( 1 );
- REQUIRE( newFeature.setAttribute( QStringLiteral( "dbl" ), 9.81 ) );
- REQUIRE( newFeature.setAttribute( QStringLiteral( "int" ), 680 ) );
- REQUIRE( newFeature.setAttribute( QStringLiteral( "str" ), QStringLiteral( "pingy" ) ) );
- // Patch both the attributes with existing attachment and the geometry
- oldFeature.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- newFeature.setGeometry( QgsGeometry( new QgsPoint( 23.398819, 41.7672147 ) ) );
- dfw.addPatch( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), oldFeature, newFeature );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "1",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "1",
- "method": "patch",
- "new": {
- "attributes": {
- "dbl": 9.81,
- "int": 680,
- "str": "pingy"
- },
- "geometry": "Point (23.39881899999999959 41.7672146999999967)"
- },
- "old": {
- "attributes": {
- "dbl": 3.14,
- "int": 42,
- "str": "stringy"
- },
- "geometry": "Point (25.96569999999999823 43.83559999999999945)"
- }
- }
- ]
- )"""" )
- .toUtf8() ) );
- }
- SECTION( "AddPatchWithExpressionField" )
- {
- DeltaFileWrapper dfw( project, workDir.filePath( QUuid::createUuid().toString() ) );
- QgsFields fields = layer->fields();
- QgsField expressionField( QStringLiteral( "expression" ), QVariant::String, QStringLiteral( "text" ) );
- // this is the easiest way to create an expression field
- REQUIRE( layer->addExpressionField( QStringLiteral( " UPPER( str ) " ), expressionField ) >= 0 );
- QgsFeature oldFeature = layer->getFeature( 1 );
- QgsFeature newFeature = layer->getFeature( 1 );
- REQUIRE( newFeature.setAttribute( QStringLiteral( "dbl" ), 9.81 ) );
- REQUIRE( newFeature.setAttribute( QStringLiteral( "int" ), 680 ) );
- REQUIRE( newFeature.setAttribute( QStringLiteral( "str" ), QStringLiteral( "pingy" ) ) );
- // Patch both the attributes with existing attachment and the geometry
- oldFeature.setGeometry( QgsGeometry( new QgsPoint( 25.9657, 43.8356 ) ) );
- newFeature.setGeometry( QgsGeometry( new QgsPoint( 23.398819, 41.7672147 ) ) );
- dfw.addPatch( layer->id(), layer->id(), QStringLiteral( "fid" ), QStringLiteral( "fid" ), oldFeature, newFeature );
- REQUIRE( QJsonDocument( getDeltasArray( dfw.toString() ) ) == QJsonDocument::fromJson( QStringLiteral( R""""(
- [
- {
- "uuid": "11111111-1111-1111-1111-111111111111",
- "clientId": "22222222-2222-2222-2222-222222222222",
- "exportId": "33333333-3333-3333-3333-333333333333",
- "localLayerId": "dummyLayerIdL1",
- "localPk": "1",
- "sourceLayerId": "dummyLayerIdS1",
- "sourcePk": "1",
- "method": "patch",
- "new": {
- "attributes": {
- "dbl": 9.81,
- "int": 680,
- "str": "pingy"
- },
- "geometry": "Point (23.39881899999999959 41.7672146999999967)"
- },
- "old": {
- "attributes": {
- "dbl": 3.14,
- "int": 42,
- "str": "stringy"
- },
- "geometry": "Point (25.96569999999999823 43.83559999999999945)"
- }
- }
- ]
- )"""" )
- .toUtf8() ) );
- }
- project->removeMapLayer( layer.get() );
- project->removeMapLayer( joinedLayer.get() );
- }
|