def test_equal_if_same_field_values(self): arg_binding_key_one = arg_binding_keys.ArgBindingKey( 'an-arg-name', binding_keys.new('an-arg-name', 'an-annotation'), provider_indirections.INDIRECTION) arg_binding_key_two = arg_binding_keys.ArgBindingKey( 'an-arg-name', binding_keys.new('an-arg-name', 'an-annotation'), provider_indirections.INDIRECTION) self.assertEqual(arg_binding_key_one, arg_binding_key_two) self.assertEqual(hash(arg_binding_key_one), hash(arg_binding_key_two)) self.assertEqual(str(arg_binding_key_one), str(arg_binding_key_two))
def test_unequal_if_not_same_arg_name(self): arg_binding_key_one = arg_binding_keys.ArgBindingKey( 'an-arg-name', binding_keys.new('an-arg-name', 'an-annotation'), provider_indirections.INDIRECTION) arg_binding_key_two = arg_binding_keys.ArgBindingKey( 'other-arg-name', binding_keys.new('other-arg-name', 'an-annotation'), provider_indirections.INDIRECTION) self.assertNotEqual(arg_binding_key_one, arg_binding_key_two) self.assertNotEqual(hash(arg_binding_key_one), hash(arg_binding_key_two)) self.assertNotEqual(str(arg_binding_key_one), str(arg_binding_key_two))
def test_multiple_provides_gives_multiple_bindings(self): @decorators.provides('foo', annotated_with='foo-annot') @decorators.provides('bar', annotated_with='bar-annot') def provide_something(self): pass provider_fn_bindings = bindings.get_provider_fn_bindings( provide_something, ['something']) self.assertEqual(set([binding_keys.new('foo', annotated_with='foo-annot'), binding_keys.new('bar', annotated_with='bar-annot')]), set([provider_fn_binding.binding_key for provider_fn_binding in provider_fn_bindings]))
def test_unequal_if_not_same_indirection(self): arg_binding_key_one = arg_binding_keys.ArgBindingKey( 'an-arg-name', binding_keys.new('an-arg-name', 'an-annotation'), provider_indirections.INDIRECTION) arg_binding_key_two = arg_binding_keys.ArgBindingKey( 'an-arg-name', binding_keys.new('an-arg-name', 'an-annotation'), provider_indirections.NO_INDIRECTION) self.assertNotEqual(arg_binding_key_one, arg_binding_key_two) self.assertNotEqual(hash(arg_binding_key_one), hash(arg_binding_key_two)) # Strings will be equal, since indirection isn't part of the string. self.assertEqual(str(arg_binding_key_one), str(arg_binding_key_two))
def test_multiple_provides_gives_multiple_bindings(self): @decorators.provides('foo', annotated_with='foo-annot') @decorators.provides('bar', annotated_with='bar-annot') def provide_something(self): pass provider_fn_bindings = bindings.get_provider_fn_bindings( provide_something, ['something']) self.assertEqual( set([binding_keys.new('foo', annotated_with='foo-annot'), binding_keys.new('bar', annotated_with='bar-annot')]), set([provider_fn_binding.binding_key for provider_fn_binding in provider_fn_bindings]))
def test_str(self): arg_binding_key = arg_binding_keys.ArgBindingKey( 'an-arg-name', binding_keys.new('an-arg-name', 'an-annotation'), provider_indirections.INDIRECTION) self.assertEqual( 'the arg named "an-arg-name" annotated with "an-annotation"', str(arg_binding_key))
def test_returns_required_binding(self): self.required_bindings.require('an-arg-name', annotated_with='annot') [required_binding] = self.required_bindings.get() self.assertEqual( binding_keys.new('an-arg-name', annotated_with='annot'), required_binding.binding_key) self.assertIn('required_bindings_test.py', required_binding.require_loc)
def test_get_child_successfully(self): other_binding_key = binding_keys.new('bar') new_injection_context = self.injection_context.get_child( _UNUSED_INJECTION_SITE_FN, bindings.new_binding_to_instance(other_binding_key, 'unused-instance', 'new-scope', lambda: 'unused-desc'))
def test_uses_default_binding_when_arg_name_and_annotation_omitted(self): @decorators.provides(in_scope='unused') def provide_foo(self): pass [provider_fn_binding] = bindings.get_provider_fn_bindings(provide_foo, ['foo']) self.assertEqual(binding_keys.new('foo'), provider_fn_binding.binding_key)
def test_can_bind_to_class(self): class SomeClass(object): pass self.binder.bind('an-arg-name', to_class=SomeClass) [expected_binding] = [ b for b in self.collected_bindings if b.binding_key == binding_keys.new('an-arg-name')]
def test_can_bind_to_instance(self): an_instance = object() self.binder.bind('an-arg-name', to_instance=an_instance) [only_binding] = self.collected_bindings self.assertEqual(binding_keys.new('an-arg-name'), only_binding.binding_key) self.assertIs(an_instance, call_provisor_fn(only_binding))
def test_can_bind_with_annotation(self): self.binder.bind('an-arg-name', annotated_with='an-annotation', to_instance='an-instance') [only_binding] = self.collected_bindings self.assertEqual( binding_keys.new('an-arg-name', 'an-annotation'), only_binding.binding_key)
def test_get_child_successfully(self): other_binding_key = binding_keys.new('bar') new_injection_context = self.injection_context.get_child( _UNUSED_INJECTION_SITE_FN, bindings.new_binding_to_instance( other_binding_key, 'unused-instance', 'new-scope', lambda: 'unused-desc'))
def test_can_bind_with_annotation(self): self.binder.bind('an-arg-name', annotated_with='an-annotation', to_instance='an-instance') [only_binding] = self.collected_bindings self.assertEqual(binding_keys.new('an-arg-name', 'an-annotation'), only_binding.binding_key)
def setUp(self): class SomeClass(object): pass self.some_binding_key = binding_keys.new('some_class') self.some_binding = new_in_default_scope(self.some_binding_key) self.another_some_binding = new_in_default_scope(self.some_binding_key)
def test_returns_binding_for_input_class(self): class SomeClass(object): pass [implicit_binding] = bindings_lib.get_implicit_class_bindings([SomeClass]) self.assertEqual(binding_keys.new('some_class'), implicit_binding.binding_key) self.assertEqual('a-provided-SomeClass', call_provisor_fn(implicit_binding))
def test_uses_provided_fn_to_map_class_names_to_arg_names(self): class SomeClass(object): pass [implicit_binding] = bindings_lib.get_implicit_class_bindings( [SomeClass], get_arg_names_from_class_name=lambda _: ['foo']) self.assertEqual(binding_keys.new('foo'), implicit_binding.binding_key)
def setUp(self): class SomeClass(object): pass self.some_binding_key = binding_keys.new('some_class') self.some_binding = new_in_default_scope( self.some_binding_key, 'a-proviser-fn') self.another_some_binding = new_in_default_scope( self.some_binding_key, 'another-proviser-fn')
def test_colliding_bindings_raises_error(self): binding_key = binding_keys.new('unused') binding_one = new_in_default_scope(binding_key) binding_two = new_in_default_scope(binding_key) binding_mapping = bindings_lib.BindingMapping( {}, {'colliding-binding-key': [binding_one, binding_two]}) self.assertRaises(errors.AmbiguousArgNameError, binding_mapping.get, 'colliding-binding-key', 'injection-site-desc')
def test_get_child_raises_error_when_scope_not_usable(self): other_binding_key = binding_keys.new('bar') self.assertRaises( errors.BadDependencyScopeError, self.injection_context.get_child, _UNUSED_INJECTION_SITE_FN, bindings.new_binding_to_instance( other_binding_key, 'unused-instance', 'unusable-scope', lambda: 'unused-desc'))
def setUp(self): self.binding_key = binding_keys.new('foo') injection_context_factory = injection_contexts.InjectionContextFactory( lambda to_scope, from_scope: to_scope != 'unusable-scope') top_injection_context = injection_context_factory.new() self.injection_context = top_injection_context.get_child( bindings.Binding(self.binding_key, 'unused-proviser-fn', 'curr-scope', 'unused-desc'))
def test_returns_binding_for_provider_fn(self): class SomeBindingSpec(bindings_lib.BindingSpec): def provide_foo(self): return 'a-foo' [implicit_binding] = bindings_lib.get_provider_bindings(SomeBindingSpec()) self.assertEqual(binding_keys.new('foo'), implicit_binding.binding_key) self.assertEqual('a-foo', call_provisor_fn(implicit_binding))
def test_returns_binding_for_correct_input_class(self): class ClassOne(object): pass class ClassTwo(object): pass implicit_bindings = bindings_lib.get_implicit_class_bindings( [ClassOne, ClassTwo]) for implicit_binding in implicit_bindings: if (implicit_binding.binding_key == binding_keys.new('class_one')): self.assertEqual( 'a-provided-ClassOne', call_provisor_fn(implicit_binding)) else: self.assertEqual(implicit_binding.binding_key, binding_keys.new('class_two')) self.assertEqual( 'a-provided-ClassTwo', call_provisor_fn(implicit_binding))
def test_uses_default_binding_when_arg_name_and_annotation_omitted(self): @decorators.provides(in_scope='unused') def provide_foo(self): pass [provider_fn_binding] = bindings.get_provider_fn_bindings( provide_foo, ['foo']) self.assertEqual(binding_keys.new('foo'), provider_fn_binding.binding_key)
def test_uses_provided_fn_to_map_class_names_to_arg_names(self): class SomeClass(object): @decorators.injectable def __init__(self): pass [explicit_binding] = bindings_lib.get_explicit_class_bindings( [SomeClass], get_arg_names_from_class_name=lambda _: ['foo']) self.assertEqual(binding_keys.new('foo'), explicit_binding.binding_key)
def test_returns_binding_for_input_explicitly_injected_class(self): class SomeClass(object): @decorators.injectable def __init__(self): pass [explicit_binding] = bindings_lib.get_explicit_class_bindings([SomeClass]) self.assertEqual(binding_keys.new('some_class'), explicit_binding.binding_key) self.assertEqual('a-provided-SomeClass', call_provisor_fn(explicit_binding))
def test_returns_binding_for_correct_input_class(self): class ClassOne(object): pass class ClassTwo(object): pass implicit_bindings = bindings_lib.get_implicit_class_bindings( [ClassOne, ClassTwo]) for implicit_binding in implicit_bindings: if (implicit_binding.binding_key == binding_keys.new('class_one')): self.assertEqual('a-provided-ClassOne', call_provisor_fn(implicit_binding)) else: self.assertEqual(implicit_binding.binding_key, binding_keys.new('class_two')) self.assertEqual('a-provided-ClassTwo', call_provisor_fn(implicit_binding))
def test_can_bind_to_class(self): class SomeClass(object): pass self.binder.bind('an-arg-name', to_class=SomeClass) [expected_binding] = [ b for b in self.collected_bindings if b.binding_key == binding_keys.new('an-arg-name') ]
def test_get_child_raises_error_when_scope_not_usable(self): other_binding_key = binding_keys.new('bar') self.assertRaises( errors.BadDependencyScopeError, self.injection_context.get_child, _UNUSED_INJECTION_SITE_FN, bindings.new_binding_to_instance(other_binding_key, 'unused-instance', 'unusable-scope', lambda: 'unused-desc'))
def test_sets_arg_values(self): @decorators.provides('an-arg-name', annotated_with='an-annotation', in_scope='a-scope-id') def provide_foo(): pass [provider_fn_binding] = bindings.get_provider_fn_bindings(provide_foo, ['foo']) self.assertEqual(binding_keys.new('an-arg-name', 'an-annotation'), provider_fn_binding.binding_key) self.assertEqual('a-scope-id', provider_fn_binding.scope_id)
def test_returns_binding_for_input_class(self): class SomeClass(object): pass [implicit_binding ] = bindings_lib.get_implicit_class_bindings([SomeClass]) self.assertEqual(binding_keys.new('some_class'), implicit_binding.binding_key) self.assertEqual('a-provided-SomeClass', call_provisor_fn(implicit_binding))
def test_returns_binding_for_provider_fn(self): class SomeBindingSpec(bindings_lib.BindingSpec): def provide_foo(self): return 'a-foo' [implicit_binding ] = bindings_lib.get_provider_bindings(SomeBindingSpec(), scoping._BUILTIN_SCOPES) self.assertEqual(binding_keys.new('foo'), implicit_binding.binding_key) self.assertEqual('a-foo', call_provisor_fn(implicit_binding))
def test_sets_arg_values(self): @decorators.provides('an-arg-name', annotated_with='an-annotation', in_scope='a-scope-id') def provide_foo(): pass [provider_fn_binding] = bindings.get_provider_fn_bindings( provide_foo, ['foo']) self.assertEqual(binding_keys.new('an-arg-name', 'an-annotation'), provider_fn_binding.binding_key) self.assertEqual('a-scope-id', provider_fn_binding.scope_id)
def setUp(self): self.binding_key = binding_keys.new('foo') self.binding = bindings.new_binding_to_instance( self.binding_key, 'an-instance', 'curr-scope', lambda: 'unused-desc') injection_context_factory = injection_contexts.InjectionContextFactory( lambda to_scope, from_scope: to_scope != 'unusable-scope') top_injection_context = injection_context_factory.new( _UNUSED_INJECTION_SITE_FN) self.injection_context = top_injection_context.get_child( _UNUSED_INJECTION_SITE_FN, self.binding)
def setUp(self): self.binding_key = binding_keys.new('foo') self.binding = bindings.Binding( self.binding_key, bindings.create_instance_proviser_fn( self.binding_key, 'an-instance'), 'curr-scope', 'unused-desc') injection_context_factory = injection_contexts.InjectionContextFactory( lambda to_scope, from_scope: to_scope != 'unusable-scope') top_injection_context = injection_context_factory.new() self.injection_context = top_injection_context.get_child(self.binding)
def test_always_calls_provider_fn(self): next_provided = [0] def provider_fn(): provided = next_provided[0] next_provided[0] += 1 return provided scope = scoping.PrototypeScope() binding_key = binding_keys.new('unused') self.assertEqual( list(range(10)), [scope.provide(binding_key, provider_fn) for _ in range(10)])
def test_uses_provided_fn_to_map_provider_fn_names_to_arg_names(self): class SomeBindingSpec(bindings_lib.BindingSpec): def some_foo(): return 'a-foo' def get_arg_names(fn_name): return ['foo'] if fn_name == 'some_foo' else [] [implicit_binding] = bindings_lib.get_provider_bindings( SomeBindingSpec(), get_arg_names_from_provider_fn_name=get_arg_names) self.assertEqual(binding_keys.new('foo'), implicit_binding.binding_key)
def test_colliding_bindings_raises_error(self): binding_key = binding_keys.new('unused') binding_one = new_in_default_scope( binding_key, bindings_lib.create_instance_proviser_fn(binding_key, 'unused')) binding_two = new_in_default_scope( binding_key, bindings_lib.create_instance_proviser_fn(binding_key, 'unused')) binding_mapping = bindings_lib.BindingMapping( {}, {'colliding-binding-key': [binding_one, binding_two]}) self.assertRaises(errors.AmbiguousArgNameError, binding_mapping.get, 'colliding-binding-key')
def setUp(self): class SomeClass(object): pass self.some_binding_key = binding_keys.new('some_class') self.some_binding = new_in_default_scope( self.some_binding_key, bindings_lib.create_instance_proviser_fn( self.some_binding_key, 'unused')) self.another_some_binding = new_in_default_scope( self.some_binding_key, bindings_lib.create_instance_proviser_fn( self.some_binding_key, 'also-unused'))
def test_returns_binding_for_input_explicitly_injected_class(self): class SomeClass(object): @decorators.injectable def __init__(self): pass [explicit_binding ] = bindings_lib.get_explicit_class_bindings([SomeClass]) self.assertEqual(binding_keys.new('some_class'), explicit_binding.binding_key) self.assertEqual('a-provided-SomeClass', call_provisor_fn(explicit_binding))
def test_uses_provided_fn_to_map_provider_fn_names_to_arg_names(self): class SomeBindingSpec(bindings_lib.BindingSpec): def some_foo(): return 'a-foo' def get_arg_names(fn_name): return ['foo'] if fn_name == 'some_foo' else [] [implicit_binding] = bindings_lib.get_provider_bindings( SomeBindingSpec(), scoping._BUILTIN_SCOPES, get_arg_names_from_provider_fn_name=get_arg_names) self.assertEqual(binding_keys.new('foo'), implicit_binding.binding_key)
def test_always_calls_provider_fn(self): next_provided = [0] def provider_fn(): provided = next_provided[0] next_provided[0] += 1 return provided scope = scoping.PrototypeScope() binding_key = binding_keys.new('unused') self.assertEqual( range(10), [scope.provide(binding_key, provider_fn) for _ in xrange(10)])
def test_without_annotation(self): binding_key = binding_keys.new('an-arg-name') self.assertEqual('the binding name "an-arg-name" (unannotated)', str(binding_key))
def test_with_annotation(self): binding_key = binding_keys.new('an-arg-name', 'an-annotation') self.assertEqual( 'the binding name "an-arg-name" (annotated with "an-annotation")', str(binding_key))
def test_get_sub_scope_successfully(self): usable_binding = bindings.new_binding_to_instance( binding_keys.new('foo'), 'unused-instance', 'usable-scope-id', lambda: 'unused-desc') self.assertEqual( 'usable-scope', self.bindable_scopes.get_sub_scope(usable_binding))
def setUp(self): self.scope = scoping.SingletonScope() self.binding_key_one = binding_keys.new('one') self.binding_key_two = binding_keys.new('two') self.provider_fn = lambda: object()
def test_get_sub_scope_successfully(self): usable_binding = bindings.new_binding_to_instance( binding_keys.new('foo'), 'unused-instance', 'usable-scope-id', lambda: 'unused-desc') self.assertEqual('usable-scope', self.bindable_scopes.get_sub_scope(usable_binding))