Updated sheerka.value() and added unit tests

This commit is contained in:
2019-12-21 16:15:54 +01:00
parent 41e0885486
commit 2474b08150
3 changed files with 14 additions and 27 deletions
-23
View File
@@ -45,7 +45,6 @@ class BuiltinConcepts(Enum):
ENUMERATION = "enum" # represents a list or a set ENUMERATION = "enum" # represents a list or a set
LIST = "list" # represents a list LIST = "list" # represents a list
CANNOT_RESOLVE_VALUE_ERROR = "value cannot be resolved" # don't know how to find concept value CANNOT_RESOLVE_VALUE_ERROR = "value cannot be resolved" # don't know how to find concept value
CONCEPT_NOT_INITIALIZED = "concept not evaluated" # Try to work on a concept that is not evaluated
NODE = "node" NODE = "node"
GENERIC_NODE = "generic node" GENERIC_NODE = "generic node"
@@ -69,7 +68,6 @@ BuiltinErrors = [str(e) for e in {
BuiltinConcepts.CONCEPT_ALREADY_DEFINED, BuiltinConcepts.CONCEPT_ALREADY_DEFINED,
BuiltinConcepts.CONCEPT_EVAL_ERROR, BuiltinConcepts.CONCEPT_EVAL_ERROR,
BuiltinConcepts.CANNOT_RESOLVE_VALUE_ERROR, BuiltinConcepts.CANNOT_RESOLVE_VALUE_ERROR,
BuiltinConcepts.CONCEPT_NOT_INITIALIZED
}] }]
""" """
@@ -293,27 +291,6 @@ class ConceptEvalError(Concept):
return self.props["property_name"].value return self.props["property_name"].value
class ConceptNotInitialized(Concept):
def __init__(self, error=None, concept=None, property_name=None):
super().__init__(BuiltinConcepts.CONCEPT_NOT_INITIALIZED,
True,
False,
BuiltinConcepts.CONCEPT_NOT_INITIALIZED,
error)
self.set_prop("concept", concept)
def __repr__(self):
return f"ConceptNotInitialized(error={self.error}, concept={self.concept})"
@property
def error(self):
return self.body
@property
def concept(self):
return self.props["concept"].value
class EnumerationConcept(Concept): class EnumerationConcept(Concept):
def __init__(self, iteration=None): def __init__(self, iteration=None):
super().__init__(BuiltinConcepts.ENUMERATION, True, False, BuiltinConcepts.ENUMERATION, iteration) super().__init__(BuiltinConcepts.ENUMERATION, True, False, BuiltinConcepts.ENUMERATION, iteration)
+5 -2
View File
@@ -546,7 +546,7 @@ class Sheerka(Concept):
return from_db return from_db
# else return new Unknown concept # else return new Unknown concept
# Note that I don't call the new() method, as it use get() -> cyclic call # Note that I don't call the new() method to prevent cyclic call
unknown_concept = Concept() unknown_concept = Concept()
template = self.concepts_cache[str(BuiltinConcepts.UNKNOWN_CONCEPT)] template = self.concepts_cache[str(BuiltinConcepts.UNKNOWN_CONCEPT)]
unknown_concept.update_from(template) unknown_concept.update_from(template)
@@ -623,7 +623,7 @@ class Sheerka(Concept):
if self.isinstance(obj, BuiltinConcepts.RETURN_VALUE) and \ if self.isinstance(obj, BuiltinConcepts.RETURN_VALUE) and \
obj.status and \ obj.status and \
self.isinstance(obj.value, BuiltinConcepts.USER_INPUT): self.isinstance(obj.value, BuiltinConcepts.USER_INPUT):
return obj.value.text return obj.value.body
if not isinstance(obj, Concept): if not isinstance(obj, Concept):
return obj return obj
@@ -632,6 +632,9 @@ class Sheerka(Concept):
return obj.get_value() return obj.get_value()
if obj.body is not None: if obj.body is not None:
if (isinstance(obj.body, list) or isinstance(obj.body, set)) and len(obj.body) == 1:
return obj.body[0]
else:
return obj.body return obj.body
return obj if allow_none_body else self.new(BuiltinConcepts.CANNOT_RESOLVE_VALUE_ERROR, body=obj) return obj if allow_none_body else self.new(BuiltinConcepts.CANNOT_RESOLVE_VALUE_ERROR, body=obj)
+8 -1
View File
@@ -3,7 +3,7 @@ import os
from os import path from os import path
import shutil import shutil
from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept, UserInputConcept
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, Property from core.concept import Concept, PROPERTIES_TO_SERIALIZE, Property
from core.sheerka import Sheerka, ExecutionContext from core.sheerka import Sheerka, ExecutionContext
from evaluators.MutipleSameSuccessEvaluator import MultipleSameSuccessEvaluator from evaluators.MutipleSameSuccessEvaluator import MultipleSameSuccessEvaluator
@@ -320,9 +320,16 @@ def test_i_cannot_instantiate_when_properties_are_not_recognized():
@pytest.mark.parametrize("concept, allow_non_body, expected", [ @pytest.mark.parametrize("concept, allow_non_body, expected", [
(None, False, None), (None, False, None),
(3.14, False, 3.14), (3.14, False, 3.14),
("foo", False, "foo"),
(True, False, True),
(Concept("name", body="foo"), False, "foo"), (Concept("name", body="foo"), False, "foo"),
(Concept("name"), True, Concept("name")), (Concept("name"), True, Concept("name")),
(ConceptWithGetValue("name").set_prop("my_prop", "my_value"), True, "my_value"), (ConceptWithGetValue("name").set_prop("my_prop", "my_value"), True, "my_value"),
(ReturnValueConcept(value="return_value"), False, "return_value"),
(ReturnValueConcept(value=Concept(key=BuiltinConcepts.USER_INPUT, body="text"), status=True), False, "text"),
(ReturnValueConcept(value=UserInputConcept("text"), status=True), False, "text"),
(Concept("name", body=["foo", "bar"]), False, ["foo", "bar"]),
(Concept("name", body=["foo"]), False, "foo"),
]) ])
def test_i_can_get_value(concept, allow_non_body, expected): def test_i_can_get_value(concept, allow_non_body, expected):
sheerka = get_sheerka() sheerka = get_sheerka()