/
testqgsmergeattributesdialog.cpp
99 lines (83 loc) · 3.5 KB
/
testqgsmergeattributesdialog.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
/***************************************************************************
testqgsmergeattributesdialog.cpp
--------------------------------
Date : Feb 2023
Copyright : (C) 2023 by Damiano Lombardi
Email : damiano 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 "qgstest.h"
#include <QObject>
#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsvectorlayer.h"
#include "qgsmergeattributesdialog.h"
class TestQgsMergeattributesDialog : public QObject
{
Q_OBJECT
public:
TestQgsMergeattributesDialog() = default;
private:
QgisApp *mQgisApp = nullptr;
private slots:
void initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
mQgisApp = new QgisApp();
}
void cleanupTestCase()
{
QgsApplication::exitQgis();
}
void test()
{
// Create test layer
QgsVectorFileWriter::SaveVectorOptions options;
QgsVectorLayer ml( "Polygon", "test", "memory" );
QVERIFY( ml.isValid() );
QTemporaryFile tmpFile( QDir::tempPath() + "/TestQgsMergeattributesDialog" );
tmpFile.open();
const QString fileName( tmpFile.fileName( ) );
options.driverName = "GPKG";
options.layerName = "test";
QString newFilename;
const QgsVectorFileWriter::WriterError error( QgsVectorFileWriter::writeAsVectorFormatV3(
&ml,
fileName,
ml.transformContext(),
options, nullptr,
&newFilename ) );
QCOMPARE( error, QgsVectorFileWriter::WriterError::NoError );
QgsVectorLayer layer( QStringLiteral( "%1|layername=test" ).arg( newFilename ), "src_test", "ogr" );
QVERIFY( layer.startEditing() );
QgsVectorDataProvider *pr = layer.dataProvider();
// Create a feature
QgsFeature f1( layer.fields(), 1 );
f1.setGeometry( QgsGeometry::fromWkt( "POLYGON((0 0, 5 0, 5 5, 0 5, 0 0))" ) );
QVERIFY( pr->addFeature( f1 ) );
QCOMPARE( layer.featureCount(), 1 );
// And a bigger feature
QgsFeature f2( layer.fields(), 2 );
f2.setGeometry( QgsGeometry::fromWkt( "POLYGON((3 3, 10 3, 10 10, 3 10, 3 3))" ) );
QVERIFY( pr->addFeature( f2 ) );
QCOMPARE( layer.featureCount(), 2 );
// Merge the attributes together
QgsMergeAttributesDialog dialog( QgsFeatureList() << f1 << f2, &layer, mQgisApp->mapCanvas() );
// At beginnning the first feature of the list is the target
QCOMPARE( dialog.targetFeatureId(), FID_NULL );
QCOMPARE( dialog.mergedAttributes()[0], "Autogenerate" );
// Check after taking feature with largest geometry
QVERIFY( QMetaObject::invokeMethod( &dialog, "mFromLargestPushButton_clicked" ) );
QCOMPARE( dialog.targetFeatureId(), f2.id() );
QCOMPARE( dialog.mergedAttributes().first(), f2.id() );
}
};
QGSTEST_MAIN( TestQgsMergeattributesDialog )
#include "testqgsmergeattributesdialog.moc"