79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
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",
|
|
)
|
|
|
|
@patch("sn_basis.functions.variable_wrapper.QgsExpressionContextUtils.setGlobalVariable")
|
|
def test_set_variable_global_writes_prefixed_name(self, mock_set_global):
|
|
variable_wrapper.set_variable("verfahrensnummer", "123", scope="global")
|
|
|
|
mock_set_global.assert_called_once_with("sn_verfahrensnummer", "123")
|
|
|
|
@patch("sn_basis.functions.variable_wrapper.QgsExpressionContextUtils.globalScope")
|
|
def test_get_variable_global_returns_empty_string_fallback(self, mock_global_scope):
|
|
mock_global_scope.return_value = _Scope("")
|
|
|
|
value = variable_wrapper.get_variable("nicht_vorhanden", scope="global")
|
|
|
|
self.assertEqual(value, "")
|
|
|
|
def test_get_variable_invalid_scope_raises(self):
|
|
with self.assertRaises(ValueError):
|
|
variable_wrapper.get_variable("foo", scope="invalid")
|
|
|
|
def test_set_variable_invalid_scope_raises(self):
|
|
with self.assertRaises(ValueError):
|
|
variable_wrapper.set_variable("foo", "bar", scope="invalid")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|