import pytest from core.builtin_concepts import ReturnValueConcept, ParserResultConcept, BuiltinConcepts from core.concept import Concept from core.tokenizer import Tokenizer from evaluators.AddConceptInSetEvaluator import AddConceptInSetEvaluator from parsers.DefaultParser import IsaConceptNode, NameNode from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka 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))) class TestAddConceptInSetEvaluator(TestUsingMemoryBasedSheerka): @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(self, ret_val, expected): context = self.get_context() assert AddConceptInSetEvaluator().matches(context, ret_val) == expected def test_i_cannot_add_if_the_concept_does_not_exists(self): context = self.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(self): context = self.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(self): sheerka, context, foo, bar = self.init_concepts("foo", "bar", create_new=True) ret_val = get_ret_val("foo", "bar") res = AddConceptInSetEvaluator().eval(context, ret_val) foo = sheerka.new("foo") # reload it assert res.status assert context.sheerka.isinstance(res.value, BuiltinConcepts.SUCCESS) assert context.sheerka.isaset(context, bar) assert context.sheerka.isinset(foo, bar) assert context.sheerka.isa(foo, bar) foo_from_sheerka = context.sheerka.get("foo") assert foo_from_sheerka.get_prop(BuiltinConcepts.ISA) == [bar] def test_i_can_add_bnf_concept_to_a_set_of_concept(self): """ This test is the reason why I have started the whole eval on demand stuff Sheerka tries to evaluate the body but it can't (as a and b are not defined) So 'foo' cannot be put is set :return: """ sheerka, context, one, two, foo, bar = self.init_concepts( "one", "two", Concept("foo", definition="(one|two)=a 'plus' (one|two)=b", body="a + b").def_prop("a").def_prop("b"), "bar", create_new=True) ret_val = get_ret_val("foo", "bar") res = AddConceptInSetEvaluator().eval(context, ret_val) foo = sheerka.new("foo") # reload it assert res.status assert context.sheerka.isinstance(res.value, BuiltinConcepts.SUCCESS) assert context.sheerka.isaset(context, bar) assert context.sheerka.isinset(foo, bar) assert context.sheerka.isa(foo, bar) foo_from_sheerka = context.sheerka.get("foo") assert foo_from_sheerka.get_prop(BuiltinConcepts.ISA) == [bar] def test_i_can_add_concept_with_a_body_to_a_set_of_concept(self): context = self.get_context() foo = Concept("foo", body="1") context.sheerka.create_new_concept(context, foo) bar = Concept("bar") context.sheerka.create_new_concept(context, 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(self): context = self.get_context() foo = Concept("foo") context.sheerka.create_new_concept(context, foo) bar = Concept("bar") context.sheerka.create_new_concept(context, 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.key == foo.key assert res.value.concept_set.key == bar.key