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_refine(self): # t = True # if t: # x = 1 # else: # x = 2 # return x g = G( T(1, tasks.ConstTask(True)), C(1, "value", 2, "$test"), T(2, tasks.IfTask(G( T(1, tasks.ConstTask(1)), C(1, "value", FINAL_TICK, "x") ), G( T(1, tasks.ConstTask(2)), C(1, "value", FINAL_TICK, "x") ))), C(2, "x", FINAL_TICK, "retval") ) target = g.get_task(START_TICK + 2) target.refine(g, START_TICK + 2, {"$test":True}) expected = G( T(1, tasks.ConstTask(True)), T((2,1), tasks.ConstTask(1)), C((2,1), "value", 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_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_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_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_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_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_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_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_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_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_call_kwargs(self): def f(a, b): return a(**b) expected = G( C(START_TICK, "a", 1, "func"), C(START_TICK, "b", 1, "kwargs"), T(1, tasks.CallTask(0, [], False, True), {'syncpoint': True}), C(1, "value", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_compare(self): def f(a, b): return a == b expected = G(C(START_TICK, "a", 1, "left"), C(START_TICK, "b", 1, "right"), T(1, tasks.BinOpTask(ast.Eq()), {'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)
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_tuple_assign(self): def f(x): a, b, c = x return c expected = G(C(START_TICK, "x", 1, "value"), T(1, tasks.UnpackTask(3), {'quick': True}), C(1, "2", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_refine_only_once(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.next_refine()[-1].callback(None) self.assertEqual(None, self.next_refine())
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_call_karg(self): def f(a, b, c): return a(foo=b, bar=c) expected = G( C(START_TICK, "a", 1, "func"), C(START_TICK, "b", 1, "karg_0"), C(START_TICK, "c", 1, "karg_1"), T(1, tasks.CallTask(0, ['foo', 'bar'], False, False), {'syncpoint': True}), C(1, "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_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_dict(self): def f(k1, v1, k2, v2): return {k1: v1, k2: v2} expected = G(C(START_TICK, "k1", 1, "key_0"), C(START_TICK, "v1", 1, "value_0"), C(START_TICK, "k2", 1, "key_1"), C(START_TICK, "v2", 1, "value_1"), T(1, tasks.DictTask(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_augassign_attr(self): def f(a, b): a.myattr += b return a expected = G( C(START_TICK, "a", 1, "target"), C(START_TICK, "b", 1, "value"), T(1, tasks.AugAttrAssignTask(ast.Add(), "myattr"), { 'quick': True, 'syncpoint': True }), C(START_TICK, "a", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_attribute_assign(self): def f(x, y): x.myattribute = y return x expected = G( C(START_TICK, "x", 1, "object"), C(START_TICK, "y", 1, "value"), T(1, tasks.AttrAssign("myattribute"), { 'quick': True, 'syncpoint': True }), 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_subscript_assign(self): def f(x, y, z): x[y] = z return x expected = G( C(START_TICK, "x", 1, "object"), C(START_TICK, "y", 1, "slice"), C(START_TICK, "z", 1, "value"), T(1, tasks.SubscriptAssign(), { 'quick': True, 'syncpoint': True }), C(START_TICK, "x", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def test_augassign_subscript(self): def f(a, b, i): a[i] += b return a expected = G( C(START_TICK, "a", 1, "target"), C(START_TICK, "b", 1, "value"), C(START_TICK, "i", 1, "slice"), T(1, tasks.AugSubscriptAssignTask(ast.Add()), { 'quick': True, 'syncpoint': True }), C(START_TICK, "a", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)