diff --git a/cosmotech/coal/store/output/channel_interface.py b/cosmotech/coal/store/output/channel_interface.py index 53a86491..de57fb59 100644 --- a/cosmotech/coal/store/output/channel_interface.py +++ b/cosmotech/coal/store/output/channel_interface.py @@ -22,10 +22,12 @@ def delete(self) -> bool: def is_available(self) -> bool: try: - return all( - all(key in self.configuration[section] for key in self.required_keys[section]) - for section in self.required_keys.keys() - ) + for section in self.required_keys.keys(): + for key in self.required_keys[section]: + if key not in self.configuration[section]: + # if key not in conf get global conf value else KeyError is raised + self.configuration[section][key] = self.configuration.root[section][key] + return True except KeyError: return False diff --git a/cosmotech/coal/utils/configuration.py b/cosmotech/coal/utils/configuration.py index 1b0e98d6..55a6d400 100644 --- a/cosmotech/coal/utils/configuration.py +++ b/cosmotech/coal/utils/configuration.py @@ -28,6 +28,8 @@ def __getattr__(self, key): except (KeyError, AttributeError): LOGGER.warning(f"dotdict Ref {_v} doesn't exist") raise ReferenceKeyError(_v) + if isinstance(_v, str) and _v.startswith("env."): + return os.getenv(_v[4:]) return _v __delattr__ = dict.__delitem__ diff --git a/tests/unit/coal/test_postgresql/test_postgresql_utils.py b/tests/unit/coal/test_postgresql/test_postgresql_utils.py index 2c3eafc0..adb03444 100644 --- a/tests/unit/coal/test_postgresql/test_postgresql_utils.py +++ b/tests/unit/coal/test_postgresql/test_postgresql_utils.py @@ -110,7 +110,6 @@ def test_get_postgresql_table_schema_not_found(self, mock_connect, base_configur # Arrange target_table_name = "test_table" _psql = PostgresUtils(base_configuration) - print(base_configuration.postgres) # Mock connection and cursor mock_conn = MagicMock() diff --git a/tests/unit/coal/test_store/test_output/test_channel_interface.py b/tests/unit/coal/test_store/test_output/test_channel_interface.py index 241cd41f..db8ba679 100644 --- a/tests/unit/coal/test_store/test_output/test_channel_interface.py +++ b/tests/unit/coal/test_store/test_output/test_channel_interface.py @@ -69,9 +69,9 @@ def test_is_available_with_required_keys_present(self): def test_is_available_with_missing_keys(self): """Test is_available when required keys are missing.""" # Arrange - channel = ChannelInterface() + configuration = {"section1": {"key1": "value1"}, "section2": {"key3": "value3"}} # missing key2 + channel = ChannelInterface(configuration) channel.required_keys = {"section1": ["key1", "key2"], "section2": ["key3"]} - channel.configuration = {"section1": {"key1": "value1"}, "section2": {"key3": "value3"}} # missing key2 # Act result = channel.is_available() @@ -82,13 +82,12 @@ def test_is_available_with_missing_keys(self): def test_is_available_with_missing_section(self): """Test is_available when a required section is missing.""" # Arrange - channel = ChannelInterface() - channel.required_keys = {"section1": ["key1", "key2"], "section2": ["key3"]} - channel.configuration = { + configuration = { "section1": {"key1": "value1", "key2": "value2"} # section2 is missing } - + channel = ChannelInterface(configuration) + channel.required_keys = {"section1": ["key1", "key2"], "section2": ["key3"]} # Act & Assert # The is_available method will raise KeyError when section is missing # This is the actual behavior of the current implementation diff --git a/tests/unit/coal/test_utils/conf.ini b/tests/unit/coal/test_utils/conf.ini index 98a1225e..3d16230c 100644 --- a/tests/unit/coal/test_utils/conf.ini +++ b/tests/unit/coal/test_utils/conf.ini @@ -8,6 +8,9 @@ test = 1 [section.sub] TEST = 2 +[bar] +alors = 'env.faismoinlmalin' + [secrets.section] secval = "sec" diff --git a/tests/unit/coal/test_utils/test_utils_configuration.py b/tests/unit/coal/test_utils/test_utils_configuration.py index 6668255e..0a038727 100644 --- a/tests/unit/coal/test_utils/test_utils_configuration.py +++ b/tests/unit/coal/test_utils/test_utils_configuration.py @@ -36,6 +36,14 @@ def test_no_config_file_with_env_var(self): c.secrets assert c.log_level == "test_value" + def test_config_file_with_env_var_prefix(self): + os.environ["CONFIG_FILE_PATH"] = os.path.abspath(os.path.join(os.path.dirname(__file__), "conf.ini")) + os.environ["faismoinlmalin"] = "la" + + c = configuration.Configuration() + + assert c.bar.alors == "la" + def test_config_file_with_secrets(self): os.environ["CONFIG_FILE_PATH"] = os.path.abspath(os.path.join(os.path.dirname(__file__), "conf.ini")) os.environ["faismoinlmalin"] = "la"