Exemplo n.º 1
0
    def test_match_invalid(self):
        rule = policies(
            regarding("/fields/foo", set_value("foo")),
            regarding(
                "/fields/bar",
                select("/fields/foo") >> (
                    lambda foo: set_value(foo.value + "bar")
                )
            ),
            regarding("/fields/bar", match("barfu"))
        )

        ps = rule.run(Partial())
        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()
        self.assertEqual(0, len(results))
Exemplo n.º 2
0
    def test_match_invalid(self):
        rule = policies(
            regarding("/fields/foo", set_value("foo")),
            regarding(
                "/fields/bar",
                select("/fields/foo") >> (
                    lambda foo: set_value(foo.value + "bar")
                )
            ),
            regarding("/fields/bar", match("barfu"))
        )

        ps = rule.run(Partial())
        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()
        self.assertEqual(0, len(results))
Exemplo n.º 3
0
    def test_append_policy(self):
        ctx = Context()
        policy = regarding('/foo', set_value(5))
        ctx.append(policy)
        policy = ctx.finalize()

        result = run_policy(policy)

        self.assertEqual(result['foo'], 5)
Exemplo n.º 4
0
    def test_subctx_policy(self):
        ctx = Context()

        subctx = ctx.subctx(
            lambda policy_rules: regarding('/foo', *policy_rules)
        )

        subctx.append(
            set_value(5)
        )

        result = run_policy(ctx.finalize())
        self.assertEqual(result['foo'], 5)
Exemplo n.º 5
0
    def test_regarding(self):
        rule = policies(
            regarding("/fields/foo", set_value("foo"))
        )

        ps = rule.run(Partial())
        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()
        self.assertEqual(1, len(results))

        _, partial = results[0]

        foo_node, _ = partial.select("/fields/foo")
        self.assertEqual(LeafPolicyNode(Value("foo")), foo_node)
Exemplo n.º 6
0
    def test_regarding(self):
        rule = policies(
            regarding("/fields/foo", set_value("foo"))
        )

        ps = rule.run(Partial())
        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()
        self.assertEqual(1, len(results))

        _, partial = results[0]

        foo_node, _ = partial.select("/fields/foo")
        self.assertEqual(LeafPolicyNode(Value("foo")), foo_node)
Exemplo n.º 7
0
    def test_attempt(self):
        rule = policies(
            regarding(
                "/fields",
                regarding(
                    "foo",
                    permit_values(["foo", "bar"]),
                    attempt(
                        match("foo"),
                        set_value("foo_updated")
                    ),
                )
            )
        )

        ps = rule.run(Partial())

        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()
        self.assertEqual(2, len(results))

        values = [r[1].select("/fields/foo")[0].value for r in results]
        self.assertEqual(["foo_updated", "bar"], values)
Exemplo n.º 8
0
    def test_attempt(self):
        rule = policies(
            regarding(
                "/fields",
                regarding(
                    "foo",
                    permit_values(["foo", "bar"]),
                    attempt(
                        match("foo"),
                        set_value("foo_updated")
                    ),
                )
            )
        )

        ps = rule.run(Partial())

        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()
        self.assertEqual(2, len(results))

        values = [r[1].select("/fields/foo")[0].value for r in results]
        self.assertEqual(["foo_updated", "bar"], values)
Exemplo n.º 9
0
 def f_ab(a, b):
     return regarding("/results/f_ab", set_value(a + b))
Exemplo n.º 10
0
 def increment(value):
     return set_value(value + 1)
Exemplo n.º 11
0
 def increment_set(_):
     counter['num'] += 1
     num = counter['num']
     return set_value(num)
Exemplo n.º 12
0
 def func(b, a):
     return set_value(a + b)
Exemplo n.º 13
0
 def with_foo(foo_value):
     return regarding('/bar', set_value(foo_value))
Exemplo n.º 14
0
 def with_value(value):
     return regarding(
         '/foo',
         set_value(value)
     )
Exemplo n.º 15
0
 def with_values(a, b):
     return set_value(a + b)
Exemplo n.º 16
0
 def f_a(a):
     return regarding("/results/f_a", set_value(a))
Exemplo n.º 17
0
 def increment_set(_):
     counter['num'] += 1
     num = counter['num']
     return set_value(num)
Exemplo n.º 18
0
 def f_b(b):
     return regarding("/results/f_b", set_value(b))
Exemplo n.º 19
0
 def increment(value):
     return set_value(value + 1)
Exemplo n.º 20
0
 def f_ac(a, c):
     return regarding("/results/f_ac", set_value(a + c))