diff --git a/functions/variable_wrapper.py b/functions/variable_wrapper.py index d3f8a2d..536bd6c 100644 --- a/functions/variable_wrapper.py +++ b/functions/variable_wrapper.py @@ -97,6 +97,10 @@ def set_variable(key: str, value: str, scope: str = "project") -> None: var_name = f"sn_{key}" if scope == "project": + current_value = get_variable(key, scope="project") + if current_value == value: + return + project = QgsProject.instance() QgsExpressionContextUtils.setProjectVariable( project, diff --git a/tests/test_variable_wrapper.py b/tests/test_variable_wrapper.py new file mode 100644 index 0000000..df64715 --- /dev/null +++ b/tests/test_variable_wrapper.py @@ -0,0 +1,56 @@ +import unittest +from unittest.mock import patch + +from sn_basis.functions import variable_wrapper + + +class _Scope: + def __init__(self, value: str): + self._value = value + + def variable(self, _name: str) -> str: + return self._value + + +class TestVariableWrapper(unittest.TestCase): + + @patch("sn_basis.functions.variable_wrapper.QgsProject.instance") + @patch("sn_basis.functions.variable_wrapper.QgsExpressionContextUtils.setProjectVariable") + @patch("sn_basis.functions.variable_wrapper.QgsExpressionContextUtils.projectScope") + def test_set_variable_project_noop_write_is_skipped( + self, + mock_project_scope, + mock_set_project_variable, + mock_project_instance, + ): + mock_project_instance.return_value = object() + mock_project_scope.return_value = _Scope("layer_1") + + variable_wrapper.set_variable("verfahrensgebiet_layer", "layer_1", scope="project") + + mock_set_project_variable.assert_not_called() + + @patch("sn_basis.functions.variable_wrapper.QgsProject.instance") + @patch("sn_basis.functions.variable_wrapper.QgsExpressionContextUtils.setProjectVariable") + @patch("sn_basis.functions.variable_wrapper.QgsExpressionContextUtils.projectScope") + def test_set_variable_project_changed_value_is_written( + self, + mock_project_scope, + mock_set_project_variable, + mock_project_instance, + ): + fake_project = object() + mock_project_instance.return_value = fake_project + mock_project_scope.return_value = _Scope("old_value") + + variable_wrapper.set_variable("verfahrensgebiet_layer", "new_value", scope="project") + + mock_set_project_variable.assert_called_once_with( + fake_project, + "sn_verfahrensgebiet_layer", + "new_value", + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_verfahrensgebiet_manager.py b/tests/test_verfahrensgebiet_manager.py new file mode 100644 index 0000000..1ee5bc9 --- /dev/null +++ b/tests/test_verfahrensgebiet_manager.py @@ -0,0 +1,39 @@ +import unittest +from unittest.mock import patch + +from sn_basis.functions import verfahrensgebiet_manager + + +class _Scope: + def __init__(self, value: str): + self._value = value + + def variable(self, _name: str) -> str: + return self._value + + +class TestVerfahrensgebietManager(unittest.TestCase): + + @patch("sn_basis.functions.verfahrensgebiet_manager._find_verfahrensgebiet_layer") + @patch("sn_basis.functions.variable_wrapper.QgsProject.instance") + @patch("sn_basis.functions.variable_wrapper.QgsExpressionContextUtils.setProjectVariable") + @patch("sn_basis.functions.variable_wrapper.QgsExpressionContextUtils.projectScope") + def test_update_without_layer_does_not_write_when_already_empty( + self, + mock_project_scope, + mock_set_project_variable, + mock_project_instance, + mock_find_layer, + ): + mock_find_layer.return_value = (None, None) + mock_project_instance.return_value = object() + mock_project_scope.return_value = _Scope("") + + result = verfahrensgebiet_manager._update_verfahrensgebiet_from_project() + + self.assertFalse(result) + mock_set_project_variable.assert_not_called() + + +if __name__ == "__main__": + unittest.main()