def test_assignment_variable_args(): input_1 = basic.Place(value=1) input_2 = basic.Place(value=2) output = basic.Place() assignment = basic.assign(output, operator.add, input_1, input_2) assignment() assert output.value == 3 input_2.value = 3 assignment() assert output.value == 4
def test_assignment_tuple_arg(): input_1 = basic.Place(value=1) input_2 = basic.Place(value=2) input_3 = basic.Place(value=3) output = basic.Place() assignment = basic.assign(output, sum, (input_1, input_2, input_3)) assignment() assert output.value == 6 input_3.value = 4 assignment() assert output.value == 7
def test_assignment_coroutine_function(): input_1 = basic.Place(value=1) input_2 = basic.Place(value=2) input_3 = basic.Place(value=3) output = basic.Place() assignment = basic.assign(output, async_sum, [input_1, input_2, input_3]) assert inspect.iscoroutinefunction(assignment) asyncio.run(assignment()) assert output.value == 6 input_3.value = 4 asyncio.run(assignment()) assert output.value == 7
def test_assignment_mixed_args(): input_1 = basic.Place(value=1) input_2 = basic.Place(value=2) output = basic.Place() # Here, we test all three types of args, a list, variables and # literals assignment = basic.assign(output, sum, [input_1, input_2, 3]) assignment() assert output.value == 6 input_2.value = 3 assignment() assert output.value == 7
def test_cell_setter_and_getter(): cell_1 = basic.Place(name="test_cell_1") assert cell_1.name == "test_cell_1" assert cell_1.value is None cell_1.value = 7 assert cell_1.value == 7 cell_2 = basic.Place(name="test_cell_2", value=1) assert cell_2.name == "test_cell_2" assert cell_2.value == 1 cell_2.value = -1 assert cell_2.value == -1
def test_assignment_str_literal_arg(): # This caused an infinite recursion because the test in the library # is for 'Sequence' when it should be for 'list' input_1 = "hello" output = basic.Place() assignment = basic.assign(output, len, input_1) assignment() assert output.value == 5
def test_single_dependency(): input_cell = basic.Place(name="input_cell", value=99) output_cell = basic.Place(name="output_cell") add = basic.assign(output_cell, sum, [input_cell]) graph = basic.DependencyGraph() basic.compute_sequential(graph) assert output_cell.value is None graph.add_dependency(add, input_cell) graph.add_dependency(output_cell, add) basic.compute_sequential(graph) assert output_cell.value == 99 input_cell.value = 87 basic.compute_sequential(graph) assert output_cell.value == 87
def test_singleton_cell(): graph = basic.DependencyGraph() singleton = basic.Place(name="singleton", value=1) computed_cells = [] singleton_wrapper = CellWrapper(singleton, computed_cells) assert graph.add_node(singleton_wrapper) is singleton_wrapper assert computed_cells == [] basic.compute_sequential(graph) assert computed_cells == [singleton]
def test_add_precedence(): # add_precedence(A, B) is a synonym for add_dependency(B, A) input_cell = basic.Place(name="input_cell", value=99) output_cell = basic.Place(name="output_cell") add = basic.assign(output_cell, sum, [input_cell]) graph = basic.DependencyGraph() basic.compute_sequential(graph) assert output_cell.value is None graph.add_precedence(input_cell, add) graph.add_precedence(add, output_cell) basic.compute_sequential(graph) assert output_cell.value == 99 input_cell.value = 87 basic.compute_sequential(graph) assert output_cell.value == 87
def test_two_dependencies(): input_cell_1 = basic.Place(name="input_cell_1", value=1) input_cell_2 = basic.Place(name="input_cell_2", value=2) child_cell_1 = basic.Place(name="child_cell_1") child_cell_2 = basic.Place(name="child_cell_2") output_cell = basic.Place(name="output_cell") add_1 = basic.assign(child_cell_1, sum, [input_cell_1, input_cell_2]) add_2 = basic.assign(child_cell_2, sum, [input_cell_1, input_cell_2]) add_3 = basic.assign(output_cell, sum, [child_cell_1, child_cell_2]) graph = basic.DependencyGraph() # No dependencies declared yet basic.compute_sequential(graph) assert output_cell.value is None wrappers = {} computed_order = [] def wrapper(cell): result = wrappers.get(cell, None) if not result: result = CellWrapper(cell, computed_order) wrappers[cell] = result return result # ====================================================================== # Add a dependency and make sure that the computation order is correct. graph.add_dependency(wrapper(add_1), wrapper(input_cell_1)) basic.compute_sequential(graph) assert len(computed_order) == 2 assert is_before(input_cell_1, add_1, computed_order) # ====================================================================== # Add a dependency and make sure that the computation order is correct. graph.add_dependency(wrapper(add_1), wrapper(input_cell_2)) computed_order.clear() assert len(computed_order) == 0 basic.compute_sequential(graph) assert len(computed_order) == 3 assert is_before(input_cell_1, add_1, computed_order) assert is_before(input_cell_2, add_1, computed_order) # ====================================================================== # Add more dependencies and make sure that the computation order is # correct. graph.add_dependency(wrapper(child_cell_1), wrapper(add_1)) graph.add_dependency(wrapper(add_2), wrapper(input_cell_1)) graph.add_dependency(wrapper(add_2), wrapper(input_cell_2)) graph.add_dependency(wrapper(child_cell_2), wrapper(add_2)) graph.add_dependency(wrapper(add_3), wrapper(child_cell_1)) graph.add_dependency(wrapper(add_3), wrapper(child_cell_2)) graph.add_dependency(wrapper(output_cell), wrapper(add_3)) computed_order.clear() assert len(computed_order) == 0 basic.compute_sequential(graph) assert len(computed_order) == 8 assert is_before(input_cell_1, add_1, computed_order) assert is_before(input_cell_2, add_1, computed_order) assert is_before(input_cell_1, add_2, computed_order) assert is_before(input_cell_2, add_2, computed_order) assert is_before(add_1, child_cell_1, computed_order) assert is_before(add_2, child_cell_2, computed_order) assert is_before(child_cell_1, add_3, computed_order) assert is_before(child_cell_2, add_3, computed_order) assert is_before(add_3, output_cell, computed_order) assert output_cell.value == 6 input_cell_2.value = 3 basic.compute_sequential(graph) assert output_cell.value == 8
def test_assignment_literal_args(): output = basic.Place() assignment = basic.assign(output, operator.add, 1, 2) assignment() assert output.value == 3