def test_portgraph_set_actor(): pg = PortGraph() assert_raises(InvalidVertex, lambda: pg.set_actor(0, None)) actor = Node() for key in ("toto", 1, "titi"): actor.add_input(key, "descr") actor.add_output(key, "descr") assert_raises(InvalidVertex, lambda: pg.set_actor(0, actor)) vid = pg.add_vertex() pg.set_actor(vid, None) assert pg.actor(vid) is None assert_raises(InvalidPort, lambda: pg.set_actor(vid, actor)) for key in actor.inputs(): pg.add_in_port(vid, key) assert_raises(InvalidPort, lambda: pg.set_actor(vid, actor)) for key in actor.outputs(): pg.add_out_port(vid, key) pg.set_actor(vid, actor) assert pg.actor(vid) == actor pg.set_actor(vid, None) assert pg.actor(vid) is None
def test_portgraph_add_actor(): pg = PortGraph() vid1 = pg.add_vertex() actor = Node() keys = {"toto", 1, "titi"} for key in keys: actor.add_input(key, "descr") actor.add_output(key, "descr") # bad actor assert_raises(AttributeError, lambda: pg.add_actor(None)) assert len(pg) == 1 assert_raises(AttributeError, lambda: pg.add_actor(None, vid1 + 1)) assert len(pg) == 1 # vertex id already used assert_raises(InvalidVertex, lambda: pg.add_actor(actor, vid1)) assert len(pg) == 1 for key in actor.inputs(): pg.add_in_port(vid1, key) for key in actor.outputs(): pg.add_out_port(vid1, key) pg.set_actor(vid1, actor) # vertex id already used assert_raises(InvalidVertex, lambda: pg.add_actor(actor, vid1)) assert len(pg) == 1 vid2 = pg.add_actor(actor) assert pg.actor(vid2) == actor assert set(actor.inputs()) == keys assert set(pg.local_id(pid) for pid in pg.in_ports(vid2)) == keys assert set(actor.outputs()) == keys assert set(pg.local_id(pid) for pid in pg.out_ports(vid2)) == keys
def test_portgraph_add_vertex(): pg = PortGraph() vid = pg.add_vertex() assert pg.nb_vertices() == 1 assert_raises(InvalidVertex, lambda: pg.add_vertex(vid)) assert len(tuple(pg.ports(vid))) == 0 assert pg.actor(vid) is None
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_portgraph_set_actor_port_order_not_important(): pg = PortGraph() actor = Node() keys = ("toto", 1, "titi") for key in keys: actor.add_input(key, "descr") actor.add_output(key, "descr") vid = pg.add_vertex() for k in reversed(keys): pg.add_in_port(vid, k) pg.add_out_port(vid, k) pg.set_actor(vid, actor) assert pg.actor(vid) == actor
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
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