import pytest from core.builtin_concepts import ReturnValueConcept, ParserResultConcept, BuiltinConcepts from core.concept import Concept from core.sheerka import Sheerka, ExecutionContext from core.tokenizer import Tokenizer from evaluators.AddConceptInSetEvaluator import AddConceptInSetEvaluator from parsers.DefaultParser import IsaConceptNode, NameNode from sdp.sheerkaDataProvider import Event def get_context(): sheerka = Sheerka(skip_builtins_in_db=True) sheerka.initialize("mem://") return ExecutionContext("test", Event(), sheerka) def get_ret_val(concept_name, concept_set_name): n1 = NameNode(list(Tokenizer(concept_name))) n2 = NameNode(list(Tokenizer(concept_set_name))) return ReturnValueConcept("some_name", True, ParserResultConcept(value=IsaConceptNode([], n1, n2))) @pytest.mark.parametrize("ret_val, expected", [ (ReturnValueConcept("some_name", True, ParserResultConcept(value=IsaConceptNode([]))), True), (ReturnValueConcept("some_name", False, ParserResultConcept(value=IsaConceptNode([]))), False), (ReturnValueConcept("some_name", True, "not a ParserResultConcept"), False), (ReturnValueConcept("some_name", True, ParserResultConcept()), False), ]) def test_i_can_match(ret_val, expected): context = get_context() assert AddConceptInSetEvaluator().matches(context, ret_val) == expected def test_i_cannot_add_if_the_concept_does_not_exists(): context = get_context() ret_val = get_ret_val("foo", "bar") res = AddConceptInSetEvaluator().eval(context, ret_val) assert not res.status assert context.sheerka.isinstance(res.value, BuiltinConcepts.UNKNOWN_CONCEPT) assert res.value.body == "foo" def test_i_cannot_add_if_the_set_does_not_exists(): context = get_context() foo = Concept("foo") context.sheerka.set_id_if_needed(foo, False) context.sheerka.add_in_cache(foo) ret_val = get_ret_val("foo", "bar") res = AddConceptInSetEvaluator().eval(context, ret_val) assert not res.status assert context.sheerka.isinstance(res.value, BuiltinConcepts.UNKNOWN_CONCEPT) assert res.value.body == "bar" def test_i_can_add_concept_to_a_set_of_concept(): context = get_context() foo = Concept("foo") context.sheerka.set_id_if_needed(foo, False) context.sheerka.add_in_cache(foo) bar = Concept("bar") context.sheerka.set_id_if_needed(bar, False) context.sheerka.add_in_cache(bar) ret_val = get_ret_val("foo", "bar") res = AddConceptInSetEvaluator().eval(context, ret_val) assert res.status assert context.sheerka.isinstance(res.value, BuiltinConcepts.SUCCESS) def test_i_can_add_concept_with_a_body_to_a_set_of_concept(): context = get_context() foo = Concept("foo", body="1") context.sheerka.set_id_if_needed(foo, False) context.sheerka.add_in_cache(foo) bar = Concept("bar") context.sheerka.set_id_if_needed(bar, False) context.sheerka.add_in_cache(bar) ret_val = get_ret_val("foo", "bar") res = AddConceptInSetEvaluator().eval(context, ret_val) assert res.status assert context.sheerka.isinstance(res.value, BuiltinConcepts.SUCCESS) def test_i_cannot_add_the_same_concept_twice(): context = get_context() foo = Concept("foo") context.sheerka.set_id_if_needed(foo, False) context.sheerka.add_in_cache(foo) bar = Concept("bar") context.sheerka.set_id_if_needed(bar, False) context.sheerka.add_in_cache(bar) ret_val = get_ret_val("foo", "bar") AddConceptInSetEvaluator().eval(context, ret_val) res = AddConceptInSetEvaluator().eval(context, ret_val) assert not res.status assert context.sheerka.isinstance(res.value, BuiltinConcepts.CONCEPT_ALREADY_IN_SET) assert res.value.concept == foo assert res.value.concept_set == bar