Skip to content

Commit e48225c

Browse files
committed
[composer] Add more tests to attribute table test suite
1 parent 7e977bc commit e48225c

File tree

3 files changed

+241
-4
lines changed

3 files changed

+241
-4
lines changed

python/core/composer/qgscomposerattributetable.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,15 @@ class QgsComposerAttributeTable : QgsComposerTable
144144
void setFeatureFilter( const QString& expression );
145145

146146
/**Returns the attributes fields which are shown by the table.
147-
* @returns a QSet of integers refering to the attributes in the vector layer
147+
* @returns a QSet of integers refering to the attributes in the vector layer. If
148+
* result is an empty QSet than all feature attributes will be shown.
148149
* @see setDisplayAttributes
149150
*/
150151
QSet<int> displayAttributes() const;
151152

152153
/**Sets the attributes to display in the table.
153154
* @param attr QSet of integer values refering to the attributes from the vector layer to show
155+
* Set to an empty QSet to show all feature attributes.
154156
* @param refresh set to true to force the table to refetch features from its vector layer
155157
* and immediately update the display of the table. This may result in the table changing size
156158
* to accomodate the new displayed feature attributes.

src/core/composer/qgscomposerattributetable.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,15 @@ class CORE_EXPORT QgsComposerAttributeTable: public QgsComposerTable
162162
void setFeatureFilter( const QString& expression );
163163

164164
/**Returns the attributes fields which are shown by the table.
165-
* @returns a QSet of integers refering to the attributes in the vector layer
165+
* @returns a QSet of integers refering to the attributes in the vector layer. If
166+
* result is an empty QSet than all feature attributes will be shown.
166167
* @see setDisplayAttributes
167168
*/
168169
QSet<int> displayAttributes() const { return mDisplayAttributes; }
169170

170171
/**Sets the attributes to display in the table.
171-
* @param attr QSet of integer values refering to the attributes from the vector layer to show
172+
* @param attr QSet of integer values refering to the attributes from the vector layer to show.
173+
* Set to an empty QSet to show all feature attributes.
172174
* @param refresh set to true to force the table to refetch features from its vector layer
173175
* and immediately update the display of the table. This may result in the table changing size
174176
* to accomodate the new displayed feature attributes.

tests/src/core/testqgscomposertable.cpp

Lines changed: 234 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgsapplication.h"
1919
#include "qgscomposition.h"
20+
#include "qgscomposermap.h"
2021
#include "qgscomposertexttable.h"
2122
#include "qgscomposerattributetable.h"
2223
#include "qgsmapsettings.h"
@@ -38,12 +39,24 @@ class TestQgsComposerTable: public QObject
3839

3940
void textTableHeadings(); //test setting/retrieving text table headers
4041
void textTableRows(); //test adding and retrieving text table rows
42+
void attributeTableHeadings(); //test retrieving attribute table headers
43+
void attributeTableRows(); //test retrieving attribute table rows
44+
void attributeTableFilterFeatures(); //test filtering attribute table rows
45+
void attributeTableSetAttributes(); //test subset of attributes in table
46+
void attributeTableAlias(); //test setting alias for attribute column
47+
void attributeTableVisibleOnly(); //test displaying only visible attributes
48+
void attributeTableSort(); //test sorting of attribute table
4149

4250
private:
4351
QgsComposition* mComposition;
52+
QgsComposerMap* mComposerMap;
4453
QgsComposerTextTable* mComposerTextTable;
4554
QgsMapSettings mMapSettings;
4655
QgsVectorLayer* mVectorLayer;
56+
QgsComposerAttributeTable* mComposerAttributeTable;
57+
58+
//compares rows in mComposerAttributeTable to expected rows
59+
void compareTable( QList<QStringList> &expectedRows );
4760
};
4861

4962
void TestQgsComposerTable::initTestCase()
@@ -52,7 +65,7 @@ void TestQgsComposerTable::initTestCase()
5265
QgsApplication::initQgis();
5366

5467
//create maplayers from testdata and add to layer registry
55-
QFileInfo vectorFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "france_parts.shp" );
68+
QFileInfo vectorFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "points.shp" );
5669
mVectorLayer = new QgsVectorLayer( vectorFileInfo.filePath(),
5770
vectorFileInfo.completeBaseName(),
5871
"ogr" );
@@ -66,6 +79,11 @@ void TestQgsComposerTable::initTestCase()
6679
mComposerTextTable = new QgsComposerTextTable( mComposition );
6780
mComposition->addItem( mComposerTextTable );
6881

82+
mComposerAttributeTable = new QgsComposerAttributeTable( mComposition );
83+
mComposition->addComposerTable( mComposerAttributeTable );
84+
mComposerAttributeTable->setVectorLayer( mVectorLayer );
85+
mComposerAttributeTable->setDisplayOnlyVisibleFeatures( false );
86+
mComposerAttributeTable->setMaximumNumberOfFeatures( 10 );
6987
}
7088

7189
void TestQgsComposerTable::cleanupTestCase()
@@ -144,8 +162,223 @@ void TestQgsComposerTable::textTableRows()
144162
}
145163
rowNumber++;
146164
}
165+
}
166+
167+
void TestQgsComposerTable::attributeTableHeadings()
168+
{
169+
//test retrieving attribute table headers
170+
QStringList expectedHeaders;
171+
expectedHeaders << "Class" << "Heading" << "Importance" << "Pilots" << "Cabin Crew" << "Staff";
147172

173+
//get header labels and compare
174+
QMap<int, QString> headerMap = mComposerAttributeTable->headerLabels();
175+
QMap<int, QString>::const_iterator headerIt = headerMap.constBegin();
176+
QString expected;
177+
QString evaluated;
178+
for ( ; headerIt != headerMap.constEnd(); ++headerIt )
179+
{
180+
evaluated = headerIt.value();
181+
expected = expectedHeaders.at( headerIt.key() );
182+
QCOMPARE( evaluated, expected );
183+
}
184+
}
185+
186+
void TestQgsComposerTable::compareTable( QList<QStringList> &expectedRows )
187+
{
188+
//retrieve rows and check
189+
QList<QgsAttributeMap> evaluatedRows;
190+
bool result = mComposerAttributeTable->getFeatureAttributes( evaluatedRows );
191+
QCOMPARE( result, true );
192+
193+
QList<QgsAttributeMap>::const_iterator resultIt = evaluatedRows.constBegin();
194+
int rowNumber = 0;
195+
int colNumber = 0;
196+
197+
//check that number of rows matches expected
198+
QCOMPARE( evaluatedRows.count(), expectedRows.count() );
199+
200+
for ( ; resultIt != evaluatedRows.constEnd(); ++resultIt )
201+
{
202+
colNumber = 0;
203+
QgsAttributeMap::const_iterator cellIt = ( *resultIt ).constBegin();
204+
for ( ; cellIt != ( *resultIt ).constEnd(); ++cellIt )
205+
{
206+
QCOMPARE(( *cellIt ).toString(), expectedRows.at( rowNumber ).at( colNumber ) );
207+
colNumber++;
208+
}
209+
//also check that number of columns matches expected
210+
QCOMPARE(( *resultIt ).count(), expectedRows.at( rowNumber ).count() );
211+
212+
rowNumber++;
213+
}
214+
}
215+
216+
void TestQgsComposerTable::attributeTableRows()
217+
{
218+
//test retrieving attribute table rows
219+
220+
QList<QStringList> expectedRows;
221+
QStringList row;
222+
row << "Jet" << "90" << "3" << "2" << "0" << "2";
223+
expectedRows.append( row );
224+
row.clear();
225+
row << "Biplane" << "0" << "1" << "3" << "3" << "6";
226+
expectedRows.append( row );
227+
row.clear();
228+
row << "Jet" << "85" << "3" << "1" << "1" << "2";
229+
expectedRows.append( row );
230+
231+
//retrieve rows and check
232+
mComposerAttributeTable->setMaximumNumberOfFeatures( 3 );
233+
compareTable( expectedRows );
234+
}
235+
236+
void TestQgsComposerTable::attributeTableFilterFeatures()
237+
{
238+
//test filtering attribute table rows
239+
mComposerAttributeTable->setMaximumNumberOfFeatures( 10 );
240+
mComposerAttributeTable->setFeatureFilter( QString( "\"Class\"='B52'" ) );
241+
mComposerAttributeTable->setFilterFeatures( true );
242+
243+
QList<QStringList> expectedRows;
244+
QStringList row;
245+
row << "B52" << "0" << "10" << "2" << "1" << "3";
246+
expectedRows.append( row );
247+
row.clear();
248+
row << "B52" << "12" << "10" << "1" << "1" << "2";
249+
expectedRows.append( row );
250+
row.clear();
251+
row << "B52" << "34" << "10" << "2" << "1" << "3";
252+
expectedRows.append( row );
253+
row.clear();
254+
row << "B52" << "80" << "10" << "2" << "1" << "3";
255+
expectedRows.append( row );
256+
257+
//retrieve rows and check
258+
compareTable( expectedRows );
259+
260+
mComposerAttributeTable->setFilterFeatures( false );
261+
}
262+
263+
void TestQgsComposerTable::attributeTableSetAttributes()
264+
{
265+
//test subset of attributes in table
266+
QSet<int> attributes;
267+
attributes << 0 << 3 << 4;
268+
mComposerAttributeTable->setDisplayAttributes( attributes );
269+
mComposerAttributeTable->setMaximumNumberOfFeatures( 3 );
270+
271+
QList<QStringList> expectedRows;
272+
QStringList row;
273+
row << "Jet" << "2" << "0";
274+
expectedRows.append( row );
275+
row.clear();
276+
row << "Biplane" << "3" << "3";
277+
expectedRows.append( row );
278+
row.clear();
279+
row << "Jet" << "1" << "1";
280+
expectedRows.append( row );
281+
282+
//retrieve rows and check
283+
compareTable( expectedRows );
284+
285+
attributes.clear();
286+
mComposerAttributeTable->setDisplayAttributes( attributes );
287+
}
288+
289+
void TestQgsComposerTable::attributeTableAlias()
290+
{
291+
//test setting alias for attribute column
292+
QMap<int, QString> fieldAliasMap;
293+
294+
fieldAliasMap.insert( 0, QString( "alias 0" ) );
295+
fieldAliasMap.insert( 3, QString( "alias 3" ) );
296+
mComposerAttributeTable->setFieldAliasMap( fieldAliasMap );
297+
298+
QStringList expectedHeaders;
299+
expectedHeaders << "alias 0" << "Heading" << "Importance" << "alias 3" << "Cabin Crew" << "Staff";
300+
301+
//get header labels and compare
302+
QMap<int, QString> headerMap = mComposerAttributeTable->headerLabels();
303+
QMap<int, QString>::const_iterator headerIt = headerMap.constBegin();
304+
QString expected;
305+
QString evaluated;
306+
for ( ; headerIt != headerMap.constEnd(); ++headerIt )
307+
{
308+
evaluated = headerIt.value();
309+
expected = expectedHeaders.at( headerIt.key() );
310+
QCOMPARE( evaluated, expected );
311+
}
312+
313+
fieldAliasMap.clear();
314+
mComposerAttributeTable->setFieldAliasMap( fieldAliasMap );
315+
}
316+
317+
void TestQgsComposerTable::attributeTableSort()
318+
{
319+
//test sorting of attribute table
320+
QList< QPair<int, bool> > sort;
321+
sort.append( qMakePair( 0, true ) );
322+
sort.append( qMakePair( 1, false ) );
323+
sort.append( qMakePair( 3, true ) );
324+
mComposerAttributeTable->setSortAttributes( sort );
325+
mComposerAttributeTable->setMaximumNumberOfFeatures( 5 );
326+
327+
QList<QStringList> expectedRows;
328+
QStringList row;
329+
row << "Biplane" << "0" << "1" << "3" << "3" << "6";
330+
expectedRows.append( row );
331+
row.clear();
332+
row << "Jet" << "95" << "3" << "1" << "1" << "2";
333+
expectedRows.append( row );
334+
row.clear();
335+
row << "Jet" << "90" << "3" << "2" << "0" << "2";
336+
expectedRows.append( row );
337+
row.clear();
338+
row << "Jet" << "90" << "3" << "1" << "0" << "1";
339+
expectedRows.append( row );
340+
row.clear();
341+
row << "Jet" << "85" << "3" << "1" << "1" << "2";
342+
expectedRows.append( row );
343+
344+
//retrieve rows and check
345+
compareTable( expectedRows );
346+
347+
sort.clear();
348+
mComposerAttributeTable->setSortAttributes( sort );
349+
}
350+
351+
void TestQgsComposerTable::attributeTableVisibleOnly()
352+
{
353+
//test displaying only visible attributes
354+
355+
mComposerMap = new QgsComposerMap( mComposition, 20, 20, 200, 100 );
356+
mComposerMap->setFrameEnabled( true );
357+
mComposition->addComposerMap( mComposerMap );
358+
mComposerMap->setNewExtent( QgsRectangle( -131.767, 30.558, -110.743, 41.070 ) );
359+
360+
mComposerAttributeTable->setComposerMap( mComposerMap );
361+
mComposerAttributeTable->setDisplayOnlyVisibleFeatures( true );
362+
363+
QList<QStringList> expectedRows;
364+
QStringList row;
365+
row << "Jet" << "90" << "3" << "2" << "0" << "2";
366+
expectedRows.append( row );
367+
row.clear();
368+
row << "Biplane" << "240" << "1" << "3" << "2" << "5";
369+
expectedRows.append( row );
370+
row.clear();
371+
row << "Jet" << "180" << "3" << "1" << "0" << "1";
372+
expectedRows.append( row );
373+
374+
//retrieve rows and check
375+
compareTable( expectedRows );
376+
377+
mComposerAttributeTable->setDisplayOnlyVisibleFeatures( false );
378+
mComposerAttributeTable->setComposerMap( 0 );
379+
mComposition->removeItem( mComposerMap );
148380
}
149381

150382
QTEST_MAIN( TestQgsComposerTable )
151383
#include "moc_testqgscomposertable.cxx"
384+

0 commit comments

Comments
 (0)