|
| 1 | +import pytest |
| 2 | +from copy import deepcopy |
| 3 | +from pathlib import Path |
| 4 | +from PIL import Image |
| 5 | +import numpy as np |
| 6 | +from spatialexperiment import construct_spatial_image_class |
| 7 | +from spatialexperiment.SpatialImage import ( |
| 8 | + LoadedSpatialImage, |
| 9 | + StoredSpatialImage, |
| 10 | + RemoteSpatialImage |
| 11 | +) |
| 12 | + |
| 13 | +def test_loaded_spatial_image_img_source(): |
| 14 | + image = Image.open("tests/images/sample_image2.png") |
| 15 | + spi_loaded = construct_spatial_image_class(image, is_url=False) |
| 16 | + |
| 17 | + assert isinstance(spi_loaded, LoadedSpatialImage) |
| 18 | + assert spi_loaded.img_source() is None |
| 19 | + assert spi_loaded.img_source(as_path=True) is None |
| 20 | + |
| 21 | + np_image = np.zeros((100, 100, 3), dtype=np.uint8) |
| 22 | + spi_loaded_np = construct_spatial_image_class(np_image) |
| 23 | + |
| 24 | + assert isinstance(spi_loaded_np, LoadedSpatialImage) |
| 25 | + assert spi_loaded_np.img_source() is None |
| 26 | + assert spi_loaded_np.img_source(as_path=True) is None |
| 27 | + |
| 28 | + |
| 29 | +def test_stored_spatial_image_img_source(): |
| 30 | + image_path = "tests/images/sample_image1.jpg" |
| 31 | + spi_stored = construct_spatial_image_class(image_path, is_url=False) |
| 32 | + |
| 33 | + assert isinstance(spi_stored, StoredSpatialImage) |
| 34 | + |
| 35 | + source_path = spi_stored.img_source() |
| 36 | + assert isinstance(source_path, Path) |
| 37 | + assert image_path in str(source_path) |
| 38 | + |
| 39 | + source_str = spi_stored.img_source(as_path=True) |
| 40 | + assert isinstance(source_str, str) |
| 41 | + assert image_path in source_str |
| 42 | + |
| 43 | + assert str(source_path) == str(spi_stored.path) |
| 44 | + |
| 45 | + |
| 46 | +def test_remote_spatial_image_img_source(): |
| 47 | + image_url = "https://example.com/test_image.jpg" |
| 48 | + spi_remote = construct_spatial_image_class(image_url, is_url=True) |
| 49 | + |
| 50 | + assert isinstance(spi_remote, RemoteSpatialImage) |
| 51 | + |
| 52 | + source = spi_remote.img_source() |
| 53 | + assert isinstance(source, str) |
| 54 | + assert source == image_url |
| 55 | + |
| 56 | + |
| 57 | +def test_remote_spatial_image_img_source_with_mock(monkeypatch): |
| 58 | + image_url = "https://example.com/test_image.jpg" |
| 59 | + spi_remote = construct_spatial_image_class(image_url, is_url=True) |
| 60 | + |
| 61 | + assert isinstance(spi_remote, RemoteSpatialImage) |
| 62 | + |
| 63 | + # Mock the _download_image method to return a fixed path |
| 64 | + mock_path = Path("/tmp/image.jpg") |
| 65 | + monkeypatch.setattr(spi_remote, "_download_image", lambda: mock_path) |
| 66 | + |
| 67 | + # Test with as_path=True (returns the cached path) |
| 68 | + source_path = spi_remote.img_source(as_path=True) |
| 69 | + assert source_path == str(mock_path) |
| 70 | + |
| 71 | + # Test default behavior returns URL |
| 72 | + assert spi_remote.img_source() == image_url |
| 73 | + |
| 74 | + |
| 75 | +def test_img_source_no_img_data(spe): |
| 76 | + tspe = deepcopy(spe) |
| 77 | + tspe.img_data = None |
| 78 | + assert not tspe.img_source() |
| 79 | + |
| 80 | + |
| 81 | +def test_img_source_no_matches(spe): |
| 82 | + with pytest.raises(ValueError): |
| 83 | + sources = spe.img_source(sample_id="foo", image_id="foo") |
| 84 | + |
| 85 | + |
| 86 | +def test_img_source_both_str(spe): |
| 87 | + res = spe.img_source(sample_id="sample_1", image_id="dice") |
| 88 | + expected_source = spe.get_img(sample_id="sample_1", image_id="dice").img_source() |
| 89 | + |
| 90 | + assert isinstance(res, Path) |
| 91 | + assert res == expected_source |
| 92 | + |
| 93 | + |
| 94 | +def test_img_source_both_str_path(spe): |
| 95 | + res = spe.img_source(sample_id="sample_1", image_id="dice", path=True) |
| 96 | + expected_source = spe.get_img(sample_id="sample_1", image_id="dice").img_source(as_path=True) |
| 97 | + |
| 98 | + assert isinstance(res, str) |
| 99 | + assert res == expected_source |
| 100 | + |
| 101 | + |
| 102 | +def test_img_source_both_true(spe): |
| 103 | + res = spe.img_source(sample_id=True, image_id=True) |
| 104 | + images = spe.get_img(sample_id=True, image_id=True) |
| 105 | + expected_sources = [img.img_source() for img in images] |
| 106 | + |
| 107 | + assert isinstance(res, list) |
| 108 | + assert res == expected_sources |
| 109 | + |
| 110 | + |
| 111 | +def test_img_source_both_true_path(spe): |
| 112 | + res = spe.img_source(sample_id=True, image_id=True, path=True) |
| 113 | + images = spe.get_img(sample_id=True, image_id=True) |
| 114 | + expected_sources = [img.img_source(as_path=True) for img in images] |
| 115 | + |
| 116 | + assert isinstance(res, list) |
| 117 | + assert res == expected_sources |
| 118 | + |
| 119 | + |
| 120 | +def test_img_source_both_none(spe): |
| 121 | + res = spe.img_source(sample_id=None, image_id=None) |
| 122 | + expected_source = spe.get_img(sample_id=None, image_id=None).img_source() |
| 123 | + |
| 124 | + assert isinstance(res, Path) |
| 125 | + assert res == expected_source |
| 126 | + |
| 127 | + |
| 128 | +def test_img_source_sample_str_image_true(spe): |
| 129 | + res = spe.img_source(sample_id="sample_1", image_id=True) |
| 130 | + images = spe.get_img(sample_id="sample_1", image_id=True) |
| 131 | + expected_sources = [img.img_source() for img in images] |
| 132 | + |
| 133 | + assert isinstance(res, list) |
| 134 | + assert res == expected_sources |
| 135 | + |
| 136 | + |
| 137 | +def test_img_source_sample_true_image_str(spe): |
| 138 | + res = spe.img_source(sample_id=True, image_id="desert") |
| 139 | + expected_source = spe.get_img(sample_id=True, image_id="desert").img_source() |
| 140 | + |
| 141 | + assert isinstance(res, Path) |
| 142 | + assert res == expected_source |
| 143 | + |
| 144 | + |
| 145 | +def test_img_source_sample_str_image_none(spe): |
| 146 | + res = spe.img_source(sample_id="sample_1", image_id=None) |
| 147 | + expected_source = spe.get_img(sample_id="sample_1", image_id=None).img_source() |
| 148 | + |
| 149 | + assert isinstance(res, Path) |
| 150 | + assert res == expected_source |
| 151 | + |
| 152 | + |
| 153 | +def test_img_source_sample_none_image_str(spe): |
| 154 | + res = spe.img_source(sample_id=None, image_id="aurora") |
| 155 | + expected_source = spe.get_img(sample_id=None, image_id="aurora").img_source() |
| 156 | + |
| 157 | + assert isinstance(res, Path) |
| 158 | + assert res == expected_source |
| 159 | + |
| 160 | + |
| 161 | +def test_img_source_sample_true_image_none(spe): |
| 162 | + res = spe.img_source(sample_id=True, image_id=None) |
| 163 | + images = spe.get_img(sample_id=True, image_id=None) |
| 164 | + expected_sources = [img.img_source() for img in images] |
| 165 | + |
| 166 | + assert isinstance(res, list) |
| 167 | + assert res == expected_sources |
| 168 | + |
| 169 | + |
| 170 | +def test_img_source_sample_none_image_true(spe): |
| 171 | + res = spe.img_source(sample_id=None, image_id=True) |
| 172 | + images = spe.get_img(sample_id=None, image_id=True) |
| 173 | + expected_sources = [img.img_source() for img in images] |
| 174 | + |
| 175 | + assert isinstance(res, list) |
| 176 | + assert res == expected_sources |
0 commit comments