From 9a2193cd98f79a3dcdb22ee9c606092d509fbbdc Mon Sep 17 00:00:00 2001 From: Tanveer singh Bhatia Date: Sat, 25 Jan 2025 18:00:35 -0700 Subject: [PATCH 1/9] Web connections proof of concept --- canflood/_pars/WebConnections.ini | 2 +- canflood/misc/webConnections.py | 29 +++++++++++++++++++++++++---- canflood/model/modcom.py | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/canflood/_pars/WebConnections.ini b/canflood/_pars/WebConnections.ini index c5458000..18bbdc9c 100644 --- a/canflood/_pars/WebConnections.ini +++ b/canflood/_pars/WebConnections.ini @@ -6,7 +6,7 @@ url=http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&versi [NPRI] group=connections-arcgisfeatureserver\ECCC_NationalPollutantReleaseInventory_NPRI -url=https://maps-cartes.ec.gc.ca/arcgis/rest/services/NPRI_INRP/NPRI_INRP/MapServer +url=https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer [NHSL] group=connections-arcgisfeatureserver\NRCan_NationalHumanSettlementLayer_NHSL diff --git a/canflood/misc/webConnections.py b/canflood/misc/webConnections.py index 9f4fffd1..4ee91de2 100644 --- a/canflood/misc/webConnections.py +++ b/canflood/misc/webConnections.py @@ -55,6 +55,9 @@ def launch(self): kwargs = {attn:getattr(self, attn) for attn in self.inherit_fieldNames} with WebConnect(**kwargs) as wrkr: newCons_d = wrkr.addAll() + wrkr.add_arcgis_rest_connection("NHSL", "https://maps-cartes.services.geo.ca/server_serveur/rest/services/NRCan/nhsl_en/MapServer") + wrkr.add_arcgis_rest_connection("NPRI", "https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer") + #======================================================================= # wrap @@ -143,7 +146,24 @@ def retrieve_fromFile(self, #pull parameters from CanFlood's file return newCons_d + def add_arcgis_rest_connection(self, name, url): + """Add an ArcGIS FeatureServer connection using QGIS API.""" + + settings = QgsSettings() + base_key = f"connections/arcgisfeatureserver/items/{name}" + + # Set required parameters + settings.setValue(f"{base_key}/url", url) + settings.setValue(f"{base_key}/username", "") # If authentication is required, update this + settings.setValue(f"{base_key}/password", "") + settings.setValue(f"{base_key}/authcfg", "") + settings.setValue(f"{base_key}/http-header", "@Variant(\0\0\0\b\0\0\0\0)") + + # Set the selected connection + settings.setValue("connections/arcgisfeatureserver/selected", name) + print(f"Added ArcGIS REST connection: {name}") + def addAll(self, #add all connections qini_fp = None, #users settings path newCons_d = None, #connections to load @@ -161,8 +181,9 @@ def addAll(self, #add all connections # initilize settings #======================================================================= assert os.path.exists(qini_fp), 'bad settings filepath: %s'%qini_fp - usets = QgsSettings(qini_fp, QSettings.IniFormat) - + #log.push('filepath'%(qini_fp)) + usets = QSettings(qini_fp, QSettings.IniFormat) + log.push("Here") #navigate to group1 """all connectins are in the qgis group""" usets.beginGroup('qgis') @@ -340,8 +361,8 @@ def checkSettings(self, wrkr = WebConnect( - newSettings_fp = r'C:\LS\03_TOOLS\CanFlood\_git\canflood\_pars\WebConnections1.ini', - qini_fp = r'C:\Users\cefect\AppData\Roaming\QGIS\QGIS3\profiles\dev\QGIS\QGIS3.ini') #setup worker + newSettings_fp = r'D:\CEFlood\CanFlood\canflood\_pars\WebConnections.ini', + qini_fp = r'C:\Users\bhati\AppData\Local\QGIS\QGIS3\profiles\dev\QGIS\QGIS3.ini') #setup worker wrkr.addAll() #add everything diff --git a/canflood/model/modcom.py b/canflood/model/modcom.py index 84213c09..281a81dc 100644 --- a/canflood/model/modcom.py +++ b/canflood/model/modcom.py @@ -2082,7 +2082,7 @@ def _par_hndl_chk(self, """ if logger is None: logger=self.logger - if absolute_fp= None: absolute_fp=self.aboslute_fp + if absolute_fp is None: absolute_fp=self.aboslute_fp log = logger.getChild('par_hndl_chk') #======================================================================= From 332bc3257ee23c3b9b80dac464abfc2a5d4f9af2 Mon Sep 17 00:00:00 2001 From: Tanveer singh Bhatia Date: Sun, 26 Jan 2025 17:03:17 -0700 Subject: [PATCH 2/9] Updated with relative paths instead of hardcoded path --- canflood/misc/webConnections.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/canflood/misc/webConnections.py b/canflood/misc/webConnections.py index 4ee91de2..7c6ef076 100644 --- a/canflood/misc/webConnections.py +++ b/canflood/misc/webConnections.py @@ -18,7 +18,7 @@ from qgis.core import QgsApplication, QgsSettings -from PyQt5.QtCore import QSettings +from PyQt5.QtCore import QSettings, QStandardPaths #============================================================================== # custom imports #============================================================================== @@ -57,6 +57,8 @@ def launch(self): newCons_d = wrkr.addAll() wrkr.add_arcgis_rest_connection("NHSL", "https://maps-cartes.services.geo.ca/server_serveur/rest/services/NRCan/nhsl_en/MapServer") wrkr.add_arcgis_rest_connection("NPRI", "https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer") + wrkr.add_arcgis_rest_connection("GAR15", "http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&version=1.0.0&coverage=GAR2015:flood_hazard_1000_yrp&width=640&height=309&crs=EPSG:4326") + wrkr.add_arcgis_rest_connection("AutomaticallyExtractedBuildings", "https://maps.geogratis.gc.ca/wms/automatic_extraction_building_footprint_en?request=getcapabilities&service=wms&layers=automatic_extraction_building_footprint_en&version=1.3.0&legend_format=image/png&feature_info_type=text/html") #======================================================================= @@ -181,9 +183,7 @@ def addAll(self, #add all connections # initilize settings #======================================================================= assert os.path.exists(qini_fp), 'bad settings filepath: %s'%qini_fp - #log.push('filepath'%(qini_fp)) usets = QSettings(qini_fp, QSettings.IniFormat) - log.push("Here") #navigate to group1 """all connectins are in the qgis group""" usets.beginGroup('qgis') @@ -359,12 +359,16 @@ def checkSettings(self, if __name__ =="__main__": - + base_dir = os.path.dirname(os.path.dirname(__file__)) + newSettings_fp1=os.path.join(base_dir, r'_pars\WebConnections.ini') + settings_dir = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation) + "/QGIS/QGIS3/profiles/dev/QGIS/QGIS3.ini" + if not os.path.exists(settings_dir): + settings_dir = settings_dir.replace('Roaming', 'Local') + wrkr = WebConnect( - newSettings_fp = r'D:\CEFlood\CanFlood\canflood\_pars\WebConnections.ini', - qini_fp = r'C:\Users\bhati\AppData\Local\QGIS\QGIS3\profiles\dev\QGIS\QGIS3.ini') #setup worker - - + newSettings_fp=newSettings_fp1, + qini_fp = settings_dir + ) wrkr.addAll() #add everything \ No newline at end of file From aa280954b36aa7533332fdf5374c7d2742dab7be Mon Sep 17 00:00:00 2001 From: Tanveer singh Bhatia Date: Thu, 30 Jan 2025 18:43:51 -0700 Subject: [PATCH 3/9] Added arcgis, wms and wcs connections --- canflood/misc/webConnections.py | 95 ++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/canflood/misc/webConnections.py b/canflood/misc/webConnections.py index 7c6ef076..26f84dcc 100644 --- a/canflood/misc/webConnections.py +++ b/canflood/misc/webConnections.py @@ -43,6 +43,21 @@ class WebConnectAction(QMenuAction): icon_location = 'menu' def launch(self): + + base_dir = os.path.dirname(os.path.dirname(__file__)) + newSettings_fp1=os.path.join(base_dir, r'_pars\WebConnections.ini') + settings_dir = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation) + "/profiles/dev/QGIS/QGIS3.ini" + if not os.path.exists(settings_dir): + settings_dir = settings_dir.replace('Roaming', 'Local') + + wrkr = WebConnect( + newSettings_fp=newSettings_fp1, + qini_fp = settings_dir + ) + wrkr.addAll() + wrkr.read_connections(newSettings_fp1) + + #======================================================================= # defai;ts #======================================================================= @@ -54,13 +69,7 @@ def launch(self): #======================================================================= kwargs = {attn:getattr(self, attn) for attn in self.inherit_fieldNames} with WebConnect(**kwargs) as wrkr: - newCons_d = wrkr.addAll() - wrkr.add_arcgis_rest_connection("NHSL", "https://maps-cartes.services.geo.ca/server_serveur/rest/services/NRCan/nhsl_en/MapServer") - wrkr.add_arcgis_rest_connection("NPRI", "https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer") - wrkr.add_arcgis_rest_connection("GAR15", "http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&version=1.0.0&coverage=GAR2015:flood_hazard_1000_yrp&width=640&height=309&crs=EPSG:4326") - wrkr.add_arcgis_rest_connection("AutomaticallyExtractedBuildings", "https://maps.geogratis.gc.ca/wms/automatic_extraction_building_footprint_en?request=getcapabilities&service=wms&layers=automatic_extraction_building_footprint_en&version=1.3.0&legend_format=image/png&feature_info_type=text/html") - - + newCons_d = wrkr.addAll() #======================================================================= # wrap #======================================================================= @@ -148,23 +157,58 @@ def retrieve_fromFile(self, #pull parameters from CanFlood's file return newCons_d + def read_connections(self, newSettings_fp = None): + config = ConfigParser() + config.read(newSettings_fp) + + for section in config.sections(): + name = section + url = config.get(section, 'url', fallback="") + group = config.get(section, 'group', fallback="") + + if "connections-arcgisfeatureserver" in group: + self.add_arcgis_rest_connection(name, url) + elif "connections-wcs" in group: + self.add_wcs_connection(name, url) + elif "connections-wms" in group: + self.add_wms_connection(name, url) + else: + print(f"Unknown connection type for {name}: {url}") + def add_arcgis_rest_connection(self, name, url): - """Add an ArcGIS FeatureServer connection using QGIS API.""" - settings = QgsSettings() base_key = f"connections/arcgisfeatureserver/items/{name}" - - # Set required parameters settings.setValue(f"{base_key}/url", url) - settings.setValue(f"{base_key}/username", "") # If authentication is required, update this + settings.setValue(f"{base_key}/username", "") settings.setValue(f"{base_key}/password", "") settings.setValue(f"{base_key}/authcfg", "") settings.setValue(f"{base_key}/http-header", "@Variant(\0\0\0\b\0\0\0\0)") - - # Set the selected connection settings.setValue("connections/arcgisfeatureserver/selected", name) + + def add_wcs_connection(self, name, url): + """Adds a Web Coverage Service (WCS) connection to QGIS.""" + settings = QgsSettings() + base_key = f"connections/ows/items/wcs/connections/items/{name}" + + settings.setValue(f"{base_key}/url", url) + settings.setValue(f"{base_key}/username", "") + settings.setValue(f"{base_key}/password", "") + settings.setValue(f"{base_key}/authcfg", "") + + settings.sync() + - print(f"Added ArcGIS REST connection: {name}") + def add_wms_connection(self, name, url): + """Adds a WMS connection to QGIS settings.""" + settings = QgsSettings() + base_key = f"connections/ows/items/wms/connections/items/{name}" + + settings.setValue(f"{base_key}/url", url) + settings.setValue(f"{base_key}/username", "") + settings.setValue(f"{base_key}/password", "") + settings.setValue(f"{base_key}/authcfg", "") + + settings.sync() def addAll(self, #add all connections qini_fp = None, #users settings path @@ -350,25 +394,4 @@ def checkSettings(self, return result, msg - - - - - - - -if __name__ =="__main__": - - base_dir = os.path.dirname(os.path.dirname(__file__)) - newSettings_fp1=os.path.join(base_dir, r'_pars\WebConnections.ini') - settings_dir = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation) + "/QGIS/QGIS3/profiles/dev/QGIS/QGIS3.ini" - if not os.path.exists(settings_dir): - settings_dir = settings_dir.replace('Roaming', 'Local') - - wrkr = WebConnect( - newSettings_fp=newSettings_fp1, - qini_fp = settings_dir - ) - wrkr.addAll() #add everything - \ No newline at end of file From 51ebab660d1326af50a5d3c3f9528c0c7d66b1ae Mon Sep 17 00:00:00 2001 From: Tanveer singh Bhatia Date: Thu, 30 Jan 2025 18:49:11 -0700 Subject: [PATCH 4/9] replaced print statement with proper error --- canflood/misc/webConnections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/canflood/misc/webConnections.py b/canflood/misc/webConnections.py index 26f84dcc..d54d9e30 100644 --- a/canflood/misc/webConnections.py +++ b/canflood/misc/webConnections.py @@ -173,7 +173,7 @@ def read_connections(self, newSettings_fp = None): elif "connections-wms" in group: self.add_wms_connection(name, url) else: - print(f"Unknown connection type for {name}: {url}") + raise ValueError(f"Unknown connection type for '{name}': {url}. Group '{group}' is not recognized.") def add_arcgis_rest_connection(self, name, url): settings = QgsSettings() From af83bad1915e4197e76496e5f42638b6c58ee93b Mon Sep 17 00:00:00 2001 From: Tanveer singh Bhatia Date: Fri, 31 Jan 2025 23:16:39 -0700 Subject: [PATCH 5/9] Added test for Web connections --- tests2/Misct/test_webconnections.py | 88 +++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests2/Misct/test_webconnections.py diff --git a/tests2/Misct/test_webconnections.py b/tests2/Misct/test_webconnections.py new file mode 100644 index 00000000..a5fb0b6f --- /dev/null +++ b/tests2/Misct/test_webconnections.py @@ -0,0 +1,88 @@ +import pytest +import configparser +from misc.webConnections import WebConnect + +# Test cases for parameterization +test_cases = { + "valid_wms_connection": { + "config_content": { + "AutomaticallyExtractedBuildings": { + "group": "connections-wms\\NRCan_AutomaticallyExtractedBuildings", # Matches the original format + "url": "https://maps.geogratis.gc.ca/wms/automatic_extraction_building_footprint_en?request=getcapabilities&service=wms&layers=automatic_extraction_building_footprint_en&version=1.3.0&legend_format=image/png&feature_info_type=text/html" + } + }, + "expected_result": "AutomaticallyExtractedBuildings" + }, + "valid_wcs_connection": { + "config_content": { + "GAR15": { + "group": "connections-wcs\\UNISDR_GAR15_GlobalRiskAssessment", # Matches the original format + "url": "http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&version=1.0.0&coverage=GAR2015:flood_hazard_1000_yrp&width=640&height=309&crs=EPSG:4326" + } + }, + "expected_result": "GAR15" + }, + "valid_arcgis_connection": { + "config_content": { + "NPRI": { + "group": "connections-arcgisfeatureserver\\ECCC_NationalPollutantReleaseInventory_NPRI", # Matches the original format + "url": "https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer" + } + }, + "expected_result": "NPRI" + }, + "invalid_connection_type": { + "config_content": { + "invalid_connection": { + "group": "connections-invalid", + "url": "http://example.com/invalid" + } + }, + "expected_error": ValueError + } +} + +@pytest.fixture(scope="function") +def web_connect(tmpdir): + """Fixture to provide an instance of WebConnect with mocked QGIS settings.""" + # Create a temporary QGIS settings file (qini_fp) + qini_fp = tmpdir.join("QGIS3.ini") + qini_fp.write("[qgis]\n") + + web_connect = WebConnect(qini_fp=str(qini_fp)) + yield web_connect + +@pytest.fixture(scope="function") +def config_file(tmpdir, request): + """Fixture to create a temporary config file with the specified content.""" + config = configparser.ConfigParser() + config.read_dict(test_cases[request.param]["config_content"]) + config_file_path = tmpdir.join("test_config.ini") + with open(config_file_path, 'w') as configfile: + config.write(configfile) + yield config_file_path + +@pytest.mark.parametrize( + "config_file, expected_result, expected_error", + [ + ("valid_wms_connection", "AutomaticallyExtractedBuildings", None), + ("valid_wcs_connection", "GAR15", None), + ("valid_arcgis_connection", "NPRI", None), + ("invalid_connection_type", None, ValueError) + ], + indirect=["config_file"] +) +def test_read_connections(web_connect, config_file, expected_result, expected_error): + """Test the read_connections function with different configurations.""" + print("CONFIG FILE:", config_file) + if expected_error: + print("EXPECTED ERROR:", expected_error) + with pytest.raises(expected_error): + web_connect.read_connections(config_file) + else: + print("EXPECTED RESULT:", expected_result) + web_connect.read_connections(config_file) + # Debugging: Print the contents of newCons_d + print("Contents of newCons_d:", web_connect.newCons_d) + # Check that the connection was added correctly + assert expected_result in web_connect.newCons_d \ No newline at end of file From 2a7ee71046679bde9675a449b29c4a6616e11f0c Mon Sep 17 00:00:00 2001 From: Tanveer singh Bhatia Date: Sat, 1 Feb 2025 18:10:04 -0700 Subject: [PATCH 6/9] Removed unused ini file path --- canflood/misc/webConnections.py | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/canflood/misc/webConnections.py b/canflood/misc/webConnections.py index d54d9e30..36cb7b24 100644 --- a/canflood/misc/webConnections.py +++ b/canflood/misc/webConnections.py @@ -13,12 +13,12 @@ #python standards import os, logging, copy -from configparser import ConfigParser, RawConfigParser +from configparser import ConfigParser import numpy as np from qgis.core import QgsApplication, QgsSettings -from PyQt5.QtCore import QSettings, QStandardPaths +from PyQt5.QtCore import QSettings #============================================================================== # custom imports #============================================================================== @@ -46,20 +46,9 @@ def launch(self): base_dir = os.path.dirname(os.path.dirname(__file__)) newSettings_fp1=os.path.join(base_dir, r'_pars\WebConnections.ini') - settings_dir = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation) + "/profiles/dev/QGIS/QGIS3.ini" - if not os.path.exists(settings_dir): - settings_dir = settings_dir.replace('Roaming', 'Local') - - wrkr = WebConnect( - newSettings_fp=newSettings_fp1, - qini_fp = settings_dir - ) - wrkr.addAll() - wrkr.read_connections(newSettings_fp1) - #======================================================================= - # defai;ts + # details #======================================================================= log = self.logger.getChild('l') @@ -69,7 +58,8 @@ def launch(self): #======================================================================= kwargs = {attn:getattr(self, attn) for attn in self.inherit_fieldNames} with WebConnect(**kwargs) as wrkr: - newCons_d = wrkr.addAll() + newCons_d = wrkr.addAll() + wrkr.read_connections(newSettings_fp1) #======================================================================= # wrap #======================================================================= @@ -94,11 +84,7 @@ def __init__(self, **kwargs): - - - - - super().__init__(**kwargs) #initilzie teh baseclass + super().__init__(**kwargs) #initilzie the baseclass #setup @@ -220,7 +206,6 @@ def addAll(self, #add all connections log = self.logger.getChild('addAll') if newCons_d is None: newCons_d = self.newCons_d if qini_fp is None: qini_fp = self.qini_fp - log.debug('addAll on %i'%len(newCons_d)) #======================================================================= From f846d194fa5e55fe5cde19b6f35398226094e9a5 Mon Sep 17 00:00:00 2001 From: Tanveer singh Bhatia Date: Tue, 4 Feb 2025 00:05:47 -0700 Subject: [PATCH 7/9] Updated test to ensure ini files updated to pass test --- canflood/misc/webConnections.py | 9 +- tests2/Misct/test_webconnections.py | 126 ++++++++++++++-------------- 2 files changed, 68 insertions(+), 67 deletions(-) diff --git a/canflood/misc/webConnections.py b/canflood/misc/webConnections.py index 36cb7b24..6145bec7 100644 --- a/canflood/misc/webConnections.py +++ b/canflood/misc/webConnections.py @@ -146,7 +146,6 @@ def retrieve_fromFile(self, #pull parameters from CanFlood's file def read_connections(self, newSettings_fp = None): config = ConfigParser() config.read(newSettings_fp) - for section in config.sections(): name = section url = config.get(section, 'url', fallback="") @@ -162,7 +161,7 @@ def read_connections(self, newSettings_fp = None): raise ValueError(f"Unknown connection type for '{name}': {url}. Group '{group}' is not recognized.") def add_arcgis_rest_connection(self, name, url): - settings = QgsSettings() + settings = QgsSettings(self.qini_fp, QSettings.IniFormat) base_key = f"connections/arcgisfeatureserver/items/{name}" settings.setValue(f"{base_key}/url", url) settings.setValue(f"{base_key}/username", "") @@ -171,9 +170,11 @@ def add_arcgis_rest_connection(self, name, url): settings.setValue(f"{base_key}/http-header", "@Variant(\0\0\0\b\0\0\0\0)") settings.setValue("connections/arcgisfeatureserver/selected", name) + settings.sync() + def add_wcs_connection(self, name, url): """Adds a Web Coverage Service (WCS) connection to QGIS.""" - settings = QgsSettings() + settings = QgsSettings(self.qini_fp, QSettings.IniFormat) base_key = f"connections/ows/items/wcs/connections/items/{name}" settings.setValue(f"{base_key}/url", url) @@ -186,7 +187,7 @@ def add_wcs_connection(self, name, url): def add_wms_connection(self, name, url): """Adds a WMS connection to QGIS settings.""" - settings = QgsSettings() + settings = QgsSettings(self.qini_fp, QSettings.IniFormat) base_key = f"connections/ows/items/wms/connections/items/{name}" settings.setValue(f"{base_key}/url", url) diff --git a/tests2/Misct/test_webconnections.py b/tests2/Misct/test_webconnections.py index a5fb0b6f..18bab54b 100644 --- a/tests2/Misct/test_webconnections.py +++ b/tests2/Misct/test_webconnections.py @@ -1,88 +1,88 @@ import pytest import configparser +import os from misc.webConnections import WebConnect - -# Test cases for parameterization -test_cases = { - "valid_wms_connection": { - "config_content": { - "AutomaticallyExtractedBuildings": { - "group": "connections-wms\\NRCan_AutomaticallyExtractedBuildings", # Matches the original format - "url": "https://maps.geogratis.gc.ca/wms/automatic_extraction_building_footprint_en?request=getcapabilities&service=wms&layers=automatic_extraction_building_footprint_en&version=1.3.0&legend_format=image/png&feature_info_type=text/html" - } - }, - "expected_result": "AutomaticallyExtractedBuildings" - }, - "valid_wcs_connection": { - "config_content": { - "GAR15": { - "group": "connections-wcs\\UNISDR_GAR15_GlobalRiskAssessment", # Matches the original format - "url": "http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&version=1.0.0&coverage=GAR2015:flood_hazard_1000_yrp&width=640&height=309&crs=EPSG:4326" - } - }, - "expected_result": "GAR15" - }, - "valid_arcgis_connection": { - "config_content": { - "NPRI": { - "group": "connections-arcgisfeatureserver\\ECCC_NationalPollutantReleaseInventory_NPRI", # Matches the original format - "url": "https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer" - } - }, - "expected_result": "NPRI" - }, - "invalid_connection_type": { - "config_content": { - "invalid_connection": { - "group": "connections-invalid", - "url": "http://example.com/invalid" - } - }, - "expected_error": ValueError - } -} +from PyQt5.QtCore import QSettings @pytest.fixture(scope="function") def web_connect(tmpdir): - """Fixture to provide an instance of WebConnect with mocked QGIS settings.""" - # Create a temporary QGIS settings file (qini_fp) + """Fixture to provide an instance of WebConnect with a temporary QGIS3.ini file.""" qini_fp = tmpdir.join("QGIS3.ini") - qini_fp.write("[qgis]\n") + qini_fp.write("[connections]\n") web_connect = WebConnect(qini_fp=str(qini_fp)) + + settings = QSettings(str(qini_fp), QSettings.IniFormat) + settings.clear() + settings.sync() + yield web_connect @pytest.fixture(scope="function") def config_file(tmpdir, request): """Fixture to create a temporary config file with the specified content.""" + test_cases = { + "valid_wms_connection": { + "config_content": { + "AutomaticallyExtractedBuildings": { + "group": "connections-wms\\NRCan_AutomaticallyExtractedBuildings", + "url": "https://maps.geogratis.gc.ca/wms/automatic_extraction_building_footprint_en?request=getcapabilities&service=wms&layers=automatic_extraction_building_footprint_en&version=1.3.0&legend_format=image/png&feature_info_type=text/html" + } + }, + "expected_key": "connections/ows/items/wms/connections/items/AutomaticallyExtractedBuildings/url" + }, + "valid_wcs_connection": { + "config_content": { + "GAR15": { + "group": "connections-wcs\\UNISDR_GAR15_GlobalRiskAssessment", + "url": "http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&version=1.0.0&coverage=GAR2015:flood_hazard_1000_yrp&width=640&height=309&crs=EPSG:4326" + } + }, + "expected_key": "connections/ows/items/wcs/connections/items/GAR15/url" + }, + "valid_arcgis_connection": { + "config_content": { + "NPRI": { + "group": "connections-arcgisfeatureserver\\ECCC_NationalPollutantReleaseInventory_NPRI", + "url": "https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer" + } + }, + "expected_key": "connections/arcgisfeatureserver/items/NPRI/url" + } + } + config = configparser.ConfigParser() config.read_dict(test_cases[request.param]["config_content"]) config_file_path = tmpdir.join("test_config.ini") with open(config_file_path, 'w') as configfile: config.write(configfile) - yield config_file_path + + return str(config_file_path), test_cases[request.param]["expected_key"] @pytest.mark.parametrize( - "config_file, expected_result, expected_error", + "config_file, expected_key", [ - ("valid_wms_connection", "AutomaticallyExtractedBuildings", None), - ("valid_wcs_connection", "GAR15", None), - ("valid_arcgis_connection", "NPRI", None), - ("invalid_connection_type", None, ValueError) + ("valid_wms_connection", "connections/ows/items/wms/connections/items/AutomaticallyExtractedBuildings/url"), + ("valid_wcs_connection", "connections/ows/items/wcs/connections/items/GAR15/url"), + ("valid_arcgis_connection", "connections/arcgisfeatureserver/items/NPRI/url") ], indirect=["config_file"] ) -def test_read_connections(web_connect, config_file, expected_result, expected_error): - """Test the read_connections function with different configurations.""" - print("CONFIG FILE:", config_file) - if expected_error: - print("EXPECTED ERROR:", expected_error) - with pytest.raises(expected_error): - web_connect.read_connections(config_file) - else: - print("EXPECTED RESULT:", expected_result) - web_connect.read_connections(config_file) - # Debugging: Print the contents of newCons_d - print("Contents of newCons_d:", web_connect.newCons_d) - # Check that the connection was added correctly - assert expected_result in web_connect.newCons_d \ No newline at end of file +def test_read_connections(web_connect, config_file, expected_key): + """Test the read_connections function and check expected QGIS3.ini structure.""" + config_path, expected_key = config_file + + # Run the connection update + web_connect.read_connections(config_path) + + # Force QSettings to write changes immediately + settings = QSettings(web_connect.qini_fp, QSettings.IniFormat) + settings.sync() # Ensure settings are written to disk + + qini_fp = web_connect.qini_fp + with open(qini_fp, "r") as f: + ini_content = f.read() + + all_keys = settings.allKeys() + + assert expected_key in all_keys, f"Expected key '{expected_key}' was not found in the updated QGIS3.ini file" From b6b45d29d3200e342460190479372a408fea2c6e Mon Sep 17 00:00:00 2001 From: Seth Bryant Date: Tue, 4 Feb 2025 16:29:49 -0700 Subject: [PATCH 8/9] Update test_webconnections.py rename --- tests2/{Misct => misct}/test_webconnections.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests2/{Misct => misct}/test_webconnections.py (100%) diff --git a/tests2/Misct/test_webconnections.py b/tests2/misct/test_webconnections.py similarity index 100% rename from tests2/Misct/test_webconnections.py rename to tests2/misct/test_webconnections.py From a9114fc60489ce73ee519037c168a735db340dcc Mon Sep 17 00:00:00 2001 From: Tanveer singh Bhatia Date: Tue, 4 Feb 2025 23:41:11 -0700 Subject: [PATCH 9/9] Refactored test code --- canflood/misc/webConnections.py | 2 +- tests2/Misct/test_webconnections.py | 88 ----------------------------- tests2/misct/test_webconnections.py | 61 ++++++++++++++++++++ 3 files changed, 62 insertions(+), 89 deletions(-) delete mode 100644 tests2/Misct/test_webconnections.py create mode 100644 tests2/misct/test_webconnections.py diff --git a/canflood/misc/webConnections.py b/canflood/misc/webConnections.py index 6145bec7..d36a36d1 100644 --- a/canflood/misc/webConnections.py +++ b/canflood/misc/webConnections.py @@ -48,7 +48,7 @@ def launch(self): newSettings_fp1=os.path.join(base_dir, r'_pars\WebConnections.ini') #======================================================================= - # details + # defaults #======================================================================= log = self.logger.getChild('l') diff --git a/tests2/Misct/test_webconnections.py b/tests2/Misct/test_webconnections.py deleted file mode 100644 index 18bab54b..00000000 --- a/tests2/Misct/test_webconnections.py +++ /dev/null @@ -1,88 +0,0 @@ -import pytest -import configparser -import os -from misc.webConnections import WebConnect -from PyQt5.QtCore import QSettings - -@pytest.fixture(scope="function") -def web_connect(tmpdir): - """Fixture to provide an instance of WebConnect with a temporary QGIS3.ini file.""" - qini_fp = tmpdir.join("QGIS3.ini") - qini_fp.write("[connections]\n") - - web_connect = WebConnect(qini_fp=str(qini_fp)) - - settings = QSettings(str(qini_fp), QSettings.IniFormat) - settings.clear() - settings.sync() - - yield web_connect - -@pytest.fixture(scope="function") -def config_file(tmpdir, request): - """Fixture to create a temporary config file with the specified content.""" - test_cases = { - "valid_wms_connection": { - "config_content": { - "AutomaticallyExtractedBuildings": { - "group": "connections-wms\\NRCan_AutomaticallyExtractedBuildings", - "url": "https://maps.geogratis.gc.ca/wms/automatic_extraction_building_footprint_en?request=getcapabilities&service=wms&layers=automatic_extraction_building_footprint_en&version=1.3.0&legend_format=image/png&feature_info_type=text/html" - } - }, - "expected_key": "connections/ows/items/wms/connections/items/AutomaticallyExtractedBuildings/url" - }, - "valid_wcs_connection": { - "config_content": { - "GAR15": { - "group": "connections-wcs\\UNISDR_GAR15_GlobalRiskAssessment", - "url": "http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&version=1.0.0&coverage=GAR2015:flood_hazard_1000_yrp&width=640&height=309&crs=EPSG:4326" - } - }, - "expected_key": "connections/ows/items/wcs/connections/items/GAR15/url" - }, - "valid_arcgis_connection": { - "config_content": { - "NPRI": { - "group": "connections-arcgisfeatureserver\\ECCC_NationalPollutantReleaseInventory_NPRI", - "url": "https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer" - } - }, - "expected_key": "connections/arcgisfeatureserver/items/NPRI/url" - } - } - - config = configparser.ConfigParser() - config.read_dict(test_cases[request.param]["config_content"]) - config_file_path = tmpdir.join("test_config.ini") - with open(config_file_path, 'w') as configfile: - config.write(configfile) - - return str(config_file_path), test_cases[request.param]["expected_key"] - -@pytest.mark.parametrize( - "config_file, expected_key", - [ - ("valid_wms_connection", "connections/ows/items/wms/connections/items/AutomaticallyExtractedBuildings/url"), - ("valid_wcs_connection", "connections/ows/items/wcs/connections/items/GAR15/url"), - ("valid_arcgis_connection", "connections/arcgisfeatureserver/items/NPRI/url") - ], - indirect=["config_file"] -) -def test_read_connections(web_connect, config_file, expected_key): - """Test the read_connections function and check expected QGIS3.ini structure.""" - config_path, expected_key = config_file - - # Run the connection update - web_connect.read_connections(config_path) - - # Force QSettings to write changes immediately - settings = QSettings(web_connect.qini_fp, QSettings.IniFormat) - settings.sync() # Ensure settings are written to disk - - qini_fp = web_connect.qini_fp - with open(qini_fp, "r") as f: - ini_content = f.read() - - all_keys = settings.allKeys() - - assert expected_key in all_keys, f"Expected key '{expected_key}' was not found in the updated QGIS3.ini file" diff --git a/tests2/misct/test_webconnections.py b/tests2/misct/test_webconnections.py new file mode 100644 index 00000000..2af39fab --- /dev/null +++ b/tests2/misct/test_webconnections.py @@ -0,0 +1,61 @@ +import pytest +import configparser +from misc.webConnections import WebConnect +from PyQt5.QtCore import QSettings + +@pytest.fixture(scope="function") +def web_connect(tmpdir): + """Fixture to provide an instance of WebConnect with a temporary QGIS3.ini file.""" + qini_fp = tmpdir.join("QGIS3.ini") + qini_fp.write("[connections]\n") + + web_connect = WebConnect(qini_fp=str(qini_fp)) + + settings = QSettings(str(qini_fp), QSettings.IniFormat) + settings.clear() + settings.sync() + + yield web_connect + +@pytest.fixture(scope="function") +def create_config(tmpdir, config_content): + """Fixture to create a temporary config file with the specified content.""" + config = configparser.ConfigParser() + config.read_dict(config_content) + config_file_path = tmpdir.join("test_config.ini") + + with open(config_file_path, 'w') as configfile: + config.write(configfile) + + return str(config_file_path) + +@pytest.mark.parametrize( + "config_content, expected_key", + [ + ({"AutomaticallyExtractedBuildings": { + "group": "connections-wms\\NRCan_AutomaticallyExtractedBuildings", + "url": "https://maps.geogratis.gc.ca/wms/automatic_extraction_building_footprint_en?request=getcapabilities&service=wms&layers=automatic_extraction_building_footprint_en&version=1.3.0&legend_format=image/png&feature_info_type=text/html" + }}, "connections/ows/items/wms/connections/items/AutomaticallyExtractedBuildings/url"), + + ({"GAR15": { + "group": "connections-wcs\\UNISDR_GAR15_GlobalRiskAssessment", + "url": "http://preview.grid.unep.ch/geoserver/wcs?bbox=-180,-89,180,84&styles=&version=1.0.0&coverage=GAR2015:flood_hazard_1000_yrp&width=640&height=309&crs=EPSG:4326" + }}, "connections/ows/items/wcs/connections/items/GAR15/url"), + + ({"NPRI": { + "group": "connections-arcgisfeatureserver\\ECCC_NationalPollutantReleaseInventory_NPRI", + "url": "https://maps-cartes.ec.gc.ca/arcgis/rest/services/STB_DGST/NPRI/MapServer" + }}, "connections/arcgisfeatureserver/items/NPRI/url") + ] +) +def test_read_connections(web_connect, create_config, config_content, expected_key): + """Test the read_connections function and check expected QGIS3.ini structure.""" + config_path = create_config + + + web_connect.read_connections(config_path) + + + settings = QSettings(web_connect.qini_fp, QSettings.IniFormat) + settings.sync() + assert expected_key in settings.allKeys(), f"Expected key '{expected_key}' was not found in the updated QGIS3.ini file"