Skip to content

Commit 9826c7f

Browse files
committed
avoid redefined outer name errors, remove unneeded/redundant fixtures that didnt actually do anything
1 parent 511b577 commit 9826c7f

File tree

5 files changed

+52
-61
lines changed

5 files changed

+52
-61
lines changed

fremorizer/tests/test_cli.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def test_cli_fremor_config_case1(cli_sos_nc_file): # pylint: disable=redefined-o
335335
(mock_pp_dir / 'ocean' / 'ts' / 'annual').mkdir(parents=True, exist_ok=True)
336336

337337
# symlink the test nc file into the mock tree
338-
src_nc = INDIR / 'reduced_ocean_monthly_1x1deg.199301-199302.sos.nc'
338+
src_nc = Path(cli_sos_nc_file)
339339
dst_nc = comp_ts_dir / src_nc.name
340340
if dst_nc.exists() or dst_nc.is_symlink():
341341
dst_nc.unlink()
@@ -412,16 +412,17 @@ def test_cli_fremor_varlist_opt_dne():
412412
assert result.exit_code == 2
413413

414414

415-
def test_cli_fremor_varlist_no_table_filter(cli_sos_nc_file, cli_sosv2_nc_file, tmp_path): # pylint: disable=redefined-outer-name
415+
def test_cli_fremor_varlist_no_table_filter(tmp_path, cli_sos_nc_file, cli_sosv2_nc_file): # pylint: disable=redefined-outer-name
416416
'''fremor varlist — no MIP table filter.
417417
Creates a variable list from the ocean_sos_var_file test data without a MIP table,
418418
so both sos and sosV2 should appear.'''
419419
output_varlist = tmp_path / 'test_varlist_no_filter.json'
420+
assert Path(cli_sos_nc_file).parent == Path(cli_sosv2_nc_file).parent, 'something wrong with input nc files'
420421

421422
result = runner.invoke(fremor, args=[
422423
"-v", "-v",
423424
"varlist",
424-
"--dir_targ", str(INDIR),
425+
"--dir_targ", str(Path(cli_sos_nc_file).parent),
425426
"--output_variable_list", str(output_varlist)
426427
])
427428
assert result.exit_code == 0, f'varlist failed: {result.output}'
@@ -439,11 +440,12 @@ def test_cli_fremor_varlist_cmip6_table_filter(cli_sos_nc_file, cli_sosv2_nc_fil
439440
'''fremor varlist — with CMIP6 Omon MIP table filter.
440441
Only sos should survive; sosV2 is not in the CMIP6 Omon table.'''
441442
output_varlist = tmp_path / 'test_varlist_cmip6_filter.json'
443+
assert Path(cli_sos_nc_file).parent == Path(cli_sosv2_nc_file).parent, 'something wrong with input nc files'
442444

443445
result = runner.invoke(fremor, args=[
444446
"-v", "-v",
445447
"varlist",
446-
"--dir_targ", str(INDIR),
448+
"--dir_targ", str(Path(cli_sos_nc_file).parent),
447449
"--output_variable_list", str(output_varlist),
448450
"--mip_table", str(CMIP6_TABLE_CONFIG)
449451
])
@@ -461,11 +463,12 @@ def test_cli_fremor_varlist_cmip7_table_filter(cli_sos_nc_file, cli_sosv2_nc_fil
461463
'''fremor varlist — with CMIP7 ocean MIP table filter.
462464
sos should survive (sos_tavg-u-hxy-sea splits to sos); sosV2 should not.'''
463465
output_varlist = tmp_path / 'test_varlist_cmip7_filter.json'
466+
assert Path(cli_sos_nc_file).parent == Path(cli_sosv2_nc_file).parent, 'something wrong with input nc files'
464467

465468
result = runner.invoke(fremor, args=[
466469
"-v", "-v",
467470
"varlist",
468-
"--dir_targ", str(INDIR),
471+
"--dir_targ", str(Path(cli_sos_nc_file).parent),
469472
"--output_variable_list", str(output_varlist),
470473
"--mip_table", str(CMIP7_TABLE_CONFIG)
471474
])
@@ -580,7 +583,7 @@ def test_cli_fremor_run_with_logfile(cli_sos_nc_file, tmp_path): # pylint: disab
580583
result = runner.invoke(fremor, args=[
581584
'-vv', '-l', str(log_path),
582585
'run', '--run_one',
583-
'--indir', str(INDIR),
586+
'--indir', str(Path(cli_sos_nc_file).parent),
584587
'--varlist', str(VARLIST),
585588
'--table_config', str(CMIP6_TABLE_CONFIG),
586589
'--exp_config', str(EXP_CONFIG),
@@ -621,11 +624,12 @@ def test_cli_fremor_run_with_logfile_omission_case(cli_sos_nc_file, cli_sosv2_nc
621624
varlist_fd, varlist_path = tempfile.mkstemp(suffix='.json')
622625
with os.fdopen(varlist_fd, 'w') as f:
623626
json.dump(varlist_data, f)
627+
assert Path(cli_sos_nc_file).parent == Path(cli_sosv2_nc_file).parent, "something wrong with input nc files"
624628

625629
result = runner.invoke(fremor, args=[
626630
'-vv', '-l', str(log_path),
627631
'run',
628-
'--indir', str(INDIR),
632+
'--indir', str(Path(cli_sos_nc_file).parent),
629633
'--varlist', varlist_path,
630634
'--table_config', str(CMIP6_TABLE_CONFIG),
631635
'--exp_config', str(EXP_CONFIG),

fremorizer/tests/test_cmor_find_subtool.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,24 @@
44
'''
55

66
import json
7-
import tempfile
87
from pathlib import Path
98

109
import pytest
1110

1211
from fremorizer.cmor_finder import make_simple_varlist, cmor_find_subtool
1312

14-
@pytest.fixture
15-
def temp_dir():
16-
''' simple pytest fixture for providing temp directory '''
17-
with tempfile.TemporaryDirectory() as tmpdir:
18-
yield tmpdir
19-
20-
21-
def test_make_simple_varlist(temp_dir):
13+
def test_make_simple_varlist(tmp_path):
2214
'''
2315
quick tests of make_simple_varlist
2416
'''
2517
# Create some dummy netCDF files
2618
nc_files = ["test.20230101.var1.nc", "test.20230101.var2.nc", "test.20230101.var3.nc"]
27-
assert Path(temp_dir).exists()
19+
assert Path(tmp_path).exists()
2820
for nc_file in nc_files:
29-
Path(temp_dir, nc_file).touch()
21+
Path(tmp_path, nc_file).touch()
3022

31-
output_file = Path(temp_dir, "varlist.json")
32-
make_simple_varlist(temp_dir, output_file)
23+
output_file = Path(tmp_path, "varlist.json")
24+
make_simple_varlist(tmp_path, output_file)
3325

3426
# Check if the output file is created
3527
assert output_file.exists()
@@ -46,19 +38,19 @@ def test_make_simple_varlist(temp_dir):
4638
assert var_list == expected_var_list
4739

4840

49-
def test_find_subtool_no_json_dir_err(temp_dir):
41+
def test_find_subtool_no_json_dir_err(tmp_path):
5042
''' test json_table_config_dir does not exist error '''
51-
target_dir_dne = Path(temp_dir) / 'foo'
43+
target_dir_dne = Path(tmp_path) / 'foo'
5244
assert not target_dir_dne.exists(), 'target dir should not exist for this test'
5345
with pytest.raises(OSError, match=f'ERROR directory {target_dir_dne} does not exist! exit.'):
5446
cmor_find_subtool(json_var_list=None,
5547
json_table_config_dir=str(target_dir_dne),
5648
opt_var_name=None)
5749

5850

59-
def test_find_subtool_no_json_files_in_dir_err(temp_dir):
51+
def test_find_subtool_no_json_files_in_dir_err(tmp_path):
6052
''' test json_table_config_dir has no files in it error '''
61-
target_dir = Path(temp_dir) / 'foo'
53+
target_dir = Path(tmp_path) / 'foo'
6254
target_dir.mkdir(exist_ok=False)
6355
assert target_dir.is_dir() and target_dir.exists(), "temp dir directory creation failed, inspect code"
6456
with pytest.raises(OSError, match=f'ERROR directory {target_dir} contains no JSON files, exit.'):
@@ -67,7 +59,7 @@ def test_find_subtool_no_json_files_in_dir_err(temp_dir):
6759
opt_var_name=None)
6860

6961

70-
def test_find_subtool_no_varlist_no_optvarname_err(temp_dir):
62+
def test_find_subtool_no_varlist_no_optvarname_err():
7163
''' test no opt_var_name AND no varlist error '''
7264
with pytest.raises(ValueError, match='ERROR: no opt_var_name given but also no content in variable list!!! exit!'):
7365
cmor_find_subtool(json_var_list=None,

fremorizer/tests/test_cmor_finder_make_simple_varlist.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from fremorizer.cmor_finder import make_simple_varlist
1111

1212

13-
@pytest.fixture
14-
def temp_netcdf_dir(tmp_path):
13+
@pytest.fixture(name='netcdf_dir_files')
14+
def temp_netcdf_dir_files(tmp_path):
1515
"""
1616
Fixture to create a temporary directory with sample NetCDF files.
1717
"""
@@ -29,8 +29,8 @@ def temp_netcdf_dir(tmp_path):
2929
return tmp_path
3030

3131

32-
@pytest.fixture
33-
def temp_netcdf_dir_single_file(tmp_path):
32+
@pytest.fixture(name='netcdf_dir_file')
33+
def temp_netcdf_dir_file(tmp_path):
3434
"""
3535
Fixture to create a temporary directory with a single NetCDF file.
3636
"""
@@ -39,23 +39,18 @@ def temp_netcdf_dir_single_file(tmp_path):
3939
return tmp_path
4040

4141

42-
@pytest.fixture
43-
def empty_dir(tmp_path):
44-
"""
45-
Fixture to create an empty temporary directory.
46-
"""
47-
return tmp_path
4842

4943

50-
def test_make_simple_varlist_success(temp_netcdf_dir, tmp_path):
44+
45+
def test_make_simple_varlist_success(netcdf_dir_files):
5146
"""
5247
Test successful creation of variable list from NetCDF files.
5348
"""
5449
# Arrange
55-
output_file = tmp_path / "varlist.json"
50+
output_file = netcdf_dir_files / "varlist.json"
5651

5752
# Act
58-
result = make_simple_varlist(str(temp_netcdf_dir), str(output_file))
53+
result = make_simple_varlist(str(netcdf_dir_files), str(output_file))
5954

6055
# Assert
6156
assert result is not None
@@ -74,12 +69,12 @@ def test_make_simple_varlist_success(temp_netcdf_dir, tmp_path):
7469
assert saved_data == result
7570

7671

77-
def test_make_simple_varlist_return_value_only(temp_netcdf_dir):
72+
def test_make_simple_varlist_return_value_only(netcdf_dir_files):
7873
"""
7974
Test make_simple_varlist with output_variable_list=None returns var_list.
8075
"""
8176
# Act
82-
result = make_simple_varlist(str(temp_netcdf_dir), None)
77+
result = make_simple_varlist(str(netcdf_dir_files), None)
8378

8479
# Assert
8580
assert result is not None
@@ -89,15 +84,15 @@ def test_make_simple_varlist_return_value_only(temp_netcdf_dir):
8984
assert "velocity" in result
9085

9186

92-
def test_make_simple_varlist_single_file_warning(temp_netcdf_dir_single_file, tmp_path):
87+
def test_make_simple_varlist_single_file_warning(netcdf_dir_file):
9388
"""
9489
Test warning when only one file is found.
9590
"""
9691
# Arrange
97-
output_file = tmp_path / "varlist.json"
92+
output_file = netcdf_dir_file / "varlist.json"
9893

9994
# Act
100-
result = make_simple_varlist(str(temp_netcdf_dir_single_file), str(output_file))
95+
result = make_simple_varlist(str(netcdf_dir_file), str(output_file))
10196

10297
# Assert
10398
assert result is not None
@@ -106,18 +101,18 @@ def test_make_simple_varlist_single_file_warning(temp_netcdf_dir_single_file, tm
106101
assert result["temp"] == "temp"
107102

108103

109-
def test_make_simple_varlist_no_files(empty_dir):
104+
def test_make_simple_varlist_no_files(tmp_path):
110105
"""
111106
Test behavior when no NetCDF files are found in directory.
112107
"""
113108
# Act
114-
result = make_simple_varlist(str(empty_dir), None)
109+
result = make_simple_varlist(str(tmp_path), None)
115110

116111
# Assert - function should return None when no files found
117112
assert result is None
118113

119114

120-
def test_make_simple_varlist_invalid_output_path(temp_netcdf_dir):
115+
def test_make_simple_varlist_invalid_output_path(netcdf_dir_file):
121116
"""
122117
Test OSError when output file cannot be written.
123118
"""
@@ -126,7 +121,7 @@ def test_make_simple_varlist_invalid_output_path(temp_netcdf_dir):
126121

127122
# Act & Assert
128123
with pytest.raises(OSError, match="output variable list created but cannot be written"):
129-
make_simple_varlist(str(temp_netcdf_dir), invalid_output_path)
124+
make_simple_varlist(str(netcdf_dir_file), invalid_output_path)
130125

131126

132127
def test_make_simple_varlist_no_matching_pattern(tmp_path):

fremorizer/tests/test_cmor_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def test_create_tmp_dir_with_exp_config(tmp_path):
239239
assert Path(result, 'CMIP7/output').is_dir()
240240

241241

242-
def test_create_tmp_dir_oserror(tmp_path):
242+
def test_create_tmp_dir_oserror():
243243
''' create_tmp_dir should raise OSError when directory creation fails '''
244244
# /dev/null is not a directory; we can't mkdir inside it
245245
with pytest.raises(OSError, match='problem creating tmp output directory'):

fremorizer/tests/test_cmor_helpers_update_grid_label.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"other_field": "some_value"
1818
}
1919

20-
@pytest.fixture
20+
@pytest.fixture(name='test_json_file')
2121
def temp_json_file(tmp_path):
2222
"""
2323
Fixture to create a temporary JSON file for testing.
@@ -33,7 +33,7 @@ def temp_json_file(tmp_path):
3333
json.dump(TEST_JSON_CONTENT, file, indent=4)
3434
return json_file
3535

36-
def test_update_grid_label_and_grid_success(temp_json_file):
36+
def test_update_grid_label_and_grid_success(test_json_file):
3737
"""
3838
Test successful update of 'grid_label' and 'grid' fields.
3939
"""
@@ -43,22 +43,22 @@ def test_update_grid_label_and_grid_success(temp_json_file):
4343
new_nom_res = "updated_nom_res"
4444

4545
# Act
46-
update_grid_and_label(temp_json_file, new_grid_label, new_grid, new_nom_res)
46+
update_grid_and_label(test_json_file, new_grid_label, new_grid, new_nom_res)
4747

4848
# Assert
49-
with open(temp_json_file, "r", encoding="utf-8") as file:
49+
with open(test_json_file, "r", encoding="utf-8") as file:
5050
data = json.load(file)
5151
assert data["grid"] == new_grid
5252
assert data["grid_label"] == new_grid_label
5353
assert data["nominal_resolution"] == new_nom_res
5454
assert data["other_field"] == "some_value" # Ensure other fields are untouched
5555

56-
def test_missing_nom_res_field(temp_json_file):
56+
def test_missing_nom_res_field(test_json_file):
5757
"""
5858
Test behavior when the 'nominal_resolution' field is missing in the JSON file.
5959
"""
6060
# Arrange
61-
with open(temp_json_file, "r+", encoding="utf-8") as file:
61+
with open(test_json_file, "r+", encoding="utf-8") as file:
6262
data = json.load(file)
6363
del data["nominal_resolution"] # Remove the 'nominal_resolution' field
6464
file.seek(0)
@@ -74,14 +74,14 @@ def test_missing_nom_res_field(temp_json_file):
7474
KeyError,
7575
match='"Error updating \'nominal_resolution\'. Ensure the field exists and is modifiable."'
7676
):
77-
update_grid_and_label(temp_json_file, new_grid_label, new_grid, new_nom_res)
77+
update_grid_and_label(test_json_file, new_grid_label, new_grid, new_nom_res)
7878

79-
def test_missing_grid_label_field(temp_json_file):
79+
def test_missing_grid_label_field(test_json_file):
8080
"""
8181
Test behavior when the 'grid_label' field is missing in the JSON file.
8282
"""
8383
# Arrange
84-
with open(temp_json_file, "r+", encoding="utf-8") as file:
84+
with open(test_json_file, "r+", encoding="utf-8") as file:
8585
data = json.load(file)
8686
del data["grid_label"] # Remove the 'grid_label' field
8787
file.seek(0)
@@ -94,14 +94,14 @@ def test_missing_grid_label_field(temp_json_file):
9494

9595
# Act & Assert
9696
with pytest.raises(KeyError, match="Error while updating 'grid_label'"):
97-
update_grid_and_label(temp_json_file, new_grid_label, new_grid, new_nom_res)
97+
update_grid_and_label(test_json_file, new_grid_label, new_grid, new_nom_res)
9898

99-
def test_missing_grid_field(temp_json_file):
99+
def test_missing_grid_field(test_json_file):
100100
"""
101101
Test behavior when the 'grid' field is missing in the JSON file.
102102
"""
103103
# Arrange
104-
with open(temp_json_file, "r+", encoding="utf-8") as file:
104+
with open(test_json_file, "r+", encoding="utf-8") as file:
105105
data = json.load(file)
106106
del data["grid"] # Remove the 'grid' field
107107
file.seek(0)
@@ -114,7 +114,7 @@ def test_missing_grid_field(temp_json_file):
114114

115115
# Act & Assert
116116
with pytest.raises(KeyError, match="Error while updating 'grid'"):
117-
update_grid_and_label(temp_json_file, new_grid_label, new_grid, new_nom_res)
117+
update_grid_and_label(test_json_file, new_grid_label, new_grid, new_nom_res)
118118

119119
def test_invalid_json_file(tmp_path):
120120
"""

0 commit comments

Comments
 (0)