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, 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): sheerka, context = self.init_concepts() sheerka.set_debug(context, True) assert sheerka.debug_activated() sheerka.set_debug(context, False) assert not sheerka.debug_activated() def test_when_debug_mode_is_activated_context_are_in_debug_mode(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) sheerka.set_debug(root_context, True) context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1 sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1 sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1 sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child assert context.id == 1 assert context.debug_enabled assert sub_context.debug_enabled assert sub_context2.debug_enabled assert sub_sub_context.debug_enabled assert not root_context.debug_enabled def test_i_can_activate_debug_for_new_context(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) sheerka.set_debug(root_context, True) sheerka.set_explicit(root_context, True) sheerka.activate_debug_for(root_context, 1) context = ExecutionContext("test", Event(), sheerka, BuiltinConcepts.TESTING, None) # context.id = 1 assert context.debug_enabled def test_i_can_activate_debug_for_new_context_2(self): """ This time children is also requested :return: """ sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) sheerka.set_debug(root_context, True) sheerka.set_explicit(root_context, True) sheerka.activate_debug_for(root_context, 1, children=True) context = ExecutionContext("test", Event(), sheerka, BuiltinConcepts.TESTING, None) # context.id = 1 assert context.debug_enabled def test_i_can_activate_debug_for_a_context_using_push(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) service = sheerka.services[SheerkaDebugManager.NAME] sheerka.set_debug(root_context, True) sheerka.set_explicit(root_context, True) sheerka.activate_debug_for(root_context, 1) context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1 sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1 assert context.id == 1 assert 1 in service.context_cache assert context.debug_enabled assert not sub_context.debug_enabled assert not root_context.debug_enabled def test_global_debug_must_be_activated_to_activate_context_debug(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) service = sheerka.services[SheerkaDebugManager.NAME] sheerka.set_debug(root_context, False) sheerka.activate_debug_for(root_context, 1) context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1 assert context.id == 1 assert 1 in service.context_cache assert not context.debug_enabled def test_i_can_activate_debug_for_sub_children(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) service = sheerka.services[SheerkaDebugManager.NAME] sheerka.set_debug(root_context, True) sheerka.set_explicit(root_context, True) sheerka.activate_debug_for(root_context, 1, children=True) context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1 sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1 sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1 sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child assert context.id == 1 assert 1 in service.context_cache assert "1+" in service.context_cache assert context.debug_enabled assert sub_context.debug_enabled assert sub_context2.debug_enabled assert sub_sub_context.debug_enabled assert not root_context.debug_enabled def test_i_can_deactivate_debug_for_a_context(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) service = sheerka.services[SheerkaDebugManager.NAME] sheerka.set_debug(root_context, True) sheerka.set_explicit(root_context, True) sheerka.activate_debug_for(root_context, 1) sheerka.deactivate_debug_for(root_context, 1) context = root_context.push(BuiltinConcepts.NOP, None) assert context.id == 1 assert 1 not in service.context_cache assert not context.debug_enabled def test_i_can_deactivate_context_but_not_children(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) service = sheerka.services[SheerkaDebugManager.NAME] sheerka.set_debug(root_context, True) sheerka.set_explicit(root_context, True) sheerka.activate_debug_for(root_context, 1, True) sheerka.deactivate_debug_for(root_context, 1) context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1 sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1 sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1 sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child assert context.id == 1 assert 1 not in service.context_cache assert "1+" in service.context_cache assert not context.debug_enabled assert sub_context.debug_enabled assert sub_context2.debug_enabled assert sub_sub_context.debug_enabled def test_i_can_deactivate_context_and_children(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) service = sheerka.services[SheerkaDebugManager.NAME] sheerka.set_debug(root_context, True) sheerka.set_explicit(root_context, True) sheerka.activate_debug_for(root_context, 1) sheerka.deactivate_debug_for(root_context, 1, children=True) context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1 sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1 sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1 sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child assert 1 not in service.context_cache assert "1+" not in service.context_cache assert not context.debug_enabled assert not sub_context.debug_enabled assert not sub_context2.debug_enabled assert not sub_sub_context.debug_enabled def test_i_can_activate_debug_for_a_variable(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] sheerka.set_debug(context) sheerka.activate_debug_for(context, "Out") assert "Out" in service.variable_cache assert sheerka.debug_activated_for("Out") sheerka.deactivate_debug_for(context, "Out") assert "Out" not in service.variable_cache assert not sheerka.debug_activated_for("Out") def test_i_can_activate_debug_for_sub_children_using_the_simplified_form(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) service = sheerka.services[SheerkaDebugManager.NAME] sheerka.set_debug(root_context, True) sheerka.set_explicit(root_context, True) sheerka.activate_debug_for(root_context, "1+") context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1 sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1 sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1 sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child assert context.id == 1 assert 1 in service.context_cache assert "1+" in service.context_cache assert context.debug_enabled assert sub_context.debug_enabled assert sub_context2.debug_enabled assert sub_sub_context.debug_enabled assert not root_context.debug_enabled @pytest.mark.parametrize("item_type", [ "vars", "rules", "concepts" ]) def test_i_can_add_a_debug_item(self, item_type): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service_name = "service_name" method_name = "method_name" item = "id" context_id = 1 context_children = True debug_id = 1 debug_children = True service.add_or_update_debug_item(context, item_type, item, service_name, method_name, context_id, context_children, debug_id, debug_children, True) item_container = f"debug_{item_type}_settings" assert getattr(service, item_container) == [DebugItem( item, service_name, method_name, context_id, context_children, debug_id, debug_children, True )] def test_i_manage_debug_item_default_values(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.add_or_update_debug_item(context, "vars", item="item") assert service.debug_vars_settings == [ DebugItem("item", None, None, None, False, None, False, True) ] def test_i_can_update_debug_item(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.add_or_update_debug_item(context, "vars", "item", "service_name", "method_name", enabled=True) service.add_or_update_debug_item(context, "vars", "item2", "service_name", "method_name", enabled=True) service.add_or_update_debug_item(context, "vars", "item", "service_name", "method_name", enabled=False) assert service.debug_vars_settings == [ DebugItem("item", "service_name", "method_name", None, False, None, False, False), DebugItem("item2", "service_name", "method_name", None, False, None, False, True), ] @pytest.mark.parametrize("settings, expected", [ ({"service": "my_service"}, True), ({"service": "other_service"}, False), ({"method": "my_method"}, True), ({"method": "other_method"}, False), ]) def test_i_can_compute_debug(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.compute_debug(context, "my_service", "my_method") == expected def test_i_can_compute_debug_for_context(self): sheerka, root_context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(root_context, True) context1 = root_context.push(BuiltinConcepts.TESTING, None) context2 = root_context.push(BuiltinConcepts.TESTING, None) service.add_or_update_debug_item(root_context, "vars", context_id=context1.id) assert service.compute_debug(context1, "my_service", "my_method") assert not service.compute_debug(context2, "my_service", "my_method") @pytest.mark.parametrize("context_children, expected", [ (True, True), (False, False), ]) def test_i_can_compute_debug_for_context_children(self, context_children, expected): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) sub_context = context.push(BuiltinConcepts.TESTING, None) sub_sub_context = sub_context.push(BuiltinConcepts.TESTING, None) service.add_or_update_debug_item(context, "vars", context_id=sub_context.id, context_children=context_children) assert service.compute_debug(sub_sub_context, "my_service", "my_method") == expected def test_compute_debug_returns_true_in_case_of_conflict(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="my_service", enabled=True) service.add_or_update_debug_item(context, "rules", service="my_service", enabled=False) assert service.compute_debug(context, "my_service", "my_method") @pytest.mark.parametrize("settings, expected", [ ({"service": "my_service"}, False), # by default debug item is False if item is not specified ({"service": "other_service"}, False), ({"method": "my_method"}, False), # by default debug item is False if item is not specified ({"method": "other_method"}, False), ({"item": "my_item"}, True), ({"item": "other_item"}, False), ({"debug_id": 10}, True), ({"debug_id": 0}, False), ]) def test_i_can_compute_debug_item(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, "rules", **settings) assert service.compute_debug_item("rules", context, "my_service", "my_method", "my_item", 10) == expected def test_i_can_compute_debug_item_using_the_correct_service(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.add_or_update_debug_item(context, "rules", service="service1", item="item", enabled=True) service.add_or_update_debug_item(context, "rules", service="service2", item="item", enabled=False) assert service.compute_debug_item("rules", context, "service1", "my_method", "item", 10) assert not service.compute_debug_item("rules", context, "service2", "my_method", "item", 10) def test_i_can_compute_debug_item_using_the_correct_method(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.add_or_update_debug_item(context, "rules", method="method1", item="item", enabled=True) service.add_or_update_debug_item(context, "rules", method="method2", item="item", enabled=False) assert service.compute_debug_item("rules", context, "my_service", "method1", "item", 10) assert not service.compute_debug_item("rules", context, "my_service", "method2", "item", 10) def test_i_can_compute_debug_item_using_the_correct_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) service.add_or_update_debug_item(context, "rules", context_id=999, item="item", enabled=False) assert service.compute_debug_item("rules", context, "my_service", "my_method", "item", 10) assert not service.compute_debug_item("rules", another_context, "my_service", "my_method", "item", 10) def test_i_can_compute_debug_item_using_the_correct_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.compute_debug_item("rules", context, "my_service", "my_method", "item", 10) assert service.compute_debug_item("rules", sub_context, "my_service", "my_method", "item", 10) assert not service.compute_debug_item("rules", another_context, "my_service", "my_method", "item", 10) def test_compute_debug_item_returns_false_in_case_of_conflict(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.add_or_update_debug_item(context, "concepts", service="my_service", item="item", enabled=True) service.add_or_update_debug_item(context, "concepts", method="my_method", item="item", enabled=False) assert not service.compute_debug_item("concepts", context, "my_service", "my_method", "item", 10) def test_compute_debug_item_returns_false_in_case_of_conflict_2(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.add_or_update_debug_item(context, "concepts", item="item", enabled=True) service.add_or_update_debug_item(context, "concepts", item="*", enabled=False) assert not service.compute_debug_item("concepts", context, "my_service", "my_method", "item", 10) @pytest.mark.parametrize("settings", [ {"service": "my_service"}, {"method": "my_method"}, ]) def test_by_default_item_is_disabled(self, settings): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.add_or_update_debug_item(context, "concepts", **settings) assert not service.compute_debug_item("concepts", context, "my_service", "my_method", "item", 10) def test_i_can_activate_debug_for_all_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", item="*") assert service.compute_debug_item("vars", context, "my_service", "my_method", "whatever variable name", 0) def test_disabled_is_the_highest_priority(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.add_or_update_debug_item(context, "concepts", service="my_service", item="item", enabled=True) service.add_or_update_debug_item(context, "concepts", method="my_method", item="item", enabled=False) service.add_or_update_debug_item(context, "concepts", context_id=context.id, item="item", enabled="value") assert not service.compute_debug_item("concepts", context, "my_service", "my_method", "item", 10) def test_string_value_is_the_second_best_priority(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.add_or_update_debug_item(context, "concepts", service="my_service", item="item", enabled=True) service.add_or_update_debug_item(context, "concepts", context_id=context.id, item="item", enabled="value") assert service.compute_debug_item("concepts", context, "my_service", "my_method", "item", 10) == "value" def test_i_can_reset_debug_settings(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.add_or_update_debug_item(context, "concepts", service="my_service") service.add_or_update_debug_item(context, "vars", service="my_service") service.add_or_update_debug_item(context, "rules", service="my_service") sheerka.reset_debug(context) assert len(service.debug_vars_settings) == 0 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)), ([None, "1+"], {}, (None, None, None, 1, True, None, True)), ([None, "xxx"], {}, (None, None, None, None, False, None, True)), (["s.m.v", "1+"], {}, ("v", "s", "m", 1, True, None, True)), ([None, None, 1], {}, (None, None, None, None, False, 1, True)), ([None, "", 1], {}, (None, None, None, None, False, 1, True)), (["s.m.v", "1+", 10], {}, ("v", "s", "m", 1, True, 10, True)), (["s.m.v", "1+", 10], {"variable": "my_var"}, ("my_var", "s", "m", 1, True, 10, True)), (["s.m.v", "1+", 10], {"service": "my_service"}, ("v", "my_service", "m", 1, True, 10, True)), (["s.m.v", "1+", 10], {"method": "my_method"}, ("v", "s", "my_method", 1, True, 10, True)), (["s.m.v", "1+", 10], {"context_id": 155}, ("v", "s", "m", 155, True, 10, True)), (["s.m.v", "1+", 10], {"context_children": False}, ("v", "s", "m", 1, False, 10, True)), (["s.m.v", "1+", 10], {"debug_id": 155}, ("v", "s", "m", 1, True, 155, True)), (["s.m.v", "1+", 10], {"enabled": False}, ("v", "s", "m", 1, True, 10, False)), ]) def test_i_can_parse_args(self, args, kwargs, expected): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] assert service.parse_debug_args("variable", *args, **kwargs) == expected def test_i_can_debug_var(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] sheerka.debug_var(context, "s.m.v", "1+", 10, variable="my_var") assert service.debug_vars_settings == [ DebugItem("my_var", "s", "m", 1, True, 10, False, True) ] assert service.debug_concepts_settings == [] assert service.debug_rules_settings == [] def test_i_can_debug_rule(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] sheerka.debug_rule(context, "s.m.v", "1+", 10, rule="my_rule") assert service.debug_rules_settings == [ DebugItem("my_rule", "s", "m", 1, True, 10, False, True) ] assert service.debug_concepts_settings == [] assert service.debug_vars_settings == [] def test_i_can_debug_concept(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] sheerka.debug_concept(context, "s.m.v", "1+", 10, concept="my_concept") assert service.debug_concepts_settings == [ DebugItem("my_concept", "s", "m", 1, True, 10, False, True) ] assert service.debug_rules_settings == [] assert service.debug_vars_settings == [] def test_state_is_saved_and_restored(self): sheerka = self.get_sheerka() ExecutionContext.ids.clear() root_context = self.get_context(sheerka) sheerka.set_debug(root_context, True) sheerka.set_explicit(root_context, False) sheerka.activate_debug_for(root_context, 1, children=True) sheerka.activate_debug_for(root_context, "SomeVar") sheerka.debug_var(root_context, "service_name.*.var") sheerka.debug_rule(root_context, 1) sheerka.debug_concept(root_context, 1001) another_service = SheerkaDebugManager(sheerka) another_service.initialize_deferred(root_context, True) assert another_service.activated assert not another_service.explicit assert another_service.context_cache == {1, "1+"} assert another_service.variable_cache == {"SomeVar"} assert another_service.debug_vars_settings == [ DebugItem('var', 'service_name', None, None, False, None, False, True)] assert another_service.debug_rules_settings == [ 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': sheerka.concepts_ids[BuiltinConcepts.NOT_FOUND], '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', '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')"