forked from NOAA-GFDL/fre-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_configure_script_yaml.py
More file actions
192 lines (169 loc) · 5.96 KB
/
test_configure_script_yaml.py
File metadata and controls
192 lines (169 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
Test configure_script_yaml
"""
import os
import shutil
from pathlib import Path
import pytest
import yaml
from jsonschema import (
SchemaError,
ValidationError,
validate
)
import metomi.rose.config
from fre.pp import configure_script_yaml as csy
from fre.yamltools import combine_yamls_script as cy
# Set what would be click options
EXPERIMENT = "c96L65_am5f7b12r1_amip"
PLATFORM = "gfdl.ncrc5-intel22-classic"
TARGET = "prod-openmp"
# Set example yaml paths, input directory
TEST_DIR = Path("fre/pp/tests")
TEST_YAML = Path("AM5_example/am5.yaml")
def test_combinedyaml_exists():
"""
Make sure combined yaml file exists
"""
assert Path(f"{TEST_DIR}/{TEST_YAML}").exists()
def test_configure_script():
"""
Tests success of configure yaml script
Creates rose-suite, regrid rose-app, remap rose-app
TO-DO: will break this up for better tests
"""
# Set home for ~/cylc-src location in script
old_home = os.environ["HOME"]
os.environ["HOME"] = str(Path(f"{TEST_DIR}/configure_yaml_out"))
# Set output directory
OUT_DIR = Path(f"{os.getenv('HOME')}/cylc-src/{EXPERIMENT}__{PLATFORM}__{TARGET}")
Path(OUT_DIR).mkdir(parents = True, exist_ok = True)
# Define combined yaml
model_yaml = f"{TEST_DIR}/{TEST_YAML}"
# Invoke configure_yaml_script.py
csy.yaml_info(model_yaml, EXPERIMENT, PLATFORM, TARGET)
os.environ["HOME"] = old_home
# Check for configuration creation and final combined yaml
assert all([ Path(f"{OUT_DIR}/{EXPERIMENT}.yaml").exists(),
Path(f"{OUT_DIR}/rose-suite.conf").exists()])
def test_validate():
"""
Test the success of validation.
"""
yml_dict = cy.consolidate_yamls(yamlfile = f"{TEST_DIR}/{TEST_YAML}",
experiment = EXPERIMENT,
platform = PLATFORM,
target = TARGET,
use = "pp",
output = None)
try:
csy.validate_yaml(yml_dict)
except:
assert False
def test_validate_fail():
"""
Test that validation fails when given the wrong yaml dictionary.
"""
yml_dict = cy.consolidate_yamls(yamlfile = f"{TEST_DIR}/{TEST_YAML}",
experiment = EXPERIMENT,
platform = PLATFORM,
target = TARGET,
use = "pp",
output = f"{Path(__file__).parent}/csy_out.yaml")
# Missing history_dir, ptmp_dir, and postprocess
wrong_yml_dict = {
"name": "exp_name",
"platform": "ptest",
"target": "ttest",
"directories": {"pp_dir": "/some/path"}
}
with pytest.raises(
ValueError,
match="Combined yaml is not valid. Please fix the errors and try again."
) as execinfo:
val_fail = csy.validate_yaml(wrong_yml_dict)
assert execinfo.type is ValueError
def test_set_rose_suite_missing_postprocess():
"""
Test that set_rose_suite raises ValueError when 'postprocess' section is missing.
"""
rose_suite = metomi.rose.config.ConfigNode()
yaml_dict = {
"name": "exp_name",
"platform": "ptest",
"target": "ttest",
"directories": {"pp_dir": "/some/path"}
}
with pytest.raises(ValueError):
csy.set_rose_suite(yaml_dict, rose_suite)
def test_set_rose_suite_no_refinediag():
"""
Test that set_rose_suite sets DO_REFINEDIAG to 'False' when no refinediag scripts are requested
"""
rose_suite = metomi.rose.config.ConfigNode()
yaml_dict = {
"postprocess": {
"settings": {"some_setting": "value"},
"refinediag": {
"example": {
"script": "/path/to/some/script",
"do_refinediag": False
}
}
},
"directories": {"pp_dir": "/some/path"},
}
csy.set_rose_suite(yaml_dict, rose_suite)
assert rose_suite.get(['template variables', 'DO_REFINEDIAG']).value == 'False'
def test_set_rose_suite_no_preanalysis():
"""
Test that set_rose_suite sets DO_PREANALYSIS to 'False' when no preanalysis scripts are requested
"""
rose_suite = metomi.rose.config.ConfigNode()
yaml_dict = {
"postprocess": {
"settings": {"some_setting": "value"},
"preanalysis": {
"example": {
"script": "/path/to/some/script",
"do_preanalysis": False
}
}
},
"directories": {"pp_dir": "/some/path"},
}
csy.set_rose_suite(yaml_dict, rose_suite)
assert rose_suite.get(['template variables', 'DO_PREANALYSIS']).value == 'False'
def test_cleanup():
shutil.rmtree(f"{TEST_DIR}/configure_yaml_out")
assert not Path(f"{TEST_DIR}/configure_yaml_out").exists()
def test_rose_suite_DO_ANALYSIS():
"""
"""
rose_suite = metomi.rose.config.ConfigNode()
yaml_dict = {
"postprocess": {"settings": {"some_setting": "value"}},
"directories": {"pp_dir": "/some/path"},
"analysis": {
"land-test": {
"required": {
"data_frequency": "mon",
"date_range": ["19800101T0000Z", "20200101T0000Z"]
},
"workflow": {
"components": ["land-test"],
"script_type": "one-shot",
"product": "ts",
"chunk_size": "P1Y"
}
}
}
}
csy.set_rose_suite(yaml_dict, rose_suite)
assert rose_suite.get(['template variables', 'DO_ANALYSIS']).value == 'True'
## to-do:
# - mock wrong schema path
# - any other raises missed
#def test_mock_validation_wrong_schema_path():
# """
# """