import pytest from core.sheerka.services.SheerkaFunctionsParametersHistory import SheerkaFunctionsParametersHistory, \ FunctionParametersObj from core.utils import sheerka_deepcopy from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka class TestSheerkaFunctionsParametersHistory(TestUsingMemoryBasedSheerka): def test_i_can_add_a_parameter_value(self): sheerka, context = self.init_concepts(cache_only=False) sheerka.om.test_only_unfreeze() service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed sheerka.om.freeze() service.record_function_parameter(context, "function", 1, "10") service.record_function_parameter(context, "function", 2, "True") service.record_function_parameter(context, "function", 3, "'string value'") assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj( context.event.get_digest(), "function", { 1: [('10', 1)], 2: [('True', 1)], 3: [("'string value'", 1)] })} # and i can serialize sheerka.om.commit(context) from_db = sheerka.om.current_sdp().get(service.FUNCTIONS_PARAMETERS_ENTRY, "function") assert from_db.event_id == context.event.get_digest() assert from_db.name == "function" assert from_db.params == { 1: [('10', 1)], 2: [('True', 1)], 3: [("'string value'", 1)] } def test_i_can_add_the_same_value_multiple_times(self): sheerka, context = self.init_concepts(cache_only=True) sheerka.om.test_only_unfreeze() service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed sheerka.om.freeze() service.record_function_parameter(context, "function", 1, "10") service.record_function_parameter(context, "function", 1, "20") service.record_function_parameter(context, "function", 2, "True") service.record_function_parameter(context, "function", 1, "20") assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj( context.event.get_digest(), "function", { 1: [('10', 1), ('20', 2)], 2: [('True', 1)], })} def test_i_can_specify_parameter_in_any_order(self): sheerka, context = self.init_concepts() sheerka.om.test_only_unfreeze() service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed sheerka.om.freeze() service.record_function_parameter(context, "function", 3, "'string value'") service.record_function_parameter(context, "function", 2, "True") assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj( context.event.get_digest(), "function", { 2: [('True', 1)], 3: [("'string value'", 1)] })} def test_no_value_is_managed(self): sheerka, context = self.init_concepts() sheerka.om.test_only_unfreeze() service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed sheerka.om.freeze() # no entry for the function assert service.get_function_parameters("function", 2) == [] # no entry for the parameter number service.record_function_parameter(context, "function", 1, "'string value'") assert service.get_function_parameters("function", 2) == [] def test_i_can_get_sorted_parameters(self): sheerka, context = self.init_concepts() sheerka.om.test_only_unfreeze() service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed sheerka.om.freeze() service.record_function_parameter(context, "function", 2, "'string value'") service.record_function_parameter(context, "function", 2, "True") service.record_function_parameter(context, "function", 2, "True") assert service.get_function_parameters("function", 2) == ["True", "'string value'"] def test_i_can_add_and_retrieve_parameters_when_multiple_ontology_layers(self): sheerka, context = self.init_concepts(cache_only=False) sheerka.om.test_only_unfreeze() service = SheerkaFunctionsParametersHistory(sheerka).initialize() # since service is no longer auto init'ed sheerka.om.freeze() service.record_function_parameter(context, "function", 1, "10") service.record_function_parameter(context, "function", 2, "True") service.record_function_parameter(context, "function", 3, "'string value'") sheerka.push_ontology(context, "new ontology") service.record_function_parameter(context, "function", 1, "20") service.record_function_parameter(context, "function", 2, "True") assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj( context.event.get_digest(), "function", { 1: [('10', 1), ("20", 1)], 2: [('True', 2)], 3: [("'string value'", 1)] })} sheerka.pop_ontology(context) assert sheerka.om.copy(service.FUNCTIONS_PARAMETERS_ENTRY) == {"function": FunctionParametersObj( context.event.get_digest(), "function", { 1: [('10', 1)], 2: [('True', 1)], 3: [("'string value'", 1)] })}