QGIS API Documentation 3.99.0-Master (752b475928d)
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 <QCryptographicHash>
22#include <QtConcurrentMap>
23
26
27static void getFeaturesForProvider( const QPair< std::shared_ptr< QgsAbstractFeatureSource >, QgsFeatureRequest > &pair )
28{
29 QgsFeatureIterator it = pair.first->getFeatures( pair.second );
30 QgsFeature f;
31 while ( it.nextFeature( f ) )
32 {
33
34 }
35}
36
37bool QgsTestUtils::testProviderIteratorThreadSafety( QgsVectorDataProvider *provider, const QgsFeatureRequest &request )
38{
39 constexpr int JOBS_TO_RUN = 100;
40 QList< QPair< std::shared_ptr< QgsAbstractFeatureSource >, QgsFeatureRequest > > jobs;
41 jobs.reserve( JOBS_TO_RUN );
42 for ( int i = 0; i < JOBS_TO_RUN; ++i )
43 {
44 jobs.append( qMakePair( std::shared_ptr< QgsAbstractFeatureSource >( provider->featureSource() ), request ) );
45 }
46
47 //freaking hammer the provider with a ton of concurrent requests.
48 //thread unsafe providers... you better be ready!!!!
49 QtConcurrent::blockingMap( jobs, getFeaturesForProvider );
50
51 return true;
52}
53
54bool QgsTestUtils::compareDomElements( const QDomElement &element1, const QDomElement &element2 )
55{
56 QString tag1 = element1.tagName();
57 tag1.replace( QRegularExpression( ".*:" ), QString() );
58 QString tag2 = element2.tagName();
59 tag2.replace( QRegularExpression( ".*:" ), QString() );
60 if ( tag1 != tag2 )
61 {
62 qDebug( "Different tag names: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
63 return false;
64 }
65
66 if ( element1.hasAttributes() != element2.hasAttributes() )
67 {
68 qDebug( "Different hasAttributes: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
69 return false;
70 }
71
72 if ( element1.hasAttributes() )
73 {
74 const QDomNamedNodeMap attrs1 = element1.attributes();
75 const QDomNamedNodeMap attrs2 = element2.attributes();
76
77 if ( attrs1.size() != attrs2.size() )
78 {
79 qDebug( "Different attributes size: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
80 return false;
81 }
82
83 for ( int i = 0; i < attrs1.size(); ++i )
84 {
85 const QDomNode node1 = attrs1.item( i );
86 const QDomAttr attr1 = node1.toAttr();
87
88 if ( !element2.hasAttribute( attr1.name() ) )
89 {
90 qDebug( "Element2 has not attribute: %s, %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data(), attr1.name().toLatin1().data() );
91 return false;
92 }
93
94 if ( element2.attribute( attr1.name() ) != attr1.value() )
95 {
96 qDebug( "Element2 attribute has not the same value: %s, %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data(), attr1.name().toLatin1().data() );
97 return false;
98 }
99 }
100 }
101
102 if ( element1.hasChildNodes() != element2.hasChildNodes() )
103 {
104 qDebug( "Different childNodes: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
105 return false;
106 }
107
108 if ( element1.hasChildNodes() )
109 {
110 const QDomNodeList nodes1 = element1.childNodes();
111 const QDomNodeList nodes2 = element2.childNodes();
112
113 if ( nodes1.size() != nodes2.size() )
114 {
115 qDebug( "Different childNodes size: %s, %s", tag1.toLatin1().data(), tag2.toLatin1().data() );
116 return false;
117 }
118
119 for ( int i = 0; i < nodes1.size(); ++i )
120 {
121 const QDomNode node1 = nodes1.at( i );
122 const QDomNode node2 = nodes2.at( i );
123 if ( node1.isElement() && node2.isElement() )
124 {
125 QDomElement elt1 = node1.toElement();
126 QDomElement elt2 = node2.toElement();
127
128 if ( !compareDomElements( elt1, elt2 ) )
129 return false;
130 }
131 else if ( node1.isText() && node2.isText() )
132 {
133 const QDomText txt1 = node1.toText();
134 const QDomText txt2 = node2.toText();
135
136 if ( txt1.data() != txt2.data() )
137 {
138 qDebug( "Different text data: %s %s", tag1.toLatin1().data(), txt1.data().toLatin1().data() );
139 qDebug( "Different text data: %s %s", tag2.toLatin1().data(), txt2.data().toLatin1().data() );
140 return false;
141 }
142 }
143 }
144 }
145
146 if ( element1.text() != element2.text() )
147 {
148 qDebug( "Different text: %s %s", tag1.toLatin1().data(), element1.text().toLatin1().data() );
149 qDebug( "Different text: %s %s", tag2.toLatin1().data(), element2.text().toLatin1().data() );
150 return false;
151 }
152
153 return true;
154}
155
156QString QgsTestUtils::sanitizeFakeHttpEndpoint( const QString &urlString )
157{
158 QString modifiedUrlString = urlString;
159
160 // For REST API using URL subpaths, normalize the subpaths
161 const int afterEndpointStartPos = static_cast<int>( modifiedUrlString.indexOf( "fake_qgis_http_endpoint" ) + strlen( "fake_qgis_http_endpoint" ) );
162 QString afterEndpointStart = modifiedUrlString.mid( afterEndpointStartPos );
163 afterEndpointStart.replace( QLatin1String( "/" ), QLatin1String( "_" ) );
164 modifiedUrlString = modifiedUrlString.mid( 0, afterEndpointStartPos ) + afterEndpointStart;
165
166 const qsizetype posQuotationMark = modifiedUrlString.indexOf( '?' );
167 QString args = modifiedUrlString.mid( posQuotationMark );
168 if ( modifiedUrlString.size() > 256 )
169 {
170 args.replace( '/', '_' );
171 args = QCryptographicHash::hash( args.toUtf8(), QCryptographicHash::Md5 ).toHex();
172 }
173 else
174 {
175 args.replace( '?', '_' );
176 args.replace( '&', '_' );
177 args.replace( '<', '_' );
178 args.replace( '>', '_' );
179 args.replace( '\"', '_' );
180 args.replace( '\'', '_' );
181 args.replace( ' ', '_' );
182 args.replace( ':', '_' );
183 args.replace( '/', '_' );
184 args.replace( '\n', '_' );
185 }
186 return modifiedUrlString.mid( 0, posQuotationMark ) + args;
187}
188
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.