Skip to content

Commit

Permalink
fix: use self.search_layer_ids again
Browse files Browse the repository at this point in the history
  • Loading branch information
jolaine committed Nov 2, 2023
1 parent 886611e commit 3244d5f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix defining layers to search from for set active layer tool

## [3.9.9] - 2023-11-01

- Find most logical closest feature from nested features with set active layer tool
Expand Down
5 changes: 3 additions & 2 deletions pickLayer/core/set_active_layer_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ def set_active_layer_using_closest_feature(
def _get_identify_results(
self, location: QgsPointXY, search_layer_ids: Optional[list[str]] = None
) -> list[QgsMapToolIdentify.IdentifyResult]:
layer_ids = search_layer_ids or self.search_layer_ids or []
layers = []
if search_layer_ids:
for layer_id in search_layer_ids:
if layer_ids:
for layer_id in layer_ids:
layer = QgsProject.instance().mapLayer(layer_id)
if isinstance(layer, QgsVectorLayer):
layers.append(layer)
Expand Down
59 changes: 59 additions & 0 deletions test/unit/test_set_active_layer_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,65 @@ def test_set_active_layer_using_closest_feature(
assert len(identify_results) == expected_num_results


@pytest.mark.parametrize(
argnames=("search_layers", "expected_num_results", "search_layer_count"),
argvalues=[
([0], 2, 1),
([0, 1], 2, 1),
([0, 1], 4, 2),
([0, 1, 2], 2, 1),
([0, 1, 2], 4, 2),
([0, 1, 2], 6, 3),
],
ids=[
"2-features-from-one-layer-found",
"2-features-from-two-layers-found",
"4-features-from-two-layers-found",
"2-features-from-three-layers-found",
"4-features-from-three-layers-found",
"6-features-from-three-layers-found",
],
)
def test_set_active_layer_using_closest_feature_with_search_layer_ids(
map_tool,
test_layers,
search_layers,
expected_num_results,
search_layer_count,
mocker,
):
map_tool.search_layer_ids = [test_layers[i].id() for i in range(search_layer_count)]

layer_ids = []

for index in search_layers:
layer_ids.append(test_layers[index].id())

m_choose_layer_from_identify_results = mocker.patch.object(
map_tool,
"_choose_layer_from_identify_results",
return_value=None,
autospec=True,
)

map_tool.set_active_layer_using_closest_feature(MOUSE_LOCATION, search_radius=2.5)

identify_results = m_choose_layer_from_identify_results.call_args.args[0]
assert len(identify_results) == expected_num_results

# check that identify results are in same order
# as requested layer ids (result may contain multiple
# features from one layer)
identify_results_layer_ids = []
previous_id = ""
for result in identify_results:
if result.mLayer.id() != previous_id:
identify_results_layer_ids.append(result.mLayer.id())
previous_id = result.mLayer.id()

assert identify_results_layer_ids == layer_ids[:search_layer_count]


@pytest.mark.parametrize(
argnames=(
"search_layers",
Expand Down

0 comments on commit 3244d5f

Please sign in to comment.