Skip to content

Commit

Permalink
tests for offline editing
Browse files Browse the repository at this point in the history
testing creating gpkg and sqlite files from shp layer representing data source and synchronize back
  • Loading branch information
signedav committed Jul 5, 2018
1 parent f80f4ec commit 85c7742
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ SET(TESTS
testqgslayerdefinition.cpp
testqgssqliteutils.cpp
testqgsmimedatautils.cpp
testqgsofflineediting.cpp
)

IF(WITH_QTWEBKIT)
Expand Down
147 changes: 147 additions & 0 deletions tests/src/core/testqgsofflineediting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/***************************************************************************
testqgsofflineediting.cpp
---------------------
begin : 3.7.2018
copyright : (C) 2018 by david signer
email : david at opengis dot 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. *
* *
***************************************************************************/
#include <QObject>

#include <QString>
#include <QStringList>
#include <QApplication>
#include <QFileInfo>
#include <QDir>

#include "qgsunittypes.h"
#include "qgsofflineediting.h"
#include "qgstest.h"
#include "qgsvectorlayerref.h"

/**
* \ingroup UnitTests
*/
class TestQgsOfflineEditing : public QObject
{
Q_OBJECT

private:
QgsOfflineEditing *mOfflineEditing = nullptr;
QgsVectorLayer *mpLayer = nullptr;
QString offlineDataPath;
QString offlineDbFile;
QStringList layerIds;
long numberOfFeatures;
int numberOfFields;

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.

void createSpatialiteAndSynchronizeBack();
void createGeopackageAndSynchronizeBack();
};

void TestQgsOfflineEditing::initTestCase()
{
//
// Runs once before any tests are run
//
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();

//OfflineEditing
mOfflineEditing = new QgsOfflineEditing();
offlineDataPath = ".";
}

void TestQgsOfflineEditing::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsOfflineEditing::init()
{
QString myFileName( TEST_DATA_DIR ); //defined in CmakeLists.txt
myFileName = myFileName + "/points.shp";
QFileInfo myMapFileInfo( myFileName );
mpLayer = new QgsVectorLayer( myMapFileInfo.filePath(),
myMapFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
QgsProject::instance()->addMapLayer( mpLayer );

numberOfFeatures = mpLayer->featureCount();
numberOfFields = mpLayer->fields().size();

layerIds.append( mpLayer->id() );
}

void TestQgsOfflineEditing::cleanup()
{
QgsProject::instance()->removeAllMapLayers();
layerIds.clear();
QDir dir( offlineDataPath );
dir.remove( offlineDbFile );
}

void TestQgsOfflineEditing::createSpatialiteAndSynchronizeBack()
{
offlineDbFile = "TestQgsOfflineEditing.sqlite";
QCOMPARE( mpLayer->name(), QStringLiteral( "points" ) );
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );
QCOMPARE( mpLayer->fields().size(), numberOfFields );

//convert
mOfflineEditing->convertToOfflineProject( offlineDataPath, offlineDbFile, layerIds, false, false );

mpLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayers().first() );
QCOMPARE( mpLayer->name(), QStringLiteral( "points (offline)" ) );
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );

//synchronize back
mOfflineEditing->synchronize();

mpLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayers().first() );
QCOMPARE( mpLayer->name(), QStringLiteral( "points" ) );
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );
QCOMPARE( mpLayer->fields().size(), numberOfFields );
}

void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
{
offlineDbFile = "TestQgsOfflineEditing.gpkg";
QCOMPARE( mpLayer->name(), QStringLiteral( "points" ) );
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );
QCOMPARE( mpLayer->fields().size(), numberOfFields );

//convert
mOfflineEditing->convertToOfflineProject( offlineDataPath, offlineDbFile, layerIds, false, true );

mpLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayers().first() );
QCOMPARE( mpLayer->name(), QStringLiteral( "points (offline)" ) );
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );
//comparing with the number +1 because GPKG created an fid
QCOMPARE( mpLayer->fields().size(), numberOfFields + 1 );

//synchronize back
mOfflineEditing->synchronize();

mpLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayers().first() );
QCOMPARE( mpLayer->name(), QStringLiteral( "points" ) );
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );
QCOMPARE( mpLayer->fields().size(), numberOfFields );
}

QGSTEST_MAIN( TestQgsOfflineEditing )
#include "testqgsofflineediting.moc"

0 comments on commit 85c7742

Please sign in to comment.