Files
Sheerka-Old/tests/core/test_SheerkaDebugManager.py
T

569 lines
26 KiB
Python

import pytest
from core.builtin_concepts import BuiltinConcepts
from core.sheerka.ExecutionContext import ExecutionContext
from core.sheerka.services.SheerkaDebugManager import SheerkaDebugManager, DebugItem
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("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("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)),
([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)]