Skip to content

Commit

Permalink
[tests] Brand new C++ test and new py test case for ogr proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 10, 2017
1 parent e85897c commit 77a73f4
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 6 deletions.
2 changes: 2 additions & 0 deletions tests/src/providers/CMakeLists.txt
Expand Up @@ -66,6 +66,8 @@ SET_TARGET_PROPERTIES(qgis_wcsprovidertest PROPERTIES

ADD_QGIS_TEST(gdalprovidertest testqgsgdalprovider.cpp)

ADD_QGIS_TEST(ogrprovidertest testqgsogrprovider.cpp)

ADD_QGIS_TEST(wmscapabilititestest
testqgswmscapabilities.cpp)
TARGET_LINK_LIBRARIES(qgis_wmscapabilititestest wmsprovider_a)
Expand Down
124 changes: 124 additions & 0 deletions tests/src/providers/testqgsogrprovider.cpp
@@ -0,0 +1,124 @@
/***************************************************************************
testqgsogrprovider.cpp - TestQgsOgrProvider
---------------------
begin : 10.11.2017
copyright : (C) 2017 by Alessandro Pasotti
email : apasotti at boundlessgeo dot 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 "qgstest.h"

//qgis includes...
#include <qgis.h>
#include <qgssettings.h>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsvectorlayer.h>
#include <qgsnetworkaccessmanager.h>

#include <QObject>

#include <cpl_conv.h>


/**
* \ingroup UnitTests
* This is a unit test for the ogr provider
*/
class TestQgsOgrProvider : 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 setupProxy();

private:
QString mTestDataDir;
QString mReport;
signals:

public slots:
};



//runs before all tests
void TestQgsOgrProvider::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>OGR Provider Tests</h1>\n" );
}

//runs after all tests
void TestQgsOgrProvider::cleanupTestCase()
{
QgsApplication::exitQgis();
QString myReportFile = QDir::tempPath() + "/qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}

void TestQgsOgrProvider::setupProxy()
{

QgsSettings settings;
{
settings.setValue( QStringLiteral( "proxy/proxyEnabled" ), true );
settings.setValue( QStringLiteral( "proxy/proxyPort" ), QStringLiteral( "1234" ) );
settings.setValue( QStringLiteral( "proxy/proxyHost" ), QStringLiteral( "myproxyhostname.com" ) );
settings.setValue( QStringLiteral( "proxy/proxyUser" ), QStringLiteral( "username" ) );
settings.setValue( QStringLiteral( "proxy/proxyPassword" ), QStringLiteral( "password" ) );
settings.setValue( QStringLiteral( "proxy/proxyExcludedUrls" ), QStringLiteral( "http://www.myhost.com|http://www.myotherhost.com" ) );
QgsNetworkAccessManager::instance()->setupDefaultProxyAndCache();
QgsVectorLayer vl( mTestDataDir + '/' + QStringLiteral( "lines.shp" ), QStringLiteral( "proxy_test" ), QLatin1Literal( "ogr" ) );
QVERIFY( vl.isValid() );
const char *proxyConfig = CPLGetConfigOption( "GDAL_HTTP_PROXY", NULL );
QCOMPARE( proxyConfig, "myproxyhostname.com:1234" );
const char *proxyCredentials = CPLGetConfigOption( "GDAL_HTTP_PROXYUSERPWD", NULL );
QCOMPARE( proxyCredentials, "username:password" );
}

{
// Test partial config
settings.setValue( QStringLiteral( "proxy/proxyEnabled" ), true );
settings.remove( QStringLiteral( "proxy/proxyPort" ) );
settings.setValue( QStringLiteral( "proxy/proxyHost" ), QStringLiteral( "myproxyhostname.com" ) );
settings.setValue( QStringLiteral( "proxy/proxyUser" ), QStringLiteral( "username" ) );
settings.remove( QStringLiteral( "proxy/proxyPassword" ) );
QgsNetworkAccessManager::instance()->setupDefaultProxyAndCache();
QgsVectorLayer vl( mTestDataDir + '/' + QStringLiteral( "lines.shp" ), QStringLiteral( "proxy_test" ), QLatin1Literal( "ogr" ) );
QVERIFY( vl.isValid() );
const char *proxyConfig = CPLGetConfigOption( "GDAL_HTTP_PROXY", NULL );
QCOMPARE( proxyConfig, "myproxyhostname.com" );
const char *proxyCredentials = CPLGetConfigOption( "GDAL_HTTP_PROXYUSERPWD", NULL );
QCOMPARE( proxyCredentials, "username" );
}

}


QGSTEST_MAIN( TestQgsOgrProvider )
#include "testqgsogrprovider.moc"
38 changes: 32 additions & 6 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -17,13 +17,12 @@
import sys
import tempfile

from qgis.core import QgsVectorLayer, QgsVectorDataProvider, QgsWkbTypes, QgsFeature, QgsFeatureRequest
from qgis.testing import (
start_app,
unittest
)
from utilities import unitTestDataPath
from osgeo import gdal, ogr # NOQA
from qgis.core import (QgsFeature, QgsFeatureRequest, QgsSettings,
QgsVectorDataProvider, QgsVectorLayer, QgsWkbTypes, QgsNetworkAccessManager)
from qgis.testing import start_app, unittest

from utilities import unitTestDataPath

start_app()
TEST_DATA_DIR = unitTestDataPath()
Expand Down Expand Up @@ -292,6 +291,33 @@ def testTriangleTINPolyhedralSurface(self):
Triangle => mapped to Polygon
"""

def testSetupProxy(self):
"""Test proxy setup"""
settings = QgsSettings()
settings.setValue("proxy/proxyEnabled", True)
settings.setValue("proxy/proxyPort", '1234')
settings.setValue("proxy/proxyHost", 'myproxyhostname.com')
settings.setValue("proxy/proxyUser", 'username')
settings.setValue("proxy/proxyPassword", 'password')
settings.setValue("proxy/proxyExcludedUrls", "http://www.myhost.com|http://www.myotherhost.com")
QgsNetworkAccessManager.instance().setupDefaultProxyAndCache()
vl = QgsVectorLayer(TEST_DATA_DIR + '/' + 'lines.shp', 'proxy_test', 'ogr')
self.assertTrue(vl.isValid())
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXY"), "myproxyhostname.com:1234")
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXYUSERPWD"), "username:password")

settings.setValue("proxy/proxyEnabled", True)
settings.remove("proxy/proxyPort")
settings.setValue("proxy/proxyHost", 'myproxyhostname.com')
settings.setValue("proxy/proxyUser", 'username')
settings.remove("proxy/proxyPassword")
settings.setValue("proxy/proxyExcludedUrls", "http://www.myhost.com|http://www.myotherhost.com")
QgsNetworkAccessManager.instance().setupDefaultProxyAndCache()
vl = QgsVectorLayer(TEST_DATA_DIR + '/' + 'lines.shp', 'proxy_test', 'ogr')
self.assertTrue(vl.isValid())
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXY"), "myproxyhostname.com")
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXYUSERPWD"), "username")


if __name__ == '__main__':
unittest.main()

0 comments on commit 77a73f4

Please sign in to comment.