def test_varargs(): def sumall(a, *args, b=0, **kwargs): return a + sum(args) + b + sum(kwargs.values()) op = operation( sumall, name="t", needs=[ "a", vararg("arg1"), vararg("arg2"), varargs("args"), optional("b"), optional("c"), ], provides="sum", ) exp = sum(range(8)) assert op.compute(dict(a=1, arg1=2, arg2=3, args=[4, 5], b=6, c=7))["sum"] == exp assert op.compute(dict(a=1, arg1=2, arg2=3, args=[4, 5], c=7))["sum"] == exp - 6 assert op.compute(dict(a=1, arg1=2, arg2=3, args=[4, 5], b=6))["sum"] == exp - 7 assert op.compute(dict(a=1, arg2=3, args=[4, 5], b=6, c=7))["sum"] == exp - 2 assert op.compute(dict(a=1, arg1=2, arg2=3, b=6, c=7))["sum"] == exp - 4 - 5 with pytest.raises(ValueError, match="Missing compulsory needs.+'a'"): assert op.compute(dict(arg1=2, arg2=3, b=6, c=7))
def test_cwd_fnop(): op = operation( str, None, needs=[ "a", "a/b", "/r/b", optional("o"), keyword("k"), implicit("i"), vararg("v1"), varargs("v2"), sfx("s1"), sfxed("s2", "s22"), vcat("vc"), ], provides=["A/B", "C", "/R"], aliases=[("A/B", "aa"), ("C", "CC"), ("/R", "RR")], cwd="root", ) exp = """ FnOp(name='str', needs=['root/a'($), 'root/a/b'($), '/r/b'($), 'root/o'($?'o'), 'root/k'($>'k'), 'root/i'($), 'root/v1'($*), 'root/v2'($+), sfx('s1'), sfxed('root/s2'($), 's22'), 'root/vc'($)], provides=['root/A/B'($), 'root/C'($), '/R'($), 'root/aa'($), 'root/CC'($), 'root/RR'($)], aliases=[('root/A/B'($), 'root/aa'($)), ('root/C'($), 'root/CC'($)), ('/R'($), 'root/RR'($))], fn='str') """ assert oneliner(op) == oneliner(exp)
def test_jetsam_n_plot_with_DEBUG(): pipe = compose( "mix", operation( str, "FUNC", needs=[ "a", sfxed("b", "foo", keyword="bb"), implicit("c"), sfxed("d", "bar"), vararg("e"), varargs("f"), ], provides=[ "A", sfxed("b", "FOO", keyword="bb"), implicit("C"), sfxed("d", "BAR", optional=True), sfx("FOOBAR"), ], aliases={ "A": "aaa", "b": "bbb", "d": "ddd" }, # FIXME: "D" is implicit! ), ) with debug_enabled(True), pytest.raises(ValueError, match="^Unsolvable"): pipe.compute() with debug_enabled(True), pytest.raises( ValueError, match="^Failed matching inputs <=> needs") as exc: pipe.compute({ "a": 1, sfxed("b", "foo"): 2, "c": 3, sfxed("d", "bar"): 4, "e": 5, "f": [6, 7], }) exc.value.jetsam.plot_fpath.unlink()