def test_exception_compute(): comp = Computation() comp.add_node('a', value=1) comp.add_node('b', lambda a: a / 0) comp.add_node('c', lambda b: b) comp.compute('c') assert comp['a'] == (States.UPTODATE, 1) assert comp.state('b') == States.ERROR assert comp.state('c') == States.STALE
def test_compute_with_unpicklable_object(): class Unpicklable(object): def __getstate__(self): raise Exception("UNPICKLABLE") comp = Computation() comp.add_node('a', value=Unpicklable()) comp.add_node('b', lambda a: None) comp.compute('b')
def test_error_stops_compute(): comp = Computation() comp.add_node('a') comp.add_node('b', lambda a: a / 0) comp.add_node('c', lambda b: b + 1) comp.insert('a', 1) comp.compute('c') assert comp['a'] == (States.UPTODATE, 1) assert comp.state('b') == States.ERROR assert comp.state('c') == States.STALE
def test_compute_multiple(): comp = Computation() comp.add_node('a', value=1) comp.add_node('b', lambda a: a + 1) comp.add_node('c', lambda a: 2 * a) comp.add_node('d', lambda b, c: b + c) comp.compute(['b', 'c']) assert comp.s.b == States.UPTODATE assert comp.s.c == States.UPTODATE assert comp.s.d != States.UPTODATE
def test_default_args(): comp = Computation() comp.add_node('x', value=1) def bar(x, some_default=0): return x + some_default comp.add_node('foo', bar) assert set(comp.i.foo) == {'x'} comp.compute('foo') assert comp.v.foo == 1
def test_with_uptodate_predecessors_but_stale_ancestors(): comp = Computation() comp.add_node('a', value=1) comp.add_node('b', lambda a: a + 1) comp.compute_all() assert comp['b'] == (States.UPTODATE, 2) comp.dag.nodes['a'][ 'state'] = States.UNINITIALIZED # This can happen due to serialization comp.add_node('c', lambda b: b + 1) comp.compute('c') assert comp['b'] == (States.UPTODATE, 2) assert comp['c'] == (States.UPTODATE, 3)
def test_state_map_with_adding_existing_node(): comp = Computation() comp.add_node('a', lambda: 1) assert comp._state_map[States.COMPUTABLE] == {'a'} assert comp._state_map[States.UPTODATE] == set() comp.compute('a') assert comp._state_map[States.COMPUTABLE] == set() assert comp._state_map[States.UPTODATE] == {'a'} comp.add_node('a', lambda: 1) assert comp._state_map[States.COMPUTABLE] == {'a'} assert comp._state_map[States.UPTODATE] == set() comp.compute('a') assert comp._state_map[States.COMPUTABLE] == set() assert comp._state_map[States.UPTODATE] == {'a'}
def test_basic(): def b(a): return a + 1 def c(a): return 2 * a def d(b, c): return b + c comp = Computation() comp.add_node("a") comp.add_node("b", b) comp.add_node("c", c) comp.add_node("d", d) assert comp.state('a') == States.UNINITIALIZED assert comp.state('c') == States.UNINITIALIZED assert comp.state('b') == States.UNINITIALIZED assert comp.state('d') == States.UNINITIALIZED assert comp._state_map[States.UNINITIALIZED] == set(['a', 'b', 'c', 'd']) comp.insert("a", 1) assert comp.state('a') == States.UPTODATE assert comp.state('b') == States.COMPUTABLE assert comp.state('c') == States.COMPUTABLE assert comp.state('d') == States.STALE assert comp.value('a') == 1 comp.compute_all() assert comp.state('a') == States.UPTODATE assert comp.state('b') == States.UPTODATE assert comp.state('c') == States.UPTODATE assert comp.state('d') == States.UPTODATE assert comp.value('a') == 1 assert comp.value('b') == 2 assert comp.value('c') == 2 assert comp.value('d') == 4 comp.insert("a", 2) comp.compute("b") assert comp.state('a') == States.UPTODATE assert comp.state('b') == States.UPTODATE assert comp.state('c') == States.COMPUTABLE assert comp.state('d') == States.STALE assert comp.value('a') == 2 assert comp.value('b') == 3 assert set(comp._get_calc_nodes("d")) == set(['c', 'd'])
def test_compute_fib_5(): n = 5 comp = Computation() def add(x, y): return x + y comp.add_node(0, value=1) comp.add_node(1, value=1) for i in range(2, n + 1): comp.add_node(i, add, kwds={'x': i - 2, 'y': i - 1}, inspect=False) comp.compute(n) assert comp.state(n) == States.UPTODATE
def test_raising_exception_compute_all(): comp = Computation() comp.add_node('a', value=1) comp.add_node('b', lambda a: a / 0) comp.add_node('c', lambda b: b) comp.compute('c', raise_exceptions=True)