Skip to content

Commit

Permalink
Merge pull request #174 from ImperialCollegeLondon/feature/split_testing
Browse files Browse the repository at this point in the history
Feature/split testing
  • Loading branch information
jacobcook1995 authored Feb 13, 2023
2 parents 699603a + 3f979e1 commit 444180c
Show file tree
Hide file tree
Showing 35 changed files with 441 additions and 430 deletions.
5 changes: 3 additions & 2 deletions docs/source/development/defining_new_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ You should first start by defining a new folder for your model (within
mkdir virtual_rainforest/models/freshwater
```

Within this folder a `python` script defining the model should be created.
Within this folder a `python` script defining the model should be created. This script
should be called "{MODEL_NAME}_model.py".

```bash
touch virtual_rainforest/models/freshwater/model.py
touch virtual_rainforest/models/freshwater/freshwater_model.py
```

This script must import a number of things to be able to set up a new `Model` class
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions tests/data/test_data/test.toml → tests/core/data/test.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[[core.data.variable]]
file = "test_data/cellid_coords.nc"
file = "cellid_coords.nc"
var_name = "temp"
[[core.data.variable]]
file = "test_data/cellid_coords.nc"
file = "cellid_coords.nc"
var_name = "prec"
[[core.data.variable]]
file = "test_data/cellid_coords.nc"
file = "cellid_coords.nc"
var_name = "elev"
[[core.data.variable]]
file = "test_data/cellid_coords.nc"
file = "cellid_coords.nc"
var_name = "vapd"
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[[core.data.variable]]
file = "test_data/cellid_coords.nc"
file = "cellid_coords.nc"
var_name = "temp"
[[core.data.variable]]
file = "test_data/cellid_coords.nc"
file = "cellid_coords.nc"
var_name = "prec"
[[core.data.variable]]
file = "test_data/cellid_coords.nc"
file = "cellid_coords.nc"
var_name = "elev"
[[core.data.variable]]
file = "test_data/cellid_coords.nc"
file = "cellid_coords.nc"
var_name = "elev"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
57 changes: 25 additions & 32 deletions tests/test_config.py → tests/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
import pytest

import virtual_rainforest.core.config as config
from tests.conftest import log_check
from virtual_rainforest.core.config import register_schema

from .conftest import log_check


@pytest.mark.parametrize(
"d_a,d_b,overlap",
Expand Down Expand Up @@ -98,8 +97,7 @@ def test_check_outfile(caplog, mocker, out_path, expected_log_entries):
(
(
CRITICAL,
"The following (user provided) config paths do not exist:\n"
"['Nonsense/file/location']",
"The following (user provided) config paths do not exist:",
),
),
),
Expand All @@ -111,13 +109,13 @@ def test_check_outfile(caplog, mocker, out_path, expected_log_entries):
(
CRITICAL,
"The following (user provided) config folders do not contain any "
"toml files:\n['.']",
"toml files:",
),
),
),
(
["tests/fixtures/", "tests/fixtures/all_config.toml"],
[Path("tests/fixtures/all_config.toml")],
["", "all_config.toml"],
["all_config.toml"],
config.ConfigurationError,
(
(
Expand All @@ -130,17 +128,23 @@ def test_check_outfile(caplog, mocker, out_path, expected_log_entries):
],
)
def test_collect_files(
caplog, mocker, cfg_paths, contents, expected_exception, expected_log_entries
caplog,
mocker,
shared_datadir,
cfg_paths,
contents,
expected_exception,
expected_log_entries,
):
"""Checks errors for missing config files."""

# Configure the mock to return a specific list of files
# Configure the mock to return a specific list of files when globbing a directory
mock_get = mocker.patch("virtual_rainforest.core.config.Path.glob")
mock_get.return_value = contents
mock_get.return_value = [shared_datadir / fn for fn in contents]

# Check that file collection fails as expected
with pytest.raises(expected_exception):
config.collect_files(cfg_paths)
config.collect_files([shared_datadir / fn for fn in cfg_paths])

log_check(caplog, expected_log_entries)

Expand Down Expand Up @@ -252,41 +256,30 @@ def test_construct_combined_schema(caplog: pytest.LogCaptureFixture) -> None:
"file_path,expected_log_entries",
[
(
"tests/fixtures/default_config.toml", # File entirely of defaults
"default_config.toml", # File entirely of defaults
(
(
INFO,
"Configuration files successfully validated!",
),
(
INFO,
"Saving all configuration details to complete_config.toml",
),
(INFO, "Configuration files successfully validated!"),
(INFO, "Saving all configuration details to"),
),
),
(
"tests/fixtures/all_config.toml", # File with no defaults
"all_config.toml", # File with no defaults
(
(
INFO,
"Configuration files successfully validated!",
),
(
INFO,
"Saving all configuration details to complete_config.toml",
),
(INFO, "Configuration files successfully validated!"),
(INFO, "Saving all configuration details to"),
),
),
],
)
def test_final_validation_log(caplog, file_path, expected_log_entries):
def test_final_validation_log(caplog, shared_datadir, file_path, expected_log_entries):
"""Checks that validation passes as expected and produces the correct output."""

config.validate_config([file_path], Path("./complete_config.toml"))
outfile = shared_datadir / "complete_config.toml"
config.validate_config([shared_datadir / file_path], outfile)

# Remove generated output file
# As a bonus tests that output file was generated correctly + to the right location
Path("./complete_config.toml").unlink()
outfile.unlink()

# Then check that the correct (critical error) log messages are emitted
log_check(caplog, expected_log_entries)
Expand Down
37 changes: 18 additions & 19 deletions tests/test_data.py → tests/core/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import pytest
from xarray import DataArray, Dataset

from tests.conftest import log_check
from virtual_rainforest.core.config import ConfigurationError

from .conftest import log_check


@pytest.mark.parametrize(
argnames=["use_grid", "exp_err", "expected_log"],
Expand Down Expand Up @@ -251,7 +250,7 @@ def test_Data_contains(fixture_data, var_name, expected):
def test_Data_load_to_dataarray_naming(caplog, shared_datadir, name, exp_log):
"""Test the coding of the name handling and replacement."""

# Setup a Data instance to match the example files generated in test_data/
# Setup a Data instance to match the example files generated in tests/core/data

from virtual_rainforest.core.data import Data
from virtual_rainforest.core.grid import Grid
Expand All @@ -272,7 +271,7 @@ def test_Data_load_to_dataarray_naming(caplog, shared_datadir, name, exp_log):
caplog.clear()

# Load the data from file
datafile = shared_datadir / "test_data/cellid_coords.nc"
datafile = shared_datadir / "cellid_coords.nc"

data[name] = load_to_dataarray(file=datafile, var_name=name)

Expand Down Expand Up @@ -323,7 +322,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["__any__"],
"test_data/cellid_dims.nc",
"cellid_dims.nc",
does_not_raise(),
None,
(
Expand All @@ -335,7 +334,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["__any__"],
"test_data/cellid_dim_too_few.nc",
"cellid_dim_too_few.nc",
pytest.raises(ValueError),
"Grid defines 100 cells, data provides 60",
(
Expand All @@ -348,7 +347,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["__any__"],
"test_data/cellid_dim_too_many.nc",
"cellid_dim_too_many.nc",
pytest.raises(ValueError),
"Grid defines 100 cells, data provides 200",
(
Expand All @@ -361,7 +360,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["__any__"],
"test_data/cellid_coords.nc",
"cellid_coords.nc",
does_not_raise(),
None,
(
Expand All @@ -373,7 +372,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["__any__"],
"test_data/cellid_coords_too_few.nc",
"cellid_coords_too_few.nc",
pytest.raises(ValueError),
"The data cell ids do not provide a one-to-one map onto grid " "cell ids.",
(
Expand All @@ -390,7 +389,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["__any__"],
"test_data/cellid_coords_bad_cellid.nc",
"cellid_coords_bad_cellid.nc",
pytest.raises(ValueError),
"The data cell ids do not provide a one-to-one map onto grid " "cell ids.",
(
Expand All @@ -407,7 +406,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["square"],
"test_data/xy_dim.nc",
"xy_dim.nc",
does_not_raise(),
None,
(
Expand All @@ -419,7 +418,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["square"],
"test_data/xy_dim_small.nc",
"xy_dim_small.nc",
pytest.raises(ValueError),
"Data XY dimensions do not match square grid",
(
Expand All @@ -432,7 +431,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["square"],
"test_data/xy_coords.nc",
"xy_coords.nc",
does_not_raise(),
None,
(
Expand All @@ -444,7 +443,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["square"],
"test_data/xy_coords_small.nc",
"xy_coords_small.nc",
pytest.raises(ValueError),
"Mapped points do not cover all cells.",
(
Expand All @@ -457,7 +456,7 @@ def fixture_load_data_grids(request):
),
pytest.param(
["square"],
"test_data/xy_coords_shifted.nc",
"xy_coords_shifted.nc",
pytest.raises(ValueError),
"Mapped points fall outside grid.",
(
Expand Down Expand Up @@ -493,7 +492,7 @@ def test_Data_load_to_dataarray_data_handling(
data name and name replacement functionality
"""

# Setup a Data instance to match the example files generated in test_data/
# Setup a Data instance to match the example files generated in tests/core/data

from virtual_rainforest.core.data import Data
from virtual_rainforest.core.readers import load_to_dataarray
Expand Down Expand Up @@ -527,7 +526,7 @@ def test_Data_load_to_dataarray_data_handling(
argnames=["file", "exp_error", "exp_msg", "exp_log"],
argvalues=[
pytest.param(
"test_data/test.toml",
"test.toml",
does_not_raise(),
None,
(
Expand All @@ -544,7 +543,7 @@ def test_Data_load_to_dataarray_data_handling(
id="valid config",
),
pytest.param(
"test_data/test_dupes.toml",
"test_dupes.toml",
pytest.raises(ConfigurationError),
"Data configuration did not load cleanly",
(
Expand Down Expand Up @@ -578,7 +577,7 @@ def test_Data_load_from_config(
config loader part of the mechanism
"""

# Setup a Data instance to match the example files generated in test_data/
# Setup a Data instance to match the example files generated in tests/core/data

from virtual_rainforest.core.config import load_in_config_files
from virtual_rainforest.core.data import Data
Expand Down
File renamed without changes.
Loading

0 comments on commit 444180c

Please sign in to comment.