From 08f90d90f93c79c667ec6ed576313b660966db51 Mon Sep 17 00:00:00 2001 From: ghiggi Date: Wed, 29 Nov 2023 20:55:00 +0100 Subject: [PATCH] Add config tests --- .../scripts/disdrodb_initialize_station.py | 6 +- disdrodb/configs.py | 10 +- disdrodb/metadata/search.py | 5 - disdrodb/metadata/writer.py | 2 +- disdrodb/tests/test_config.py | 93 +++++++++++++++++++ disdrodb/tests/test_donfig_config.py | 51 ++++++++++ 6 files changed, 150 insertions(+), 17 deletions(-) create mode 100644 disdrodb/tests/test_config.py create mode 100644 disdrodb/tests/test_donfig_config.py diff --git a/disdrodb/api/scripts/disdrodb_initialize_station.py b/disdrodb/api/scripts/disdrodb_initialize_station.py index a35d5f43..a3382786 100644 --- a/disdrodb/api/scripts/disdrodb_initialize_station.py +++ b/disdrodb/api/scripts/disdrodb_initialize_station.py @@ -18,7 +18,7 @@ import click -from disdrodb.utils.scripts import click_station_arguments, click_base_dir_option +from disdrodb.utils.scripts import click_base_dir_option, click_station_arguments sys.tracebacklimit = 0 # avoid full traceback error if occur @@ -37,8 +37,8 @@ def disdrodb_initialize_station( # Processing options base_dir: str = None, ): - """Initialize the DISDRODB directory structure for a station. - + """Initialize the DISDRODB directory structure for a station. + It adds the relevant directories and the default issue and metadata YAML files.. Parameters \n diff --git a/disdrodb/configs.py b/disdrodb/configs.py index fe1443d7..ee506025 100644 --- a/disdrodb/configs.py +++ b/disdrodb/configs.py @@ -94,20 +94,14 @@ def read_disdrodb_configs() -> Dict[str, str]: return config_dict -def _get_config_key(key): - """Return the config key if `value` is None.""" - value = read_disdrodb_configs().get(key, None) - if value is None: - raise ValueError(f"The DISDRODB {key} parameter has not been defined ! ") - return value - - def get_base_dir(base_dir=None): """Return the DISDRODB base directory.""" import disdrodb if base_dir is None: base_dir = disdrodb.config["base_dir"] + if base_dir is None: + raise TypeError("Expecting base_dir to be a string or Path in the donfig.config.") base_dir = str(base_dir) # convert Path to str return base_dir diff --git a/disdrodb/metadata/search.py b/disdrodb/metadata/search.py index 8c29ffad..8074fd7e 100644 --- a/disdrodb/metadata/search.py +++ b/disdrodb/metadata/search.py @@ -21,13 +21,8 @@ import glob import os -from disdrodb.api.info import infer_campaign_name_from_path, infer_data_source_from_path from disdrodb.api.path import define_metadata_filepath from disdrodb.configs import get_base_dir -from disdrodb.metadata.manipulation import sort_metadata_dictionary -from disdrodb.metadata.standards import get_valid_metadata_keys -from disdrodb.utils.yaml import read_yaml, write_yaml - def get_list_metadata( diff --git a/disdrodb/metadata/writer.py b/disdrodb/metadata/writer.py index 0f954108..be725051 100644 --- a/disdrodb/metadata/writer.py +++ b/disdrodb/metadata/writer.py @@ -72,7 +72,7 @@ def write_default_metadata(filepath: str) -> None: # Write the metadata metadata = sort_metadata_dictionary(metadata) - + write_yaml(metadata, filepath=filepath, sort_keys=False) return None diff --git a/disdrodb/tests/test_config.py b/disdrodb/tests/test_config.py new file mode 100644 index 00000000..859d92af --- /dev/null +++ b/disdrodb/tests/test_config.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 + +# -----------------------------------------------------------------------------. +# Copyright (c) 2021-2023 DISDRODB developers +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# -----------------------------------------------------------------------------. +"""Check DISDRODB configuration files.""" +import os + +import pytest + + +def test_define_disdrodb_configs_new_config(tmp_path, mocker): + import disdrodb + + mocker.patch("disdrodb.configs._define_config_filepath", return_value=str(tmp_path / ".config_disdrodb.yml")) + disdrodb.configs.define_disdrodb_configs(base_dir="test_dir", zenodo_token="test_token") + assert os.path.exists(tmp_path / ".config_disdrodb.yml") + + +def test_update_disdrodb_configs(tmp_path, mocker): + import disdrodb + from disdrodb.utils.yaml import read_yaml + + config_fpath = str(tmp_path / ".config_disdrodb.yml") + mocker.patch("disdrodb.configs._define_config_filepath", return_value=config_fpath) + + # Initialize + disdrodb.configs.define_disdrodb_configs(base_dir="test_dir/DISDRODB", zenodo_token="test_token") + assert os.path.exists(config_fpath) + + config_dict = read_yaml(config_fpath) + config_dict["base_dir"] == "test_dir/DISDRODB" + + # Update + disdrodb.configs.define_disdrodb_configs(base_dir="new_test_dir/DISDRODB", zenodo_sandbox_token="new_token") + assert os.path.exists(config_fpath) + config_dict = read_yaml(config_fpath) + config_dict["base_dir"] == "new_test_dir/DISDRODB" + config_dict["zenodo_token"] == "test_token" + config_dict["zenodo_sandbox_token"] == "new_token" + + +def test_get_base_dir(): + import disdrodb + from disdrodb.configs import get_base_dir + + # Check that if input is not None, return the specified base_dir + assert get_base_dir(base_dir="test/DISDRODB") == "test/DISDRODB" + + # Check that if no config YAML file specified, return None + with pytest.raises(TypeError): + get_base_dir() + + # Set base_dir in the donfig config and check it return it ! + disdrodb.config.set({"base_dir": "another_test_dir/DISDRODB"}) + assert get_base_dir() == "another_test_dir/DISDRODB" + + # Now test that return the one from the temporary disdrodb.config donfig object + with disdrodb.config.set({"base_dir": "new_test_dir/DISDRODB"}): + assert get_base_dir() == "new_test_dir/DISDRODB" + + # And check it return the default one + assert get_base_dir() == "another_test_dir/DISDRODB" + + +@pytest.mark.parametrize("sandbox,expected_token", [(False, "my_zenodo_token"), (True, "my_sandbox_zenodo_token")]) +def test_get_zenodo_token(sandbox, expected_token): + import disdrodb + from disdrodb.configs import get_zenodo_token + + disdrodb.config.set({"zenodo_token": None}) + disdrodb.config.set({"zenodo_sandbox_token": None}) + + # Check raise error if Zenodo Token not specified + with pytest.raises(ValueError): + get_zenodo_token(sandbox=sandbox) + + disdrodb.config.set({"zenodo_token": "my_zenodo_token"}) + disdrodb.config.set({"zenodo_sandbox_token": "my_sandbox_zenodo_token"}) + assert get_zenodo_token(sandbox=sandbox) == expected_token diff --git a/disdrodb/tests/test_donfig_config.py b/disdrodb/tests/test_donfig_config.py new file mode 100644 index 00000000..d6f6a763 --- /dev/null +++ b/disdrodb/tests/test_donfig_config.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +# -----------------------------------------------------------------------------. +# Copyright (c) 2021-2023 DISDRODB developers +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# -----------------------------------------------------------------------------. +"""Check DISDRODB configuration files.""" +import os + +import pytest + + +def test_disdrodb_config_takes_environment_variable(): + os.environ["DISDRODB_BASE_DIR"] = "MY_BASE_DIR" + + import disdrodb + + disdrodb.config.get("base_dir") == "MY_BASE_DIR" + + +@pytest.mark.parametrize("key", ["base_dir", "zenodo_token", "zenodo_sandbox_token"]) +def test_disdrodb_config_donfig(key): + import disdrodb + + # Assert donfig key context manager + with disdrodb.config.set({key: "dummy_string"}): + assert disdrodb.config.get(key) == "dummy_string" + + # # Assert if not initialized, defaults to None + # assert disdrodb.config.get(key) is None + + # Now initialize + disdrodb.config.set({key: "dummy_string"}) + assert disdrodb.config.get(key) == "dummy_string" + + # Now try context manager again + with disdrodb.config.set({key: "new_dummy_string"}): + assert disdrodb.config.get(key) == "new_dummy_string" + assert disdrodb.config.get(key) == "dummy_string"