Improved PyhtonEvaluator in order to use methods that need context

This commit is contained in:
2020-05-18 22:35:59 +02:00
parent c822ff6a7f
commit 95dc147bbd
14 changed files with 187 additions and 74 deletions
@@ -143,6 +143,16 @@ class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
assert sheerka.isinstance(res.body, BuiltinConcepts.CHICKEN_AND_EGG)
assert set(res.body.body) == {one, two, five}
def test_i_can_give_the_same_information_in_many_ways(self):
sheerka, context, one, two = self.init_concepts("one", "two")
service = sheerka.services[SheerkaComparisonManager.NAME]
service.is_greater_than(context, "prop_name", two, one)
service.is_less_than(context, "prop_name", one, two)
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
assert weighted == {"1001": 1, "1002": 2}
def test_methods_are_correctly_bound(self):
sheerka, context, one, two = self.init_concepts("one", "two")
res = sheerka.is_greater_than(context, "prop_name", two, one)
+19
View File
@@ -25,6 +25,7 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("text, expected", [
("1 + 1", 2),
("test()", "I have access to Sheerka !"),
("sheerka.test()", "I have access to Sheerka !"),
("a=10\na", 10),
])
@@ -37,6 +38,24 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
assert evaluated.status
assert evaluated.value == expected
def test_i_can_eval_using_context(self):
context = self.get_context()
parsed = PythonParser().parse(context, "test_using_context('value for param1', 10)")
evaluated = PythonEvaluator().eval(context, parsed)
assert evaluated.status
assert evaluated.value.startswith("I have access to Sheerka ! param1='value for param1', param2=10, event=")
def test_i_can_eval_using_context_when_self_is_not_sheerka(self):
sheerka, context = self.init_concepts()
parsed = PythonParser().parse(context, "create_new_concept(Concept('foo'))")
evaluated = PythonEvaluator().eval(context, parsed)
assert evaluated.status
assert sheerka.has_key("foo")
@pytest.mark.parametrize("concept", [
Concept("foo"),
Concept("foo", body="2"),
+30 -1
View File
@@ -192,7 +192,7 @@ as:
assert sheerka.isinstance(res[0].value, BuiltinConcepts.NOP)
def test_i_can_recognize_concept_with_variable(self):
sheerka, context, concept_foo, concept_hello = self.init_concepts(
sheerka, context, concept_foo, concept_hello = self.init_concepts(
"foo",
Concept(name="hello a").def_var("a"),
create_new=True)
@@ -869,6 +869,35 @@ as:
# assert res[0].status
# assert isinstance(res[0].body, Concept)
def test_i_can_express_comparison(self):
definitions = [
"def concept one",
"def concept two",
"def concept three",
"def concept four",
]
sheerka = self.init_scenario(definitions)
res = sheerka.evaluate_user_input("is_greater_than('some_prop', two, one)")
assert res[0].status
res = sheerka.evaluate_user_input("is_less_than('some_prop', two, three)")
assert res[0].status
res = sheerka.evaluate_user_input("get_concepts_weights('some_prop')")
assert res[0].status
assert res[0].body == {'1001': 1, '1002': 2, '1003': 3}
# test i use a concept to define relation
sheerka.evaluate_user_input("def concept a > b as is_greater_than('some_prop', a, b)")
res = sheerka.evaluate_user_input("eval four > three")
assert res[0].status
res = sheerka.evaluate_user_input("get_concepts_weights('some_prop')")
assert res[0].status
assert res[0].body == {'1001': 1, '1002': 2, '1003': 3, '1004': 4}
class TestSheerkaNonRegFile(TestUsingFileBasedSheerka):
def test_i_can_def_several_concepts(self):