Skip to content

Commit 39a0b16

Browse files
Some cleanup of the tests and use more unittests mock functionalities.
1 parent 6c2c052 commit 39a0b16

File tree

4 files changed

+78
-98
lines changed

4 files changed

+78
-98
lines changed

tests/test_mom6_input.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
2-
import filecmp
2+
from unittest.mock import mock_open, patch
3+
from pathlib import Path
34

4-
from test_utils import MockFile
55
from access.parsers.mom6_input import Mom6Input, write_mom6_input, read_mom6_input
66

77

@@ -18,22 +18,19 @@ def simple_mom6_input():
1818

1919

2020
@pytest.fixture()
21-
def simple_mom6_input_file(tmp_path):
22-
file = tmp_path / "simple_mom6_input_file"
23-
mom6_input_str = """BOOL = True
21+
def simple_mom6_input_file():
22+
return """BOOL = True
2423
DT = 1800.0
2524
IGNORED_DIRECTIVE = 3
2625
INCORRECT_DIRECTIVE = 2
2726
N_SMOOTH = 4
2827
REGRIDDING_COORDINATE_MODE = 'ZSTAR'
2928
"""
30-
return MockFile(file, mom6_input_str)
3129

3230

3331
@pytest.fixture()
34-
def complex_mom6_input_file(tmp_path):
35-
file = tmp_path / "complex_mom6_input_file"
36-
mom6_input_str = """
32+
def complex_mom6_input_file():
33+
return """
3734
/* This is a comment
3835
spanning two lines */
3936
REGRIDDING_COORDINATE_MODE = Z*
@@ -50,13 +47,11 @@ def complex_mom6_input_file(tmp_path):
5047
TO_BE_REMOVED = 10.0
5148
BOOL = True
5249
"""
53-
return MockFile(file, mom6_input_str)
5450

5551

5652
@pytest.fixture()
57-
def modified_mom6_input_file(tmp_path):
58-
file = tmp_path / "modified_mom6_input_file"
59-
mom6_input_str = """
53+
def modified_mom6_input_file():
54+
return """
6055
6156
6257
REGRIDDING_COORDINATE_MODE = Z*
@@ -75,35 +70,40 @@ def modified_mom6_input_file(tmp_path):
7570
7671
ADDED_VAR = 32
7772
"""
78-
return MockFile(file, mom6_input_str)
7973

8074

81-
def test_read_mom6_input(tmp_path, simple_mom6_input, simple_mom6_input_file):
82-
mom6_input_from_file = read_mom6_input(file_name=simple_mom6_input_file.file)
75+
@patch("pathlib.Path.is_file", new=lambda file: True)
76+
def test_read_mom6_input(simple_mom6_input, simple_mom6_input_file):
77+
with patch("builtins.open", mock_open(read_data=simple_mom6_input_file)) as m:
78+
config = read_mom6_input(file_name="simple_mom6_input_file")
8379

84-
assert mom6_input_from_file == simple_mom6_input
80+
assert config == simple_mom6_input
8581

8682

87-
def test_write_mom6_input(tmp_path, simple_mom6_input, simple_mom6_input_file):
88-
file = tmp_path / "MOM_input"
89-
write_mom6_input(simple_mom6_input, file)
83+
def test_write_mom6_input(simple_mom6_input, simple_mom6_input_file):
84+
with patch("pathlib.Path.open", mock_open()) as m:
85+
write_mom6_input(simple_mom6_input, Path("config_file"))
9086

91-
assert filecmp.cmp(file, simple_mom6_input_file.file)
87+
assert simple_mom6_input_file == "".join(call.args[0] for call in m().write.mock_calls)
9288

9389

94-
def test_round_trip_mom6_input(tmp_path, complex_mom6_input_file, modified_mom6_input_file):
95-
mom6_input_from_file = Mom6Input(file_name=complex_mom6_input_file.file)
96-
mom6_input_from_file["dt"] = 900.0
97-
mom6_input_from_file["ADDED_VAR"] = 1
98-
mom6_input_from_file["ADDED_VAR"] = 32
99-
del mom6_input_from_file["N_SMOOTH"]
100-
mom6_input_from_file["N_SMOOTH"] = 4
101-
del mom6_input_from_file["TO_BE_REMOVED"]
90+
@patch("pathlib.Path.is_file", new=lambda file: True)
91+
def test_round_trip_mom6_input(complex_mom6_input_file, modified_mom6_input_file):
92+
with patch("builtins.open", mock_open(read_data=complex_mom6_input_file)) as m1:
93+
config = read_mom6_input(file_name="complex_config_file")
10294

103-
write_mom6_input(mom6_input_from_file, tmp_path / "MOM_input_new")
95+
config["dt"] = 900.0
96+
config["ADDED_VAR"] = 1
97+
config["ADDED_VAR"] = 32
98+
del config["N_SMOOTH"]
99+
config["N_SMOOTH"] = 4
100+
del config["TO_BE_REMOVED"]
104101

105-
assert mom6_input_from_file["ADDED_VAR"] == 32
106-
assert filecmp.cmp(tmp_path / "MOM_input_new", modified_mom6_input_file.file)
102+
with patch("pathlib.Path.open", mock_open()) as m:
103+
write_mom6_input(config, Path("some_other_config_file"))
104+
105+
assert config["ADDED_VAR"] == 32
106+
assert modified_mom6_input_file == "".join(call.args[0] for call in m().write.mock_calls)
107107

108108

109109
def test_read_missing_mom6_file():

tests/test_nuopc_config.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
2-
import filecmp
2+
from unittest.mock import mock_open, patch
3+
from pathlib import Path
34

4-
from test_utils import MockFile
55
from access.parsers.nuopc_config import read_nuopc_config, write_nuopc_config
66

77

@@ -30,9 +30,8 @@ def simple_nuopc_config():
3030

3131

3232
@pytest.fixture()
33-
def simple_nuopc_config_file(tmp_path):
34-
file = tmp_path / "simple_config_file"
35-
resource_file_str = """DRIVER_attributes::
33+
def simple_nuopc_config_file():
34+
return """DRIVER_attributes::
3635
Verbosity = off
3736
cime_model = cesm
3837
logFilePostFix = .log
@@ -53,38 +52,39 @@ def simple_nuopc_config_file(tmp_path):
5352
::
5453
5554
"""
56-
return MockFile(file, resource_file_str)
5755

5856

5957
@pytest.fixture()
60-
def invalid_nuopc_config_file(tmp_path):
61-
file = tmp_path / "invalid_config_file"
62-
resource_file_str = """DRIVER_attributes::
58+
def invalid_nuopc_config_file():
59+
return """DRIVER_attributes::
6360
Verbosity: off
6461
cime_model - cesm
6562
::
6663
6764
COMPONENTS::: atm ocn
6865
"""
69-
return MockFile(file, resource_file_str)
7066

7167

72-
def test_read_nuopc_config(tmp_path, simple_nuopc_config, simple_nuopc_config_file):
73-
config_from_file = read_nuopc_config(file_name=simple_nuopc_config_file.file)
68+
@patch("pathlib.Path.is_file", new=lambda file: True)
69+
def test_read_nuopc_config(simple_nuopc_config, simple_nuopc_config_file):
70+
with patch("builtins.open", mock_open(read_data=simple_nuopc_config_file)) as m:
71+
config = read_nuopc_config(file_name="simple_nuopc_config_file")
7472

75-
assert config_from_file == simple_nuopc_config
73+
assert config == simple_nuopc_config
7674

7775

78-
def test_write_nuopc_config(tmp_path, simple_nuopc_config, simple_nuopc_config_file):
79-
file = tmp_path / "config_file"
80-
write_nuopc_config(simple_nuopc_config, file)
76+
def test_write_nuopc_config(simple_nuopc_config, simple_nuopc_config_file):
77+
with patch("builtins.open", mock_open()) as m:
78+
write_nuopc_config(simple_nuopc_config, Path("config_file"))
8179

82-
assert filecmp.cmp(file, simple_nuopc_config_file.file)
80+
assert simple_nuopc_config_file == "".join(call.args[0] for call in m().write.mock_calls)
8381

8482

85-
def test_read_invalid_nuopc_config_file(tmp_path, invalid_nuopc_config_file):
86-
with pytest.raises(ValueError):
87-
read_nuopc_config(file_name=invalid_nuopc_config_file.file)
83+
@patch("pathlib.Path.is_file", new=lambda file: True)
84+
def test_read_invalid_nuopc_config_file(invalid_nuopc_config_file):
85+
with patch("builtins.open", mock_open(read_data=invalid_nuopc_config_file)) as m:
86+
with pytest.raises(ValueError):
87+
read_nuopc_config(file_name="invalid_nuopc_config_file")
8888

8989

9090
def test_read_missing_nuopc_config_file():

tests/test_payu_config_yaml.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
2-
import filecmp
2+
from unittest.mock import mock_open, patch
3+
from pathlib import Path
34

4-
from test_utils import MockFile
55
from access.parsers.payu_config_yaml import read_payu_config_yaml, write_payu_config_yaml
66

77

@@ -25,9 +25,8 @@ def simple_payu_config():
2525

2626

2727
@pytest.fixture()
28-
def simple_payu_config_file(tmp_path):
29-
file = tmp_path / "simple_payu_config_file.yaml"
30-
payu_file_str = """project: x77
28+
def simple_payu_config_file():
29+
return """project: x77
3130
ncpus: 48
3231
jobfs: 10GB
3332
mem: 192GB
@@ -40,13 +39,11 @@ def simple_payu_config_file(tmp_path):
4039
- /some/path/to/inputs/1deg/cice
4140
- /some/path/to/inputs/1deg/share
4241
"""
43-
return MockFile(file, payu_file_str)
4442

4543

4644
@pytest.fixture()
47-
def complex_payu_config_file(tmp_path):
48-
file = tmp_path / "complex_payu_config_file.yaml"
49-
payu_file_str = """# PBS configuration
45+
def complex_payu_config_file():
46+
return """# PBS configuration
5047
5148
# If submitting to a different project to your default, uncomment line below
5249
# and change project code as appropriate; also set shortpath below
@@ -71,13 +68,11 @@ def complex_payu_config_file(tmp_path):
7168
- /some/path/to/inputs/1deg/share # shared inputs
7269
7370
"""
74-
return MockFile(file, payu_file_str)
7571

7672

7773
@pytest.fixture()
78-
def modified_payu_config_file(tmp_path):
79-
file = tmp_path / "modified_payu_config_file.yaml"
80-
payu_file_str = """# PBS configuration
74+
def modified_payu_config_file():
75+
return """# PBS configuration
8176
8277
# If submitting to a different project to your default, uncomment line below
8378
# and change project code as appropriate; also set shortpath below
@@ -102,29 +97,33 @@ def modified_payu_config_file(tmp_path):
10297
- /some/path/to/inputs/1deg/share # shared inputs
10398
10499
"""
105-
return MockFile(file, payu_file_str)
106100

107101

108-
def test_read_payu_config(tmp_path, simple_payu_config, simple_payu_config_file):
109-
config_from_file = read_payu_config_yaml(file_name=simple_payu_config_file.file)
102+
@patch("pathlib.Path.is_file", new=lambda file: True)
103+
def test_read_payu_config(simple_payu_config, simple_payu_config_file):
104+
with patch("io.open", mock_open(read_data=simple_payu_config_file)) as m:
105+
config = read_payu_config_yaml(file_name="simple_payu_config_file")
110106

111-
assert config_from_file == simple_payu_config
107+
assert config == simple_payu_config
112108

113109

114-
def test_write_payu_config(tmp_path, simple_payu_config, simple_payu_config_file):
115-
file = tmp_path / "config_file"
116-
write_payu_config_yaml(simple_payu_config, file)
110+
def test_write_payu_config(simple_payu_config, simple_payu_config_file):
111+
with patch("io.open", mock_open()) as m:
112+
write_payu_config_yaml(simple_payu_config, Path("config_file"))
117113

118-
assert filecmp.cmp(file, simple_payu_config_file.file)
114+
assert simple_payu_config_file == "".join(call.args[0] for call in m().write.mock_calls)
119115

120116

121-
def test_round_trip_payu_config(tmp_path, complex_payu_config_file, modified_payu_config_file):
122-
config = read_payu_config_yaml(complex_payu_config_file.file)
123-
config["ncpus"] = 64
124-
config["input"][0] = "/some/other/path/to/inputs/1deg/mom"
125-
write_payu_config_yaml(config, tmp_path / "config.yaml")
117+
@patch("pathlib.Path.is_file", new=lambda file: True)
118+
def test_round_trip_payu_config(complex_payu_config_file, modified_payu_config_file):
119+
with patch("io.open", mock_open(read_data=complex_payu_config_file)) as m:
120+
config = read_payu_config_yaml(file_name="complex_config_file")
126121

127-
assert filecmp.cmp(tmp_path / "config.yaml", modified_payu_config_file.file)
122+
config["ncpus"] = 64
123+
config["input"][0] = "/some/other/path/to/inputs/1deg/mom"
124+
write_payu_config_yaml(config, Path("some_other_config_file"))
125+
126+
assert modified_payu_config_file == "".join(call.args[0] for call in m().write.mock_calls)
128127

129128

130129
def test_read_missing_payu_config():

tests/test_utils.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)