Skip to content

Commit

Permalink
Tests - Run pyupgrade on all Python test files
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry authored and nyalldawson committed Jan 31, 2023
1 parent a953b34 commit e2cfb8d
Show file tree
Hide file tree
Showing 458 changed files with 2,297 additions and 2,829 deletions.
4 changes: 4 additions & 0 deletions .flake8
Expand Up @@ -127,3 +127,7 @@ exclude =
python/server/auto_additions/,
# Plenty of star imports used
python/PyQt/
cmake/
# People using a virtualenv
.venv/
venv/
15 changes: 7 additions & 8 deletions tests/src/python/test_authmanager_oauth2_ows.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tests for auth manager WMS/WFS using QGIS Server through OAuth2
enabled qgis_wrapped_server.py.
Expand Down Expand Up @@ -167,8 +166,8 @@ def setUpClass(cls):
assert cls.port != 0

# We need a valid port before we setup the oauth configuration
cls.token_uri = '%s://%s:%s/token' % (cls.protocol, cls.hostname, cls.port)
cls.refresh_token_uri = '%s://%s:%s/refresh' % (cls.protocol, cls.hostname, cls.port)
cls.token_uri = f'{cls.protocol}://{cls.hostname}:{cls.port}/token'
cls.refresh_token_uri = f'{cls.protocol}://{cls.hostname}:{cls.port}/refresh'
# Need a random authcfg or the cache will bites us back!
cls.authcfg_id = setup_oauth(cls.username, cls.password, cls.token_uri, cls.refresh_token_uri, str(random.randint(0, 10000000)))
# This is to test wrong credentials
Expand All @@ -178,7 +177,7 @@ def setUpClass(cls):
assert cls.auth_config.isValid()

# Wait for the server process to start
assert waitServer('%s://%s:%s' % (cls.protocol, cls.hostname, cls.port)), "Server is not responding! %s://%s:%s" % (cls.protocol, cls.hostname, cls.port)
assert waitServer(f'{cls.protocol}://{cls.hostname}:{cls.port}'), f"Server is not responding! {cls.protocol}://{cls.hostname}:{cls.port}"

@classmethod
def tearDownClass(cls):
Expand All @@ -205,13 +204,13 @@ def _getWFSLayer(cls, type_name, layer_name=None, authcfg=None):
parms = {
'srsname': 'EPSG:4326',
'typename': type_name,
'url': '%s://%s:%s/?map=%s' % (cls.protocol, cls.hostname, cls.port, cls.project_path),
'url': f'{cls.protocol}://{cls.hostname}:{cls.port}/?map={cls.project_path}',
'version': 'auto',
'table': '',
}
if authcfg is not None:
parms.update({'authcfg': authcfg})
uri = ' '.join([("%s='%s'" % (k, v)) for k, v in list(parms.items())])
uri = ' '.join([(f"{k}='{v}'") for k, v in list(parms.items())])
wfs_layer = QgsVectorLayer(uri, layer_name, 'WFS')
return wfs_layer

Expand All @@ -224,7 +223,7 @@ def _getWMSLayer(cls, layers, layer_name=None, authcfg=None):
layer_name = 'wms_' + layers.replace(',', '')
parms = {
'crs': 'EPSG:4326',
'url': '%s://%s:%s/?map=%s' % (cls.protocol, cls.hostname, cls.port, cls.project_path),
'url': f'{cls.protocol}://{cls.hostname}:{cls.port}/?map={cls.project_path}',
'format': 'image/png',
# This is needed because of a really weird implementation in QGIS Server, that
# replaces _ in the the real layer name with spaces
Expand All @@ -235,7 +234,7 @@ def _getWMSLayer(cls, layers, layer_name=None, authcfg=None):
}
if authcfg is not None:
parms.update({'authcfg': authcfg})
uri = '&'.join([("%s=%s" % (k, v.replace('=', '%3D'))) for k, v in list(parms.items())])
uri = '&'.join([("{}={}".format(k, v.replace('=', '%3D'))) for k, v in list(parms.items())])
wms_layer = QgsRasterLayer(uri, layer_name, 'wms')
return wms_layer

Expand Down
3 changes: 1 addition & 2 deletions tests/src/python/test_authmanager_ogr.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tests for auth manager Basic Auth OGR connection credentials injection
Expand Down Expand Up @@ -95,7 +94,7 @@ def testConnections(self):
pr = QgsProviderRegistry.instance().createProvider('ogr', '')
for uri, expanded in TEST_URIS.items():
pr.setDataSourceUri(uri % self.authcfg)
self.assertTrue(expanded in pr.dataSourceUri(True), "%s != %s" % (expanded, pr.dataSourceUri(True)))
self.assertTrue(expanded in pr.dataSourceUri(True), f"{expanded} != {pr.dataSourceUri(True)}")

# Test sublayers
for uri, expanded in TEST_URIS.items():
Expand Down
3 changes: 1 addition & 2 deletions tests/src/python/test_authmanager_ogr_postgres.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tests for auth manager Basic Auth access to postgres.
Expand Down Expand Up @@ -183,7 +182,7 @@ def setUpClass(cls):
test_sql = os.path.join(unitTestDataPath('provider'), 'testdata_pg.sql')
subprocess.check_call([os.path.join(QGIS_POSTGRES_EXECUTABLE_PATH, 'psql'), '-h', 'localhost', '-p', cls.port, '-f', test_sql, cls.dbname], env=env)
# Create a role
subprocess.check_call([os.path.join(QGIS_POSTGRES_EXECUTABLE_PATH, 'psql'), '-h', 'localhost', '-p', cls.port, '-c', 'CREATE ROLE "%s" WITH SUPERUSER LOGIN PASSWORD \'%s\'' % (cls.username, cls.password), cls.dbname], env=env)
subprocess.check_call([os.path.join(QGIS_POSTGRES_EXECUTABLE_PATH, 'psql'), '-h', 'localhost', '-p', cls.port, '-c', f'CREATE ROLE "{cls.username}" WITH SUPERUSER LOGIN PASSWORD \'{cls.password}\'', cls.dbname], env=env)

@classmethod
def tearDownClass(cls):
Expand Down
17 changes: 8 additions & 9 deletions tests/src/python/test_authmanager_password_ows.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tests for auth manager WMS/WFS using QGIS Server through HTTP Basic
enabled qgis_wrapped_server.py.
Expand Down Expand Up @@ -107,7 +106,7 @@ def setUpClass(cls):
cls.port = int(re.findall(br':(\d+)', line)[0])
assert cls.port != 0
# Wait for the server process to start
assert waitServer('%s://%s:%s' % (cls.protocol, cls.hostname, cls.port)), "Server is not responding! %s://%s:%s" % (cls.protocol, cls.hostname, cls.port)
assert waitServer(f'{cls.protocol}://{cls.hostname}:{cls.port}'), f"Server is not responding! {cls.protocol}://{cls.hostname}:{cls.port}"

@classmethod
def tearDownClass(cls):
Expand All @@ -134,13 +133,13 @@ def _getWFSLayer(cls, type_name, layer_name=None, authcfg=None):
parms = {
'srsname': 'EPSG:4326',
'typename': type_name,
'url': '%s://%s:%s/?map=%s' % (cls.protocol, cls.hostname, cls.port, cls.project_path),
'url': f'{cls.protocol}://{cls.hostname}:{cls.port}/?map={cls.project_path}',
'version': 'auto',
'table': '',
}
if authcfg is not None:
parms.update({'authcfg': authcfg})
uri = ' '.join([("%s='%s'" % (k, v)) for k, v in list(parms.items())])
uri = ' '.join([(f"{k}='{v}'") for k, v in list(parms.items())])
wfs_layer = QgsVectorLayer(uri, layer_name, 'WFS')
return wfs_layer

Expand All @@ -153,7 +152,7 @@ def _getWMSLayer(cls, layers, layer_name=None, authcfg=None):
layer_name = 'wms_' + layers.replace(',', '')
parms = {
'crs': 'EPSG:4326',
'url': '%s://%s:%s/?map=%s' % (cls.protocol, cls.hostname, cls.port, cls.project_path),
'url': f'{cls.protocol}://{cls.hostname}:{cls.port}/?map={cls.project_path}',
# This is needed because of a really weird implementation in QGIS Server, that
# replaces _ in the the real layer name with spaces
'layers': urllib.parse.quote(layers.replace('_', ' ')),
Expand All @@ -163,7 +162,7 @@ def _getWMSLayer(cls, layers, layer_name=None, authcfg=None):
}
if authcfg is not None:
parms.update({'authcfg': authcfg})
uri = '&'.join([("%s=%s" % (k, v.replace('=', '%3D'))) for k, v in list(parms.items())])
uri = '&'.join([("{}={}".format(k, v.replace('=', '%3D'))) for k, v in list(parms.items())])
wms_layer = QgsRasterLayer(uri, layer_name, 'wms')
return wms_layer

Expand All @@ -174,7 +173,7 @@ def _getGeoJsonLayer(cls, type_name, layer_name=None, authcfg=None):
"""
if layer_name is None:
layer_name = 'geojson_' + type_name
uri = '%s://%s:%s/?MAP=%s&SERVICE=WFS&REQUEST=GetFeature&TYPENAME=%s&VERSION=2.0.0&OUTPUTFORMAT=geojson' % (cls.protocol, cls.hostname, cls.port, cls.project_path, urllib.parse.quote(type_name))
uri = f'{cls.protocol}://{cls.hostname}:{cls.port}/?MAP={cls.project_path}&SERVICE=WFS&REQUEST=GetFeature&TYPENAME={urllib.parse.quote(type_name)}&VERSION=2.0.0&OUTPUTFORMAT=geojson'
if authcfg is not None:
uri += " authcfg='%s'" % authcfg
geojson_layer = QgsVectorLayer(uri, layer_name, 'ogr')
Expand Down Expand Up @@ -219,7 +218,7 @@ def testInvalidAuthFileDownload(self):
"WIDTH": "500",
"CRS": "EPSG:3857"
}.items())])
url = '%s://%s:%s/%s' % (self.protocol, self.hostname, self.port, qs)
url = f'{self.protocol}://{self.hostname}:{self.port}/{qs}'

destination = tempfile.mktemp()
loop = QEventLoop()
Expand Down Expand Up @@ -255,7 +254,7 @@ def testValidAuthFileDownload(self):
"WIDTH": "500",
"CRS": "EPSG:3857"
}.items())])
url = '%s://%s:%s/%s' % (self.protocol, self.hostname, self.port, qs)
url = f'{self.protocol}://{self.hostname}:{self.port}/{qs}'

destination = tempfile.mktemp()
loop = QEventLoop()
Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_authmanager_password_postgres.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tests for auth manager Password access to postgres.
Expand Down
11 changes: 5 additions & 6 deletions tests/src/python/test_authmanager_pki_ows.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tests for auth manager WMS/WFS using QGIS Server through PKI
enabled qgis_wrapped_server.py.
Expand Down Expand Up @@ -129,7 +128,7 @@ def setUpClass(cls):
cls.port = int(re.findall(br':(\d+)', line)[0])
assert cls.port != 0
# Wait for the server process to start
assert waitServer('%s://%s:%s' % (cls.protocol, cls.hostname, cls.port)), "Server is not responding! %s://%s:%s" % (cls.protocol, cls.hostname, cls.port)
assert waitServer(f'{cls.protocol}://{cls.hostname}:{cls.port}'), f"Server is not responding! {cls.protocol}://{cls.hostname}:{cls.port}"

@classmethod
def tearDownClass(cls):
Expand All @@ -156,13 +155,13 @@ def _getWFSLayer(cls, type_name, layer_name=None, authcfg=None):
parms = {
'srsname': 'EPSG:4326',
'typename': type_name,
'url': '%s://%s:%s/?map=%s' % (cls.protocol, cls.hostname, cls.port, cls.project_path),
'url': f'{cls.protocol}://{cls.hostname}:{cls.port}/?map={cls.project_path}',
'version': 'auto',
'table': '',
}
if authcfg is not None:
parms.update({'authcfg': authcfg})
uri = ' '.join([("%s='%s'" % (k, v)) for k, v in list(parms.items())])
uri = ' '.join([(f"{k}='{v}'") for k, v in list(parms.items())])
wfs_layer = QgsVectorLayer(uri, layer_name, 'WFS')
return wfs_layer

Expand All @@ -175,7 +174,7 @@ def _getWMSLayer(cls, layers, layer_name=None, authcfg=None):
layer_name = 'wms_' + layers.replace(',', '')
parms = {
'crs': 'EPSG:4326',
'url': '%s://%s:%s/?map=%s' % (cls.protocol, cls.hostname, cls.port, cls.project_path),
'url': f'{cls.protocol}://{cls.hostname}:{cls.port}/?map={cls.project_path}',
'format': 'image/png',
# This is needed because of a really weird implementation in QGIS Server, that
# replaces _ in the the real layer name with spaces
Expand All @@ -186,7 +185,7 @@ def _getWMSLayer(cls, layers, layer_name=None, authcfg=None):
}
if authcfg is not None:
parms.update({'authcfg': authcfg})
uri = '&'.join([("%s=%s" % (k, v.replace('=', '%3D'))) for k, v in list(parms.items())])
uri = '&'.join([("{}={}".format(k, v.replace('=', '%3D'))) for k, v in list(parms.items())])
wms_layer = QgsRasterLayer(uri, layer_name, 'wms')
return wms_layer

Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_authmanager_pki_postgres.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tests for auth manager PKI access to postgres.
Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_authmanager_proxy.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tests for auth manager Basic configuration update proxy
Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_authsettingswidget.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tests for authentication widget
Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_console.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for the console
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_core_additions.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for core additions
From build dir, run: ctest -R PyCoreAdditions -V
Expand Down
3 changes: 1 addition & 2 deletions tests/src/python/test_db_manager_gpkg.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for the DBManager GPKG plugin
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -453,7 +452,7 @@ def testAmphibiousMode(self, ):
connection = createDbPlugin('gpkg', connectionName)
connection.connect()
db = connection.database()
res = db.connector._execute(None, "SELECT St_area({}) from foo".format(db.tables()[0].fields()[1].name))
res = db.connector._execute(None, f"SELECT St_area({db.tables()[0].fields()[1].name}) from foo")
results = [row for row in res]
self.assertEqual(results,
[(215229.265625,), (247328.171875,), (261752.78125,), (547597.2109375,), (15775.7578125,),
Expand Down
3 changes: 1 addition & 2 deletions tests/src/python/test_db_manager_postgis.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for the DBManager GPKG plugin
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -183,7 +182,7 @@ def setUpProvider(cls, authId):
if provider is None:
raise Exception("cannot create postgres provider")
if not provider.isValid():
raise Exception("Created postgres provider is not valid: {}".format(str(provider.errors())))
raise Exception(f"Created postgres provider is not valid: {str(provider.errors())}")
# save provider config that is the way how db_manager is aware of a PG connection
cls.addConnectionConfig(TEST_CONNECTION_NAME, uri)

Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_db_manager_spatialite.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for the DBManager SPATIALITE plugin
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_db_manager_sql_window.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for the DBManager SQL Window
From build dir, run: ctest -R PyQgsDBManagerSQLWindow -V
Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_disabled_tests.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""Contains tests which reveal broken behavior in QGIS.
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down
3 changes: 1 addition & 2 deletions tests/src/python/test_hana_utils.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""Helper utilities for HANA provider.
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -130,4 +129,4 @@ def cleanUp(conn, schema_name):
def generateSchemaName(conn, prefix):
sql = "SELECT REPLACE(CURRENT_UTCDATE, '-', '') || '_' || BINTOHEX(SYSUUID) FROM DUMMY;"
uid = QgsHanaProviderUtils.executeSQL(conn, sql, return_result=True)
return '{}_{}'.format(prefix, uid)
return f'{prefix}_{uid}'
1 change: 0 additions & 1 deletion tests/src/python/test_layer_dependencies.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsSnappingUtils (complement to C++-based tests)
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down
10 changes: 4 additions & 6 deletions tests/src/python/test_offline_editing_wfs.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Offline editing Tests.
Expand All @@ -20,7 +19,6 @@
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
from builtins import str

__author__ = 'Alessandro Pasotti'
__date__ = '05/15/2016'
Expand Down Expand Up @@ -121,15 +119,15 @@ def _getOnlineLayer(self, type_name, layer_name=None):
parms = {
'srsname': 'EPSG:4326',
'typename': type_name,
'url': 'http://127.0.0.1:%s/%s/?map=%s' % (self.port,
self.counter,
self.project_path),
'url': 'http://127.0.0.1:{}/{}/?map={}'.format(self.port,
self.counter,
self.project_path),
'version': 'auto',
'table': '',
# 'sql': '',
}
self.counter += 1
uri = ' '.join([("%s='%s'" % (k, v)) for k, v in list(parms.items())])
uri = ' '.join([(f"{k}='{v}'") for k, v in list(parms.items())])
wfs_layer = QgsVectorLayer(uri, layer_name, 'WFS')
wfs_layer.setParent(QgsApplication.authManager())
assert wfs_layer.isValid()
Expand Down
5 changes: 2 additions & 3 deletions tests/src/python/test_plugindependencies.py
@@ -1,4 +1,3 @@
# coding=utf-8
"""QGIS Plugin dependencies test
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -190,10 +189,10 @@ def test_find_dependencies(self):


def pluginSuite():
return unittest.makeSuite(PluginDependenciesTest, 'test')
return unittest.defaultTestLoader.loadTestsFromTestCase(PluginDependenciesTest, 'test')


if __name__ == "__main__":
suite = unittest.makeSuite(PluginDependenciesTest)
suite = unittest.defaultTestLoader.loadTestsFromTestCase(PluginDependenciesTest)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
1 change: 0 additions & 1 deletion tests/src/python/test_processing_alg_decorator.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for the @alg processing algorithm.
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down
1 change: 0 additions & 1 deletion tests/src/python/test_processing_importintopostgis.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for Processing Export to Postgis algorithm.
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down
5 changes: 2 additions & 3 deletions tests/src/python/test_processing_packagelayers.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for Processing Package Layers algorithm.
.. note:: This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -231,9 +230,9 @@ def _test(parameters, expected_ids):

# Check export
for layer_name in list(expected_ids.keys()):
l = QgsVectorLayer(self.temp_export_path + '|layername={}'.format(layer_name), layer_name)
l = QgsVectorLayer(self.temp_export_path + f'|layername={layer_name}', layer_name)
self.assertTrue(l.isValid())
ids = set([l.id() for l in l.getFeatures()])
ids = {l.id() for l in l.getFeatures()}
self.assertEqual(ids, expected_ids[layer_name], layer_name + str(ids))

region = QgsProject.instance().mapLayersByName('region')[0]
Expand Down
2 changes: 0 additions & 2 deletions tests/src/python/test_project_storage_base.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests base for the database project storage.
.. note:: This program is free software; you can redistribute it and/or modify
Expand All @@ -7,7 +6,6 @@
(at your option) any later version.
"""
from builtins import next

__author__ = 'Julien Cabieces'
__date__ = '2022-04-19'
Expand Down

0 comments on commit e2cfb8d

Please sign in to comment.