Refactor tests to match project style guidelines
- Removed class-based test structure - Moved imports to top of file - Added section dividers (============) between test groups - Removed inline imports - Followed same structure as test_serving_chat_sampling_params.py - All tests are now standalone functions with proper docstrings This aligns with the test style guide at docs/contributing/ci/tests_style.md Co-authored-by: hsliuustc0106 <222337142+hsliuustc0106@users.noreply.github.com>
This commit is contained in:
1 parent
3dd27e4365
commit
8afbc59636
1 file changed
+83
-79
@@ -1,113 +1,117 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
"""
|
"""
|
||||||
Unit tests for OmniOpenAIServingChat modalities filtering logic.
|
Unit tests for OmniOpenAIServingChat modalities filtering.
|
||||||
|
|
||||||
Tests the specific filtering logic for modalities parameter in non-streaming
|
Tests that modalities parameter correctly filters outputs in non-streaming
|
||||||
chat completions. These are unit tests focused on the filtering mechanism
|
chat completions while preserving usage/billing information.
|
||||||
without requiring the full serving infrastructure.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
class TestModalitiesFilteringLogic:
|
# =============================================================================
|
||||||
"""Tests for modalities filtering logic."""
|
# Tests for modalities set creation logic
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
def test_requested_modalities_set_creation_with_list(self):
|
|
||||||
"""Test that requested_modalities set is created from list."""
|
|
||||||
from unittest.mock import MagicMock
|
|
||||||
|
|
||||||
# Simulate what happens in the code
|
def test_requested_modalities_set_creation_with_list():
|
||||||
request = MagicMock()
|
"""Test that requested_modalities set is created from list."""
|
||||||
request.modalities = ["text", "audio"]
|
# Simulate what happens in the code
|
||||||
|
request = MagicMock()
|
||||||
|
request.modalities = ["text", "audio"]
|
||||||
|
|
||||||
# This is the logic from the fix
|
# This is the logic from the fix
|
||||||
requested_modalities = (
|
requested_modalities = set(request.modalities) if hasattr(request, "modalities") and request.modalities else None
|
||||||
set(request.modalities) if hasattr(request, "modalities") and request.modalities else None
|
|
||||||
)
|
|
||||||
|
|
||||||
assert requested_modalities == {"text", "audio"}
|
assert requested_modalities == {"text", "audio"}
|
||||||
|
|
||||||
def test_requested_modalities_set_creation_with_none(self):
|
|
||||||
"""Test that requested_modalities is None when modalities is None."""
|
|
||||||
from unittest.mock import MagicMock
|
|
||||||
|
|
||||||
request = MagicMock()
|
def test_requested_modalities_set_creation_with_none():
|
||||||
request.modalities = None
|
"""Test that requested_modalities is None when modalities is None."""
|
||||||
|
request = MagicMock()
|
||||||
|
request.modalities = None
|
||||||
|
|
||||||
requested_modalities = (
|
requested_modalities = set(request.modalities) if hasattr(request, "modalities") and request.modalities else None
|
||||||
set(request.modalities) if hasattr(request, "modalities") and request.modalities else None
|
|
||||||
)
|
|
||||||
|
|
||||||
assert requested_modalities is None
|
assert requested_modalities is None
|
||||||
|
|
||||||
def test_requested_modalities_set_creation_when_missing(self):
|
|
||||||
"""Test that requested_modalities is None when attribute missing."""
|
|
||||||
from unittest.mock import MagicMock
|
|
||||||
|
|
||||||
request = MagicMock(spec=[]) # No modalities attribute
|
def test_requested_modalities_set_creation_when_missing():
|
||||||
|
"""Test that requested_modalities is None when attribute missing."""
|
||||||
|
request = MagicMock(spec=[]) # No modalities attribute
|
||||||
|
|
||||||
requested_modalities = (
|
requested_modalities = set(request.modalities) if hasattr(request, "modalities") and request.modalities else None
|
||||||
set(request.modalities) if hasattr(request, "modalities") and request.modalities else None
|
|
||||||
)
|
|
||||||
|
|
||||||
assert requested_modalities is None
|
assert requested_modalities is None
|
||||||
|
|
||||||
def test_modality_filtering_text_in_set(self):
|
|
||||||
"""Test that text output passes filter when text is requested."""
|
|
||||||
requested_modalities = {"text", "audio"}
|
|
||||||
final_output_type = "text"
|
|
||||||
|
|
||||||
# This is the filtering logic from the fix
|
# =============================================================================
|
||||||
should_filter = requested_modalities is not None and final_output_type not in requested_modalities
|
# Tests for modality filtering logic
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
assert not should_filter # text should NOT be filtered
|
|
||||||
|
|
||||||
def test_modality_filtering_audio_not_in_set(self):
|
def test_modality_filtering_text_in_set():
|
||||||
"""Test that audio output is filtered when only text requested."""
|
"""Test that text output passes filter when text is requested."""
|
||||||
requested_modalities = {"text"}
|
requested_modalities = {"text", "audio"}
|
||||||
final_output_type = "audio"
|
final_output_type = "text"
|
||||||
|
|
||||||
should_filter = requested_modalities is not None and final_output_type not in requested_modalities
|
# This is the filtering logic from the fix
|
||||||
|
should_filter = requested_modalities is not None and final_output_type not in requested_modalities
|
||||||
|
|
||||||
assert should_filter # audio SHOULD be filtered
|
assert not should_filter # text should NOT be filtered
|
||||||
|
|
||||||
def test_modality_filtering_with_none_modalities(self):
|
|
||||||
"""Test that nothing is filtered when modalities is None."""
|
|
||||||
requested_modalities = None
|
|
||||||
final_output_type = "text"
|
|
||||||
|
|
||||||
should_filter = requested_modalities is not None and final_output_type not in requested_modalities
|
def test_modality_filtering_audio_not_in_set():
|
||||||
|
"""Test that audio output is filtered when only text requested."""
|
||||||
|
requested_modalities = {"text"}
|
||||||
|
final_output_type = "audio"
|
||||||
|
|
||||||
assert not should_filter # Nothing should be filtered
|
should_filter = requested_modalities is not None and final_output_type not in requested_modalities
|
||||||
|
|
||||||
final_output_type = "audio"
|
assert should_filter # audio SHOULD be filtered
|
||||||
should_filter = requested_modalities is not None and final_output_type not in requested_modalities
|
|
||||||
|
|
||||||
assert not should_filter # Nothing should be filtered
|
|
||||||
|
|
||||||
def test_usage_extraction_logic(self):
|
def test_modality_filtering_with_none_modalities():
|
||||||
"""Test that usage info is extracted correctly from request_output."""
|
"""Test that nothing is filtered when modalities is None."""
|
||||||
from unittest.mock import MagicMock
|
requested_modalities = None
|
||||||
|
final_output_type = "text"
|
||||||
|
|
||||||
# Mock the omni_outputs structure
|
should_filter = requested_modalities is not None and final_output_type not in requested_modalities
|
||||||
omni_outputs = MagicMock()
|
|
||||||
omni_outputs.final_output_type = "text"
|
|
||||||
omni_outputs.request_output = MagicMock()
|
|
||||||
omni_outputs.request_output.prompt_token_ids = [1] * 10
|
|
||||||
omni_outputs.request_output.encoder_prompt_token_ids = None
|
|
||||||
|
|
||||||
mock_output = MagicMock()
|
assert not should_filter # Nothing should be filtered
|
||||||
mock_output.token_ids = [1] * 20
|
|
||||||
omni_outputs.request_output.outputs = [mock_output]
|
|
||||||
|
|
||||||
# Simulate the usage extraction logic
|
final_output_type = "audio"
|
||||||
final_output_type = omni_outputs.final_output_type
|
should_filter = requested_modalities is not None and final_output_type not in requested_modalities
|
||||||
if final_output_type == "text" and omni_outputs.request_output is not None:
|
|
||||||
final_res = omni_outputs.request_output
|
|
||||||
if final_res.prompt_token_ids is not None:
|
|
||||||
num_prompt_tokens = len(final_res.prompt_token_ids)
|
|
||||||
if final_res.encoder_prompt_token_ids is not None:
|
|
||||||
num_prompt_tokens += len(final_res.encoder_prompt_token_ids)
|
|
||||||
num_generated_tokens = sum(len(output.token_ids) for output in final_res.outputs)
|
|
||||||
|
|
||||||
assert num_prompt_tokens == 10
|
assert not should_filter # Nothing should be filtered
|
||||||
assert num_generated_tokens == 20
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Tests for usage extraction logic
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
|
||||||
|
def test_usage_extraction_logic():
|
||||||
|
"""Test that usage info is extracted correctly from request_output."""
|
||||||
|
# Mock the omni_outputs structure
|
||||||
|
omni_outputs = MagicMock()
|
||||||
|
omni_outputs.final_output_type = "text"
|
||||||
|
omni_outputs.request_output = MagicMock()
|
||||||
|
omni_outputs.request_output.prompt_token_ids = [1] * 10
|
||||||
|
omni_outputs.request_output.encoder_prompt_token_ids = None
|
||||||
|
|
||||||
|
mock_output = MagicMock()
|
||||||
|
mock_output.token_ids = [1] * 20
|
||||||
|
omni_outputs.request_output.outputs = [mock_output]
|
||||||
|
|
||||||
|
# Simulate the usage extraction logic
|
||||||
|
final_output_type = omni_outputs.final_output_type
|
||||||
|
if final_output_type == "text" and omni_outputs.request_output is not None:
|
||||||
|
final_res = omni_outputs.request_output
|
||||||
|
if final_res.prompt_token_ids is not None:
|
||||||
|
num_prompt_tokens = len(final_res.prompt_token_ids)
|
||||||
|
if final_res.encoder_prompt_token_ids is not None:
|
||||||
|
num_prompt_tokens += len(final_res.encoder_prompt_token_ids)
|
||||||
|
num_generated_tokens = sum(len(output.token_ids) for output in final_res.outputs)
|
||||||
|
|
||||||
|
assert num_prompt_tokens == 10
|
||||||
|
assert num_generated_tokens == 20
|
||||||
Reference in new issue
Block a user