diff --git a/tests/test_config.py b/tests/test_config.py index c5621418..eefc4297 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -6,7 +6,7 @@ import shutil -def test_write_config(): +def test_write_config(tmp_path): expt_name = "testing" latitude_extent = [16.0, 27] @@ -17,6 +17,7 @@ def test_write_config(): ## Place where all your input files go input_dir = Path( os.path.join( + tmp_path, expt_name, "inputs", ) @@ -25,11 +26,12 @@ def test_write_config(): ## Directory where you'll run the experiment from run_dir = Path( os.path.join( + tmp_path, expt_name, "run_files", ) ) - data_path = Path("data") + data_path = Path(tmp_path / "data") for path in (run_dir, input_dir, data_path): os.makedirs(str(path), exist_ok=True) @@ -49,7 +51,7 @@ def test_write_config(): expt_name="test", boundaries=["south", "north"], ) - config_dict = expt.write_config_file() + config_dict = expt.write_config_file(tmp_path / "testing_config.json") assert config_dict["longitude_extent"] == tuple(longitude_extent) assert config_dict["latitude_extent"] == tuple(latitude_extent) assert config_dict["date_range"] == date_range @@ -80,7 +82,7 @@ def test_write_config(): shutil.rmtree(data_path) -def test_load_config(): +def test_load_config(tmp_path): expt_name = "testing" @@ -92,6 +94,7 @@ def test_load_config(): ## Place where all your input files go input_dir = Path( os.path.join( + tmp_path, expt_name, "inputs", ) @@ -100,11 +103,12 @@ def test_load_config(): ## Directory where you'll run the experiment from run_dir = Path( os.path.join( + tmp_path, expt_name, "run_files", ) ) - data_path = Path("data") + data_path = Path(tmp_path / "data") for path in (run_dir, input_dir, data_path): os.makedirs(str(path), exist_ok=True) @@ -122,9 +126,11 @@ def test_load_config(): mom_input_dir=input_dir, toolpath_dir="", ) - path = "testing_config.json" + path = os.path.join(tmp_path, "testing_config.json") config_expt = expt.write_config_file(path) - new_expt = rmom6.create_experiment_from_config(os.path.join(path)) + new_expt = rmom6.create_experiment_from_config( + os.path.join(path), mom_input_folder=tmp_path, mom_run_folder=tmp_path + ) assert str(new_expt) == str(expt) print(new_expt.vgrid) print(expt.vgrid) @@ -136,8 +142,3 @@ def test_load_config(): assert os.path.exists(new_expt.mom_input_dir / "hgrid.nc") & os.path.exists( new_expt.mom_input_dir / "vcoord.nc" ) - shutil.rmtree(run_dir) - shutil.rmtree(input_dir) - shutil.rmtree(data_path) - shutil.rmtree(new_expt.mom_run_dir) - shutil.rmtree(new_expt.mom_input_dir) diff --git a/tests/test_expt_class.py b/tests/test_expt_class.py index aff3a4b8..d459aed3 100644 --- a/tests/test_expt_class.py +++ b/tests/test_expt_class.py @@ -60,8 +60,8 @@ def test_setup_bathymetry( number_vertical_layers=number_vertical_layers, layer_thickness_ratio=layer_thickness_ratio, depth=depth, - mom_run_dir=mom_run_dir, - mom_input_dir=mom_input_dir, + mom_run_dir=tmp_path / mom_run_dir, + mom_input_dir=tmp_path / mom_input_dir, toolpath_dir=toolpath_dir, hgrid_type=hgrid_type, ) @@ -255,8 +255,8 @@ def test_ocean_forcing( number_vertical_layers=number_vertical_layers, layer_thickness_ratio=layer_thickness_ratio, depth=depth, - mom_run_dir=mom_run_dir, - mom_input_dir=mom_input_dir, + mom_run_dir=tmp_path / mom_run_dir, + mom_input_dir=tmp_path / mom_input_dir, toolpath_dir=toolpath_dir, hgrid_type=hgrid_type, ) @@ -452,8 +452,8 @@ def test_rectangular_boundaries( number_vertical_layers=number_vertical_layers, layer_thickness_ratio=layer_thickness_ratio, depth=depth, - mom_run_dir=mom_run_dir, - mom_input_dir=mom_input_dir, + mom_run_dir=tmp_path / mom_run_dir, + mom_input_dir=tmp_path / mom_input_dir, toolpath_dir=toolpath_dir, hgrid_type=hgrid_type, boundaries=["east"], diff --git a/tests/test_manish_branch.py b/tests/test_manish_branch.py index 8724770e..98755c8d 100644 --- a/tests/test_manish_branch.py +++ b/tests/test_manish_branch.py @@ -154,24 +154,9 @@ def dummy_bathymetry_data(): class TestAll: - @classmethod - def setup_class(self): # tmp_path is a pytest fixture - expt_name = "testing" - ## User-1st, test if we can even read the angled nc files. - self.dump_files_dir = Path("testing_outputs") - os.makedirs(self.dump_files_dir, exist_ok=True) - self.expt = rmom6.experiment.create_empty( - expt_name=expt_name, - mom_input_dir=self.dump_files_dir, - mom_run_dir=self.dump_files_dir, - ) - - @classmethod - def teardown_class(cls): - shutil.rmtree(cls.dump_files_dir) @pytest.fixture(scope="module") - def full_legit_expt_setup(self, dummy_bathymetry_data): + def full_legit_expt_setup(self, dummy_bathymetry_data, tmp_path): expt_name = "testing" @@ -183,6 +168,7 @@ def full_legit_expt_setup(self, dummy_bathymetry_data): ## Place where all your input files go input_dir = Path( os.path.join( + tmp_path, expt_name, "inputs", ) @@ -191,11 +177,12 @@ def full_legit_expt_setup(self, dummy_bathymetry_data): ## Directory where you'll run the experiment from run_dir = Path( os.path.join( + tmp_path, expt_name, "run_files", ) ) - data_path = Path("data") + data_path = Path(tmp_path / "data") for path in (run_dir, input_dir, data_path): os.makedirs(str(path), exist_ok=True) bathy_path = data_path / "bathymetry.nc" @@ -218,50 +205,102 @@ def full_legit_expt_setup(self, dummy_bathymetry_data): ) return expt - def test_full_legit_expt_setup(self, full_legit_expt_setup): - assert str(full_legit_expt_setup) + def test_full_legit_expt_setup(self, tmp_path, dummy_bathymetry_data): + expt_name = "testing" + latitude_extent = [16.0, 27] + longitude_extent = [192, 209] + + date_range = ["2005-01-01 00:00:00", "2005-02-01 00:00:00"] + + ## Place where all your input files go + input_dir = Path( + os.path.join( + tmp_path, + expt_name, + "inputs", + ) + ) - # @pytest.mark.skipif( - # IN_GITHUB_ACTIONS, reason="Test doesn't work in Github Actions." - # ) - def test_tides(self, dummy_tidal_data): + ## Directory where you'll run the experiment from + run_dir = Path( + os.path.join( + tmp_path, + expt_name, + "run_files", + ) + ) + data_path = Path(tmp_path / "data") + for path in (run_dir, input_dir, data_path): + os.makedirs(str(path), exist_ok=True) + bathy_path = data_path / "bathymetry.nc" + bathymetry = dummy_bathymetry_data + bathymetry.to_netcdf(bathy_path) + ## User-1st, test if we can even read the angled nc files. + expt = rmom6.experiment( + longitude_extent=longitude_extent, + latitude_extent=latitude_extent, + date_range=date_range, + resolution=0.05, + number_vertical_layers=75, + layer_thickness_ratio=10, + depth=4500, + minimum_depth=5, + mom_run_dir=run_dir, + mom_input_dir=input_dir, + toolpath_dir="", + ) + assert str(expt) + + def test_tides(self, dummy_tidal_data, tmp_path): """ Test the main setup tides function! """ - + expt_name = "testing" + ## User-1st, test if we can even read the angled nc files. + expt = rmom6.experiment.create_empty( + expt_name=expt_name, + mom_input_dir=tmp_path, + mom_run_dir=tmp_path, + ) # Generate Fake Tidal Data ds_h, ds_u = dummy_tidal_data # Save to Fake Folder - ds_h.to_netcdf(self.dump_files_dir / "h_fake_tidal_data.nc") - ds_u.to_netcdf(self.dump_files_dir / "u_fake_tidal_data.nc") + ds_h.to_netcdf(tmp_path / "h_fake_tidal_data.nc") + ds_u.to_netcdf(tmp_path / "u_fake_tidal_data.nc") # Set other required variables needed in setup_tides # Lat Long - self.expt.longitude_extent = (-5, 5) - self.expt.latitude_extent = (0, 30) + expt.longitude_extent = (-5, 5) + expt.latitude_extent = (0, 30) # Grid Type - self.expt.hgrid_type = "even_spacing" - # Dates - self.expt.date_range = ("2000-01-01", "2000-01-02") - self.expt.segments = {} + expt.hgrid_type = "even_spacing" + # DatesÆ’ + expt.date_range = ("2000-01-01", "2000-01-02") + expt.segments = {} # Generate Hgrid Data - self.expt.resolution = 0.1 - self.expt.hgrid = self.expt._make_hgrid() + expt.resolution = 0.1 + expt.hgrid = expt._make_hgrid() # Create Forcing Folder - os.makedirs(self.dump_files_dir / "forcing", exist_ok=True) + os.makedirs(tmp_path / "forcing", exist_ok=True) - self.expt.setup_boundary_tides( - self.dump_files_dir / "h_fake_tidal_data.nc", - self.dump_files_dir / "u_fake_tidal_data.nc", + expt.setup_boundary_tides( + tmp_path / "h_fake_tidal_data.nc", + tmp_path / "u_fake_tidal_data.nc", ) - def test_change_MOM_parameter(self): + def test_change_MOM_parameter(self, tmp_path): """ Test the change MOM parameter function, as well as read_MOM_file and write_MOM_file under the hood. """ - + expt_name = "testing" + ## User-1st, test if we can even read the angled nc files. + expt = rmom6.experiment.create_empty( + expt_name=expt_name, + mom_input_dir=tmp_path, + mom_run_dir=tmp_path, + ) # Copy over the MOM Files to the dump_files_dir base_run_dir = Path( os.path.join( @@ -271,22 +310,29 @@ def test_change_MOM_parameter(self): ) ) shutil.copytree( - base_run_dir / "common_files", self.expt.mom_run_dir, dirs_exist_ok=True + base_run_dir / "common_files", expt.mom_run_dir, dirs_exist_ok=True ) - MOM_override_dict = self.expt.read_MOM_file_as_dict("MOM_override") - og = self.expt.change_MOM_parameter("DT", "30", "COOL COMMENT") - MOM_override_dict_new = self.expt.read_MOM_file_as_dict("MOM_override") + MOM_override_dict = expt.read_MOM_file_as_dict("MOM_override") + og = expt.change_MOM_parameter("DT", "30", "COOL COMMENT") + MOM_override_dict_new = expt.read_MOM_file_as_dict("MOM_override") assert MOM_override_dict_new["DT"]["value"] == "30" assert MOM_override_dict["DT"]["value"] == og assert MOM_override_dict_new["DT"]["comment"] == "COOL COMMENT\n" - def test_properties_empty(self): + def test_properties_empty(self, tmp_path): """ Test the properties """ - dss = self.expt.era5 - dss_2 = self.expt.tides_boundaries - dss_3 = self.expt.ocean_state_boundaries - dss_4 = self.expt.initial_condition - dss_5 = self.expt.bathymetry_property + expt_name = "testing" + ## User-1st, test if we can even read the angled nc files. + expt = rmom6.experiment.create_empty( + expt_name=expt_name, + mom_input_dir=tmp_path, + mom_run_dir=tmp_path, + ) + dss = expt.era5 + dss_2 = expt.tides_boundaries + dss_3 = expt.ocean_state_boundaries + dss_4 = expt.initial_condition + dss_5 = expt.bathymetry_property print(dss, dss_2, dss_3, dss_4, dss_5)