2
2
# SPDX-License-Identifier: Apache-2.0
3
3
4
4
import pytest
5
- from unittest .mock import mock_open , patch
6
- from pathlib import Path
7
5
8
- from access .parsers .payu_config_yaml import read_payu_config_yaml , write_payu_config_yaml
6
+ from access .parsers .payu_config_yaml import YAMLParser
7
+
8
+
9
+ @pytest .fixture (scope = "module" )
10
+ def parser ():
11
+ """Fixture instantiating the parser."""
12
+ return YAMLParser ()
9
13
10
14
11
15
@pytest .fixture ()
12
16
def simple_payu_config ():
17
+ """Fixture returning a dictionary storing a payu config file."""
13
18
return {
14
19
"project" : "x77" ,
15
20
"ncpus" : 48 ,
@@ -29,6 +34,7 @@ def simple_payu_config():
29
34
30
35
@pytest .fixture ()
31
36
def simple_payu_config_file ():
37
+ """Fixture returning the contents of a simple payu config file."""
32
38
return """project: x77
33
39
ncpus: 48
34
40
jobfs: 10GB
@@ -45,7 +51,8 @@ def simple_payu_config_file():
45
51
46
52
47
53
@pytest .fixture ()
48
- def complex_payu_config_file ():
54
+ def payu_config_file ():
55
+ """Fixture returning the contents of a more complex payu config file."""
49
56
return """# PBS configuration
50
57
51
58
# If submitting to a different project to your default, uncomment line below
@@ -75,6 +82,7 @@ def complex_payu_config_file():
75
82
76
83
@pytest .fixture ()
77
84
def modified_payu_config_file ():
85
+ """Fixture returning the contents the previous payu config file after introducing some modifications."""
78
86
return """# PBS configuration
79
87
80
88
# If submitting to a different project to your default, uncomment line below
@@ -93,7 +101,6 @@ def modified_payu_config_file():
93
101
94
102
model: access-om3
95
103
96
- exe: /some/path/to/access-om3-MOM6-CICE6
97
104
input:
98
105
- /some/other/path/to/inputs/1deg/mom # MOM6 inputs
99
106
- /some/path/to/inputs/1deg/cice # CICE inputs
@@ -102,33 +109,19 @@ def modified_payu_config_file():
102
109
"""
103
110
104
111
105
- @patch ("pathlib.Path.is_file" , new = lambda file : True )
106
- def test_read_payu_config (simple_payu_config , simple_payu_config_file ):
107
- with patch ("io.open" , mock_open (read_data = simple_payu_config_file )) as m :
108
- config = read_payu_config_yaml (file_name = "simple_payu_config_file" )
109
-
110
- assert config == simple_payu_config
111
-
112
-
113
- def test_write_payu_config (simple_payu_config , simple_payu_config_file ):
114
- with patch ("io.open" , mock_open ()) as m :
115
- write_payu_config_yaml (simple_payu_config , Path ("config_file" ))
116
-
117
- assert simple_payu_config_file == "" .join (call .args [0 ] for call in m ().write .mock_calls )
118
-
112
+ def test_read_payu_config (parser , simple_payu_config , simple_payu_config_file ):
113
+ """Test parsing of a simple file."""
114
+ config = parser .parse (simple_payu_config_file )
119
115
120
- @patch ("pathlib.Path.is_file" , new = lambda file : True )
121
- def test_round_trip_payu_config (complex_payu_config_file , modified_payu_config_file ):
122
- with patch ("io.open" , mock_open (read_data = complex_payu_config_file )) as m :
123
- config = read_payu_config_yaml (file_name = "complex_config_file" )
116
+ assert config == simple_payu_config
124
117
125
- config ["ncpus" ] = 64
126
- config ["input" ][0 ] = "/some/other/path/to/inputs/1deg/mom"
127
- write_payu_config_yaml (config , Path ("some_other_config_file" ))
128
118
129
- assert modified_payu_config_file == "" .join (call .args [0 ] for call in m ().write .mock_calls )
119
+ def test_round_trip_payu_config (parser , payu_config_file , modified_payu_config_file ):
120
+ """Test round-trip parsing of a more complex file with mutation of the config."""
121
+ config = parser .parse (payu_config_file )
130
122
123
+ config ["ncpus" ] = 64
124
+ config ["input" ][0 ] = "/some/other/path/to/inputs/1deg/mom"
125
+ del config ["exe" ]
131
126
132
- def test_read_missing_payu_config ():
133
- with pytest .raises (FileNotFoundError ):
134
- read_payu_config_yaml (file_name = "garbage" )
127
+ assert modified_payu_config_file == str (config )
0 commit comments