Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

101 webconnections not being added #115

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
9 changes: 5 additions & 4 deletions canflood/misc/webConnections.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ def retrieve_fromFile(self, #pull parameters from CanFlood's file
def read_connections(self, newSettings_fp = None):
cefect marked this conversation as resolved.
Show resolved Hide resolved
config = ConfigParser()
config.read(newSettings_fp)

for section in config.sections():
name = section
url = config.get(section, 'url', fallback="")
Expand All @@ -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):
cefect marked this conversation as resolved.
Show resolved Hide resolved
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", "")
Expand All @@ -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):
cefect marked this conversation as resolved.
Show resolved Hide resolved
"""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)
Expand All @@ -186,7 +187,7 @@ def add_wcs_connection(self, name, url):

def add_wms_connection(self, name, url):
cefect marked this conversation as resolved.
Show resolved Hide resolved
"""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)
Expand Down
126 changes: 63 additions & 63 deletions tests2/Misct/test_webconnections.py
ceftanveer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
import pytest
import configparser
import os
from misc.webConnections import WebConnect

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cefect Updated test code to ensure, mock user ini file is update and then only test passes.

# 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
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use explicit parameters. i.e., I expect to see 'config_path' in the pytest parameters if this is the parameter you are using. i.e., don't use a nested dictionary like this.

Also, what is going on with 'expected_key' here? It looks like you define it twice?

Also, why not just use the uri info in .\canflood\_pars\WebConnections.ini? That would save you having to write out all these url strings AND provide a test on the .ini file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored the code and used explicit parameters. and removed the redundancy of expected_key.

@cefect just tried to keep the test separate from the script. Do you suggest using already present WebConnections.ini ?


# 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()
ceftanveer marked this conversation as resolved.
Show resolved Hide resolved

all_keys = settings.allKeys()

assert expected_key in all_keys, f"Expected key '{expected_key}' was not found in the updated QGIS3.ini file"