Example #1
0
def test_parameter_number():
    assert Parameter.number("iterations").to_dict() == {
        "name": "iterations", "description": "iterations", "schema": {"type": "number"}
    }
    assert Parameter.number("iterations", description="Iterations.").to_dict() == {
        "name": "iterations", "description": "Iterations.", "schema": {"type": "number"}
    }
    assert Parameter.number("iterations", default=12.34).to_dict() == {
        "name": "iterations", "description": "iterations", "schema": {"type": "number"}, "optional": True,
        "default": 12.34
    }
Example #2
0
def test_pgnode_parameter_fahrenheit():
    from openeo.processes import divide, subtract
    pg = divide(x=subtract(x=Parameter.number("f", description="Fahrenheit"),
                           y=32),
                y=1.8)
    assert pg.flat_graph() == {
        "subtract1": {
            "process_id": "subtract",
            "arguments": {
                "x": {
                    "from_parameter": "f"
                },
                "y": 32
            }
        },
        "divide1": {
            "process_id": "divide",
            "arguments": {
                "x": {
                    "from_node": "subtract1"
                },
                "y": 1.8
            },
            "result": True
        },
    }
def test_pgnode_parameter_basic():
    pg = openeo.processes.add(x=Parameter.number("a", description="A."), y=42)
    assert pg.flat_graph() == {
        "add1": {
            "process_id": "add",
            "arguments": {"x": {"from_parameter": "a"}, "y": 42},
            "result": True
        }
    }
Example #4
0
def test_store_collision(con100, requests_mock):
    subtract = {
        "minusy": {
            "process_id": "multiply",
            "arguments": {"x": {"from_parameter": "y"}, "y": -1}
        },
        "add": {
            "process_id": "add",
            "arguments": {"x": {"from_parameter": "x"}, "y": {"from_node": "minusy"}, },
            "result": True,
        }
    }

    def check_body(request):
        body = request.json()
        assert body == {
            "process_graph": subtract,
            "parameters": [
                {"name": "x", "description": "x", "schema": {"type": "number"}},
                {"name": "y", "description": "y", "schema": {"type": "number"}}
            ],
            "public": False,
        }
        return True

    requests_mock.get(API_URL + "/processes", json={"processes": [{"id": "add"}, {"id": "subtract"}]})
    adapter1 = requests_mock.put(API_URL + "/process_graphs/my_subtract", additional_matcher=check_body)
    adapter2 = requests_mock.put(API_URL + "/process_graphs/subtract", additional_matcher=check_body)

    parameters = [Parameter.number("x"), Parameter.number("y")]
    with pytest.warns(None) as recorder:
        udp = con100.save_user_defined_process("my_subtract", subtract, parameters=parameters)
    assert isinstance(udp, RESTUserDefinedProcess)
    assert adapter1.call_count == 1
    assert len(recorder) == 0

    with pytest.warns(UserWarning, match="same id as a pre-defined process") as recorder:
        udp = con100.save_user_defined_process("subtract", subtract, parameters=parameters)
    assert isinstance(udp, RESTUserDefinedProcess)
    assert adapter2.call_count == 1
    assert len(recorder) == 1
Example #5
0
def test_build_process_dict_from_pg_dict():
    actual = build_process_dict(process_graph={
        "add": {
            "process_id": "add",
            "arguments": {
                "x": {
                    "from_parameter": "data"
                },
                "y": 1
            },
            "result": True
        }
    },
                                process_id="increment",
                                summary="Increment value",
                                description="Add 1 to input.",
                                parameters=[Parameter.number(name="data")],
                                returns={"schema": {
                                    "type": "number"
                                }})
    expected = {
        "id":
        "increment",
        "description":
        "Add 1 to input.",
        "summary":
        "Increment value",
        "process_graph": {
            "add": {
                "arguments": {
                    "x": {
                        "from_parameter": "data"
                    },
                    "y": 1
                },
                "process_id": "add",
                "result": True
            }
        },
        "parameters": [{
            "name": "data",
            "description": "data",
            "schema": {
                "type": "number"
            }
        }],
        "returns": {
            "schema": {
                "type": "number"
            }
        },
    }
    assert actual == expected
Example #6
0
def test_build_process_dict_from_process(con100):
    from openeo.processes import add
    data = Parameter.number("data")
    proc = add(x=data, y=1)
    actual = build_process_dict(process_graph=proc,
                                process_id="increment",
                                summary="Increment value",
                                description="Add 1 to input.",
                                parameters=[data],
                                returns={"schema": {
                                    "type": "number"
                                }})
    expected = {
        "id":
        "increment",
        "description":
        "Add 1 to input.",
        "summary":
        "Increment value",
        "process_graph": {
            "add1": {
                "arguments": {
                    "x": {
                        "from_parameter": "data"
                    },
                    "y": 1
                },
                "process_id": "add",
                "result": True
            }
        },
        "parameters": [{
            "name": "data",
            "description": "data",
            "schema": {
                "type": "number"
            }
        }],
        "returns": {
            "schema": {
                "type": "number"
            }
        },
    }
    assert actual == expected