Skip to content

Commit 6d5a372

Browse files
committed
Add more tests.
1 parent cdaa3ed commit 6d5a372

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

tests/test_wsireader.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,3 +2934,38 @@ def test_read_rect_fsspec_reader_baseline(sample_svs: Path, tmp_path: Path) -> N
29342934
assert isinstance(im_region, np.ndarray)
29352935
assert im_region.dtype == "uint8"
29362936
assert im_region.shape == (*size[::-1], 3)
2937+
2938+
2939+
def test_fsspec_reader_open_invalid_json_file(tmp_path: Path) -> None:
2940+
"""Ensure JSONDecodeError is handled properly.
2941+
2942+
Pass invalid JSON to FsspecJsonWSIReader.is_valid_zarr_fsspec.
2943+
"""
2944+
json_path = tmp_path / "invalid.json"
2945+
json_path.write_text("{invalid json}") # Corrupt JSON
2946+
2947+
assert not FsspecJsonWSIReader.is_valid_zarr_fsspec(str(json_path))
2948+
2949+
2950+
def test_fsspec_reader_open_oserror_handling() -> None:
2951+
"""Ensure OSError is handled properly.
2952+
2953+
Pass no existent JSON to FsspecJsonWSIReader.is_valid_zarr_fsspec.
2954+
2955+
"""
2956+
with patch("builtins.open", side_effect=OSError("File not found")):
2957+
result = FsspecJsonWSIReader.is_valid_zarr_fsspec("non_existent.json")
2958+
2959+
assert result is False, "Function should return False for OSError"
2960+
2961+
2962+
def test_fsspec_reader_open_pass_empty_json(tmp_path: Path) -> None:
2963+
"""Ensure JSONDecodeError is handled properly.
2964+
2965+
Pass empty JSON to FsspecJsonWSIReader.is_valid_zarr_fsspec.
2966+
2967+
"""
2968+
json_path = tmp_path / "empty.json"
2969+
json_path.write_text("{}")
2970+
2971+
assert not FsspecJsonWSIReader.is_valid_zarr_fsspec(str(json_path))

tiatoolbox/utils/tiff_to_fsspec.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ def main(svs_file_path: str, json_file_path: str, final_url: str) -> None:
7575
zattrs = json.loads(json_data[".zattrs"])
7676

7777
# Ensure "multiscales" exists and is a list
78-
if "multiscales" not in zattrs or not isinstance(zattrs["multiscales"], list):
78+
if "multiscales" not in zattrs or not isinstance(
79+
zattrs["multiscales"], list
80+
): # pragma: no cover
7981
zattrs["multiscales"] = [{}] # Initialize as a list with an empty dictionary
8082

8183
# Update metadata into `.zattrs`

tiatoolbox/wsicore/wsireader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,7 +3523,7 @@ def page_area(page: tifffile.TiffPage) -> float:
35233523
)
35243524
self._zarr_lru_cache = zarr.LRUStoreCache(self._zarr_store, max_size=cache_size)
35253525
self._zarr_group = zarr.open(self._zarr_lru_cache)
3526-
if not isinstance(self._zarr_group, zarr.hierarchy.Group):
3526+
if not isinstance(self._zarr_group, zarr.hierarchy.Group): # pragma: no cover
35273527
group = zarr.hierarchy.group()
35283528
group[0] = self._zarr_group
35293529
self._zarr_group = group
@@ -3867,7 +3867,7 @@ def is_valid_zarr_fsspec(file_path: str) -> bool:
38673867
data = json.load(file)
38683868

38693869
# Basic validation for fsspec Zarr JSON structure
3870-
if not isinstance(data, dict) and ".zattrs" in data:
3870+
if ".zattrs" not in data:
38713871
logger.error("Field .zattrs missing in '%s'.", file_path)
38723872
return False
38733873

@@ -4058,7 +4058,7 @@ def time(string: str) -> datetime:
40584058
svs_tags = dict(parse_svs_tag(string) for string in key_value_pairs)
40594059
raw["SVS Tags"] = svs_tags
40604060
mpp = svs_tags.get("MPP")
4061-
if mpp is not None:
4061+
if mpp is not None: # pragma: no cover
40624062
mpp = [mpp] * 2
40634063
objective_power = svs_tags.get("AppMag")
40644064

@@ -4515,7 +4515,7 @@ def parse_generic_tiff_metadata(pages: TiffPages) -> dict:
45154515
res_units = pages[0].tags.get("ResolutionUnit")
45164516
res_x = pages[0].tags.get("XResolution")
45174517
res_y = pages[0].tags.get("YResolution")
4518-
if (
4518+
if ( # pragma: no cover
45194519
all(x is not None for x in [res_units, res_x, res_y])
45204520
and res_units.value != 1
45214521
):

0 commit comments

Comments
 (0)