|
1 | 1 | # Copyright (c) Microsoft. All rights reserved.
|
2 | 2 |
|
3 | 3 |
|
| 4 | +import tempfile |
| 5 | +from pathlib import Path |
| 6 | + |
4 | 7 | import pytest
|
5 | 8 | from numpy import array
|
6 | 9 |
|
7 | 10 | from semantic_kernel.contents.binary_content import BinaryContent
|
| 11 | +from semantic_kernel.exceptions.content_exceptions import ContentInitializationError |
8 | 12 |
|
9 | 13 | test_cases = [
|
10 | 14 | pytest.param(BinaryContent(uri="http://test_uri"), id="uri"),
|
@@ -111,3 +115,94 @@ def test_element_roundtrip(binary):
|
111 | 115 | @pytest.mark.parametrize("binary", test_cases)
|
112 | 116 | def test_to_dict(binary):
|
113 | 117 | assert binary.to_dict() == {"type": "binary", "binary": {"uri": str(binary)}}
|
| 118 | + |
| 119 | + |
| 120 | +def test_can_read_with_data(): |
| 121 | + """Test can_read property returns True when data is available.""" |
| 122 | + binary = BinaryContent(data=b"test_data", mime_type="application/pdf") |
| 123 | + assert binary.can_read is True |
| 124 | + |
| 125 | + |
| 126 | +def test_can_read_without_data(): |
| 127 | + """Test can_read property returns False when no data is available.""" |
| 128 | + binary = BinaryContent(uri="http://example.com/file.pdf") |
| 129 | + assert binary.can_read is False |
| 130 | + |
| 131 | + |
| 132 | +def test_can_read_empty(): |
| 133 | + """Test can_read property returns False for empty BinaryContent.""" |
| 134 | + binary = BinaryContent() |
| 135 | + assert binary.can_read is False |
| 136 | + |
| 137 | + |
| 138 | +def test_from_file_success(): |
| 139 | + """Test from_file class method successfully creates BinaryContent from a file.""" |
| 140 | + test_data = b"This is test file content" |
| 141 | + with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
| 142 | + temp_file.write(test_data) |
| 143 | + temp_file_path = temp_file.name |
| 144 | + |
| 145 | + try: |
| 146 | + binary = BinaryContent.from_file(temp_file_path, mime_type="application/pdf") |
| 147 | + assert binary.data == test_data |
| 148 | + assert binary.mime_type == "application/pdf" |
| 149 | + assert binary.uri == temp_file_path |
| 150 | + assert binary.can_read is True |
| 151 | + # Verify data_string works (should be base64 encoded) |
| 152 | + assert binary.data_string == "VGhpcyBpcyB0ZXN0IGZpbGUgY29udGVudA==" |
| 153 | + finally: |
| 154 | + Path(temp_file_path).unlink() |
| 155 | + |
| 156 | + |
| 157 | +def test_from_file_with_path_object(): |
| 158 | + """Test from_file class method works with Path objects.""" |
| 159 | + test_data = b"Path object test content" |
| 160 | + with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
| 161 | + temp_file.write(test_data) |
| 162 | + temp_path = Path(temp_file.name) |
| 163 | + |
| 164 | + try: |
| 165 | + binary = BinaryContent.from_file(temp_path, mime_type="text/plain") |
| 166 | + assert binary.data == test_data |
| 167 | + assert binary.mime_type == "text/plain" |
| 168 | + assert binary.uri == str(temp_path) |
| 169 | + # Verify data_string works (should be base64 encoded) |
| 170 | + assert binary.data_string == "UGF0aCBvYmplY3QgdGVzdCBjb250ZW50" |
| 171 | + finally: |
| 172 | + temp_path.unlink() |
| 173 | + |
| 174 | + |
| 175 | +def test_from_file_binary_data(): |
| 176 | + """Test from_file handles binary data correctly without encoding errors.""" |
| 177 | + # Test with actual binary PDF-like data |
| 178 | + test_data = b'%PDF-1.4\n%\xf6\xe4\xfc\xdf\n1 0 obj\n<<\n/Type /Catalog' |
| 179 | + with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as temp_file: |
| 180 | + temp_file.write(test_data) |
| 181 | + temp_file_path = temp_file.name |
| 182 | + |
| 183 | + try: |
| 184 | + binary = BinaryContent.from_file(temp_file_path, mime_type="application/pdf") |
| 185 | + assert binary.data == test_data |
| 186 | + assert binary.mime_type == "application/pdf" |
| 187 | + assert binary.can_read is True |
| 188 | + # Should not raise Unicode decode error |
| 189 | + data_string = binary.data_string |
| 190 | + assert isinstance(data_string, str) |
| 191 | + assert len(data_string) > 0 |
| 192 | + finally: |
| 193 | + Path(temp_file_path).unlink() |
| 194 | + |
| 195 | + |
| 196 | +def test_from_file_nonexistent(): |
| 197 | + """Test from_file raises FileNotFoundError for nonexistent files.""" |
| 198 | + with pytest.raises(FileNotFoundError, match="File not found"): |
| 199 | + BinaryContent.from_file("/nonexistent/file.pdf") |
| 200 | + |
| 201 | + |
| 202 | +def test_from_file_directory(): |
| 203 | + """Test from_file raises ContentInitializationError for directories.""" |
| 204 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 205 | + with pytest.raises(ContentInitializationError, match="Path is not a file"): |
| 206 | + BinaryContent.from_file(temp_dir) |
| 207 | + |
| 208 | + |
0 commit comments