From a8c83965f6aefc395ff6ac41205a790df58c3d12 Mon Sep 17 00:00:00 2001 From: Toan Quach Date: Wed, 27 Nov 2024 15:22:28 +0700 Subject: [PATCH] refactor checker code --- .../checkers/_data_node_config_checker.py | 51 ++++++++++++------- .../checkers/test_data_node_config_checker.py | 12 ++--- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/taipy/core/config/checkers/_data_node_config_checker.py b/taipy/core/config/checkers/_data_node_config_checker.py index f81b65dae0..aa93a452e5 100644 --- a/taipy/core/config/checkers/_data_node_config_checker.py +++ b/taipy/core/config/checkers/_data_node_config_checker.py @@ -10,7 +10,7 @@ # specific language governing permissions and limitations under the License. from datetime import timedelta -from typing import Dict, List, cast +from typing import Any, Callable, Dict, List, Tuple, cast from taipy.common.config._config import _Config from taipy.common.config.checker._checkers._config_checker import _ConfigChecker @@ -46,7 +46,7 @@ def _check(self) -> IssueCollector: self._check_scope(data_node_config_id, data_node_config) self._check_validity_period(data_node_config_id, data_node_config) self._check_required_properties(data_node_config_id, data_node_config) - self._check_callable(data_node_config_id, data_node_config) + self._check_class_type(data_node_config_id, data_node_config) self._check_generic_read_write_fct_and_args(data_node_config_id, data_node_config) self._check_exposed_type(data_node_config_id, data_node_config) return self._collector @@ -196,28 +196,43 @@ def _check_generic_read_write_fct_and_args(self, data_node_config_id: str, data_ f"DataNodeConfig `{data_node_config_id}` must be populated with a Callable function.", ) - def _check_callable(self, data_node_config_id: str, data_node_config: DataNodeConfig): - properties_to_check = { + @staticmethod + def _get_class_type_and_properties() -> Dict[str, List[Tuple[Any, List[str]]]]: + return { DataNodeConfig._STORAGE_TYPE_VALUE_GENERIC: [ - DataNodeConfig._OPTIONAL_READ_FUNCTION_GENERIC_PROPERTY, - DataNodeConfig._OPTIONAL_WRITE_FUNCTION_GENERIC_PROPERTY, + ( + Callable, + [ + DataNodeConfig._OPTIONAL_READ_FUNCTION_GENERIC_PROPERTY, + DataNodeConfig._OPTIONAL_WRITE_FUNCTION_GENERIC_PROPERTY, + ], + ) ], DataNodeConfig._STORAGE_TYPE_VALUE_SQL: [ - DataNodeConfig._REQUIRED_WRITE_QUERY_BUILDER_SQL_PROPERTY, - DataNodeConfig._OPTIONAL_APPEND_QUERY_BUILDER_SQL_PROPERTY, + ( + Callable, + [ + DataNodeConfig._REQUIRED_WRITE_QUERY_BUILDER_SQL_PROPERTY, + DataNodeConfig._OPTIONAL_APPEND_QUERY_BUILDER_SQL_PROPERTY, + ], + ), ], } - if data_node_config.storage_type in properties_to_check.keys(): - for prop_key in properties_to_check[data_node_config.storage_type]: - prop_value = data_node_config.properties.get(prop_key) if data_node_config.properties else None - if prop_value and not callable(prop_value): - self._error( - prop_key, - prop_value, - f"`{prop_key}` of DataNodeConfig `{data_node_config_id}` must be" - f" populated with a Callable function.", - ) + def _check_class_type(self, data_node_config_id: str, data_node_config: DataNodeConfig): + properties_to_check = self._get_class_type_and_properties() + + for storage_type in properties_to_check.keys(): + for class_type, prop_keys in properties_to_check[storage_type]: + for prop_key in prop_keys: + prop_value = data_node_config.properties.get(prop_key) if data_node_config.properties else None + if prop_value and not isinstance(prop_value, class_type): + self._error( + prop_key, + prop_value, + f"`{prop_key}` of DataNodeConfig `{data_node_config_id}` must be" + f" populated with a {class_type.__name__}.", + ) def _check_exposed_type(self, data_node_config_id: str, data_node_config: DataNodeConfig): if not isinstance(data_node_config.exposed_type, str): diff --git a/tests/core/config/checkers/test_data_node_config_checker.py b/tests/core/config/checkers/test_data_node_config_checker.py index 9a4037b6b0..ca75449c0b 100644 --- a/tests/core/config/checkers/test_data_node_config_checker.py +++ b/tests/core/config/checkers/test_data_node_config_checker.py @@ -513,12 +513,12 @@ def test_check_callable_properties(self, caplog): Config.check() assert len(Config._collector.errors) == 2 expected_error_message_1 = ( - "`write_query_builder` of DataNodeConfig `new` must be populated with a Callable function." + "`write_query_builder` of DataNodeConfig `new` must be populated with a Callable." " Current value of property `write_query_builder` is 1." ) assert expected_error_message_1 in caplog.text expected_error_message_2 = ( - "`append_query_builder` of DataNodeConfig `new` must be populated with a Callable function." + "`append_query_builder` of DataNodeConfig `new` must be populated with a Callable." " Current value of property `append_query_builder` is 2." ) assert expected_error_message_2 in caplog.text @@ -530,7 +530,7 @@ def test_check_callable_properties(self, caplog): Config.check() assert len(Config._collector.errors) == 1 expected_error_messages = [ - "`write_fct` of DataNodeConfig `new` must be populated with a Callable function. Current value" + "`write_fct` of DataNodeConfig `new` must be populated with a Callable. Current value" " of property `write_fct` is 12.", ] assert all(message in caplog.text for message in expected_error_messages) @@ -542,7 +542,7 @@ def test_check_callable_properties(self, caplog): Config.check() assert len(Config._collector.errors) == 1 expected_error_messages = [ - "`read_fct` of DataNodeConfig `new` must be populated with a Callable function. Current value" + "`read_fct` of DataNodeConfig `new` must be populated with a Callable. Current value" " of property `read_fct` is 5.", ] assert all(message in caplog.text for message in expected_error_messages) @@ -554,9 +554,9 @@ def test_check_callable_properties(self, caplog): Config.check() assert len(Config._collector.errors) == 2 expected_error_messages = [ - "`write_fct` of DataNodeConfig `new` must be populated with a Callable function. Current value" + "`write_fct` of DataNodeConfig `new` must be populated with a Callable. Current value" " of property `write_fct` is 9.", - "`read_fct` of DataNodeConfig `new` must be populated with a Callable function. Current value" + "`read_fct` of DataNodeConfig `new` must be populated with a Callable. Current value" " of property `read_fct` is 5.", ] assert all(message in caplog.text for message in expected_error_messages)