Skip to content

Commit

Permalink
test_access_esm1p5_config.py: Added tests from #21 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeGat committed Aug 7, 2024
1 parent 37dbc68 commit 3728a2a
Showing 1 changed file with 154 additions and 2 deletions.
156 changes: 154 additions & 2 deletions src/model_config_tests/qa/test_access_esm1p5_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,164 @@

"""ACCESS-ESM1.5 specific configuration tests"""

import re
import warnings
from typing import Any

import pytest

from model_config_tests.util import get_git_branch_name

### Bunch of expected values for tests ###
VALID_REALMS: set[str] = {"atmosphere", "land", "ocean", "ocnBgchm", "seaIce"}
VALID_KEYWORDS: set[str] = {"global", "access-esm1.5"}
VALID_NOMINAL_RESOLUTION: str = "100 km"
VALID_REFERENCE: str = "https://doi.org/10.1071/ES19035"
VALID_PREINDUSTRIAL_START: dict[str, int] = {"year": 101, "month": 1, "day": 1}
VALID_HISTORICAL_START: dict[str, int] = {"year": 1850, "month": 1, "day": 1}
VALID_RUNTIME: dict[str, int] = {"year": 1, "month": 0, "day": 0}
VALID_RESTART_FREQ: str = "20YS"


### Some functions to avoid copying assertion error text
def error_field_nonexistence(field: str, file: str) -> str:
return f"Field '{field}' is null or does not exist in {file}."

Check warning on line 27 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L27

Added line #L27 was not covered by tests


def error_field_incorrect(field: str, file: str, expected: Any) -> str:
return f"Field '{field}' in {file} is not expected value: {expected}"

Check warning on line 31 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L31

Added line #L31 was not covered by tests


class AccessEsm1p5Branch:
"""Use the naming patterns of the branch name to infer information of
the ACCESS-ESM1.5 config"""

def __init__(self, branch_name):
self.branch_name = branch_name
self.config_scenario = self.set_config_scenario()
self.config_modifiers = self.set_config_modifiers()

Check warning on line 41 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L39-L41

Added lines #L39 - L41 were not covered by tests

def set_config_scenario(self) -> str:
# Regex below is split into three sections:
# Config type start section: '^.+-' for 'release-', 'dev-'...
# Scenario section: '([^+]+)' for 'preindustrial', 'historical'...anything that isn't the '+' modifier sigil
# Modifiers end section: '(?:\+.+)*' any amount of '+modifer' sections
scenario_match = re.match(r"^.+-([^+]+)(?:\+.+)*$", self.branch_name)
if len(scenario_match.group) == 0:
pytest.fail(

Check warning on line 50 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L48-L50

Added lines #L48 - L50 were not covered by tests
f"Could not find a scenario in the branch {self.branch_name}. "
+ "Branches must be of the form 'type-scenario[+modifier...]'. "
+ "See README.md for more information."
)
return scenario_match.group[0]

Check warning on line 55 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L55

Added line #L55 was not covered by tests

def set_config_modifiers(self) -> list[str]:
# Regex below is essentially 'give me the 'modifier' part in all the '+modifier's in the branch name'
return re.findall(r"\+([^+]+)", self.branch_name)

Check warning on line 59 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L59

Added line #L59 was not covered by tests


@pytest.fixture(scope="class")
def branch(control_path, target_branch):
branch_name = target_branch
if branch_name is None:

Check warning on line 65 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L64-L65

Added lines #L64 - L65 were not covered by tests
# Default to current branch name
branch_name = get_git_branch_name(control_path)
assert (

Check warning on line 68 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L67-L68

Added lines #L67 - L68 were not covered by tests
branch_name is not None
), f"Failed getting git branch name of control path: {control_path}"
warnings.warn(

Check warning on line 71 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L71

Added line #L71 was not covered by tests
"Target branch is not specifed, defaulting to current git branch: "
f"{branch_name}. As some ACCESS-ESM1.5 tests infer information, "
"such as resolution, from the target branch name, some tests may "
"not be run. To set use --target-branch flag in pytest call"
)

return AccessEsm1p5Branch(branch_name)

Check warning on line 78 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L78

Added line #L78 was not covered by tests


@pytest.mark.access_esm1p5
class TestAccessEsm1p5:
"""ACCESS-ESM1.5 Specific configuration and metadata tests"""

def test_pass(self):
pass
@pytest.mark.parametrize(
"field,expected", [("realm", VALID_REALMS), ("keyword", VALID_KEYWORDS)]
)
def test_metadata_field_equal_expected_sequence(self, field, expected, metadata):

assert (

Check warning on line 90 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L90

Added line #L90 was not covered by tests
field in metadata and metadata[field] is not None
), error_field_nonexistence(field, "metadata.yaml")

field_set: set[str] = set(metadata[field])

Check warning on line 94 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L94

Added line #L94 was not covered by tests

assert field_set == expected, error_field_incorrect(

Check warning on line 96 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L96

Added line #L96 was not covered by tests
field, "metadata.yaml", "sequence", expected
)

@pytest.mark.parametrize(
"field,expected",
[
("nominal_resolution", VALID_NOMINAL_RESOLUTION),
("reference", VALID_REFERENCE),
],
)
def test_metadata_field_equal_expected_value(self, field, expected, metadata):
assert field in metadata and metadata[field] == expected, error_field_incorrect(

Check warning on line 108 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L108

Added line #L108 was not covered by tests
field, "metadata.yaml", expected
)

def test_config_start(self, branch, config):
assert (

Check warning on line 113 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L113

Added line #L113 was not covered by tests
"calendar" in config
and config["calendar"] is not None
and "start" in config["calendar"]
and config["calendar"]["start"] is not None
), error_field_nonexistence("calendar.start", "config.yaml")

start: dict[str, int] = config["calendar"]["start"]

Check warning on line 120 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L120

Added line #L120 was not covered by tests

match branch.scenario_name:
case "preindustrial":
assert start == VALID_PREINDUSTRIAL_START, error_field_incorrect(

Check warning on line 124 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L122-L124

Added lines #L122 - L124 were not covered by tests
"calendar.start", "config.yaml", VALID_PREINDUSTRIAL_START
)
case "historical":
assert start == VALID_HISTORICAL_START, error_field_incorrect(

Check warning on line 128 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L127-L128

Added lines #L127 - L128 were not covered by tests
"calendar.start", "config.yaml", VALID_HISTORICAL_START
)
case _:
pytest.fail(f"Cannot test unknown scenario {branch.scenario_name}.")

Check warning on line 132 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L131-L132

Added lines #L131 - L132 were not covered by tests

def test_config_runtime(self, config):
assert (

Check warning on line 135 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L135

Added line #L135 was not covered by tests
"calendar" in config
and config["calendar"] is not None
and "runtime" in config["calendar"]
and config["calendar"]["runtime"] is not None
), error_field_nonexistence("calendar.runtime", "config.yaml")

runtime: dict[str, int] = config["calendar"]["runtime"]

Check warning on line 142 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L142

Added line #L142 was not covered by tests

assert runtime == VALID_RUNTIME, error_field_incorrect(

Check warning on line 144 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L144

Added line #L144 was not covered by tests
"calendar.runtime", "config.yaml", VALID_RUNTIME
)

def test_config_restart_freq(self, config):
assert (

Check warning on line 149 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L149

Added line #L149 was not covered by tests
"restart_freq" in config and config["restart_freq"] is not None
), error_field_nonexistence("restart_freq", "config.yaml")
assert config["restart_freq"] == VALID_RESTART_FREQ, error_field_incorrect(

Check warning on line 152 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L152

Added line #L152 was not covered by tests
"restart_freq", "config.yaml", VALID_RESTART_FREQ
)

def test_mppncombine_fast_collate_exe(self, config):
# TODO: We don't check for high resolution here like we do in the ACCESS-OM2 version of the test. Should we?
pattern = r"/g/data/vk83/apps/mppnccombine-fast/.*/bin/mppnccombine-fast"
if "collate" in config:
assert re.match(

Check warning on line 160 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L158-L160

Added lines #L158 - L160 were not covered by tests
pattern, config["collate"]["exe"]
), "Expect collate executable set to mppnccombine-fast"

assert config["collate"][

Check warning on line 164 in src/model_config_tests/qa/test_access_esm1p5_config.py

View check run for this annotation

Codecov / codecov/patch

src/model_config_tests/qa/test_access_esm1p5_config.py#L164

Added line #L164 was not covered by tests
"mpi"
], "Expect `mpi: true` when using mppnccombine-fast"

0 comments on commit 3728a2a

Please sign in to comment.