Exemplo n.º 1
0
    def test_regarding_scoping(self):
        assertEqual = self.assertEqual

        @policy_rule_func
        def expect_scope(expected="/", msg=None):
            def for_actual(actual):
                def checker():
                    return assertEqual(actual, expected, msg)
                return check(checker)

            return scope() >> for_actual

        rule = regarding(
            "",
            expect_scope("/", 0),
            regarding("a", expect_scope("/a", 1)),
            expect_scope("/", 2),
            regarding(
                "b",
                expect_scope("/b", 3),
                regarding("c", expect_scope("/b/c", 4)),
                expect_scope("/b", 5),
            ),
        )

        rule.run(Partial.from_obj({}))
Exemplo n.º 2
0
    def test_regarding_scoping(self):
        assertEqual = self.assertEqual

        @policy_rule_func
        def expect_scope(expected="/", msg=None):
            def for_actual(actual):
                def checker():
                    return assertEqual(actual, expected, msg)
                return check(checker)

            return scope() >> for_actual

        rule = regarding(
            "",
            expect_scope("/", 0),
            regarding("a", expect_scope("/a", 1)),
            expect_scope("/", 2),
            regarding(
                "b",
                expect_scope("/b", 3),
                regarding("c", expect_scope("/b/c", 4)),
                expect_scope("/b", 5),
            ),
        )

        rule.run(Partial.from_obj({}))
Exemplo n.º 3
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.º 4
0
    def test_regarding_return(self):
        rule = regarding("/foo")
        ps = rule.run(Partial.from_obj({"foo": 5}))

        results = ps.getValue()
        values = [r[0] for r in results]

        self.assertEqual([5], values)
Exemplo n.º 5
0
    def test_regarding_return(self):
        rule = regarding("/foo")
        ps = rule.run(Partial.from_obj({"foo": 5}))

        results = ps.getValue()
        values = [r[0] for r in results]

        self.assertEqual([5], values)
Exemplo n.º 6
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.º 7
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.º 8
0
    def test_fail(self):
        rule = policies(
            regarding(
                "/fields",
                regarding(
                    "foo",
                    permit_values(["foo", "bar"]),
                    fail(),
                )
            )
        )

        ps = rule.run(Partial())

        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()
        self.assertEqual(0, len(results))
Exemplo n.º 9
0
    def test_fail(self):
        rule = policies(
            regarding(
                "/fields",
                regarding(
                    "foo",
                    permit_values(["foo", "bar"]),
                    fail(),
                )
            )
        )

        ps = rule.run(Partial())

        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()
        self.assertEqual(0, len(results))
Exemplo n.º 10
0
    def test_permit_values(self):
        # case 1, both values come through
        rule = policies(
            regarding("/fields/foo", permit_values(["foo", "bar"])),
        )

        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", "bar"], values)

        # case 2, one value gets filtered through
        rule = policies(
            regarding("/fields/foo", permit_values(["foo", "bar"])),
            regarding("/fields/foo", match("foo"))
        )

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

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

        values = [r[1].select("/fields/foo")[0].value for r in results]
        self.assertEqual(["foo"], values)

        # case 3, this one gets tricky... we can do the match() first!
        rule = policies(
            regarding("/fields/foo", match("foo")),
            regarding("/fields/foo", permit_values(["foo", "bar"]))
        )

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

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

        values = [r[1].select("/fields/foo")[0].value for r in results]
        self.assertEqual(["foo"], values)
Exemplo n.º 11
0
    def test_permit_values(self):
        # case 1, both values come through
        rule = policies(
            regarding("/fields/foo", permit_values(["foo", "bar"])),
        )

        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", "bar"], values)

        # case 2, one value gets filtered through
        rule = policies(
            regarding("/fields/foo", permit_values(["foo", "bar"])),
            regarding("/fields/foo", match("foo"))
        )

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

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

        values = [r[1].select("/fields/foo")[0].value for r in results]
        self.assertEqual(["foo"], values)

        # case 3, this one gets tricky... we can do the match() first!
        rule = policies(
            regarding("/fields/foo", match("foo")),
            regarding("/fields/foo", permit_values(["foo", "bar"]))
        )

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

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

        values = [r[1].select("/fields/foo")[0].value for r in results]
        self.assertEqual(["foo"], values)
Exemplo n.º 12
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.º 13
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.º 14
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.º 15
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.º 16
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.º 17
0
    def test_define_as(self):
        definition = Value(5)

        rule = policies(
            regarding(
                "/fields/foo",
                define_as(definition)
            )
        )

        ps = rule.run(Partial())

        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()

        values = [r[1].select("/fields/foo")[0].value for r in results]

        self.assertEqual([5], values)
Exemplo n.º 18
0
    def test_define_as(self):
        definition = Value(5)

        rule = policies(
            regarding(
                "/fields/foo",
                define_as(definition)
            )
        )

        ps = rule.run(Partial())

        self.assertTrue(isinstance(ps, List))

        results = ps.getValue()

        values = [r[1].select("/fields/foo")[0].value for r in results]

        self.assertEqual([5], values)
Exemplo n.º 19
0
    def test_subctx_noop_policy(self):
        ctx = Context()

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

        def with_foo(foo_value):
            return regarding('/bar', set_value(foo_value))

        ctx.append(with_foo, foo_ctx_value)

        # foo is only used as a value - never being applied policy
        # rules itself.
        # ctx therefore should only have 1 policy, the `with_foo`
        # function above that just sets {foo: *, bar: foo}
        items, error_handler = ctx.get_finalized_items()
        self.assertEqual(len(items), 1)

        result = run_policy(ctx.finalize(), {"foo": "zebra"})
        self.assertEqual(result['bar'], "zebra")
Exemplo n.º 20
0
 def f_ac(a, c):
     return regarding("/results/f_ac", set_value(a + c))
Exemplo n.º 21
0
 def get_policies(value):
     return regarding(
         "/fields/foo",
         (check(lambda: value) >> set_value)
     )
Exemplo n.º 22
0
 def get_policies(value):
     return regarding(
         "/fields/foo",
         (check(lambda: value) >> set_value)
     )
Exemplo n.º 23
0
 def f_a(a):
     return regarding("/results/f_a", set_value(a))
Exemplo n.º 24
0
 def f_b(b):
     return regarding("/results/f_b", set_value(b))
Exemplo n.º 25
0
 def with_foo(foo_value):
     return regarding('/bar', set_value(foo_value))
Exemplo n.º 26
0
 def f_ab(a, b):
     return regarding("/results/f_ab", set_value(a + b))
Exemplo n.º 27
0
 def with_value(value):
     return regarding(
         '/foo',
         set_value(value)
     )