Skip to content

Commit 154f284

Browse files
committed
Add test for upload_document_image
1 parent f177e5c commit 154f284

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/test_blob_manager.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,56 @@ async def mock_delete_blob(*args, **kwargs):
175175
await blob_manager.remove_blob()
176176

177177

178+
@pytest.mark.asyncio
179+
@pytest.mark.skipif(sys.version_info.minor < 10, reason="requires Python 3.10 or higher")
180+
async def test_upload_document_image(monkeypatch, mock_env):
181+
# Create a blob manager with an image container
182+
blob_manager = BlobManager(
183+
endpoint=f"https://{os.environ['AZURE_STORAGE_ACCOUNT']}.blob.core.windows.net",
184+
credential=MockAzureCredential(),
185+
container=os.environ["AZURE_STORAGE_CONTAINER"],
186+
account=os.environ["AZURE_STORAGE_ACCOUNT"],
187+
resource_group=os.environ["AZURE_STORAGE_RESOURCE_GROUP"],
188+
subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"],
189+
image_container="test-image-container",
190+
)
191+
192+
# Create a test file and image bytes
193+
with NamedTemporaryFile(suffix=".pdf") as temp_file:
194+
document_file = File(temp_file.file)
195+
# Create a simple 1x1 transparent PNG image
196+
image_bytes = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x04\x00\x00\x00\xb5\x1c\x0c\x02\x00\x00\x00\x0bIDATx\xdac\xfc\xff\xff?\x00\x05\xfe\x02\xfe\xa3\xb8\xfb\x26\x00\x00\x00\x00IEND\xaeB`\x82"
197+
image_filename = "test_image.png"
198+
image_page_num = 0
199+
200+
# No need to mock PIL - it will process the tiny PNG image
201+
# PIL operations will be simple and fast with this small image
202+
203+
# Mock container client operations
204+
async def mock_exists(*args, **kwargs):
205+
return True
206+
207+
monkeypatch.setattr("azure.storage.blob.aio.ContainerClient.exists", mock_exists)
208+
209+
expected_blob_name = f"{os.path.basename(temp_file.name)}/page{image_page_num}/{image_filename}"
210+
211+
async def mock_upload_blob(self, name, *args, **kwargs):
212+
assert name == expected_blob_name
213+
return azure.storage.blob.aio.BlobClient.from_blob_url(
214+
"https://test.blob.core.windows.net/test-image-container/test-image-url",
215+
credential=MockAzureCredential(),
216+
)
217+
218+
monkeypatch.setattr("azure.storage.blob.aio.ContainerClient.upload_blob", mock_upload_blob)
219+
220+
# Call the method and verify the results
221+
result_url = await blob_manager.upload_document_image(
222+
document_file, image_bytes, image_filename, image_page_num
223+
)
224+
225+
assert result_url == "https://test.blob.core.windows.net/test-image-container/test-image-url"
226+
227+
178228
def test_get_managed_identity_connection_string(mock_env, blob_manager):
179229
assert (
180230
blob_manager.get_managedidentity_connectionstring()

0 commit comments

Comments
 (0)