import ast import pytest from core.builtin_concepts import ReturnValueConcept, BuiltinConcepts from core.sheerka import Sheerka, ExecutionContext import core.builtin_helpers def test_i_can_use_expect_one_when_empty(): sheerka = get_sheerka() res = core.builtin_helpers.expect_one(get_context(sheerka), []) assert not res.status assert sheerka.isinstance(res.value, BuiltinConcepts.IS_EMPTY) def test_i_can_use_expect_one_when_too_many_success(): sheerka = get_sheerka() items = [ ReturnValueConcept("who", True, "value1"), ReturnValueConcept("who", True, "value2"), ] res = core.builtin_helpers.expect_one(get_context(sheerka), items) assert not res.status assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_SUCCESS) assert res.value.obj == items assert res.parents == items def test_i_can_use_expect_one_when_same_success(): sheerka = get_sheerka() items = [ ReturnValueConcept("who", True, "value"), ReturnValueConcept("who", True, "value"), ] res = core.builtin_helpers.expect_one(get_context(sheerka), items) assert res.status assert res.value == items[0].value assert res.parents == items def test_i_can_use_expect_when_only_errors_1(): sheerka = get_sheerka() items = [ ReturnValueConcept("who", False, None), ] res = core.builtin_helpers.expect_one(get_context(sheerka), items) assert not res.status assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS) assert res.value.obj == items assert res.parents == items def test_i_can_use_expect_when_only_errors_2(): sheerka = get_sheerka() items = [ ReturnValueConcept("who", False, None), ReturnValueConcept("who", False, None), ] res = core.builtin_helpers.expect_one(get_context(sheerka), items) assert not res.status assert sheerka.isinstance(res.value, BuiltinConcepts.TOO_MANY_ERRORS) assert res.value.obj == items assert res.parents == items def test_i_can_use_expect_one_when_one_success_1(): sheerka = get_sheerka() items = [ ReturnValueConcept("who", True, None), ] res = core.builtin_helpers.expect_one(get_context(sheerka), items) assert res.status assert res.body == items[0].body assert res.parents == items def test_i_can_use_expect_one_when_one_success_2(): sheerka = get_sheerka() items = [ ReturnValueConcept("who", False, None), ReturnValueConcept("who", True, None), ReturnValueConcept("who", False, None), ] res = core.builtin_helpers.expect_one(get_context(sheerka), items) assert res.status assert res.body == items[1].body assert res.parents == items def test_i_can_use_expect_one_when_not_a_list_true(): sheerka = get_sheerka() item = ReturnValueConcept("who", True, None) res = core.builtin_helpers.expect_one(get_context(sheerka), item) assert res.status assert res == item def test_i_can_use_expect_one_when_not_a_list_false(): sheerka = get_sheerka() item = ReturnValueConcept("who", False, None) res = core.builtin_helpers.expect_one(get_context(sheerka), item) assert not res.status assert res == item @pytest.mark.parametrize("expression, vars_to_include, vars_to_exclude, expected_expr", [ ("a == 1", [], [], []), ("a == 1", ["a"], [], ["a == 1"]), ("a == 1", [], ["a"], []), ("predicate(a)", [], [], []), ("predicate(a)", ["a"], [], ["predicate(a)"]), ("predicate(a, b)", ["a"], [], ["predicate(a, b)"]), ("predicate(a, b)", ["b"], [], ["predicate(a, b)"]), ("predicate(a, b)", ["a", "b"], [], ["predicate(a, b)"]), ("predicate(a, b)", ["a"], ["b"], []), ("a + b == 1", [], [], []), ("a + b == 1", ["a"], [], ["a + b == 1"]), ("a + b == 1", ["a"], ["b"], []), ("a + b == 1", ["b"], [], ["a + b == 1"]), ("a + b == 1", ["a", "b"], [], ["a + b == 1"]), ("a == 1 and b == 2", [], [], []), ("a == 1 and b == 2", ["a"], [], ["a == 1"]), ("a == 1 and b == 2", ["b"], [], ["b == 2"]), ("a == 1 and b == 2", ["a"], ["b"], ["a == 1"]), ("a == 1 and b == 2", ["a", "b"], [], ["a == 1 and b == 2"]), ("predicate(a,c) and predicate(b,c)", ["a", "b"], [], ["predicate(a,c) and predicate(b,c)"]), ("not(a == 1)", [], [], []), ("not(a == 1)", ["a"], [], ["not(a==1)"]), ("a == 1 or b == 2", [], [], []), ("a == 1 or b == 2", ["a"], [], ["a == 1"]), ("a == 1 or b == 2", ["b"], [], ["b == 2"]), ("a == 1 or b == 2", ["a", "b"], [], ["a == 1 or b == 2"]), ("predicate(a,c) or predicate(b,c)", ["a", "b"], [], ["predicate(a,c) or predicate(b,c)"]), ]) def test_i_can_extract_predicates(expression, vars_to_include, vars_to_exclude, expected_expr): sheerka = get_sheerka() expected = [ast.parse(expr, mode="eval") for expr in expected_expr] actual = core.builtin_helpers.extract_predicates(sheerka, expression, vars_to_include, vars_to_exclude) assert len(actual) == len(expected) for i in range(len(actual)): assert dump_ast(actual[i]) == dump_ast(expected[i]) def get_sheerka(): sheerka = Sheerka(skip_builtins_in_db=True) sheerka.initialize("mem://") return sheerka def get_context(sheerka): return ExecutionContext("test", "xxx", sheerka) def dump_ast(node): dump = ast.dump(node) for to_remove in [", ctx=Load()", ", kind=None", ", type_ignores=[]"]: dump = dump.replace(to_remove, "") return dump