Beispiel #1
0
def test_func_node_return_value():
    # no outputs
    def func():
        pass

    n = FuncNode(func)
    assert n() == ()

    # function explicitly returns None
    def func(a, b):
        return None

    n = FuncNode(func)
    assert n((1, 2)) == (None, )

    # single value output
    def func():
        return 1

    n = FuncNode(func)
    assert n() == (1, )

    def func():
        tup = (1, 2)
        return tup

    n = FuncNode(func)
    assert n() == ((1, 2), )

    # multi outputs
    def func():
        return 'a', 1

    n = FuncNode(func)
    assert n() == ('a', 1)
def test_func_node_output_names_are_unique():
    def func():
        a = 1
        return a, a, 2

    n = FuncNode(func)
    assert tuple(n.outputs()) == ("a1", "a2", "res")
Beispiel #3
0
def test_func_node_output_names_are_unique():
    def func():
        a = 1
        return a, a, 2

    n = FuncNode(func)
    assert tuple(n.outputs()) == ('a1', 'a2', 'res')
Beispiel #4
0
def test_func_node_no_args_or_kwds():
    def func1(a, *args):
        return None

    assert_raises(TypeError, lambda: FuncNode(func1))

    def func2(**kwds):
        return None

    assert_raises(TypeError, lambda: FuncNode(func2))
def test_func_node_return_none_differ_from_no_return():
    def func():
        return None

    n = FuncNode(func)
    assert tuple(n.outputs()) == ("None",)

    def func():
        print None

    n = FuncNode(func)
    assert tuple(n.outputs()) == ()
def test_func_node_args_name_detection():
    def func():
        return None

    n = FuncNode(func)
    assert tuple(n.inputs()) == ()

    def func(a, b):
        return None

    n = FuncNode(func)
    assert tuple(n.inputs()) == ("a", "b")
def test_func_node_return_type_detection():
    def func(a):
        return a

    n = FuncNode(func)
    assert n.output("a").type is None

    def func(a):
        return a

    func.__rettypes__ = ["IInt"]
    n = FuncNode(func)
    assert n.output("a").type == "IInt"

    @rettype("IInt")
    def func(a):
        return a

    n = FuncNode(func)
    assert n.output("a").type == "IInt"

    @rettype("IInt", "IFloat")
    def func(a, b):
        return a, b

    n = FuncNode(func)
    assert n.output("a").type == "IInt"
    assert n.output("b").type == "IFloat"
def test_func_node_args_type_detection():
    def func(a):
        return a

    n = FuncNode(func)
    assert n.input("a").type is None

    def func(a):
        return a

    func.__argtypes__ = ["IInt"]
    n = FuncNode(func)
    assert n.input("a").type == "IInt"

    @argtype("IInt")
    def func(a):
        return a

    n = FuncNode(func)
    assert n.input("a").type == "IInt"

    @argtype("IInt", "IFloat")
    def func(a, b):
        return a + b

    n = FuncNode(func)
    assert n.input("a").type == "IInt"
    assert n.input("b").type == "IFloat"
Beispiel #9
0
def test_evaluation_do_not_reevaluate_same_node():
    visited = []

    def func():
        visited.append("yes")

    pg = PortGraph()
    n = FuncNode(func)
    pg.add_actor(n, 0)
    pg.add_actor(n, 1)

    env = EvaluationEnvironment()
    algo = BruteEvaluation(pg)

    ws = WorkflowState(pg)
    algo.eval(env, ws, 0)
    assert len(visited) == 1

    algo.eval(env, ws, 1)
    assert len(visited) == 2

    algo.eval(env, ws, 0)
    assert len(visited) == 2

    env.new_execution()
    algo.eval(env, ws, 0)
    assert len(visited) == 3
Beispiel #10
0
def test_func_node_inline():
    def tata(a, b):
        c = a + b
        return c

    n = FuncNode(tata)
    assert n((1, 2)) == (3, )
Beispiel #11
0
def test_evaluation_propagated_upstream():
    visited = []

    def func(txt):
        visited.append(txt)
        return txt

    pg = PortGraph()
    n = FuncNode(func)
    pg.add_actor(n, 0)
    pg.add_actor(n, 1)
    pg.connect(pg.out_port(0, 'txt'), pg.in_port(1, 'txt'))

    algo = BruteEvaluation(pg)

    ws = WorkflowState(pg)
    env = EvaluationEnvironment()
    ws.store_param(pg.in_port(0, 'txt'), "txt", 0)

    algo.eval(env, ws, 0)
    assert len(visited) == 1
    algo.eval(env, ws, 1)
    assert len(visited) == 2

    env.new_execution()
    algo.eval(env, ws, 1)
    assert len(visited) == 4
Beispiel #12
0
def test_evaluation_needs_ready_to_evaluate_state():
    def func(a, b):
        c = a + b
        return c

    pg = PortGraph()
    n = FuncNode(func)
    pg.add_actor(n, 0)

    algo = BruteEvaluation(pg)

    ws = WorkflowState(pg)
    assert_raises(EvaluationError, lambda: algo.eval(None, ws))
Beispiel #13
0
def test_func_node_return_outputs():
    def func():
        return 1

    n = FuncNode(func)
    assert tuple(n.outputs()) == ('res', )

    def func():
        a = 1
        return a

    n = FuncNode(func)
    assert tuple(n.outputs()) == ('a', )

    def func():
        a = 1
        return a, 1

    n = FuncNode(func)
    assert tuple(n.outputs()) == ('a', 'res')
Beispiel #14
0
def test_func_node_args_name_detection():
    def func():
        return None

    n = FuncNode(func)
    assert tuple(n.inputs()) == ()

    def func(a, b):
        return None

    n = FuncNode(func)
    assert tuple(n.inputs()) == ('a', 'b')
Beispiel #15
0
def test_func_node_return_none_differ_from_no_return():
    def func():
        return None

    n = FuncNode(func)
    assert tuple(n.outputs()) == ('None', )

    def func():
        print None

    n = FuncNode(func)
    assert tuple(n.outputs()) == ()
Beispiel #16
0
def test_lazy_evaluate_at_least_once_each_node():
    evaluated = []

    def func():
        evaluated.append('bla')

    pg = PortGraph()
    vid = pg.add_actor(FuncNode(func))
    assert pg.actor(vid).is_lazy()

    algo = LazyEvaluation(pg)
    env = EvaluationEnvironment()
    ws = WorkflowState(pg)

    algo.eval(env, ws)
    assert len(evaluated) == 1
    algo.eval(env, ws)
    assert len(evaluated) == 1
Beispiel #17
0
def test_evaluation_eval_all_nodes():
    visited = []

    def func():
        visited.append("yes")

    pg = PortGraph()
    n = FuncNode(func)
    pg.add_actor(n, 0)
    pg.add_actor(n, 1)

    algo = BruteEvaluation(pg)

    env = EvaluationEnvironment()
    ws = WorkflowState(pg)
    algo.eval(env, ws)

    assert len(visited) == 2
Beispiel #18
0
def test_evaluation_clear():
    def func():
        pass

    pg = PortGraph()
    n = FuncNode(func)
    pg.add_actor(n, 0)
    pg.add_actor(n, 1)

    algo = BruteEvaluation(pg)

    env = EvaluationEnvironment()
    ws = WorkflowState(pg)
    algo.eval(env, ws)

    assert not algo.requires_evaluation(env, ws)

    ws.clear()

    assert algo.requires_evaluation(env, ws)
Beispiel #19
0
def test_lazy_do_not_reevaluate_node_if_same_execution():
    evaluated = []

    def func(txt):
        evaluated.append(txt)
        return txt

    pg = PortGraph()
    vid = pg.add_actor(FuncNode(func))
    assert pg.actor(vid).is_lazy()

    algo = LazyEvaluation(pg)
    env = EvaluationEnvironment()
    ws = WorkflowState(pg)
    ws.store_param(pg.in_port(vid, 'txt'), 'toto', env.current_execution())

    algo.eval(env, ws)
    assert len(evaluated) == 1

    algo.eval_node(env, ws, vid)
    assert len(evaluated) == 1
Beispiel #20
0
def test_lazy_always_reevaluate_non_lazy_nodes():
    evaluated = []

    def func(txt):
        evaluated.append(txt)
        return txt

    pg = PortGraph()
    vid = pg.add_actor(FuncNode(func))
    pg.actor(vid).set_lazy(False)

    algo = LazyEvaluation(pg)
    env = EvaluationEnvironment()
    ws = WorkflowState(pg)
    ws.store_param(pg.in_port(vid, 'txt'), 'toto', env.current_execution())

    algo.eval(env, ws)
    assert len(evaluated) == 1

    env.new_execution()
    algo.eval(env, ws)
    assert len(evaluated) == 2
def test_func_node_return_outputs():
    def func():
        return 1

    n = FuncNode(func)
    assert tuple(n.outputs()) == ("res",)

    def func():
        a = 1
        return a

    n = FuncNode(func)
    assert tuple(n.outputs()) == ("a",)

    def func():
        a = 1
        return a, 1

    n = FuncNode(func)
    assert tuple(n.outputs()) == ("a", "res")
Beispiel #22
0
def test_func_node_return_type_detection():
    def func(a):
        return a

    n = FuncNode(func)
    assert n.output('a').type is None

    def func(a):
        return a

    func.__rettypes__ = ["IInt"]
    n = FuncNode(func)
    assert n.output('a').type == "IInt"

    @rettype("IInt")
    def func(a):
        return a

    n = FuncNode(func)
    assert n.output('a').type == "IInt"

    @rettype("IInt", "IFloat")
    def func(a, b):
        return a, b

    n = FuncNode(func)
    assert n.output('a').type == "IInt"
    assert n.output('b').type == "IFloat"
Beispiel #23
0
def test_func_node_args_type_detection():
    def func(a):
        return a

    n = FuncNode(func)
    assert n.input('a').type is None

    def func(a):
        return a

    func.__argtypes__ = ["IInt"]
    n = FuncNode(func)
    assert n.input('a').type == "IInt"

    @argtype("IInt")
    def func(a):
        return a

    n = FuncNode(func)
    assert n.input('a').type == "IInt"

    @argtype("IInt", "IFloat")
    def func(a, b):
        return a + b

    n = FuncNode(func)
    assert n.input('a').type == "IInt"
    assert n.input('b').type == "IFloat"
Beispiel #24
0
def test_evaluation_affect_output_to_right_ports():
    def func(a, b):
        c = a + b
        d = a * 2
        return c, d

    # simple order
    pg = PortGraph()
    n = FuncNode(func)
    pg.add_vertex(0)
    pg.add_in_port(0, 'a', 0)
    pg.add_in_port(0, 'b', 1)
    pg.add_out_port(0, 'c', 2)
    pg.add_out_port(0, 'd', 3)

    pg.set_actor(0, n)

    algo = BruteEvaluation(pg)
    env = EvaluationEnvironment()
    ws = WorkflowState(pg)
    ws.store_param(0, 'a', 0)
    ws.store_param(1, 'b', 0)
    algo.eval(env, ws)

    assert ws.get(2) == 'ab'
    assert ws.get(3) == 'aa'

    # reverse input orders
    pg = PortGraph()
    n = FuncNode(func)
    pg.add_vertex(0)
    pg.add_in_port(0, 'b', 0)
    pg.add_in_port(0, 'a', 1)
    pg.add_out_port(0, 'c', 2)
    pg.add_out_port(0, 'd', 3)

    pg.set_actor(0, n)

    algo = BruteEvaluation(pg)
    env = EvaluationEnvironment()
    ws = WorkflowState(pg)
    ws.store_param(0, 'a', 0)
    ws.store_param(1, 'b', 0)
    algo.eval(env, ws)

    assert ws.get(2) == 'ba'
    assert ws.get(3) == 'bb'

    # reverse output order
    pg = PortGraph()
    n = FuncNode(func)
    pg.add_vertex(0)
    pg.add_in_port(0, 'a', 0)
    pg.add_in_port(0, 'b', 1)
    pg.add_out_port(0, 'd', 2)
    pg.add_out_port(0, 'c', 3)

    pg.set_actor(0, n)

    algo = BruteEvaluation(pg)
    env = EvaluationEnvironment()
    ws = WorkflowState(pg)
    ws.store_param(0, 'a', 0)
    ws.store_param(1, 'b', 0)
    algo.eval(env, ws)

    assert ws.get(3) == 'ab'
    assert ws.get(2) == 'aa'