def test_while_break(self): def f(x): while x: if True: break return x with graph_factory(): expected = G( C(START_TICK, "x", 1, "$test"), C(START_TICK, "x", 1, "x"), T( 1, tasks.WhileTask( False, False, G( "body", C(START_TICK, "x", 2, "$test"), C(START_TICK, "x", 2, "x"), T(1, tasks.ConstTask(True), {'quick': True}), C(1, "value", 2, "$breaked"), T( 2, tasks.WhileTask(True, True, G("body"), G("else"))), ), G("else"))), C(START_TICK, "x", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_for_break(self): def f(lst, x): for x in lst: if True: break return x with graph_factory(): expected = G( C(START_TICK, "lst", 1, "iterable"), T(1, tasks.IterTask(), {'quick': True}), C(1, "value", 2, "$iterator"), C(START_TICK, "x", 2, "x"), T( 2, tasks.ForTask( False, False, G( "body", T(1, tasks.ConstTask(True), {'quick': True}), C(1, "value", 2, "$breaked"), C(START_TICK, "$target", 2, "x"), C(START_TICK, "$iterator", 2, "$iterator"), T(2, tasks.ForTask(True, True, G("body"), G("else"))), C(2, "x", FINAL_TICK, "x")), G("else"))), C(2, "x", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_task_by_property(self): pkg = PackageSource('testpkg', 'pkgfile', 'NONE') exec_task1 = ExecTask("exec_name1", pkg, ('a', ), ('b', )) exec_task2 = ExecTask("exec_name2", pkg, ('a', ), ('b', )) exec_task3 = ExecTask("exec_name3", pkg, ('a', ), ('b', )) graph = G( C(START_TICK, 'a', 1, 'a'), C(START_TICK, 'context', 1, 'context'), T(1, exec_task1, { 'name': 'test_exec1', 'path': 'test_exec1', 'prop1': 'a' }), T(2, exec_task2, { 'name': 'test_exec2', 'path': 'test_exec2', 'prop1': 'a' }), T(3, exec_task3, { 'name': 'test_exec3', 'path': 'test_exec3', 'prop1': 'b' }), C(1, 'b', FINAL_TICK, 'b'), ) self.assertEquals([Tick.parse_tick(1)], get_ticks_by_property(graph, 'name', 'test_exec1')) self.assertEquals( [Tick.parse_tick(1), Tick.parse_tick(2)], get_ticks_by_property(graph, 'prop1', 'a')) self.assertEquals([], get_ticks_by_property(graph, 'prop2', 'a'))
def test_unassigned_out(self): subgraph = G( C(START_TICK, "sin", 1, "x"), T(1, "subtask"), C(1, "y", FINAL_TICK, "sout") ) g = G( C(START_TICK, "in", 1, "sin"), C(START_TICK, "sout2", 1, "sout2"), T(1, "task_to_replace"), C(1, "sout", FINAL_TICK, "out"), C(1, "sout2", FINAL_TICK, "out2") ) expected = G( C(START_TICK, "in", (1,1), "x"), T((1,1), "subtask"), C((1,1), "y", FINAL_TICK, "out"), C(START_TICK, "sout2", FINAL_TICK, "out2") ) refine.replace_task(g, START_TICK + 1, subgraph) utils.assert_graph_equal(expected, g)
def test_while(self): def f(x): while x: pass return x with graph_factory(): expected = G( C(START_TICK, "x", 1, "$test"), C(START_TICK, "x", 1, "x"), T( 1, tasks.WhileTask( False, False, G( "body", C(START_TICK, "x", 1, "$test"), C(START_TICK, "x", 1, "x"), T( 1, tasks.WhileTask(True, False, G("body"), G("else"))), ), G("else"))), C(START_TICK, "x", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_get_task_state_waiting_for_inputs(self): g = G( T(1, tasks.ConstTask(None)), C(1, "value", 2, "in"), T(2, "task"), C(2, "out", FINAL_TICK, "retval") ) self.target.execute(g, {}) self.assertEqual(traverser.TaskState.WAITING_FOR_INPUTS, self.target.get_task_state(TICK2))
def test_injest_result(self): g = G( T(1, tasks.ConstTask(None)), C(1, "value", 2, "in"), T(2, "task"), C(2, "out", FINAL_TICK, "retval") ) self.target.execute(g, {}) self.next_ready()[-1].callback(traverser.EvalResult({"value":"Hello"})) self.assertEqual((TICK2, "task", {"in":"Hello"}), self.next_ready()[1:-1])
def test_get_task_state_evaluated(self): g = G( T(1, tasks.ConstTask(None)), C(1, "value", 2, "in"), T(2, "task"), C(2, "out", FINAL_TICK, "retval") ) self.target.execute(g, {}) self.next_ready()[-1].callback(traverser.EvalResult({"value":"Hello"})) self.assertEqual(traverser.TaskState.EVALUATED, self.target.get_task_state(TICK1))
def test_get_task_state_waiting_for_refiner(self): task = MockTask("in") g = G( T(1, tasks.ConstTask(None)), C(1, "value", 2, "in"), T(2, task), C(2, "out", FINAL_TICK, "retval") ) self.target.execute(g, {"a": "refinedata"}) self.assertEqual(traverser.TaskState.WAITING_FOR_REFINE_INPUTS, self.target.get_task_state(TICK2))
def test_call_builtin(self): def f(): return __pydron_new_cell__("x") #@UndefinedVariable expected = G( T(1, tasks.ConstTask("x"), {'quick': True}), C(1, "value", 2, "arg0"), T(2, tasks.BuiltinCallTask(builtins.__pydron_new_cell__, 1), {'quick': True}), C(2, "value", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_readglobal(self): def f(): return __pydron_read_global__( "TestTranslator") #@UndefinedVariable expected = G(T(1, tasks.ConstTask("TestTranslator"), {'quick': True}), C(1, "value", 2, "var"), T(2, tasks.ReadGlobal(__name__), {'quick': True}), C(2, "value", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_mocking(self): self.target.add_task(START_TICK + 100, "task1", {'nicename':'test1'}) self.target.add_task(START_TICK + 101, "task2", {'nicename':'test2'}) self.target.connect(graph.Endpoint(START_TICK + 100, 'out'), graph.Endpoint(START_TICK + 101, 'in')) expected = G( T(100, 'task1', {'nicename': 'test1'}), C(100, 'out', 101, 'in'), T(101, 'task2', {'nicename': 'test2'}), ) utils.assert_graph_equal(expected, self.target)
def test_raise(self): def f(x): raise x return x expected = G(T(1, tasks.ConstTask(None), {'quick': True}), C(1, "value", 2, "inst"), C(START_TICK, "x", 2, "type"), C(1, "value", 2, "tback"), T(2, tasks.RaiseTask(), {'quick': True}), C(START_TICK, "x", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_refine_body_break(self): # def f(lst, x): # for x in lst: # if True: # break # return x with graph_factory(): g = G( C(START_TICK, "lst", 1, "iterable"), T(1, tasks.IterTask()), C(1, "value", 2, "$iterator"), C(START_TICK, "x", 2, "x"), T(2, tasks.ForTask(False, False, G("body", T(1, tasks.ConstTask(True)), C(1, "value", 2, "$breaked"), C(START_TICK, "$target",2 , "x"), C(START_TICK, "$iterator", 2, "$iterator"), T(2, tasks.ForTask(True, True, G("body"), G("else"))), C(2, "x", FINAL_TICK, "x") ), G("else"))), C(2, "x", FINAL_TICK, "retval") ) target = g.get_task(START_TICK + 2) target.refine(g, START_TICK + 2, {"$iterator":iter([1,2,3])}) with graph_factory(): expected = G( C(START_TICK, "lst", 1, "iterable"), T(1, tasks.IterTask()), C(1, "value", (2,1,2,2), "$iterator"), T((2,1,1), tasks.ConstTask(1)), T((2,1,2,1), tasks.ConstTask(True)), C((2,1,1), "value", (2,1,2,2), "x"), C((2,1,2,1), "value", (2,1,2,2), "$breaked"), T((2,1,2,2), tasks.ForTask(True, True, G("body", T(1, tasks.ConstTask(True)), C(1, "value", 2, "$breaked"), C(START_TICK, "$target",2 , "x"), C(START_TICK, "$iterator", 2, "$iterator"), T(2, tasks.ForTask(True, True, G("body"), G("else"))), C(2, "x", FINAL_TICK, "x") ), G("else"))), C((2,1,2,2), "x", FINAL_TICK, "retval") ) utils.assert_graph_equal(expected, g)
def test_simple_task(self): """ A simple one-step pipeline """ def testpipe(x,y): u,v=test_exec(a=x, b=y) return u,v graph=build_graph(testpipe) self.assertTrue(isinstance(graph,Graph)) # there is just a single task - hence just a single node / tick self.assertEqual(1, len(graph.get_all_ticks())) task=graph.get_task(graph.get_all_ticks()[0]) self.assertTrue(isinstance(task,ExecTask)) self.assertEqual('test_exec',task.command) self.assertEqual(set(['a','b']),set(task.inputnames)) self.assertEqual(set(['c','d']),set(task.outputnames)) expected = G( C(START_TICK, 'a', 1,'a'), C(START_TICK, 'b', 1,'b'), C(START_TICK, 'context', 1,'context'), T(1, task, {'name': 'test_exec', 'path':'test_exec'}), C(1, 'c', FINAL_TICK,'test_exec.c'), C(1, 'd', FINAL_TICK,'test_exec.d') ) expected.set_task_property(FINAL_TICK,'aliases', {'test_exec.c':'test_exec.c', 'test_exec.d':'test_exec.d'}) utils.assert_graph_equal(expected, graph)
def test_nested(self): graph = build_graph(testpipe_nested) # there are two tasks self.assertEqual(1, len(graph.get_all_ticks())) nested=graph.get_task(graph.get_all_ticks()[0]) self.assertTrue(isinstance(nested,NestedGraphTask)) self.assertEqual('test_task_nested',nested.name) task1=ExecTask('test_exec', Package('test'), ['a','b'], ['c','d']) task2=ExecTask('test_exec', Package('test'), ['a','b'], ['c','d']) subgraph = G( C(START_TICK, 'a', 1,'a'), C(START_TICK, 'b', 1,'b'), C(START_TICK, 'context', 1,'context'), C(START_TICK, 'context', 2,'context'), T(1, task1, {'name': 'test_exec', 'path':'test_exec'}), C(1, 'c', 2, 'a'), C(1, 'd', 2, 'b'), T(2, task2, {'name': 'test_exec2', 'path':'test_exec2'}), C(1, 'c', FINAL_TICK,'test_exec.c'), C(2, 'c', FINAL_TICK,'test_exec2.c'), C(2, 'd', FINAL_TICK,'test_exec2.d') ) subgraph.set_task_property(FINAL_TICK,'aliases', {'test_exec.c':'test_exec.c', 'test_exec2.c':'test_exec2.c', 'test_exec2.d':'test_exec2.d'}) utils.assert_graph_equal(subgraph, nested.body_graph) expected = G( C(START_TICK, 'x', 1,'x'), C(START_TICK, 'y', 1,'y'), C(START_TICK, 'context', 1,'context'), T(1, nested, {'name': 'test_task_nested', 'path':'test_task_nested'}), C(1, 'test_exec.c', FINAL_TICK,'test_task_nested.test_exec.c'), C(1, 'test_exec2.c', FINAL_TICK,'test_task_nested.test_exec2.c'), C(1, 'test_exec2.d', FINAL_TICK,'test_task_nested.test_exec2.d') ) expected.set_task_property(FINAL_TICK,'aliases', {'test_task_nested.test_exec.c':'test_task_nested.test_exec.c', 'test_task_nested.test_exec2.c':'test_task_nested.test_exec2.c', 'test_task_nested.test_exec2.d':'test_task_nested.test_exec2.d'}) utils.assert_graph_equal(expected, graph)
def test_no_inputs_task_ready(self): g = G( T(1, tasks.ConstTask(None)), C(1, "value", FINAL_TICK, "retval") ) self.target.execute(g, {}) self.assertEqual((TICK1, tasks.ConstTask(None), {}), self.next_ready()[1:-1]) self.assertEqual(None, self.next_ready())
def test_inline(self): def testpipe(x,y): return test_task(x=x,y=y) def test_task(x,y): u,v=test_exec(a=x, b=y) w,z=test_exec(name="test_exec2", a=u, b=v) return u,w,z graph = build_graph(testpipe) # there are two tasks self.assertEqual(2, len(graph.get_all_ticks())) ticks=sorted(graph.get_all_ticks()) task1=graph.get_task(ticks[0]) task2=graph.get_task(ticks[1]) self.assertTrue(isinstance(task1,ExecTask)) self.assertEqual('test_exec',task1.command) self.assertEqual(set(['a','b']),set(task1.inputnames)) self.assertEqual(set(['c','d']),set(task1.outputnames)) self.assertTrue(isinstance(task2,ExecTask)) self.assertEqual('test_exec',task2.command) self.assertEqual(set(['a','b']),set(task2.inputnames)) self.assertEqual(set(['c','d']),set(task2.outputnames)) expected = G( C(START_TICK, 'a', 1,'a'), C(START_TICK, 'b', 1,'b'), C(START_TICK, 'context', 1,'context'), C(START_TICK, 'context', 2,'context'), T(1, task1, {'name': 'test_exec', 'path':'test_exec'}), C(1, 'c', 2, 'a'), C(1, 'd', 2, 'b'), T(2, task2, {'name': 'test_exec2', 'path':'test_exec2'}), C(1, 'c', FINAL_TICK,'test_exec.c'), C(2, 'c', FINAL_TICK,'test_exec2.c'), C(2, 'd', FINAL_TICK,'test_exec2.d') ) expected.set_task_property(FINAL_TICK,'aliases', {'test_exec.c':'test_exec.c', 'test_exec2.c':'test_exec2.c', 'test_exec2.d':'test_exec2.d'}) utils.assert_graph_equal(expected, graph)
def test_str(self): def f(): return "Hello World!" expected = G(T(1, tasks.ConstTask("Hello World!"), {'quick': True}), C(1, "value", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_eval_not_before_refine(self): task = MockTask("in") g = G( C(START_TICK, "a", 1, "in"), T(1, task), C(1, "out", FINAL_TICK, "retval") ) self.target.execute(g, {"a": "refinedata"}) self.assertEqual(None, self.next_ready())
def test_nested_FunctionDef(self): def f(): def g(): return None return g expected = G( T( 1, tasks.FunctionDefTask( "scheduler", 'g', [], None, None, 0, G(T(1, tasks.ConstTask(None), {'quick': True}), C(1, "value", FINAL_TICK, "retval"))), {'quick': True}), C(1, "function", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_nested_task(self): pkg = PackageSource('testpkg', 'pkgfile', 'NONE') exec_task = ExecTask("exec_name", pkg, ('a', ), ('b', )) subgraph = G( C(START_TICK, 'a', 1, 'a'), C(START_TICK, 'context', 1, 'context'), T(1, exec_task, { 'name': 'test_exec', 'path': 'test_exec' }), C(1, 'b', FINAL_TICK, 'b'), ) subgraph_task_tick = subgraph.get_all_ticks()[0] graph = Graph() task = NestedGraphTask(subgraph, 'test_nested') tick = START_TICK + 1 graph.add_task(tick, task, { 'name': 'test_nested', 'path': 'test_nested' }) source = Endpoint(START_TICK, 'a') dest = Endpoint(tick, 'a') graph.connect(source, dest) source = Endpoint(START_TICK, 'context') dest = Endpoint(tick, 'context') graph.connect(source, dest) source = Endpoint(tick, 'b') dest = Endpoint(FINAL_TICK, 'b') graph.connect(source, dest) task.refine(graph, Tick.parse_tick(1), {'context': {}, 'a': {}}) expected_graph_task_tick = subgraph_task_tick << Tick.parse_tick(1) expected = G( C(START_TICK, 'a', expected_graph_task_tick, 'a'), C(START_TICK, 'context', expected_graph_task_tick, 'context'), T(expected_graph_task_tick, exec_task, { 'name': 'test_exec', 'path': 'test_nested.test_exec' }), C(expected_graph_task_tick, 'b', FINAL_TICK, 'b'), ) utils.assert_graph_equal(expected, graph)
def test_finish(self): g = G( T(1, tasks.ConstTask(None)), C(1, "value", FINAL_TICK, "retval") ) d = self.target.execute(g, {}) self.next_ready()[-1].callback(traverser.EvalResult({"value":"Hello"})) outputs = extract(d) self.assertEqual({"retval":"Hello"}, outputs)
def test_get_task_state_refining(self): task = MockTask("in") g = G( C(START_TICK, "a", 1, "in"), T(1, task), C(1, "out", FINAL_TICK, "retval") ) self.target.execute(g, {"a": "refinedata"}) self.assertEqual(traverser.TaskState.REFINING, self.target.get_task_state(TICK1))
def test_finish_nomoretasks(self): g = G( T(1, tasks.ConstTask(None)), C(1, "value", FINAL_TICK, "retval") ) self.target.execute(g, {}) self.next_ready()[-1].callback({"value":"Hello"}) self.assertIsNone(self.next_ready())
def test_attribute(self): def f(a): return a.myattr expected = G(C(START_TICK, "a", 1, "object"), T(1, tasks.AttributeTask("myattr"), {'quick': True}), C(1, "value", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_refine_called(self): task = MockTask("in") g = G( C(START_TICK, "a", 1, "in"), T(1, task), C(1, "out", FINAL_TICK, "retval") ) self.target.execute(g, {"a": "refinedata"}) self.assertEqual((TICK1, task, {"in": "refinedata"}), self.next_refine()[1:-1])
def test_unaryop(self): def f(a): return ~a expected = G(C(START_TICK, "a", 1, "value"), T(1, tasks.UnaryOpTask(ast.Invert()), {'quick': True}), C(1, "value", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_tuple(self): def f(v1, v2): return (v1, v2) expected = G(C(START_TICK, "v1", 1, "value_0"), C(START_TICK, "v2", 1, "value_1"), T(1, tasks.TupleTask(2), {'quick': True}), C(1, "value", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_slice(self): def f(a, b): return a[b] expected = G(C(START_TICK, "a", 1, "object"), C(START_TICK, "b", 1, "slice"), T(1, tasks.SubscriptTask(), {'quick': True}), C(1, "value", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)