Skip to content

Commit

Permalink
Update QA pytests (#58)
Browse files Browse the repository at this point in the history
* Update QA pytests:

- Add test for license file and contents
- Change access-om2 nominal resolutions to strings
- Change metadata schema url to point to one schema commit - schemas should be backwards compatible
- Update Metadata and License URLs
- Add test for no UUID in metadata

---------

Co-authored-by: Aidan Heerdegen <[email protected]>
  • Loading branch information
jo-basevi and aidanheerdegen authored Apr 8, 2024
1 parent b845231 commit 5a0e48f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
9 changes: 4 additions & 5 deletions test/test_access_om2_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
# Nominal resolutions are sourced from CMIP6 controlled vocabulary
# https://github.com/WCRP-CMIP/CMIP6_CVs/blob/main/CMIP6_nominal_resolution.json
NOMINAL_RESOLUTION = {
'025deg': {'25 km'},
'01deg': {'10 km'},
'1deg': {'100 km'}
'025deg': '25 km',
'01deg': '10 km',
'1deg': '100 km'
}


Expand Down Expand Up @@ -159,6 +159,5 @@ def test_metadata_nominal_resolution(self, metadata, branch):
expected = NOMINAL_RESOLUTION[branch.resolution]
assert ('nominal_resolution' in metadata
and metadata['nominal_resolution'] == expected), (
f"Expected nominal_resolution field set to: {expected} " +
f"\nnominal_resolution: {metadata['nominal_resolution']}"
f"Expected nominal_resolution field set to: {expected}"
)
55 changes: 37 additions & 18 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
import jsonschema
import yaml

# Experiment Metadata Schema
BASE_SCHEMA_URL = "https://raw.githubusercontent.com/ACCESS-NRI/schema"
BASE_SCHEMA_PATH = "au.org.access-nri/model/output/experiment-metadata"
DEFAULT_SCHEMA_VERSION = "1-0-0"
DEFAULT_SCHEMA_COMMIT = "bfc15e3c6fa20d492ccfa0c4706805d64c531e7c"
SCHEMA_VERSION = "1-0-3"
SCHEMA_COMMIT = "4b7207e47afe402a732c58741ff66acc5f93b8cf"

# CC BY 4.0 License
LICENSE = "CC-BY-4.0"
LICENSE_URL = "https://creativecommons.org/licenses/by/4.0/legalcode.txt"


@pytest.fixture(scope="class")
Expand Down Expand Up @@ -160,22 +165,11 @@ def test_no_scripts_in_top_level_directory(self, control_path):

def test_validate_metadata(self, metadata):
# Get schema from Github
schema_version = metadata.get("schema_version", DEFAULT_SCHEMA_VERSION)
schema_path = f"{BASE_SCHEMA_PATH}/{schema_version}.json"
schema_path = f"{BASE_SCHEMA_PATH}/{SCHEMA_VERSION}.json"
url = f"{BASE_SCHEMA_URL}/{SCHEMA_COMMIT}/{schema_path}"

url = f"{BASE_SCHEMA_URL}/main/{schema_path}"
response = requests.get(url)
if response.status_code != 200:
# Use default schema
warnings.warn(
f"Failed to retrieve schema from url: {url}\n" +
f"Defaulting to schema version: {DEFAULT_SCHEMA_VERSION}"
)
schema_path = f"{BASE_SCHEMA_PATH}/{DEFAULT_SCHEMA_VERSION}.json"

url = f"{BASE_SCHEMA_URL}/{DEFAULT_SCHEMA_COMMIT}/{schema_path}"
response = requests.get(url)
assert response.status_code == 200
assert response.status_code == 200
schema = response.json()

# In schema version (1-0-0), required fields are name, experiment_uuid,
Expand All @@ -195,7 +189,32 @@ def test_validate_metadata(self, metadata):
def test_metadata_contains_fields(self, field, metadata):
assert field in metadata, f"{field} field shoud be defined in metadata"

def test_metadata_does_contain_UUID(self, metadata):
assert 'experiment_uuid' not in metadata, (
"`experiment_uuid` should not be defined in metadata, " +
"as this is an configuration rather than an experiment. "
)

def test_metadata_license(self, metadata):
assert 'license' in metadata and metadata['license'] == 'CC-BY-4.0', (
"The license should be set to CC-BY-4.0"
assert 'license' in metadata and metadata['license'] == LICENSE, (
f"The license should be set to {LICENSE}"
)

def test_license_file(self, control_path):
license_path = control_path / 'LICENSE'
assert license_path.exists(), (
f"LICENSE file should exist and equal to {LICENSE} found here: " +
LICENSE_URL
)

response = requests.get(LICENSE_URL)
assert response.status_code == 200
license = response.text

with open(license_path, 'r') as f:
content = f.read()

assert content == license, (
f"LICENSE file should be equal to {LICENSE} found here: " +
LICENSE_URL
)

0 comments on commit 5a0e48f

Please sign in to comment.