def test_get_from_context_default_value(self): def function(a, b=10): return a + b context = dict() wrapped = get_from_context(context, function) assert_that(calling(wrapped), raises(KeyError)) context = dict(a=190) wrapped = get_from_context(context, function) assert_that(wrapped(), is_(200)) context = dict(a=190, b=11) wrapped = get_from_context(context, function) assert_that(wrapped(), is_(201))
def test_get_from_context_default_value(self): def function(a, b=10): return a + b context = dict() wrapped = get_from_context(context, function) assert_that(calling(wrapped), raises(ContextKeyNotFound)) context = dict(a=190) wrapped = get_from_context(context, function) assert_that(wrapped(), is_(200)) context = dict(a=190, b=11) wrapped = get_from_context(context, function) assert_that(wrapped(), is_(201))
def test_temporarily_replace_missing_keys(self): context = dict() func = binds(arg="param")(lambda param: param) wrapped = get_from_context(context, func) wrapped = temporarily_replace_context_keys(context, wrapped) assert_that(calling(wrapped), raises(KeyError))
def test_temporarily_replace_context_keys_wrong_order(self): context = dict(arg=123) func = binds(arg="param")(lambda param: param) wrapped = temporarily_replace_context_keys(context, func) wrapped = get_from_context(context, wrapped) assert_that(calling(wrapped), raises(ContextKeyNotFound))
def test_temporarily_replace_context_keys(self): context = dict(arg=123) func = binds(arg="param")(lambda param: param) wrapped = get_from_context(context, func) wrapped = temporarily_replace_context_keys(context, wrapped) assert_that(wrapped(), is_(123))
def test_temporarily_replace_missing_keys(self): context = dict() func = binds(arg="param")(lambda param: param) wrapped = get_from_context(context, func) wrapped = temporarily_replace_context_keys(context, wrapped) assert_that(calling(wrapped), raises(KeyError))
def test_temporarily_replace_context_keys(self): context = dict(arg=123) func = binds(arg="param")(lambda param: param) wrapped = get_from_context(context, func) wrapped = temporarily_replace_context_keys(context, wrapped) assert_that(wrapped(), is_(123))
def test_missing_context_key(self): context = dict() wrapped = get_from_context(context, lambda arg: arg) assert_that( calling(wrapped), raises( ContextKeyNotFound, "Failed to find context key `arg` during evaluation of `<function TestDecorators.test_missing_context_key" # noqa ), )
def test_multiple_decorators(self): @extracts("res_obj") class Extractor: def __call__(self, arg): return arg @extracts("res_func") def extractor(arg): return arg context = dict(arg=123) wrapped_object = save_to_context(context, get_from_context(context, Extractor())) wrapped_function = save_to_context(context, get_from_context(context, extractor)) wrapped_object() wrapped_function() assert_that(context, is_(equal_to(dict( arg=123, res_obj=123, res_func=123, ))))
def test_get_from_context_function_and_methods(self): context = dict(arg=123) def function(arg): return arg class example: class_number = 200 def __init__(self): self.instance_number = 100 def method(self, arg): return self.instance_number + arg @staticmethod def static_method(arg): return arg @classmethod def class_method(cls, arg): return cls.class_number + arg wrapped_function = get_from_context(context, function) assert_that(wrapped_function(), is_(123)) wrapped_method = get_from_context(context, example().method) assert_that(wrapped_method(), is_(223)) wrapped_static_method = get_from_context(context, example().static_method) assert_that(wrapped_static_method(), is_(123)) wrapped_class_method = get_from_context(context, example().class_method) assert_that(wrapped_class_method(), is_(323)) wrapped_static_method_ref = get_from_context(context, example.static_method) assert_that(wrapped_static_method_ref(), is_(123)) wrapped_class_method_ref = get_from_context(context, example.class_method) assert_that(wrapped_class_method_ref(), is_(323))
def test_get_from_context_function_and_methods(self): context = dict(arg=123) def function(arg): return arg class example: class_number = 200 def __init__(self): self.instance_number = 100 def method(self, arg): return self.instance_number + arg @staticmethod def static_method(arg): return arg @classmethod def class_method(cls, arg): return cls.class_number + arg wrapped_function = get_from_context(context, function) assert_that(wrapped_function(), is_(123)) wrapped_method = get_from_context(context, example().method) assert_that(wrapped_method(), is_(223)) wrapped_static_method = get_from_context(context, example().static_method) assert_that(wrapped_static_method(), is_(123)) wrapped_class_method = get_from_context(context, example().class_method) assert_that(wrapped_class_method(), is_(323)) wrapped_static_method_ref = get_from_context(context, example.static_method) assert_that(wrapped_static_method_ref(), is_(123)) wrapped_class_method_ref = get_from_context(context, example.class_method) assert_that(wrapped_class_method_ref(), is_(323))
def test_get_from_context(self): context = dict(arg=200) wrapped = get_from_context(context, lambda arg: arg) assert_that(wrapped(), is_(200))
def test_get_from_context_multiple_args(self): context = dict(char2='b', char1='a') wrapped = get_from_context(context, lambda char1, char2: char1 + char2) assert_that(wrapped(), is_('ab'))
def test_get_from_context(self): context = dict(arg=200) wrapped = get_from_context(context, lambda arg: arg) assert_that(wrapped(), is_(200))
def test_get_from_context_multiple_args(self): context = dict(char2='b', char1='a') wrapped = get_from_context(context, lambda char1, char2: char1 + char2) assert_that(wrapped(), is_('ab'))