Inability to test for parameter `relative_path` in calls of `backends.Backend.update_object()` and `backends.Backend.commit_object()` from `model.base.Referable` using `unittest.mock.Mock`
Especially during execution of the model.base.Referable.commit()
function, the relative_path
variable is built iteratively by traversing the Referable-tree. Whenever there is a nonempty source, the backends.Backend.commit_object()
function is called with the current relative_path
. Therefore, the relative_path
variable is likely to change after a call. This is impractical, because unittest.mock.call
seems to only log a reference to the variables that were used in that call, rather than saving a duplicate of the current state of those variables during function call.
Here is a minimal example that shows this behavior:
from unittest import mock
test_mock = mock.Mock()
test_parameter = ["only_entry_of_this_list"]
test_mock(argument=test_parameter)
test_parameter.append("another_entry_of_this_list")
test_mock.assert_has_calls([mock.call(argument=["only_entry_of_this_list"])])
leads to:
AssertionError: Calls not found.
Expected: [call(argument=['only_entry_of_this_list'])]
Actual: [call(argument=['only_entry_of_this_list', 'another_entry_of_this_list'])]
Therefore, it is impossible to check, if the relative_path
has been correct during the time of the function call, using the unittest.mock.Mock
-object.
While I have reviewed with the debugger, that the function gets called with the correct relative_path
at each time, I don't see a way to "prove it" using unittests, without altering the actual code to allow for testing, which in my opinion shouldn't really be done.