Ejemplo n.º 1
0
 def test_is_input_correct_for_dependent_variable():
     v1 = Variable("v1")
     op = mock_op("op")
     v2 = op.op(v1)
     assert not v2.isinput()
     v2 = op.op(1)
     assert not v2.isinput()
Ejemplo n.º 2
0
    def test_string_representation():
        mocked_function = MagicMock(__name__="function")
        mocked_op = op(mocked_function)
        variable = Variable(name="input")

        result = mocked_op.op(arg=variable)

        assert str(result) == "function(arg=input)"
Ejemplo n.º 3
0
    def test_call_returns_variable_if_some_arg_variable():
        operation = mock_op("op")
        input_var = Variable("input")
        return_var = operation(a=1, b=input_var)

        assert not operation._run.called
        assert isinstance(return_var, Variable)
        assert return_var.dependencies == {"b": input_var}
Ejemplo n.º 4
0
    def test_returns_variable_if_some_arg_variable():
        mocked_function = MagicMock(return_value="Return value")
        mocked_op = op(mocked_function)

        input_var = Variable("input")
        return_var = mocked_op(a=1, b=input_var)

        assert not mocked_function.called
        assert isinstance(return_var, Variable)
        assert return_var.dependencies == {"b": input_var}
Ejemplo n.º 5
0
def graph_raising():
    graph = lambda: None  # noqa: E731
    op0 = mock_op("op0", exception=ValueError("raised by op0"))
    graph.input = Variable("input")
    output0 = op0(arg=graph.input)
    op1 = mock_op("op1")
    output1 = op1(graph.input, arg1=output0)

    graph.output = [output0, output1]

    return graph
Ejemplo n.º 6
0
def graph():
    graph = lambda: None  # noqa: E731
    op0 = mock_op("op0")
    graph.input = Variable("input")
    output0 = op0(arg=graph.input)
    op1 = mock_op("op1")
    output1 = op1(graph.input, arg1=output0)

    graph.output = [output0, output1]

    return graph
Ejemplo n.º 7
0
    def test_req_update_func_called():
        operation = mock_op("op")

        var = Variable("input")
        res = operation(a=1, b=var)

        output_req = MockReq("Output requirement")
        reqs = solve_requirements(output_requirements={res: output_req})

        expected = {res: output_req, var: MockReq("Arg requirement")}

        assert reqs == expected
        res.op.arg_requirements.assert_called_once_with(output_req, "b")
Ejemplo n.º 8
0
class TestOpClass:
    @staticmethod
    def test_call_invokes_function_if_args_invariable():
        operation = mock_op("op")
        return_value = operation(a=1, b="b")

        operation._run.assert_called_once_with(a=1, b="b")
        assert return_value == "op_return_value"

    @staticmethod
    def test_call_returns_variable_if_some_arg_variable():
        operation = mock_op("op")
        input_var = Variable("input")
        return_var = operation(a=1, b=input_var)

        assert not operation._run.called
        assert isinstance(return_var, Variable)
        assert return_var.dependencies == {"b": input_var}

    @staticmethod
    def test_invocation_warns_on_variable_argument():
        operation = mock_op("op")
        with pytest.deprecated_call():
            operation(Variable("v"), 0)

    @staticmethod
    def test_returned_variable_has_correct_name():
        operation = mock_op("op")
        variable = Variable(name="input")
        result = operation.op(arg=variable)

        assert str(result) == "op(arg=input)"

    @staticmethod
    def test_invocation_warns_not_on_invariable_argument():
        operation = mock_op("op")
        with pytest.warns(None) as record:
            operation(0, 0)

        assert len(record) == 0

    @staticmethod
    @pytest.mark.parametrize("argument", [
        pytest.param(0, id="Non-variable argument"),
        pytest.param(Variable("v"), id="Variable argument")
    ])
    def test_op_method_always_returns_a_variable_instance(argument):
        operation = mock_op("op")
        result = operation.op(argument)

        assert isinstance(result, Variable)
Ejemplo n.º 9
0
 def test_is_input_correct_for_independent_variable():
     v1 = Variable("v1")
     assert v1.isinput()
Ejemplo n.º 10
0
    def test_str_is_correct_for_dependent_variable():
        v1 = Variable("v1")
        v2 = Variable("v2")
        v3 = mock_op("op").op(v1, kw1=v2)

        assert str(v3) == "op(v1, kw1=v2)"
Ejemplo n.º 11
0
    def test_str_is_correct_for_independent_variable():
        var = Variable("v1")

        assert str(var) == "v1"
Ejemplo n.º 12
0
 def test_validator_raises_if_independent_variable_is_not_given_a_name():
     with pytest.raises(ValueError):
         _ = Variable()
Ejemplo n.º 13
0
 def test_validator_raises_if_dependent_variable_is_given_a_name():
     with pytest.raises(ValueError):
         _ = Variable("name", dependencies={"a": Variable("name")})
Ejemplo n.º 14
0
    def test_returned_variable_has_correct_name():
        operation = mock_op("op")
        variable = Variable(name="input")
        result = operation.op(arg=variable)

        assert str(result) == "op(arg=input)"
Ejemplo n.º 15
0
 def test_invocation_warns_on_variable_argument():
     operation = mock_op("op")
     with pytest.deprecated_call():
         operation(Variable("v"), 0)