|
| 1 | +"""Tests for utils.py""" |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from data_store import DataStore |
| 9 | + |
| 10 | + |
| 11 | +class TestDataStore: |
| 12 | + """Test the interface for the data sources""" |
| 13 | + def setup(self): |
| 14 | + self.data_path = Path(__file__).resolve().parent.parent.parent / "data" |
| 15 | + self.fname = "693_UNCI.dcm" |
| 16 | + |
| 17 | + def test_data_path(self): |
| 18 | + """Test the path to the data is correct.""" |
| 19 | + s = DataStore() |
| 20 | + assert self.data_path == s.data_path |
| 21 | + |
| 22 | + def test_get_path(self): |
| 23 | + """Test retrieving a file path.""" |
| 24 | + s = DataStore() |
| 25 | + result = s.get_path(self.fname) |
| 26 | + assert isinstance(result, str) |
| 27 | + assert os.fspath(self.data_path / self.fname) == result |
| 28 | + |
| 29 | + with pytest.raises(ValueError, match=r"No file found named"): |
| 30 | + s.get_path("693_UNCI") |
| 31 | + |
| 32 | + def test_get_path_raises_bad_fname(self): |
| 33 | + """Test get_path raises if no matching filename.""" |
| 34 | + s = DataStore() |
| 35 | + msg = r"No file found named 'no matching name.txt'" |
| 36 | + with pytest.raises(ValueError, match=msg): |
| 37 | + s.get_path("no matching name.txt") |
| 38 | + |
| 39 | + def test_get_path_raises_bad_dtype(self): |
| 40 | + """Test get_path raises if unsupported dtype.""" |
| 41 | + s = DataStore() |
| 42 | + msg = r"No files available for the data type" |
| 43 | + with pytest.raises(ValueError, match=msg): |
| 44 | + s.get_path(self.fname, dtype=1) |
| 45 | + |
| 46 | + def test_get_paths(self): |
| 47 | + """Test get_paths.""" |
| 48 | + s = DataStore() |
| 49 | + files = s.get_paths('693*') |
| 50 | + assert len(files) == 3 |
| 51 | + assert isinstance(files[0], str) |
| 52 | + |
| 53 | + def test_get_paths_bad_dtype(self): |
| 54 | + """Test get_paths returns empty list if unsupported dtype.""" |
| 55 | + s = DataStore() |
| 56 | + assert [] == s.get_paths("*", dtype=1) |
0 commit comments