First implementation of Debugger for SyaNodeParser

This commit is contained in:
2020-12-03 21:50:48 +01:00
parent 4f899280c4
commit 8b86998225
48 changed files with 1781 additions and 1795 deletions
+347 -1
View File
@@ -1,12 +1,23 @@
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, NotInit
from core.sheerka.ExecutionContext import ExecutionContext
from core.sheerka.services.SheerkaDebugManager import SheerkaDebugManager, DebugItem
from core.sheerka.services.SheerkaDebugManager import SheerkaDebugManager, DebugItem, ConceptDebugObj
from parsers.PythonParser import PythonNode
from sdp.sheerkaDataProvider import Event
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class DummyObj:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return f"DummyObj(a={self.a}, b={self.b})"
class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
def test_i_can_activate_debug(self):
@@ -478,12 +489,85 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
assert len(service.debug_rules_settings) == 0
assert len(service.debug_concepts_settings) == 0
@pytest.mark.parametrize("settings, expected", [
({"service": "my_service", "item": "var_name"}, {"var_name"}),
({"method": "my_method", "item": "var_name"}, {"var_name"}),
({"debug_id": 0, "item": "var_name"}, {"var_name"}),
({"service": "my_service", "item": "*"}, {"*"}),
({"method": "my_method", "item": "*"}, {"*"}),
({"debug_id": 0, "item": "*"}, {"*"}),
({"service": "my_service"}, set()),
({"method": "my_method"}, set()),
({"debug_id": 0}, set()),
])
def test_i_can_get_enabled_items(self, settings, expected):
sheerka, context = self.init_concepts()
service = sheerka.services[SheerkaDebugManager.NAME]
service.set_debug(context, True)
service.add_or_update_debug_item(context, "vars", **settings)
assert service.get_enabled_items("vars", context, "my_service", "my_method", 0) == expected
def test_i_can_get_enabled_items_for_context(self):
sheerka, context = self.init_concepts()
another_context = context.push(BuiltinConcepts.TESTING, None)
service = sheerka.services[SheerkaDebugManager.NAME]
service.set_debug(context, True)
service.add_or_update_debug_item(context, "rules", context_id=context.id, item="item", enabled=True)
assert service.get_enabled_items("rules", context, "my_service", "my_method", 10) == {"item"}
assert service.get_enabled_items("rules", another_context, "my_service", "my_method", 10) == set()
def test_i_can_get_enabled_items_for_sub_context(self):
sheerka, context = self.init_concepts()
sub_context = context.push(BuiltinConcepts.TESTING, None)
another_context = self.get_context(sheerka)
service = sheerka.services[SheerkaDebugManager.NAME]
service.set_debug(context, True)
service.add_or_update_debug_item(context, "rules", context_id=context.id, item="item", context_children=True)
assert service.get_enabled_items("rules", context, "my_service", "my_method", 10) == {"item"}
assert service.get_enabled_items("rules", sub_context, "my_service", "my_method", 10) == {"item"}
assert service.get_enabled_items("rules", another_context, "my_service", "my_method", 10) == set()
def test_i_can_get_all_enabled_items(self):
sheerka, context = self.init_concepts()
service = sheerka.services[SheerkaDebugManager.NAME]
service.set_debug(context, True)
service.add_or_update_debug_item(context, "vars", service="s1", item="v11")
service.add_or_update_debug_item(context, "vars", service="s2", item="v21")
service.add_or_update_debug_item(context, "vars", method="m1", item="v12")
service.add_or_update_debug_item(context, "vars", method="m2", item="v22")
service.add_or_update_debug_item(context, "vars", debug_id=1, item="v13")
service.add_or_update_debug_item(context, "vars", debug_id=2, item="v23")
service.add_or_update_debug_item(context, "vars", service="s1", method="m1", item="v111")
assert service.get_enabled_items("vars", context, "s1", "m1", 1) == {"v11", "v12", "v13", "v111"}
def test_i_can_manage_duplicates_when_get_enabled_items(self):
sheerka, context = self.init_concepts()
service = sheerka.services[SheerkaDebugManager.NAME]
service.set_debug(context, True)
service.add_or_update_debug_item(context, "vars", service="s1", item="var_name")
service.add_or_update_debug_item(context, "vars", method="m1", item="*")
service.add_or_update_debug_item(context, "vars", debug_id=1, item="var_name")
service.add_or_update_debug_item(context, "vars", service="s1", method="m1", item="*")
assert service.get_enabled_items("vars", context, "s1", "m1", 1) == {"var_name", "*"}
@pytest.mark.parametrize("args, kwargs, expected", [
(["my_service.my_method.my_var"], {}, ("my_var", "my_service", "my_method", None, False, None, True)),
(["*.*.my_var"], {}, ("my_var", None, None, None, False, None, True)),
(["my_service"], {}, (None, "my_service", None, None, False, None, True)),
(["my_service.my_method"], {}, (None, "my_service", "my_method", None, False, None, True)),
(["*.*.*"], {}, ("*", None, None, None, False, None, True)),
(["*.*.*.*.*"], {}, ("*.*.*", None, None, None, False, None, True)),
(["s.m.var.in.multi.parts"], {}, ("var.in.multi.parts", "s", "m", None, False, None, True)),
([1], {}, ("1", None, None, None, False, None, True)),
(["", 1], {}, (None, None, None, 1, False, None, True)),
([None, 1], {}, (None, None, None, 1, False, None, True)),
@@ -566,3 +650,265 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
DebugItem('1', None, None, None, False, None, False, True)]
assert another_service.debug_concepts_settings == [
DebugItem('1001', None, None, None, False, None, False, True)]
def test_i_can_inspect_concept_all_attributes(self):
sheerka, context, foo = self.init_concepts("foo")
foo.values() # freeze known attributes
foo.set_value("new_var", "var_value")
res = sheerka.inspect(context, foo)
assert res.body == {"#type#": "Concept",
"id": foo.id,
"name": foo.name,
"key": foo.key,
"value.new_var": "'var_value'"}
def test_i_can_inspect_concept_specified_attributes(self):
sheerka, context, foo = self.init_concepts("foo")
foo.values() # freeze known attributes
foo.set_value("new_var", "var_value")
res = sheerka.inspect(context, foo, "id", "value.new_var")
assert res.body == {"id": foo.id, "value.new_var": "'var_value'"}
def test_i_can_inspect_concept_specified_attributes_using_short_name(self):
sheerka, context, foo, bar = self.init_concepts(Concept("foo").def_var("var_name", "default_value"), "bar")
foo.set_value("var_name", "var_value")
foo.get_compiled()["var_name"] = bar
res = sheerka.inspect(context, foo, "id", "var_name")
assert res.body == {"id": foo.id,
"meta.var_name": "'default_value'",
"compiled.var_name": bar,
"value.var_name": "'var_value'"}
def test_i_can_inspect_object_all_attributes(self):
sheerka, context = self.init_concepts()
python_node = PythonNode("one + 1").init_ast()
res = sheerka.inspect(context, python_node)
assert set(res.body.keys()) == {"#type#", 'ast_', 'ast_str', 'compiled', 'objects', 'source'}
def test_i_can_inspect_object_specified_attributes(self):
sheerka, context = self.init_concepts()
python_node = PythonNode("one + 1").init_ast()
res = sheerka.inspect(context, python_node, "#type#", "source", "ast_str")
assert res.body == {
"#type#": "PythonNode",
'ast_str': "Expression(body=BinOp(left=Name(id='one'), op=Add(), right=Constant(value=1)))",
'source': 'one + 1'}
def test_i_can_inspect_object_with_as_bag_all_attributes(self):
sheerka, context = self.init_concepts()
res = sheerka.inspect(context, context)
assert res.body == {'#type#': 'ExecutionContext',
'_children': [],
'action': '__TESTING',
'concepts': None,
'context': None,
'desc': None,
'digest': 'xxx',
'elapsed': 0,
'elapsed_str': '0.0 ms',
'id': context.id,
'inputs': {},
'obj': None,
'status': None,
'values': {},
'who': 'test'}
def test_i_can_inspect_object_with_as_bag_specified_attributes(self):
sheerka, context = self.init_concepts()
res = sheerka.inspect(context, context, "who", "_children")
assert res.body == {'_children': [],
'who': 'test'}
def test_i_can_inspect_object_with_concept(self):
sheerka, context, foo = self.init_concepts("foo")
foo.values() # freeze known attributes
sheerka.set_attr(foo, "new_var", "var_value")
dummy = DummyObj(foo, "value")
res = sheerka.inspect(context, dummy)
assert res.body == {'#type#': 'DummyObj', 'a': foo, 'b': 'value'}
def test_i_can_inspect_object_with_concept_when_as_bag(self):
sheerka, context, foo = self.init_concepts("foo")
foo.values() # freeze known attributes
sheerka.set_attr(foo, "new_var", "var_value")
dummy = DummyObj(foo, "value")
res = sheerka.inspect(context, dummy, as_bag=True)
assert res.body == {'#type#': 'DummyObj',
'a': {'#type#': 'Concept',
'id': '1001',
'key': 'foo',
'name': 'foo',
'value.new_var': "'var_value'"},
'b': 'value'}
def test_i_can_inspect_object_with_concept_when_values(self):
sheerka, context, foo = self.init_concepts("foo")
foo.values() # freeze known attributes
sheerka.set_attr(foo, "new_var", "var_value")
dummy = DummyObj(foo, "value")
res = sheerka.inspect(context, dummy, values=True)
assert res.body == {'#type#': 'DummyObj', 'a': ConceptDebugObj(foo), 'b': 'value'}
def test_i_can_inspect_concept_with_concept(self):
sheerka, context, foo, bar = self.init_concepts(Concept("foo").def_var("var_name"), "bar")
foo.set_value("var_name", bar)
res = sheerka.inspect(context, foo)
assert res.body == {'#type#': 'Concept',
'id': '1001',
'key': 'foo',
'name': 'foo',
'value.var_name': bar}
def test_i_can_inspect_concept_with_concept_when_as_bag(self):
sheerka, context, foo, bar = self.init_concepts(Concept("foo").def_var("var_name"), "bar")
foo.set_value("var_name", bar)
res = sheerka.inspect(context, foo, as_bag=True)
assert res.body == {'#type#': 'Concept',
'id': '1001',
'key': 'foo',
'name': 'foo',
'value.var_name': {'#type#': 'Concept',
'id': '1002',
'key': 'bar',
'name': 'bar'}}
def test_i_can_inspect_concept_with_concept_when_values(self):
sheerka, context, foo, bar = self.init_concepts(Concept("foo").def_var("var_name"), "bar")
foo.set_value("var_name", bar)
res = sheerka.inspect(context, foo, values=True)
assert res.body == {'#type#': 'Concept',
'id': '1001',
'key': 'foo',
'name': 'foo',
'value.var_name': ConceptDebugObj(bar)}
def test_i_can_inspect_execution_context_item(self):
sheerka, context = self.init_concepts()
ExecutionContext.ids.clear()
sheerka.evaluate_user_input("def concept one as 1")
results = list(sheerka.get_last_results(context).body)
user_input_ret_val = results[0].inputs["user_input"]
return_values = results[0].values["return_values"]
res = sheerka.inspect(context, 0)
assert res.body == {'inputs': {'user_input': user_input_ret_val},
'values.return_values': return_values}
def test_i_can_inspect_execution_context_values(self):
sheerka, context = self.init_concepts()
ExecutionContext.ids.clear()
sheerka.evaluate_user_input("def concept one as 1")
results = list(sheerka.get_last_results(context).body)
user_input_ret_val = results[0].inputs["user_input"]
return_values = results[0].values["return_values"]
res = sheerka.inspect(context, 0, values=True)
assert res.body == {'inputs': {'user_input': user_input_ret_val},
'values.return_values': [ConceptDebugObj(return_values[0].body.body)]}
def test_i_can_inspect_when_a_property_does_not_exist(self):
sheerka, context, foo = self.init_concepts("foo")
foo.values() # freeze known attributes
sheerka.set_attr(foo, "new_var", "var_value")
dummy = DummyObj(foo, "value")
res = sheerka.inspect(context, dummy, "#type#", "fake", "a", "b")
assert res.body == {'#type#': 'DummyObj',
'fake': "** Not Found **",
'a': foo,
'b': 'value'}
def test_i_can_inspect_when_properties_are_specified_several_times(self):
sheerka, context, foo = self.init_concepts("foo")
foo.values() # freeze known attributes
sheerka.set_attr(foo, "new_var", "var_value")
dummy = DummyObj(foo, "value")
res = sheerka.inspect(context, dummy, "#type#", "a", "b", "a")
assert res.body == {'#type#': 'DummyObj',
'a': foo,
'b': 'value'}
def test_i_cannot_inspect_execution_context_item_if_no_last_item(self):
sheerka, context = self.init_concepts()
res = sheerka.inspect(context, 0)
assert res.body == {'#type#': 'NotFound',
'id': '70',
'key': '__NOT_FOUND',
'name': '__NOT_FOUND',
'body': 'no digest'}
def test_i_can_inspect_values(self):
sheerka, context, table, how, little = self.init_concepts(
"table",
Concept("how is x").def_var("x"),
Concept("little x").def_var("x"),
create_new=True
)
return_values = sheerka.evaluate_user_input("how is little table")
res = sheerka.inspect(context, return_values[0], values=True)
concept_debug_obj = ConceptDebugObj(return_values[0].body)
assert res.body == {
'body': concept_debug_obj,
'#type#': 'ReturnValueConcept',
'id': '43',
'key': '__RETURN_VALUE',
'message': None,
'name': '__RETURN_VALUE',
'parents': [concept_debug_obj],
'status': True,
'value': concept_debug_obj,
'who': 'evaluators.OneSuccess'}
# I also can print it using bag
res = sheerka.inspect(context, return_values[0], '#type#', "who", "status", "value", values=True, as_bag=True)
assert res.body == {'#type#': 'ReturnValueConcept',
'who': 'evaluators.OneSuccess',
'status': True,
'value': {'#type#': 'Concept',
'compiled.x': {'#type#': 'Concept',
'compiled.x': {'#type#': 'Concept',
'id': '1001',
'key': 'table',
'name': 'table'},
'id': '1003',
'key': 'little __var__0',
'meta.x': "'table'",
'name': 'little x'},
'id': '1002',
'key': 'how is __var__0',
'meta.x': "'little table'",
'name': 'how is x'}}
def test_i_can_display_meta_and_compile_attributes_using_concept_debug_obj(self):
foo = Concept("foo", id=1001, key="foo_key").def_var("x", "x_meta").def_var("y", "y_meta")
foo.get_compiled()["x"] = Concept("bar", id=1002).def_var("a", "a_meta").set_value("a", "a_value")
foo.set_value("x", "x_value")
foo.set_value("y", NotInit)
foo.values() # freeze attributes
foo.set_value("z", "extra_value")
assert str(ConceptDebugObj(foo)) == \
"(:foo|1001:meta.x='x_meta', meta.y='y_meta', compiled.x=(:bar|1002:meta.a='a_meta', value.a='a_value'), value.x='x_value', value.z='extra_value')"