Skip to content

Commit

Permalink
fix broken tests using new custom fixture NamedTemporaryDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
2320sharon committed Jun 11, 2024
1 parent b160b26 commit 34a9f82
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
28 changes: 28 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@

script_dir = os.path.dirname(os.path.abspath(__file__))

# create a custom context manager to create a temporary directory & clean it up after use
class NamedTemporaryDirectory:
def __init__(self, name):
self.name = name
self.path = os.path.join(tempfile.gettempdir(), name)
os.makedirs(self.path, exist_ok=True)

def __enter__(self):
return self.path

def __exit__(self, exc_type, exc_val, exc_tb):
for root, dirs, files in os.walk(self.path, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(self.path)

@pytest.fixture
def named_temp_dir(request):
# Retrieve the parameter from the request (this would be the name of the temporary directory)
dir_name = request.param
# Setup phase: create the temporary directory with the provided name
with NamedTemporaryDirectory(dir_name) as temp_dir:
yield temp_dir # Provide the directory to the test function
# Teardown phase: cleanup is handled by the NamedTemporaryDirectory context manager


@pytest.fixture(scope="session")
def box_no_shorelines_transects():
geojson = {
Expand Down
21 changes: 13 additions & 8 deletions tests/test_coastseg_map.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json

import os
from coastseg import shoreline
from coastseg import transects
from coastseg import roi
Expand Down Expand Up @@ -166,8 +166,8 @@ def test_save_config(coastseg_map_with_selected_roi_layer, tmp_path):
expected_config_geojson_path = tmp_path / "config_gdf.geojson"
assert expected_config_geojson_path.exists()


def test_save_config_empty_roi_settings(coastseg_map_with_selected_roi_layer, tmp_path):
@pytest.mark.parametrize('named_temp_dir', ['CoastSeg'], indirect=True)
def test_save_config_empty_roi_settings(coastseg_map_with_selected_roi_layer, named_temp_dir):
"""test_save_config_empty_roi_settings tests if save configs will save both a config.json and
config_gdf.geojson to the filepath directory when coastseg_map's rois do not have roi_settings.
It should also create roi_settings for coastseg_map's rois
Expand All @@ -179,15 +179,20 @@ def test_save_config_empty_roi_settings(coastseg_map_with_selected_roi_layer, tm
Selected ROIs have id:["17"]
tmp_path (WindowsPath): temporary directory
"""
# The named_temp_dir fixture created a temporary directory named 'CoastSeg'
tmp_CoastSeg_path = named_temp_dir
actual_coastsegmap = coastseg_map_with_selected_roi_layer
assert actual_coastsegmap.rois.roi_settings == {}
filepath = str(tmp_path)
if type(tmp_CoastSeg_path) != str:
filepath = str(tmp_CoastSeg_path)
else:
filepath = tmp_CoastSeg_path
roi_id = "17"
actual_coastsegmap.save_config(filepath)
# roi_settings was empty before. save_config should have created it
assert actual_coastsegmap.rois.roi_settings != {}
expected_config_json_path = tmp_path / "config.json"
assert expected_config_json_path.exists()
expected_config_json_path = os.path.join( tmp_CoastSeg_path, "config.json")
assert os.path.exists(expected_config_json_path)
with open(expected_config_json_path, "r", encoding="utf-8") as input_file:
data = json.load(input_file)
# test if roi id was saved as key and key fields exist
Expand All @@ -199,8 +204,8 @@ def test_save_config_empty_roi_settings(coastseg_map_with_selected_roi_layer, tm
assert "landsat_collection" in data[roi_id]
assert "sitename" in data[roi_id]
assert "filepath" in data[roi_id]
expected_config_geojson_path = tmp_path / "config_gdf.geojson"
assert expected_config_geojson_path.exists()
expected_config_geojson_path= os.path.join( tmp_CoastSeg_path,"config_gdf.geojson")
assert os.path.exists(expected_config_geojson_path)


def test_load_json_config_without_rois(valid_coastseg_map_with_settings, tmp_data_path):
Expand Down
12 changes: 8 additions & 4 deletions tests/test_imports.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import platform
from coastseg.file_utilities import load_package_resource
import importlib
import pytest


def test_import_load_package_resource():
Expand All @@ -24,9 +24,10 @@ def test_import_bounding_boxes():
except ImportError:
assert False, "Failed to import bounding_boxes"


def test_import_coastseg_logs():
@pytest.mark.parametrize('named_temp_dir', ['CoastSeg'], indirect=True)
def test_import_coastseg_logs(named_temp_dir):
try:
# The named_temp_dir fixture created a temporary directory named 'CoastSeg'
from coastseg import coastseg_logs
except ImportError:
assert False, "Failed to import coastseg_logs"
Expand All @@ -53,8 +54,11 @@ def test_import_downloads():
assert False, "Failed to import downloads"


def test_import_download_tide_model():
# Use the `pytest.mark.parametrize` to pass the parameter to the fixture
@pytest.mark.parametrize('named_temp_dir', ['CoastSeg'], indirect=True)
def test_import_download_tide_model(named_temp_dir):
try:
# The named_temp_dir fixture created a temporary directory named 'CoastSeg'
from coastseg import download_tide_model
except ImportError:
assert False, "Failed to import download_tide_model"
Expand Down

0 comments on commit 34a9f82

Please sign in to comment.