Files
Plugin_SN_Basis/tests/test_variable_wrapper.py
T

57 lines
1.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",
)
if __name__ == "__main__":
unittest.main()