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_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_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_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_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_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_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_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 f_ab(a, b): return regarding("/results/f_ab", set_value(a + b))
def increment(value): return set_value(value + 1)
def increment_set(_): counter['num'] += 1 num = counter['num'] return set_value(num)
def func(b, a): return set_value(a + b)
def with_foo(foo_value): return regarding('/bar', set_value(foo_value))
def with_value(value): return regarding( '/foo', set_value(value) )
def with_values(a, b): return set_value(a + b)
def f_a(a): return regarding("/results/f_a", set_value(a))
def increment_set(_): counter['num'] += 1 num = counter['num'] return set_value(num)
def f_b(b): return regarding("/results/f_b", set_value(b))
def increment(value): return set_value(value + 1)
def f_ac(a, c): return regarding("/results/f_ac", set_value(a + c))