Ejemplo n.º 1
0
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)))
Ejemplo n.º 2
0
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)))
Ejemplo n.º 3
0
def test_object_graph_use():
    """
    Explicitly create a set of components.

    """
    graph = create_object_graph(name="test", )
    parent, _ = graph.use("parent", "hello_world")
    assert_that(parent, is_(instance_of(Parent)))
Ejemplo n.º 4
0
def test_importing():
    """
    Importing can be used to access object graph members.

    """
    graph = create_object_graph(name="test", )
    graph.use("hello_world")
    graph.export("some.module")
    from some.module import hello_world
    assert_that(hello_world, is_(equal_to("hello world")))
Ejemplo n.º 5
0
def test_create_object_graph():
    """
    Construct an object graph from bound factories and entry points.

    """
    graph = create_object_graph(name="test", )
    assert_that(graph.child, is_(instance_of(Child)))
    assert_that(graph.child.value, is_(equal_to("default_value")))
    assert_that(graph.parent, is_(instance_of(Parent)))
    assert_that(graph.parent.child, is_(equal_to(graph.child)))
    assert_that(graph.hello_world, is_(equal_to("hello world")))
Ejemplo n.º 6
0
def test_get_component_name_function():
    """
    Utility function `get_component_name` works.

    """
    graph = create_object_graph(name="test", )
    graph.use("parent")
    graph.lock()

    assert_that(get_component_name(graph, graph.parent),
                is_(equal_to("parent")))
Ejemplo n.º 7
0
def test_importing():
    """
    Importing can be used to access object graph members.

    """
    graph = create_object_graph(
        name="test",
    )
    graph.use("hello_world")
    graph.export("some.module")
    from some.module import hello_world
    assert_that(hello_world, is_(equal_to("hello world")))
Ejemplo n.º 8
0
def test_object_graph_use():
    """
    Explicitly create a set of components.

    """
    graph = create_object_graph(
        name="test",
    )
    parent, _ = graph.use(
        "parent",
        "hello_world"
    )
    assert_that(parent, is_(instance_of(Parent)))
Ejemplo n.º 9
0
def test_create_object_graph():
    """
    Construct an object graph from bound factories and entry points.

    """
    graph = create_object_graph(
        name="test",
    )
    assert_that(graph.child, is_(instance_of(Child)))
    assert_that(graph.child.value, is_(equal_to("default_value")))
    assert_that(graph.parent, is_(instance_of(Parent)))
    assert_that(graph.parent.child, is_(equal_to(graph.child)))
    assert_that(graph.hello_world, is_(equal_to("hello world")))
Ejemplo n.º 10
0
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))
Ejemplo n.º 11
0
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))
Ejemplo n.º 12
0
def test_object_graph_use():
    """
    Explicitly create a set of components.

    """
    graph = create_object_graph(name="test", )
    parent, _ = graph.use("parent", "hello_world")
    assert_that(graph.get('parent'), is_(instance_of(Parent)))
    assert_that(parent, is_(instance_of(Parent)))
    assert_that(
        list(graph.items()),
        has_items(
            contains("parent", is_(instance_of(Parent))),
            contains("child", is_(instance_of(Child))),
            contains("hello_world", is_(equal_to("hello world"))),
        ))
Ejemplo n.º 13
0
def test_object_graph_locking():
    """
    Locking and unlocking a graph effects its ability to create components.

    """
    graph = create_object_graph(name="test", )
    [parent] = graph.use("parent")
    graph.lock()

    # can access already created object
    assert_that(graph.parent, is_(equal_to(parent)))
    # cannot create a new object
    assert_that(
        calling(graph.use).with_args("hello_world"), raises(LockedGraphError))

    graph.unlock()

    # can create an object again
    [hello_world] = graph.use("hello_world")
    assert_that(hello_world, is_(equal_to("hello world")))
Ejemplo n.º 14
0
def test_object_graph_locking():
    """
    Locking and unlocking a graph effects its ability to create components.

    """
    graph = create_object_graph(
        name="test",
    )
    [parent] = graph.use("parent")
    graph.lock()

    # can access already created object
    assert_that(graph.parent, is_(equal_to(parent)))
    # cannot create a new object
    assert_that(calling(graph.use).with_args("hello_world"), raises(LockedGraphError))

    graph.unlock()

    # can create an object again
    [hello_world] = graph.use("hello_world")
    assert_that(hello_world, is_(equal_to("hello world")))
Ejemplo n.º 15
0
def graph():
    return create_object_graph(name="example", testing=True)