def test_add(): """ add a node to a Graph """ from arbiter.graph import Graph graph = Graph() assert_equals(graph.nodes, frozenset()) assert_equals(graph.roots, frozenset()) assert_false('foo' in graph) graph.add('foo') assert_equals(graph.nodes, frozenset(('foo',))) assert_equals(graph.roots, frozenset(('foo',))) assert_true('foo' in graph) assert_equals(graph.children('foo'), frozenset()) assert_equals(graph.parents('foo'), frozenset()) assert_false(graph.ancestor_of('foo', 'foo')) graph.add('bar', ('foo', 'baz')) assert_equals(graph.nodes, frozenset(('foo', 'bar', 'baz'))) assert_equals(graph.roots, frozenset(('foo',))) assert_true('foo' in graph) assert_true('bar' in graph) assert_true('baz' in graph) assert_equals(graph.children('foo'), frozenset(('bar',))) assert_equals(graph.children('bar'), frozenset()) assert_equals(graph.children('baz'), frozenset(('bar',))) assert_equals(graph.parents('foo'), frozenset()) assert_equals(graph.parents('bar'), frozenset(('foo', 'baz'))) assert_equals(graph.parents('baz'), frozenset()) assert_false(graph.ancestor_of('foo', 'foo')) assert_false(graph.ancestor_of('foo', 'bar')) assert_false(graph.ancestor_of('foo', 'baz')) assert_true(graph.ancestor_of('bar', 'foo')) assert_false(graph.ancestor_of('bar', 'bar')) assert_true(graph.ancestor_of('bar', 'baz')) assert_false(graph.ancestor_of('baz', 'foo')) assert_false(graph.ancestor_of('baz', 'bar')) assert_false(graph.ancestor_of('baz', 'baz')) assert_raises(ValueError, graph.add, 'baz', ('bar',)) assert_raises(ValueError, graph.add, 'ouroboros', ('ouroboros',)) assert_raises(ValueError, graph.add, 'foo') assert_equals(graph.nodes, frozenset(('foo', 'bar', 'baz'))) assert_equals(graph.roots, frozenset(('foo',)))
def test_naming(): """ Node names just need to be hashable. """ from arbiter.graph import Graph, Strategy graph = Graph() for name in (1, float('NaN'), 0, None, '', frozenset(), (), False, sum): graph.add('child1', frozenset((name,))) graph.add(name) assert_true(name in graph) assert_equals(graph.nodes, frozenset((name, 'child1'))) assert_equals(graph.roots, frozenset((name,))) assert_equals(graph.children(name), frozenset(('child1',))) assert_equals(graph.parents(name), frozenset()) assert_false(graph.ancestor_of(name, name)) graph.add('child2', frozenset((name,))) assert_equals( graph.children(name), frozenset(('child1', 'child2')) ) graph.remove(name) graph.remove('child1') graph.remove('child2') assert_equals(graph.nodes, frozenset()) graph.add(None) graph.add('', parents=((),)) graph.add((), parents=(frozenset(),)) graph.add(frozenset()) assert_equals(graph.nodes, frozenset((None, '', (), frozenset()))) assert_equals(graph.roots, frozenset((None, frozenset()))) graph.remove((), strategy=Strategy.remove) assert_equals(graph.nodes, frozenset((None, frozenset()))) assert_equals(graph.roots, frozenset((None, frozenset()))) assert_raises(TypeError, graph.add, []) assert_raises(TypeError, graph.add, 'valid', parents=([],))
def test_remove_promote(): """ Remove a node (transitively making its parents its children's parents) from a Graph. """ from arbiter.graph import Graph graph = Graph() graph.add('aye') graph.add('insect') graph.add('bee', ('aye', 'insect')) graph.add('cee', ('bee',)) graph.add('child', ('stub', 'stub2')) graph.add('grandchild', ('child',)) assert_equals( graph.nodes, frozenset( ( 'aye', 'insect', 'bee', 'cee', 'child', 'stub', 'stub2', 'grandchild', ) ) ) assert_equals(graph.roots, frozenset(('aye', 'insect'))) # two new parents assert_equals(graph.remove('bee'), frozenset(('bee',))) assert_equals( graph.nodes, frozenset( ( 'aye', 'insect', 'cee', 'child', 'stub', 'stub2', 'grandchild', ) ) ) assert_equals( graph.roots, frozenset(('aye', 'insect')) ) assert_equals(graph.children('aye'), frozenset(('cee',))) assert_equals(graph.children('insect'), frozenset(('cee',))) assert_equals(graph.parents('cee'), frozenset(('aye', 'insect'))) # now with stubs assert_equals(graph.remove('child'), frozenset(('child',))) assert_equals( graph.nodes, frozenset( ( 'aye', 'insect', 'cee', 'stub', 'stub2', 'grandchild', ) ) ) assert_equals( graph.roots, frozenset(('aye', 'insect')) ) assert_equals(graph.children('stub'), frozenset(('grandchild',))) assert_equals(graph.children('stub2'), frozenset(('grandchild',))) assert_equals( graph.parents('grandchild'), frozenset(('stub', 'stub2')) ) # delete a stub assert_equals(graph.remove('stub2'), frozenset(('stub2',))) assert_equals( graph.nodes, frozenset( ( 'aye', 'insect', 'cee', 'stub', 'grandchild', ) ) ) assert_equals( graph.roots, frozenset(('aye', 'insect')) ) assert_equals(graph.children('stub'), frozenset(('grandchild',))) assert_equals(graph.parents('grandchild'), frozenset(('stub',)))