Skip to content

Commit

Permalink
#346 Fix tests for combined compile operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Dana Singh authored and Dana Singh committed Feb 19, 2025
1 parent c44e93c commit 0a126ad
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions fre/yamltools/tests/test_combine_yamls_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import yaml
import pprint
from jsonschema import validate
from fre.yamltools import combine_yamls as cy_original
from fre.yamltools import combine_yamls_script as cy


Expand Down Expand Up @@ -71,32 +70,34 @@ def test_merged_compile_yamls():
use = "compile"

# Merge the yamls
cy_original.consolidate_yamls(modelyaml, COMP_EXPERIMENT, COMP_PLATFORM, COMP_TARGET, use)

# Move combined yaml to output location
shutil.move(f"{IN_DIR}/combined-am5.yaml", COMP_OUT_DIR)

# Check that the combined yaml exists
assert Path(f"{COMP_OUT_DIR}/combined-{COMP_EXPERIMENT}.yaml").exists()
try:
cy.consolidate_yamls(modelyaml, COMP_EXPERIMENT, COMP_PLATFORM, COMP_TARGET, use, output = None)
except:
assert False

def test_combined_compileyaml_validation():
"""
Validate the combined compile yaml
"""
combined_yamlfile =f"{COMP_OUT_DIR}/combined-{COMP_EXPERIMENT}.yaml"
schema_file = os.path.join(SCHEMA_DIR, "fre_make.json")
# Model yaml path
modelyaml = str(Path(f"{IN_DIR}/am5.yaml"))
use = "compile"

with open(combined_yamlfile,'r') as cf:
yml = yaml.safe_load(cf)
# Merge the yamls
try:
out = cy.consolidate_yamls(modelyaml, COMP_EXPERIMENT, COMP_PLATFORM, COMP_TARGET, use, output = None)
except:
assert False

schema_file = os.path.join(SCHEMA_DIR, "fre_make.json")
with open(schema_file,'r') as f:
s = f.read()
schema = json.loads(s)

# If the yaml is valid, no issues
# If the yaml is not valid, error
try:
validate(instance=yml,schema=schema)
validate(instance=out,schema=schema)
except:
assert False

Expand All @@ -111,13 +112,9 @@ def test_combined_compileyaml_combinefail():

# Merge the yamls - should fail since there is no compile yaml specified in the model yaml
try:
cy_original.consolidate_yamls(modelyaml, COMP_EXPERIMENT, COMP_PLATFORM, COMP_TARGET, use)
# Move combined yaml to output location
shutil.move(f"{IN_DIR}/compile_yamls/compile_fail/combined-am5-wrong_compilefile.yaml", COMP_OUT_DIR)
out = cy.consolidate_yamls(modelyaml, COMP_EXPERIMENT, COMP_PLATFORM, COMP_TARGET, use, output = None)
except:
print("EXPECTED FAILURE")
# Move combined yaml to output location
shutil.move(f"{IN_DIR}/compile_yamls/compile_fail/combined-am5-wrong_compilefile.yaml", COMP_OUT_DIR)
assert True

def test_combined_compileyaml_validatefail():
Expand All @@ -130,27 +127,22 @@ def test_combined_compileyaml_validatefail():
use = "compile"

# Merge the yamls
cy_original.consolidate_yamls(modelyaml, COMP_EXPERIMENT, COMP_PLATFORM, COMP_TARGET, use)

# Move combined yaml to output location
shutil.move(f"{IN_DIR}/compile_yamls/compile_fail/combined-am5-wrong_datatype.yaml", COMP_OUT_DIR)
try:
out = cy.consolidate_yamls(modelyaml, COMP_EXPERIMENT, COMP_PLATFORM, COMP_TARGET, use, output = None)
except:
assert False

# Validate against schema; should fail
wrong_combined = Path(f"{COMP_OUT_DIR}/combined-am5-wrong_datatype.yaml")
schema_file = os.path.join(SCHEMA_DIR, "fre_make.json")

# Open/load combined yaml file
with open(wrong_combined,'r') as cf:
yml = yaml.safe_load(cf)

# Open/load schema.jaon
with open(schema_file,'r') as f:
s = f.read()
schema = json.loads(s)

# Validation should fail
try:
validate(instance=yml,schema=schema)
validate(instance=out,schema=schema)
except:
assert True

Expand Down Expand Up @@ -196,9 +188,8 @@ def test_combined_ppyaml_validation():
except:
assert False

schema_dir = Path(__file__).resolve().parents[2]
schema_path = os.path.join(schema_dir, 'gfdl_msd_schemas', 'FRE', 'fre_pp.json')
with open(schema_path,'r') as f:
schema_file = os.path.join(SCHEMA_DIR, "fre_pp.json")
with open(schema_file,'r') as f:
s = f.read()
schema = json.loads(s)

Expand Down

0 comments on commit 0a126ad

Please sign in to comment.