import pytest from core.builtin_concepts import BuiltinConcepts from core.sheerka.ExecutionContext import ExecutionContext from core.sheerka.services.SheerkaDebugManager import SheerkaDebugManager, DebugVarSetting, DebugRuleSetting from sdp.sheerkaDataProvider import Event from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka 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("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.debug_var(context, **settings) assert service.compute_debug("my_service", "my_method", context) == 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.debug_var(root_context, context_id=context1.id) assert service.compute_debug("my_service", "my_method", context1) assert not service.compute_debug("my_service", "my_method", context2) def test_i_can_disable_debug_setting(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.debug_var(context, service="my_service", enabled=False) assert not service.compute_debug("my_service", "my_method", context) def test_i_can_compute_combination_of_debug_settings(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.debug_var(context, service="my_service", enabled=True) service.debug_var(context, method="my_method", enabled=False) service.debug_var(context, variable="xxx", enabled=False) # is not used service.debug_var(context, debug_id=1, enabled=False) # is not used assert not service.compute_debug("my_service", "my_method", context) # False > True assert service.compute_debug("my_service", "another_method", context) assert not service.compute_debug("another_service", "my_method", context) def test_variable_debug_is_deactivated_by_default(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) assert not service.compute_var_debug("my_service", "my_method", 0, "my_variable", 0) def test_i_can_activate_variable_debug_for_all_variables(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.debug_var(context, variable="*") assert service.compute_var_debug("my_service", "my_method", 0, "my_variable", 0) == True def test_i_can_activate_variable_display_for_all_variables(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.debug_var(context, variable="*", enabled='list') assert service.compute_var_debug("my_service", "my_method", 0, "my_variable", 0) == 'list' def test_i_can_disable_variable_debug(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.debug_var(context, variable="my_variable", enabled=False) assert not service.compute_var_debug("my_service", "my_method", 0, "my_variable", 0) @pytest.mark.parametrize("enabled_1, enabled_2, expected", [ (False, False, False), (False, True, False), (False, 'list', False), (True, False, False), (True, True, True), (True, 'list', 'list'), ('list', False, False), ('list', True, 'list'), ('list', 'list', 'list'), ]) def test_i_can_compute_combination_of_debug_var_settings(self, enabled_1, enabled_2, expected): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.debug_var(context, variable="my_variable", enabled=enabled_1) service.debug_var(context, variable="*", enabled=enabled_2) assert service.compute_var_debug("my_service", "my_method", 0, "my_variable", 0) == expected @pytest.mark.parametrize("settings, expected", [ ({"service": "my_service"}, True), ({"service": "other_service"}, False), ({"method": "my_method"}, True), ({"method": "other_method"}, False), ({"context_id": 0}, True), ({"context_id": 1}, False), ]) def test_service_and_method_and_context_id_are_tested_before_disabling_a_variable(self, settings, expected): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) settings["variable"] = "my_variable" service.debug_var(context, **settings, enabled=True) assert service.compute_var_debug("my_service", "my_method", 0, "my_variable", 0) == expected @pytest.mark.parametrize("context_children, expected", [ (True, True), (False, False), ]) def test_i_can_compute_debug_var_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.debug_var(context, context_id=sub_context.id, context_children=context_children) assert service.compute_debug("my_service", "my_method", sub_sub_context) == expected def test_i_can_update_debug_setting(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.debug_var(context, service="my_service", enabled=True) service.debug_var(context, service="my_service", enabled=False) assert len(service.debug_vars_settings) == 1 assert not service.debug_vars_settings[0].enabled service.debug_var(context, service="my_service", enabled=True) assert len(service.debug_vars_settings) == 1 assert service.debug_vars_settings[0].enabled def test_i_can_reset_debug_settings(self): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.debug_var(context, service="my_service") assert service.compute_debug("my_service", "my_method", context) service.reset_debug(context) assert not service.compute_debug("my_service", "my_method", context) @pytest.mark.parametrize("settings, expected", [ ({"rule": "1"}, True), ({"rule": "2"}, False), ({"context_id": 0}, True), ({"context_id": 1}, False), ({"debug_id": 0}, True), ({"debug_id": 1}, False), ]) def test_i_can_compute_rules_debug_settings(self, settings, expected): sheerka, context = self.init_concepts() service = sheerka.services[SheerkaDebugManager.NAME] service.set_debug(context, True) service.debug_rule(context, **settings) assert service.compute_debug_rule("1", 0, 0) == expected 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_rule(root_context, "1", 10, 15) sheerka.debug_var(root_context, service="service_name") 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_rules_settings == [ DebugRuleSetting("1", 10, 15, True) ] assert another_service.debug_vars_settings == [ DebugVarSetting('service_name', None, None, None, False, None, False, True)]