def test_unbind_threadlocal(self): """ Test that unbinding from threadlocal works for keys that exist and does not raise error when they do not exist. """ clear_threadlocal() bind_threadlocal(a=234, b=34) assert {"a": 234, "b": 34} == merge_threadlocal_context(None, None, {}) unbind_threadlocal("a") assert {"b": 34} == merge_threadlocal_context(None, None, {}) unbind_threadlocal("non-existing-key") assert {"b": 34} == merge_threadlocal_context(None, None, {})
def test_clear(self): """ The thread-local context can be cleared, causing any previously bound variables to not be included in merge_threadlocal_context's result. """ bind_threadlocal(a=1) clear_threadlocal() assert {"b": 2} == merge_threadlocal_context(None, None, {"b": 2})
def test_bind_and_merge(self): """ Binding a variable causes it to be included in the result of merge_threadlocal_context. """ bind_threadlocal(a=1) assert { "a": 1, "b": 2 } == merge_threadlocal_context(None, None, {"b": 2})
def test_multiple_binds(self): """ Multiple calls to bind_threadlocal accumulate values instead of replacing them. """ bind_threadlocal(a=1, b=2) bind_threadlocal(c=3) assert { "a": 1, "b": 2, "c": 3 } == merge_threadlocal_context(None, None, {"b": 2})
def test_merge_works_without_bind(self): """ merge_threadlocal_context returns values as normal even when there has been no previous calls to bind_threadlocal. """ assert {"b": 2} == merge_threadlocal_context(None, None, {"b": 2})