def test_resolve_entry_point(): """ Resolving an entry point function works without binding. """ registry = Registry() factory = registry.resolve("hello_world") assert_that(factory, is_(not_none()))
def test_bind_and_resolve(): """ Binding a function allows it be resolved. """ registry = Registry() registry.bind("foo", create_foo) factory = registry.resolve("foo") assert_that(factory, is_(equal_to(create_foo)))
def test_bind_duplicate(): """ Binding to the same key multiple times raises an error. """ registry = Registry() registry.bind("foo", create_foo) assert_that( calling(registry.bind).with_args("foo", create_bar), raises(AlreadyBoundError), )
def test_binding(): """ Binding registers function. """ registry = Registry() @binding("foo", registry=registry) def func(*args, **kwargs): pass assert_that(registry.resolve("foo"), is_(equal_to(func)))
def test_object_graph_partial_use(): """ Partial initialization succeeds partially and is recoverable. """ registry = Registry() create_first = Mock() create_first.return_value = "first" create_second = Mock() create_second.side_effect = [Exception, "second"] create_third = Mock() create_third.side_effect = "third" registry.bind("first", create_first) registry.bind("second", create_second) registry.bind("third", create_third) graph = create_object_graph("test", registry=registry) # exception raised from initial call to create_second assert_that( calling(graph.use).with_args("first", "second", "third"), raises(Exception)) # first and second were called, but not third assert_that(create_first.call_count, is_(equal_to(1))) assert_that(create_second.call_count, is_(equal_to(1))) assert_that(create_third.call_count, is_(equal_to(0))) # second call succeeds [first, second, third] = graph.use("first", "second", "third") # first was not called, second was called again, and third called for the first time assert_that(create_first.call_count, is_(equal_to(1))) assert_that(create_second.call_count, is_(equal_to(2))) assert_that(create_third.call_count, is_(equal_to(1)))
def test_handle_with_no_context(): """ Test that when no context is added the dispatcher behaves sanely. """ def loader(metadata): return dict(sqs_consumer=dict(sqs_queue_url=FOO_QUEUE_URL, ), ) graph = create_object_graph("example", testing=True, loader=loader, registry=Registry()) @binding("sqs_message_handlers", graph._registry) def configure_message_handlers(graph): return { FooSchema.MEDIA_TYPE: foo_handler, } # remove the sqs_message_context from the graph so we can test the dispatcher # defaulting logic graph._registry.entry_points.pop('sqs_message_context') message = dict(bar="baz") result = graph.sqs_message_dispatcher.handle_message( FooSchema.MEDIA_TYPE, message) assert_that(result, is_(equal_to(True))) assert_that(graph.sqs_message_dispatcher.sqs_message_context(message), is_(equal_to(dict())))
def test_handle(): """ Test that the dispatcher handles a message and assigns context. """ def loader(metadata): return dict(sqs_consumer=dict(sqs_queue_url=FOO_QUEUE_URL, ), ) graph = create_object_graph("example", testing=True, loader=loader, registry=Registry()) @binding("sqs_message_handlers", graph._registry) def configure_message_handlers(graph): return { FooSchema.MEDIA_TYPE: foo_handler, } message = dict(bar="baz") sqs_message_context = Mock(return_value=dict()) graph.sqs_message_dispatcher.sqs_message_context = sqs_message_context result = graph.sqs_message_dispatcher.handle_message( FooSchema.MEDIA_TYPE, message) assert_that(result, is_(equal_to(True))) sqs_message_context.assert_called_once_with(message)
def test_object_graph_partial_use(): """ Partial initialization succeeds partially and is recoverable. """ registry = Registry() create_first = Mock() create_first.return_value = "first" create_second = Mock() create_second.side_effect = [Exception, "second"] create_third = Mock() create_third.side_effect = "third" registry.bind("first", create_first) registry.bind("second", create_second) registry.bind("third", create_third) graph = create_object_graph("test", registry=registry) # exception raised from initial call to create_second assert_that(calling(graph.use).with_args("first", "second", "third"), raises(Exception)) # first and second were called, but not third assert_that(create_first.call_count, is_(equal_to(1))) assert_that(create_second.call_count, is_(equal_to(1))) assert_that(create_third.call_count, is_(equal_to(0))) # second call succeeds [first, second, third] = graph.use("first", "second", "third") # first was not called, second was called again, and third called for the first time assert_that(create_first.call_count, is_(equal_to(1))) assert_that(create_second.call_count, is_(equal_to(2))) assert_that(create_third.call_count, is_(equal_to(1)))
def test_resolve_not_found(): """ Resolving an unbound function raises an error. """ registry = Registry() assert_that( calling(registry.resolve).with_args("foo"), raises(NotBoundError), )
def test_object_graph_cycle(): """ Graph cycles are detected. """ registry = Registry() @binding("cycle", registry=registry) def create_cycle(graph): return graph.cycle graph = create_object_graph("test", registry=registry) assert_that( calling(graph.use).with_args("cycle"), raises(CyclicGraphError))
def test_handle_with_skipping(): """ Test that skipping works """ def loader(metadata): return dict(sqs_consumer=dict(sqs_queue_url=FOO_QUEUE_URL, ), ) graph = create_object_graph("example", testing=True, loader=loader, registry=Registry()) @binding("sqs_message_handlers", graph._registry) def configure_message_handlers(graph): return { FooSchema.MEDIA_TYPE: skipping_handler, } message = dict(bar="baz") result = graph.sqs_message_dispatcher.handle_message( FooSchema.MEDIA_TYPE, message) assert_that(result, is_(equal_to(False)))
def setup(self): self.metadata = Metadata("test") self.registry = Registry()