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_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_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)))