Skip to content

Commit

Permalink
fix(profiles): profiles listing was returning the unfiltered list (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts authored Apr 25, 2024
2 parents 06c0a7c + cc8ca17 commit 5309317
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
10 changes: 6 additions & 4 deletions qgis_deployment_toolbelt/jobs/generic_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def filter_profiles_folder(
"""Parse a folder structure to filter on QGIS profiles folders.
Returns:
tuple[QdtProfile] | None: tuple of profiles objects or Non if no profile
folder found
tuple[QdtProfile] | None: tuple of profiles objects matching criteria or
None if no profile folder found
"""
# first, try to get folders containing a profile.json
li_qgis_qdt_profiles: list[QdtProfile] = [
Expand All @@ -135,6 +135,7 @@ def filter_profiles_folder(
profiles_matched, profiles_unmatched = self.filter_profiles_on_rules(
tup_qdt_profiles=tuple(li_qgis_qdt_profiles)
)

if not len(profiles_matched):
logger.warning(
f"None of the {len(li_qgis_qdt_profiles)} profiles meet the deployment "
Expand All @@ -145,10 +146,11 @@ def filter_profiles_folder(
if len(profiles_unmatched):
logger.info(
f"{len(profiles_unmatched)}/{len(li_qgis_qdt_profiles)} profiles "
f"do not meet the conditions for deployment."
"do not meet the conditions for deployment: "
f"{', '.join([p.name for p in profiles_unmatched])}"
)

return tuple(li_qgis_qdt_profiles)
return tuple(profiles_matched)

@lru_cache(maxsize=1024)
def filter_profiles_on_rules(
Expand Down
43 changes: 43 additions & 0 deletions tests/fixtures/profiles/good_profile_rules_never_deployed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"$schema": "https://raw.githubusercontent.com/Guts/qgis-deployment-cli/main/docs/schemas/profile/qgis_profile.json",
"name": "QDT excluded by rules",
"folder_name": "qdt_never_deployed",
"description": "QGIS profile made to demonstrate how rules engine can exclude a profile from deployment.",
"author": "Julien Moura",
"email": "[email protected]",
"qgisMinimumVersion": "3.34",
"qgisMaximumVersion": "3.99",
"version": "1.3.0",
"plugins": [
{
"name": "QGIS Resource Sharing ",
"folder_name": "qgis_resource_sharing",
"location": "remote",
"official_repository": true,
"plugin_id": 2733,
"version": "1.0.0"
},
{
"name": "QTribu",
"folder_name": "qtribu",
"official_repository": true,
"plugin_id": 2733,
"version": "0.16.0"
}
],
"rules": [
{
"name": "To be not deployed",
"description": "Rules defining that the profile is deployed only during the 1985 year.",
"conditions": {
"all": [
{
"path": "$.date.current_year",
"value": 1985,
"operator": "equal"
}
]
}
}
]
}
34 changes: 31 additions & 3 deletions tests/test_job_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
# for whole test
python -m unittest tests.test_job_generic
# for specific
python -m unittest tests.test_job_generic.TestJobGeneric.test_validate_options
python -m unittest tests.test_job_generic.TestJobGeneric.test_listing_profiles_folder
"""

# #############################################################################
# ########## Libraries #############
# ##################################

# Standard library
import tempfile
import unittest
from pathlib import Path

# package
from qgis_deployment_toolbelt.exceptions import (
JobOptionBadName,
JobOptionBadValue,
JobOptionBadValueType,
)

# package
from qgis_deployment_toolbelt.jobs.generic_job import GenericJob

# #############################################################################
Expand Down Expand Up @@ -64,6 +65,33 @@ def setUpClass(cls):
}

# -- TESTS ---------------------------------------------------------
def test_listing_profiles_folder(self):
"""Test profiles listing."""
fixtures_profiles_folder = Path("tests/fixtures/profiles")
with tempfile.TemporaryDirectory(
prefix="QDT_test_profiles_listing_",
ignore_cleanup_errors=True,
) as tmpdirname:
tmp_folder_path = Path(tmpdirname).joinpath("profiles")
# simulate a QGIS profiles folder structure in the temp folder
for p in fixtures_profiles_folder.glob("good_profile_*.json"):
dest_file = tmp_folder_path.joinpath(f"test_{p.stem}/profile.json")
dest_file.parent.mkdir(parents=True, exist_ok=True)
dest_file.write_text(p.read_text(encoding="UTF-8"), encoding="UTF-8")

filtered_profiles = self.generic_job.filter_profiles_folder(
start_parent_folder=tmp_folder_path
)

self.assertIsInstance(filtered_profiles, tuple)
self.assertGreater(len(filtered_profiles), 0)

# length must be -1 because of a profile which is excluded by a rule
self.assertEqual(
len(filtered_profiles),
len(list(tmp_folder_path.glob("**/profile.json"))) - 1,
)

def test_validate_options_bad_input_type(self):
"""Test validate_options method"""

Expand Down

0 comments on commit 5309317

Please sign in to comment.