Skip to content

Commit 02adc85

Browse files
committed
conftest.py
1 parent 2b7f9fb commit 02adc85

2 files changed

Lines changed: 84 additions & 27 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
# (C) Crown Copyright 2026, Met Office.
3+
# The LICENSE.md file contains full licensing details.
4+
from pathlib import Path
5+
6+
7+
def mock_data_dir():
8+
return Path(__file__).parent.parent.parent / "unittest" / "mock_data"
9+
10+
11+
def recipe_paths_yml_fp():
12+
return mock_data_dir() / "recipe_paths.yml"
13+
14+
15+
def model_runs_yml_fp():
16+
return mock_data_dir() / "model_runs.yml"
17+
18+
19+
def cmip6_datasets_yml_fp():
20+
return mock_data_dir() / "cmip6_datasets.yml"
21+
22+
23+
def original_recipe_radiation_budget_fp():
24+
return mock_data_dir() / "original_recipe_radiation_budget.yml"
25+
26+
27+
def recipe_with_additional_datasets_yml_fp():
28+
return mock_data_dir() / "recipe_with_additional_datasets.yml"
29+
30+
31+
def updated_recipe_radiation_budget_yml_fp():
32+
return mock_data_dir() / "updated_recipe_radiation_budget.yml"
33+
34+
35+
def kgo_dir():
36+
return Path(__file__).parent.parent.parent / "unittest" / "kgo"
37+
38+
39+
def no_ds_in_recipe_fp():
40+
return kgo_dir() / "blank_recipe_radiation_budget.yml"
41+
42+
43+
def recipe_additional_datasets_removed_yml_fp():
44+
return kgo_dir() / "recipe_additional_datasets_removed.yml"
45+
46+
47+
def extended_radiation_budget_recipe_yml_fp():
48+
return kgo_dir() / "extended_radiation_budget_recipe.yml"

CMEW/app/configure_for/bin/test_update_recipe_file.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,55 +25,60 @@
2525
remove_additional_datasets,
2626
update_recipe_file,
2727
)
28-
from pathlib import Path
2928
import shutil
3029
import yaml
3130

32-
33-
mock_data_dir = Path(__file__).parent.parent.parent / "unittest" / "mock_data"
34-
recipe_format_file = mock_data_dir / "recipe_paths.yml"
35-
model_runs_yml_fp = mock_data_dir / "model_runs.yml"
36-
cmip6_datasets_yml_fp = mock_data_dir / "cmip6_datasets.yml"
37-
original_recipe_fp = mock_data_dir / "original_recipe_radiation_budget.yml"
38-
extra_ds_in_recipe_fp = mock_data_dir / "recipe_with_additional_datasets.yml"
39-
updated_recipe_fp = mock_data_dir / "updated_recipe_radiation_budget.yml"
40-
41-
kgo_dir = Path(__file__).parent.parent.parent / "unittest" / "kgo"
42-
no_ds_in_recipe_fp = kgo_dir / "blank_recipe_radiation_budget.yml"
43-
no_extras_in_recipe_fp = kgo_dir / "recipe_additional_datasets_removed.yml"
44-
extended_recipe_fp = kgo_dir / "extended_radiation_budget_recipe.yml"
31+
from configure_for_conftest import (
32+
recipe_paths_yml_fp,
33+
model_runs_yml_fp,
34+
cmip6_datasets_yml_fp,
35+
original_recipe_radiation_budget_fp,
36+
recipe_with_additional_datasets_yml_fp,
37+
updated_recipe_radiation_budget_yml_fp,
38+
no_ds_in_recipe_fp,
39+
recipe_additional_datasets_removed_yml_fp,
40+
extended_radiation_budget_recipe_yml_fp,
41+
)
4542

4643

4744
def test_return_blank_recipe():
48-
with open(no_ds_in_recipe_fp, "r") as file_handle:
45+
with open(str(no_ds_in_recipe_fp()), "r") as file_handle:
4946
expected = yaml.safe_load(file_handle)
50-
actual = return_blank_recipe(str(original_recipe_fp))
47+
actual = return_blank_recipe(str(original_recipe_radiation_budget_fp()))
5148
assert actual == expected
5249

5350

5451
def test_add_extra_datasets():
55-
with open(extended_recipe_fp, "r") as file_handle_1:
52+
with open(
53+
str(extended_radiation_budget_recipe_yml_fp()), "r"
54+
) as file_handle_1:
5655
expected = yaml.safe_load(file_handle_1)
5756

58-
with open(updated_recipe_fp, "r") as file_handle_2:
57+
with open(
58+
str(updated_recipe_radiation_budget_yml_fp()), "r"
59+
) as file_handle_2:
5960
pre_recipe = yaml.safe_load(file_handle_2)
6061

6162
# Using str(filepath) here as update_recipe_file.py uses os, not pathlib
62-
actual = add_extra_datasets(pre_recipe, str(cmip6_datasets_yml_fp))
63+
actual = add_extra_datasets(pre_recipe, str(cmip6_datasets_yml_fp()))
6364
assert actual == expected
6465

6566

6667
def test_remove_additional_datasets():
6768
recipe_id = "mock_entry"
68-
with open(no_extras_in_recipe_fp, "r") as file_handle_1:
69+
with open(
70+
str(recipe_additional_datasets_removed_yml_fp()), "r"
71+
) as file_handle_1:
6972
expected = yaml.safe_load(file_handle_1)
7073

71-
with open(extra_ds_in_recipe_fp, "r") as file_handle_2:
74+
with open(
75+
str(recipe_with_additional_datasets_yml_fp()), "r"
76+
) as file_handle_2:
7277
pre_recipe = yaml.safe_load(file_handle_2)
7378

7479
# Using str(filepath) here as update_recipe_file.py uses os, not pathlib
7580
actual = remove_additional_datasets(
76-
pre_recipe, recipe_id, str(recipe_format_file)
81+
pre_recipe, recipe_id, str(recipe_paths_yml_fp())
7782
)
7883
assert actual == expected
7984

@@ -83,7 +88,9 @@ def test_update_recipe_file(tmp_path):
8388
# Copy the original recipe to a tmp_path location to allow it to be
8489
# overwritten.
8590
path_to_temp_recipe = tmp_path / "tmp_recipe.yml"
86-
shutil.copy(original_recipe_fp, path_to_temp_recipe)
91+
shutil.copy(
92+
str(original_recipe_radiation_budget_fp()), path_to_temp_recipe
93+
)
8794

8895
# Mock the environmental variable 'RECIPE PATH' to the tmp_path location
8996
# where the original recipe is stored.
@@ -94,16 +101,18 @@ def test_update_recipe_file(tmp_path):
94101

95102
update_recipe_file(
96103
recipe_path,
97-
model_runs_yml_fp,
98-
cmip6_datasets_yml_fp,
104+
str(model_runs_yml_fp()),
105+
str(cmip6_datasets_yml_fp()),
99106
recipe_id,
100-
recipe_format_file,
107+
str(recipe_paths_yml_fp()),
101108
)
102109

103110
with open(path_to_temp_recipe, "r") as file_handle_1:
104111
actual_lines = file_handle_1.readlines()
105112

106-
with open(extended_recipe_fp, "r") as file_handle_2:
113+
with open(
114+
str(extended_radiation_budget_recipe_yml_fp()), "r"
115+
) as file_handle_2:
107116
kgo_with_comment = file_handle_2.readlines()
108117

109118
# Remove the five comment lines at the top of

0 commit comments

Comments
 (0)