Skip to content

Commit

Permalink
Raise exception when path points to non-existent file. (#68)
Browse files Browse the repository at this point in the history
* Raise exception when path points to non-existent file.

* Add small unit tests for partial coverage of `set_data`.
  • Loading branch information
drewoldag authored Oct 31, 2023
1 parent b886bc6 commit 025a2e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/rail/core/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ def set_data(self, tag, data, path=None, do_read=True):
else:
if path is None:
arg_data = data
elif not os.path.isfile(path):
raise FileNotFoundError(f"Unable to find file: {path}")
else:
arg_data = None

Expand Down
27 changes: 27 additions & 0 deletions tests/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,30 @@ def test_common_params():
assert par.default == 0.1
assert par.value == 0.1
assert par.dtype == float


def test_set_data_nonexistent_file():
"""Create an instance of a child class of RailStage. Exercise the `set_data`
method and pass in a path to a nonexistent file. A `FileNotFound` exception
should be raised.
"""

col_map = ColumnMapper.make_stage(name="col_map", columns={})
with pytest.raises(FileNotFoundError) as err:
col_map.set_data("model", None, path="./bad_directory/no_file.py")
assert "Unable to find file" in err.context

def test_set_data_real_file():
"""Create an instance of a child class of RailStage. Exercise the `set_data`
method and pass in a path to model. The output of set_data should be `None`.
"""
DS = RailStage.data_store
DS.clear()
model_path = os.path.join(RAILDIR, "rail", "examples_data", "estimation_data", "data", "CWW_HDFN_prior.pkl")
DS.add_data("model", None, ModelHandle, path=model_path)

col_map = ColumnMapper.make_stage(name="col_map", columns={})

ret_val = col_map.set_data("model", None, path=model_path, do_read=False)

assert ret_val is None

0 comments on commit 025a2e2

Please sign in to comment.