QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgstestutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstestutils.cpp
3 --------------------
4 begin : January 2018
5 copyright : (C) 2018 by Nyall Dawson
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgstestutils.h"
17
18#include "qgsconnectionpool.h"
20
21#include <QtConcurrentMap>
22
25
26static void getFeaturesForProvider( const QPair< std::shared_ptr< QgsAbstractFeatureSource >, QgsFeatureRequest > &pair )
27{
28 QgsFeatureIterator it = pair.first->getFeatures( pair.second );
29 QgsFeature f;
30 while ( it.nextFeature( f ) )
31 {
32
33 }
34}
35
36bool QgsTestUtils::testProviderIteratorThreadSafety( QgsVectorDataProvider *provider, const QgsFeatureRequest &request )
37{
38 constexpr int JOBS_TO_RUN = 100;
39 QList< QPair< std::shared_ptr< QgsAbstractFeatureSource >, QgsFeatureRequest > > jobs;
40 jobs.reserve( JOBS_TO_RUN );
41 for ( int i = 0; i < JOBS_TO_RUN; ++i )
42 {
43 jobs.append( qMakePair( std::shared_ptr< QgsAbstractFeatureSource >( provider->featureSource() ), request ) );
44 }
45
46 //freaking hammer the provider with a ton of concurrent requests.
47 //thread unsafe providers... you better be ready!!!!
48 QtConcurrent::blockingMap( jobs, getFeaturesForProvider );
49
50 return true;
51}
52
53bool QgsTestUtils::compareDomElements( const QDomElement &element1, const QDomElement &element2 )
54{
55 QString tag1 = element1.tagName();
56 tag1.replace( QRegularExpression( ".*:" ), QString() );
57 QString tag2 = element2.tagName();
58 tag2.replace( QRegularExpression( ".*:" ), QString() );
59 if ( tag1 != tag2 )
60 {
61 qDebug( "Different tag names: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
62 return false;
63 }
64
65 if ( element1.hasAttributes() != element2.hasAttributes() )
66 {
67 qDebug( "Different hasAttributes: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
68 return false;
69 }
70
71 if ( element1.hasAttributes() )
72 {
73 const QDomNamedNodeMap attrs1 = element1.attributes();
74 const QDomNamedNodeMap attrs2 = element2.attributes();
75
76 if ( attrs1.size() != attrs2.size() )
77 {
78 qDebug( "Different attributes size: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
79 return false;
80 }
81
82 for ( int i = 0; i < attrs1.size(); ++i )
83 {
84 const QDomNode node1 = attrs1.item( i );
85 const QDomAttr attr1 = node1.toAttr();
86
87 if ( !element2.hasAttribute( attr1.name() ) )
88 {
89 qDebug( "Element2 has not attribute: %s, %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data(), attr1.name().toLatin1().data() );
90 return false;
91 }
92
93 if ( element2.attribute( attr1.name() ) != attr1.value() )
94 {
95 qDebug( "Element2 attribute has not the same value: %s, %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data(), attr1.name().toLatin1().data() );
96 return false;
97 }
98 }
99 }
100
101 if ( element1.hasChildNodes() != element2.hasChildNodes() )
102 {
103 qDebug( "Different childNodes: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
104 return false;
105 }
106
107 if ( element1.hasChildNodes() )
108 {
109 const QDomNodeList nodes1 = element1.childNodes();
110 const QDomNodeList nodes2 = element2.childNodes();
111
112 if ( nodes1.size() != nodes2.size() )
113 {
114 qDebug( "Different childNodes size: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
115 return false;
116 }
117
118 for ( int i = 0; i < nodes1.size(); ++i )
119 {
120 const QDomNode node1 = nodes1.at( i );
121 const QDomNode node2 = nodes2.at( i );
122 if ( node1.isElement() && node2.isElement() )
123 {
124 QDomElement elt1 = node1.toElement();
125 QDomElement elt2 = node2.toElement();
126
127 if ( !compareDomElements( elt1, elt2 ) )
128 return false;
129 }
130 else if ( node1.isText() && node2.isText() )
131 {
132 const QDomText txt1 = node1.toText();
133 const QDomText txt2 = node2.toText();
134
135 if ( txt1.data() != txt2.data() )
136 {
137 qDebug( "Different text data: %s %s", tag1.toLatin1().data(), txt1.data().toLatin1().data() );
138 qDebug( "Different text data: %s %s", tag2.toLatin1().data(), txt2.data().toLatin1().data() );
139 return false;
140 }
141 }
142 }
143 }
144
145 if ( element1.text() != element2.text() )
146 {
147 qDebug( "Different text: %s %s", tag1.toLatin1().data(), element1.text().toLatin1().data() );
148 qDebug( "Different text: %s %s", tag2.toLatin1().data(), element2.text().toLatin1().data() );
149 return false;
150 }
151
152 return true;
153}
154
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Base class for vector data providers.
virtual QgsAbstractFeatureSource * featureSource() const =0
Returns feature source object that can be used for querying provider's data.