Fixed bug when evaluating numbers several times

This commit is contained in:
2020-11-27 11:01:02 +01:00
parent cac732bd93
commit 4f899280c4
16 changed files with 887 additions and 491 deletions
+274 -131
View File
@@ -1,7 +1,7 @@
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.sheerka.ExecutionContext import ExecutionContext
from core.sheerka.services.SheerkaDebugManager import SheerkaDebugManager, DebugVarSetting, DebugRuleSetting
from core.sheerka.services.SheerkaDebugManager import SheerkaDebugManager, DebugItem
from sdp.sheerkaDataProvider import Event
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@@ -224,6 +224,66 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
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),
@@ -235,8 +295,8 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
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
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()
@@ -245,109 +305,16 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
context1 = root_context.push(BuiltinConcepts.TESTING, None)
context2 = root_context.push(BuiltinConcepts.TESTING, None)
service.debug_var(root_context, context_id=context1.id)
service.add_or_update_debug_item(root_context, "vars", 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
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_var_for_context_children(self, context_children, expected):
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)
@@ -355,49 +322,223 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
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
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_i_can_update_debug_setting(self):
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.debug_var(context, service="my_service", enabled=True)
service.debug_var(context, service="my_service", enabled=False)
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 len(service.debug_vars_settings) == 1
assert not service.debug_vars_settings[0].enabled
assert service.compute_debug(context, "my_service", "my_method")
service.debug_var(context, service="my_service", enabled=True)
assert len(service.debug_vars_settings) == 1
assert service.debug_vars_settings[0].enabled
@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.debug_var(context, service="my_service")
assert service.compute_debug("my_service", "my_method", context)
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")
service.reset_debug(context)
assert not service.compute_debug("my_service", "my_method", context)
sheerka.reset_debug(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),
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_compute_rules_debug_settings(self, settings, expected):
def test_i_can_parse_args(self, args, kwargs, 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
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()
@@ -408,8 +549,9 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
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")
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)
@@ -418,8 +560,9 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
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)]
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)]