/
testqgsmdalprovider.cpp
146 lines (126 loc) · 5.54 KB
/
testqgsmdalprovider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/***************************************************************************
testqgsmdalprovider.cpp
--------------------------------------
Date : Decemeber 2018
Copyright : (C) 2018 by Peter Petrik
Email : zilolv@gmail.com
***************************************************************************
* *
* 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 <limits>
#include "qgstest.h"
#include <QObject>
#include <QString>
#include <QStringList>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
//qgis includes...
#include <qgis.h>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsmeshdataprovider.h>
/**
* \ingroup UnitTests
* This is a unit test for the gdal provider
*/
class TestQgsMdalProvider : public QObject
{
Q_OBJECT
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 load();
void filters();
void encodeDecodeUri();
private:
QString mTestDataDir;
QString mReport;
};
//runs before all tests
void TestQgsMdalProvider::initTestCase()
{
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
mReport = QStringLiteral( "<h1>MDAL Provider Tests</h1>\n" );
}
//runs after all tests
void TestQgsMdalProvider::cleanupTestCase()
{
QgsApplication::exitQgis();
const QString myReportFile = QDir::tempPath() + "/qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}
void TestQgsMdalProvider::filters()
{
const QString meshFilters = QgsProviderRegistry::instance()->fileMeshFilters();
QVERIFY( meshFilters.contains( "*.2dm" ) );
const QString datasetFilters = QgsProviderRegistry::instance()->fileMeshDatasetFilters();
QVERIFY( datasetFilters.contains( "*.dat" ) );
}
void TestQgsMdalProvider::encodeDecodeUri()
{
QgsProviderMetadata *mdalMetadata = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "mdal" ) );
// simple file uri
QVariantMap parts = mdalMetadata->decodeUri( QStringLiteral( "/home/data/test.nc" ) );
QCOMPARE( parts.value( QStringLiteral( "path" ) ).toString(), QStringLiteral( "/home/data/test.nc" ) );
QCOMPARE( parts.value( QStringLiteral( "driver" ) ).toString(), QString() );
QCOMPARE( parts.value( QStringLiteral( "layerName" ) ).toString(), QString() );
QCOMPARE( mdalMetadata->encodeUri( parts ), QStringLiteral( "/home/data/test.nc" ) );
// uri with driver and layer name
parts = mdalMetadata->decodeUri( QStringLiteral( "netcdf:\"/home/data/test.nc\":layer3" ) );
QCOMPARE( parts.value( QStringLiteral( "path" ) ).toString(), QStringLiteral( "/home/data/test.nc" ) );
QCOMPARE( parts.value( QStringLiteral( "driver" ) ).toString(), QStringLiteral( "netcdf" ) );
QCOMPARE( parts.value( QStringLiteral( "layerName" ) ).toString(), QStringLiteral( "layer3" ) );
QCOMPARE( mdalMetadata->encodeUri( parts ), QStringLiteral( "netcdf:\"/home/data/test.nc\":layer3" ) );
// uri with driver
parts = mdalMetadata->decodeUri( QStringLiteral( "Ugrid:\"/home/data/test.nc\"" ) );
QCOMPARE( parts.value( QStringLiteral( "path" ) ).toString(), QStringLiteral( "/home/data/test.nc" ) );
QCOMPARE( parts.value( QStringLiteral( "driver" ) ).toString(), QStringLiteral( "Ugrid" ) );
QCOMPARE( parts.value( QStringLiteral( "layerName" ) ).toString(), QString() );
QCOMPARE( mdalMetadata->encodeUri( parts ), QStringLiteral( "Ugrid:\"/home/data/test.nc\"" ) );
}
void TestQgsMdalProvider::load()
{
{
const QString file = QStringLiteral( TEST_DATA_DIR ) + "/mesh/quad_flower.2dm";
QgsDataProvider *provider = QgsProviderRegistry::instance()->createProvider(
QStringLiteral( "mdal" ),
file,
QgsDataProvider::ProviderOptions()
);
QgsMeshDataProvider *mp = dynamic_cast< QgsMeshDataProvider * >( provider );
QVERIFY( mp );
QVERIFY( mp->isValid() );
delete provider;
}
{
const QString file = QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/goodluckwiththisfilename.2dm" );
QgsDataProvider *provider = QgsProviderRegistry::instance()->createProvider(
QStringLiteral( "mdal" ),
file,
QgsDataProvider::ProviderOptions()
);
QgsMeshDataProvider *mp = dynamic_cast< QgsMeshDataProvider * >( provider );
QVERIFY( mp );
QVERIFY( !mp->isValid() );
delete provider;
}
}
QGSTEST_MAIN( TestQgsMdalProvider )
#include "testqgsmdalprovider.moc"