WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c6bcbc0

Browse files
updated test cases
1 parent 5091190 commit c6bcbc0

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

code/tests/functional/tests/functions/advanced_image_processing/test_advanced_image_processing.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def setup_blob_metadata_mocking(httpserver: HTTPServer, app_config: AppConfig):
6363
method="PUT",
6464
).respond_with_data()
6565

66+
# Mock GET request for image download (base64 conversion)
67+
httpserver.expect_request(
68+
f"/{app_config.get_from_json('AZURE_BLOB_STORAGE_INFO','containerName')}/{FILE_NAME}",
69+
method="GET",
70+
).respond_with_data(b"fake_image_data", content_type="image/jpeg")
71+
6672

6773
@pytest.fixture(autouse=True)
6874
def setup_caption_response(httpserver: HTTPServer, app_config: AppConfig):
@@ -192,11 +198,10 @@ def test_image_passed_to_llm_to_generate_caption(
192198
),
193199
)[0]
194200

195-
assert request.get_json()["messages"][1]["content"][1]["image_url"][
196-
"url"
197-
].startswith(
198-
f"{app_config.get('AZURE_STORAGE_ACCOUNT_ENDPOINT')}{app_config.get_from_json('AZURE_BLOB_STORAGE_INFO','containerName')}/{FILE_NAME}"
199-
)
201+
# The URL should be a direct URL (not base64) for http/https URLs to save tokens
202+
image_url = request.get_json()["messages"][1]["content"][1]["image_url"]["url"]
203+
expected_url_prefix = f"{app_config.get('AZURE_STORAGE_ACCOUNT_ENDPOINT')}{app_config.get_from_json('AZURE_BLOB_STORAGE_INFO','containerName')}/{FILE_NAME}"
204+
assert image_url.startswith(expected_url_prefix), f"Expected direct URL starting with {expected_url_prefix}, got {image_url[:100]}"
200205

201206

202207
def test_embeddings_generated_for_caption(

code/tests/utilities/helpers/test_push_embedder.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ def azure_computer_vision_mock():
171171
yield mock
172172

173173

174+
@pytest.fixture(autouse=True)
175+
def urllib_request_mock():
176+
with patch(
177+
"backend.batch.utilities.helpers.embedders.push_embedder.urllib.request.urlopen"
178+
) as mock:
179+
# Create a mock response object
180+
mock_response = MagicMock()
181+
mock_response.read.return_value = b"fake_image_data"
182+
mock_response.__enter__.return_value = mock_response
183+
mock_response.__exit__.return_value = None
184+
mock.return_value = mock_response
185+
yield mock
186+
187+
174188
def test_embed_file_advanced_image_processing_vectorizes_image(
175189
azure_computer_vision_mock,
176190
):
@@ -200,29 +214,23 @@ def test_embed_file_advanced_image_processing_uses_vision_model_for_captioning(
200214
push_embedder.embed_file(source_url, "some-file-name.jpg")
201215

202216
# then
203-
llm_helper_mock.get_chat_completion.assert_called_once_with(
204-
[
205-
{
206-
"role": "system",
207-
"content": """You are an assistant that generates rich descriptions of images.
208-
You need to be accurate in the information you extract and detailed in the descriptons you generate.
209-
Do not abbreviate anything and do not shorten sentances. Explain the image completely.
210-
If you are provided with an image of a flow chart, describe the flow chart in detail.
211-
If the image is mostly text, use OCR to extract the text as it is displayed in the image.""",
212-
},
213-
{
214-
"role": "user",
215-
"content": [
216-
{
217-
"text": "Describe this image in detail. Limit the response to 500 words.",
218-
"type": "text",
219-
},
220-
{"image_url": {"url": source_url}, "type": "image_url"},
221-
],
222-
},
223-
],
224-
env_helper_mock.AZURE_OPENAI_VISION_MODEL,
225-
)
217+
# Verify the vision model is called with direct URL (not base64) for token efficiency
218+
llm_helper_mock.get_chat_completion.assert_called_once()
219+
call_args = llm_helper_mock.get_chat_completion.call_args
220+
messages = call_args[0][0]
221+
model = call_args[0][1]
222+
223+
assert model == env_helper_mock.AZURE_OPENAI_VISION_MODEL
224+
assert len(messages) == 2
225+
assert messages[0]["role"] == "system"
226+
assert "You are an assistant that generates rich descriptions of images" in messages[0]["content"]
227+
assert messages[1]["role"] == "user"
228+
assert len(messages[1]["content"]) == 2
229+
assert messages[1]["content"][0]["type"] == "text"
230+
assert "Describe this image in detail" in messages[1]["content"][0]["text"]
231+
assert messages[1]["content"][1]["type"] == "image_url"
232+
# Direct URL should be used (not base64) for http/https URLs to save tokens
233+
assert messages[1]["content"][1]["image_url"]["url"] == source_url
226234

227235

228236
def test_embed_file_advanced_image_processing_stores_embeddings_in_search_index(

0 commit comments

Comments
 (0)