コード例 #1
0
ファイル: test_core.py プロジェクト: apogiatzis/pyungo
def test_schema():

    from jsonschema import ValidationError

    schema = {
        "type": "object",
        "properties": {
            "a": {"type": "number"},
            "b": {"type": "number"}
        }
    }

    graph = Graph(schema=schema)

    @graph.register(
        inputs=['a', 'b'],
        outputs=['c']
    )
    def f_my_function(a, b):
        return a + b

    with pytest.raises(ValidationError) as err:
        graph.calculate(data={'a': 1, 'b': '2'})

    msg = "'2' is not of type 'number'"
    assert msg in str(err.value)

    res = graph.calculate(data={'a': 1, 'b': 2})
    assert res == 3
コード例 #2
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_inputs_not_used():
    graph = Graph()

    @graph.register(inputs=["a", "b"], outputs=["c"])
    def f_my_function(a, b):
        return a + b

    with pytest.raises(PyungoError) as err:
        graph.calculate(data={"a": 6, "b": 4, "e": 7})

    assert "The following inputs are not used by the model: ['e']" in str(err.value)
コード例 #3
0
ファイル: test_core.py プロジェクト: apogiatzis/pyungo
def test_missing_input():
    graph = Graph()

    @graph.register(inputs=['a', 'b'], outputs=['c'])
    def f_my_function(a, b):
        return a + b

    with pytest.raises(PyungoError) as err:
        graph.calculate(data={'a': 6})
    
    assert "The following inputs are needed: ['b']" in str(err.value)
コード例 #4
0
ファイル: test_core.py プロジェクト: apogiatzis/pyungo
def test_inputs_collision():
    graph = Graph()

    @graph.register(inputs=['a', 'b'], outputs=['c'])
    def f_my_function(a, b):
        return a + b

    with pytest.raises(PyungoError) as err:
        graph.calculate(data={'a': 6, 'b': 4, 'c': 7})
    
    assert "The following inputs are already used in the model: ['c']" in str(err.value)
コード例 #5
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_no_side_effects_if_deepcopy_is_left_at_default():
    graph = Graph()

    @graph.register()
    def f(c, e):
        c["a"] += 1
        f = c["a"] + e
        return f

    d = {"a": 1}
    res = graph.calculate(data={"c": d, "e": 2})
    assert res == 4
    res = graph.calculate(data={"c": d, "e": 2})
    assert res == 4
コード例 #6
0
ファイル: test_core.py プロジェクト: Tosa95/pyungo
def test_no_side_effects_if_deepcopy_is_left_at_default():
    graph = Graph()

    @graph.register()
    def f(c, e):
        c['a'] += 1
        f = c['a'] + e
        return f

    d = {'a': 1}
    res = graph.calculate(data={'c': d, 'e': 2})
    assert res == 4
    res = graph.calculate(data={'c': d, 'e': 2})
    assert res == 4
コード例 #7
0
ファイル: test_core.py プロジェクト: Tosa95/pyungo
def test_no_side_effects_if_deepcopy_enabled():
    graph = Graph(do_deepcopy=True)

    @graph.register()
    def f(c, e):
        c['a'] += 1
        f = c['a'] + e
        return f

    d = {'a': 1}
    res = graph.calculate(data={'c': d, 'e': 2})
    assert res == 4
    res = graph.calculate(data={'c': d, 'e': 2})
    assert res == 4
コード例 #8
0
ファイル: test_core.py プロジェクト: Tosa95/pyungo
def test_no_deepcopy_doesnt_copy():
    graph = Graph(do_deepcopy=False)

    @graph.register()
    def f(c, e):
        c['a'] += 1
        f = c['a'] + e
        return f

    d = {'a': 1}
    res = graph.calculate(data={'c': d, 'e': 2})
    assert res == 4
    res = graph.calculate(data={'c': d, 'e': 2})
    assert res == 5
コード例 #9
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_no_deepcopy_doesnt_copy():
    graph = Graph(do_deepcopy=False)

    @graph.register()
    def f(c, e):
        c["a"] += 1
        f = c["a"] + e
        return f

    d = {"a": 1}
    res = graph.calculate(data={"c": d, "e": 2})
    assert res == 4
    res = graph.calculate(data={"c": d, "e": 2})
    assert res == 5
コード例 #10
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_no_side_effects_if_deepcopy_enabled():
    graph = Graph(do_deepcopy=True)

    @graph.register()
    def f(c, e):
        c["a"] += 1
        f = c["a"] + e
        return f

    d = {"a": 1}
    res = graph.calculate(data={"c": d, "e": 2})
    assert res == 4
    res = graph.calculate(data={"c": d, "e": 2})
    assert res == 4
コード例 #11
0
ファイル: test_core.py プロジェクト: apogiatzis/pyungo
def test_circular_dependency():
    graph = Graph()

    @graph.register(inputs=['a', 'b', 'd'], outputs=['c'])
    def f_my_function(a, b):
        return a + b

    @graph.register(inputs=['c'], outputs=['d'])
    def f_my_function2(c):
        return c / 2.

    with pytest.raises(PyungoError) as err:
        graph.calculate(data={'a': 6, 'b': 4})

    assert "A cyclic dependency exists amongst" in str(err.value)
コード例 #12
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_circular_dependency():
    graph = Graph()

    @graph.register(inputs=["a", "b", "d"], outputs=["c"])
    def f_my_function(a, b):
        return a + b

    @graph.register(inputs=["c"], outputs=["d"])
    def f_my_function2(c):
        return c / 2.0

    with pytest.raises(PyungoError) as err:
        graph.calculate(data={"a": 6, "b": 4})

    assert "A cyclic dependency exists amongst" in str(err.value)
コード例 #13
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_contract_outputs():
    from contracts import ContractNotRespected

    graph = Graph()

    @graph.register(inputs=["a", "b"], outputs=[Output("c", contract="int,>0")])
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={"a": 2, "b": 3})
    assert res == 5

    with pytest.raises(ContractNotRespected) as err:
        res = graph.calculate(data={"a": -4, "b": 3})

    assert "Condition -1 > 0 not respected" in str(err.value)
コード例 #14
0
ファイル: test_core.py プロジェクト: Tosa95/pyungo
def test_contract_outputs():
    from contracts import ContractNotRespected

    graph = Graph()

    @graph.register(inputs=['a', 'b'],
                    outputs=[Output('c', contract='int,>0')])
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={'a': 2, 'b': 3})
    assert res == 5

    with pytest.raises(ContractNotRespected) as err:
        res = graph.calculate(data={'a': -4, 'b': 3})

    assert "Condition -1 > 0 not respected" in str(err.value)
コード例 #15
0
ファイル: test_core.py プロジェクト: Tosa95/pyungo
def test_passing_data_to_node_definition():
    graph = Graph()

    @graph.register(inputs=['a', {'b': 2}], outputs=['c'])
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={'a': 5})
    assert res == 7
コード例 #16
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_passing_data_to_node_definition():
    graph = Graph()

    @graph.register(inputs=["a", {"b": 2}], outputs=["c"])
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={"a": 5})
    assert res == 7
コード例 #17
0
ファイル: test_core.py プロジェクト: Tosa95/pyungo
def test_Input_type_input():
    graph = Graph()

    @graph.register(inputs=[Input(name='a'), 'b'], outputs=['c'])
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={'a': 2, 'b': 3})

    assert res == 5
コード例 #18
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_Input_type_input():
    graph = Graph()

    @graph.register(inputs=[Input(name="a"), "b"], outputs=["c"])
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={"a": 2, "b": 3})

    assert res == 5
コード例 #19
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_optional_kwargs():
    graph = Graph()

    @graph.register(inputs=["a"], kwargs=["b"], outputs=["c"])
    def f(a, b=2):
        return a + b

    res = graph.calculate(data={"a": 1})

    assert res == 3
    assert graph.data["c"] == 3
コード例 #20
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_iterable_on_single_output():
    graph = Graph()

    @graph.register(inputs=["a", "b"], outputs=["c"])
    def f_my_function(a, b):
        return list(range(a)) + [b]

    res = graph.calculate(data={"a": 2, "b": 3})

    assert res == [0, 1, 3]
    assert graph.data["c"] == [0, 1, 3]
コード例 #21
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_args_kwargs():
    graph = Graph()

    @graph.register(inputs=["a", "b"], args=["c"], kwargs=["d"], outputs=["e"])
    def f_my_function(a, b, *args, **kwargs):
        return a + b + args[0] + kwargs["d"]

    res = graph.calculate(data={"a": 2, "b": 3, "c": 4, "d": 5})

    assert res == 14
    assert graph.data["e"] == 14
コード例 #22
0
ファイル: test_core.py プロジェクト: Tosa95/pyungo
def test_args_kwargs():
    graph = Graph()

    @graph.register(inputs=['a', 'b'], args=['c'], kwargs=['d'], outputs=['e'])
    def f_my_function(a, b, *args, **kwargs):
        return a + b + args[0] + kwargs['d']

    res = graph.calculate(data={'a': 2, 'b': 3, 'c': 4, 'd': 5})

    assert res == 14
    assert graph.data['e'] == 14
コード例 #23
0
ファイル: test_core.py プロジェクト: apogiatzis/pyungo
def test_optional_kwargs():
    graph = Graph()

    @graph.register(inputs=['a'], kwargs=['b'], outputs=['c'])
    def f(a, b=2):
        return a + b

    res = graph.calculate(data={'a': 1})

    assert res == 3
    assert graph.data['c'] == 3
コード例 #24
0
ファイル: test_core.py プロジェクト: apogiatzis/pyungo
def test_iterable_on_single_output():
    graph = Graph()

    @graph.register(inputs=['a', 'b'], outputs=['c'])
    def f_my_function(a, b):
        return list(range(a)) + [b]

    res = graph.calculate(data={'a': 2, 'b': 3})

    assert res == [0, 1, 3]
    assert graph.data['c'] == [0, 1, 3]
コード例 #25
0
ファイル: test_core.py プロジェクト: apogiatzis/pyungo
def test_no_explicit_inputs_outputs_simple():
    graph = Graph()

    @graph.register()
    def f(a, b):
        c = a + b
        return c

    res = graph.calculate(data={'a': 1, 'b': 2})

    assert res == 3
    assert graph.data['c'] == 3
コード例 #26
0
ファイル: test_core.py プロジェクト: Tosa95/pyungo
def test_provide_inputs_outputs():
    inputs = [Input('a'), Input('b')]
    outputs = [Output('c')]

    graph = Graph(inputs=inputs, outputs=outputs)

    @graph.register(inputs=['a', 'b'], outputs=['c'])
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={'a': 2, 'b': 3})
    assert res == 5
コード例 #27
0
ファイル: test_core.py プロジェクト: Tosa95/pyungo
def test_map():
    graph = Graph()

    @graph.register(inputs=[Input('a', map='q'),
                            Input('b', map='w')],
                    outputs=[Output('c', map='e')])
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={'q': 2, 'w': 3})
    assert res == 5
    assert graph.data['e'] == 5
コード例 #28
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_provide_inputs_outputs():
    inputs = [Input("a"), Input("b")]
    outputs = [Output("c")]

    graph = Graph(inputs=inputs, outputs=outputs)

    @graph.register(inputs=["a", "b"], outputs=["c"])
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={"a": 2, "b": 3})
    assert res == 5
コード例 #29
0
ファイル: test_core.py プロジェクト: suimong/pyungo
def test_map():
    graph = Graph()

    @graph.register(
        inputs=[Input("a", map="q"), Input("b", map="w")],
        outputs=[Output("c", map="e")],
    )
    def f_my_function(a, b):
        return a + b

    res = graph.calculate(data={"q": 2, "w": 3})
    assert res == 5
    assert graph.data["e"] == 5
コード例 #30
0
ファイル: test_core.py プロジェクト: apogiatzis/pyungo
def test_simple():
    graph = Graph()

    @graph.register(inputs=['a', 'b'], outputs=['c'])
    def f_my_function(a, b):
        return a + b

    @graph.register(inputs=['d', 'a'], outputs=['e'])
    def f_my_function3(d, a):
        return d - a

    @graph.register(inputs=['c'], outputs=['d'])
    def f_my_function2(c):
        return c / 10.

    res = graph.calculate(data={'a': 2, 'b': 3})
    assert res == -1.5
    assert graph.data['e'] == -1.5

    # make sure it is indepodent
    res = graph.calculate(data={'a': 2, 'b': 3})
    assert res == -1.5
    assert graph.data['e'] == -1.5