|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from pydantic import ValidationError |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from inference.core.workflows.core_steps.transformations.load_image_from_url.v1 import ( |
| 7 | + BlockManifest, |
| 8 | + LoadImageFromUrlBlockV1, |
| 9 | +) |
| 10 | +from inference.core.workflows.execution_engine.entities.base import ( |
| 11 | + ImageParentMetadata, |
| 12 | + WorkflowImageData, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize("type_alias", ["roboflow_core/load_image_from_url@v1"]) |
| 17 | +@pytest.mark.parametrize("url_input", ["https://example.com/image.jpg", "$inputs.image_url"]) |
| 18 | +@pytest.mark.parametrize("cache_input", [True, False, "$inputs.cache_enabled"]) |
| 19 | +def test_load_image_from_url_manifest_validation_when_valid_input_given( |
| 20 | + type_alias: str, url_input: str, cache_input |
| 21 | +) -> None: |
| 22 | + # given |
| 23 | + raw_manifest = { |
| 24 | + "type": type_alias, |
| 25 | + "name": "load_image", |
| 26 | + "url": url_input, |
| 27 | + "cache": cache_input, |
| 28 | + } |
| 29 | + |
| 30 | + # when |
| 31 | + result = BlockManifest.model_validate(raw_manifest) |
| 32 | + |
| 33 | + # then |
| 34 | + assert result == BlockManifest( |
| 35 | + name="load_image", |
| 36 | + type=type_alias, |
| 37 | + url=url_input, |
| 38 | + cache=cache_input, |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("field_to_delete", ["type", "name", "url"]) |
| 43 | +def test_load_image_from_url_manifest_validation_when_required_field_missing( |
| 44 | + field_to_delete: str, |
| 45 | +) -> None: |
| 46 | + # given |
| 47 | + raw_manifest = { |
| 48 | + "type": "roboflow_core/load_image_from_url@v1", |
| 49 | + "name": "load_image", |
| 50 | + "url": "https://example.com/image.jpg", |
| 51 | + "cache": True, |
| 52 | + } |
| 53 | + del raw_manifest[field_to_delete] |
| 54 | + |
| 55 | + # when |
| 56 | + with pytest.raises(ValidationError): |
| 57 | + _ = BlockManifest.model_validate(raw_manifest) |
| 58 | + |
| 59 | + |
| 60 | +def test_load_image_from_url_manifest_validation_with_default_cache() -> None: |
| 61 | + # given |
| 62 | + raw_manifest = { |
| 63 | + "type": "roboflow_core/load_image_from_url@v1", |
| 64 | + "name": "load_image", |
| 65 | + "url": "https://example.com/image.jpg", |
| 66 | + # cache field omitted - should default to True |
| 67 | + } |
| 68 | + |
| 69 | + # when |
| 70 | + result = BlockManifest.model_validate(raw_manifest) |
| 71 | + |
| 72 | + # then |
| 73 | + assert result.cache is True |
| 74 | + |
| 75 | + |
| 76 | +@patch("inference.core.workflows.core_steps.transformations.load_image_from_url.v1.load_image_from_url") |
| 77 | +def test_load_image_from_url_block_run_success(mock_load_image_from_url) -> None: |
| 78 | + # given |
| 79 | + test_url = "https://www.peta.org/wp-content/uploads/2023/05/wild-raccoon.jpg" |
| 80 | + mock_numpy_image = np.zeros((480, 640, 3), dtype=np.uint8) |
| 81 | + mock_load_image_from_url.return_value = mock_numpy_image |
| 82 | + |
| 83 | + block = LoadImageFromUrlBlockV1() |
| 84 | + |
| 85 | + # when |
| 86 | + result = block.run(url=test_url, cache=True) |
| 87 | + |
| 88 | + # then |
| 89 | + assert "image" in result |
| 90 | + assert isinstance(result["image"], WorkflowImageData) |
| 91 | + assert np.array_equal(result["image"].numpy_image, mock_numpy_image) |
| 92 | + assert isinstance(result["image"].parent_metadata, ImageParentMetadata) |
| 93 | + assert result["image"].parent_metadata.parent_id is not None |
| 94 | + |
| 95 | + # Verify the underlying function was called with correct parameters |
| 96 | + mock_load_image_from_url.assert_called_once_with(value=test_url) |
| 97 | + |
| 98 | + |
| 99 | +@patch("inference.core.workflows.core_steps.transformations.load_image_from_url.v1.load_image_from_url") |
| 100 | +def test_load_image_from_url_block_run_caching_behavior(mock_load_image_from_url) -> None: |
| 101 | + # given |
| 102 | + test_url = "https://example.com/cached-image.jpg" |
| 103 | + mock_numpy_image = np.zeros((50, 50, 3), dtype=np.uint8) |
| 104 | + mock_load_image_from_url.return_value = mock_numpy_image |
| 105 | + |
| 106 | + block = LoadImageFromUrlBlockV1() |
| 107 | + |
| 108 | + # when - first call should load the image |
| 109 | + result1 = block.run(url=test_url, cache=True) |
| 110 | + |
| 111 | + # when - second call with same URL should use cache |
| 112 | + result2 = block.run(url=test_url, cache=True) |
| 113 | + |
| 114 | + # then |
| 115 | + assert "image" in result1 |
| 116 | + assert "image" in result2 |
| 117 | + |
| 118 | + # Both results should have identical image data |
| 119 | + assert np.array_equal(result1["image"].numpy_image, result2["image"].numpy_image) |
| 120 | + |
| 121 | + # The underlying function should only be called once due to caching |
| 122 | + mock_load_image_from_url.assert_called_once_with(value=test_url) |
| 123 | + |
| 124 | + |
| 125 | +@patch("inference.core.workflows.core_steps.transformations.load_image_from_url.v1.load_image_from_url") |
| 126 | +def test_load_image_from_url_block_run_error_handling(mock_load_image_from_url) -> None: |
| 127 | + # given |
| 128 | + test_url = "https://nonexistent.example.com/image.jpg" |
| 129 | + mock_load_image_from_url.side_effect = Exception("Could not load image from url") |
| 130 | + |
| 131 | + block = LoadImageFromUrlBlockV1() |
| 132 | + |
| 133 | + # when/then |
| 134 | + with pytest.raises(RuntimeError) as exc_info: |
| 135 | + block.run(url=test_url, cache=False) |
| 136 | + |
| 137 | + assert "Failed to load image from URL" in str(exc_info.value) |
| 138 | + assert test_url in str(exc_info.value) |
| 139 | + mock_load_image_from_url.assert_called_once_with(value=test_url) |
| 140 | + |
| 141 | + |
| 142 | +def test_load_image_from_url_block_manifest_outputs() -> None: |
| 143 | + # given/when |
| 144 | + outputs = BlockManifest.describe_outputs() |
| 145 | + |
| 146 | + # then |
| 147 | + assert len(outputs) == 1 |
| 148 | + assert outputs[0].name == "image" |
| 149 | + assert "image" in [kind.name for kind in outputs[0].kind] |
| 150 | + |
| 151 | + |
| 152 | +def test_load_image_from_url_block_compatibility() -> None: |
| 153 | + # given/when |
| 154 | + compatibility = BlockManifest.get_execution_engine_compatibility() |
| 155 | + |
| 156 | + # then |
| 157 | + assert compatibility == ">=1.0.0,<2.0.0" |
| 158 | + |
| 159 | + |
| 160 | +# Tests for Requirement 4: URL validation at runtime |
| 161 | +@patch("inference.core.workflows.core_steps.transformations.load_image_from_url.v1.load_image_from_url") |
| 162 | +def test_load_image_from_url_block_validates_invalid_url_format_at_runtime(mock_load_image_from_url) -> None: |
| 163 | + # given |
| 164 | + invalid_url = "not-a-valid-url" |
| 165 | + mock_load_image_from_url.side_effect = Exception("Providing images via non https:// URL is not supported") |
| 166 | + |
| 167 | + block = LoadImageFromUrlBlockV1() |
| 168 | + |
| 169 | + # when/then |
| 170 | + with pytest.raises(RuntimeError) as exc_info: |
| 171 | + block.run(url=invalid_url, cache=False) |
| 172 | + |
| 173 | + assert "Failed to load image from URL" in str(exc_info.value) |
| 174 | + assert invalid_url in str(exc_info.value) |
| 175 | + mock_load_image_from_url.assert_called_once_with(value=invalid_url) |
| 176 | + |
| 177 | + |
| 178 | +# Tests for Requirement 5: Image extension validation |
| 179 | +@patch("inference.core.workflows.core_steps.transformations.load_image_from_url.v1.load_image_from_url") |
| 180 | +def test_load_image_from_url_block_validates_non_image_extension_at_runtime(mock_load_image_from_url) -> None: |
| 181 | + # given |
| 182 | + non_image_url = "https://example.com/document.pdf" |
| 183 | + mock_load_image_from_url.side_effect = Exception("Could not decode bytes as image") |
| 184 | + |
| 185 | + block = LoadImageFromUrlBlockV1() |
| 186 | + |
| 187 | + # when/then |
| 188 | + with pytest.raises(RuntimeError) as exc_info: |
| 189 | + block.run(url=non_image_url, cache=False) |
| 190 | + |
| 191 | + assert "Failed to load image from URL" in str(exc_info.value) |
| 192 | + assert non_image_url in str(exc_info.value) |
| 193 | + mock_load_image_from_url.assert_called_once_with(value=non_image_url) |
0 commit comments