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({}))
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))
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)
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)
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))
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)
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)
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)
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)
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)
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")
def f_ac(a, c): return regarding("/results/f_ac", set_value(a + c))
def get_policies(value): return regarding( "/fields/foo", (check(lambda: value) >> set_value) )
def f_a(a): return regarding("/results/f_a", set_value(a))
def f_b(b): return regarding("/results/f_b", set_value(b))
def with_foo(foo_value): return regarding('/bar', set_value(foo_value))
def f_ab(a, b): return regarding("/results/f_ab", set_value(a + b))
def with_value(value): return regarding( '/foo', set_value(value) )