Skip to content

Commit

Permalink
Add config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiggi committed Nov 29, 2023
1 parent 79d53e5 commit 08f90d9
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 17 deletions.
6 changes: 3 additions & 3 deletions disdrodb/api/scripts/disdrodb_initialize_station.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
10 changes: 2 additions & 8 deletions disdrodb/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions disdrodb/metadata/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion disdrodb/metadata/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
93 changes: 93 additions & 0 deletions disdrodb/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------.
"""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
51 changes: 51 additions & 0 deletions disdrodb/tests/test_donfig_config.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------.
"""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"

0 comments on commit 08f90d9

Please sign in to comment.