fix: resolve test failures - floating point precision and fragile imports

- Use pytest.approx() for floating point cost comparisons to handle precision
  issues (0.0022500000000000003 vs 0.00225)
- Remove fragile sys.path.insert() from openai test that breaks in CI
- pytest discovers imports properly without manual path manipulation

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Chris Hunter 2026-04-27 15:28:32 +02:00
parent acd31f7062
commit 8980b77d4d
3 changed files with 7 additions and 19 deletions

View file

@ -94,7 +94,7 @@ class TestManusResponsesAPICostCalculation:
# Verify cost was calculated
assert isinstance(result, ResponsesAPIResponse)
assert "response_cost" in result._hidden_params
assert result._hidden_params["response_cost"] == 0.00225
assert result._hidden_params["response_cost"] == pytest.approx(0.00225)
@patch("litellm.llms.openai.responses.transformation.generic_cost_per_token")
def test_transform_get_response_api_response_extracts_model_from_response(
@ -133,7 +133,7 @@ class TestManusResponsesAPICostCalculation:
# Verify cost was calculated using model from response
assert isinstance(result, ResponsesAPIResponse)
assert "response_cost" in result._hidden_params
assert result._hidden_params["response_cost"] == 0.0015
assert result._hidden_params["response_cost"] == pytest.approx(0.0015)
@patch("litellm.llms.openai.responses.transformation.generic_cost_per_token")
def test_transform_get_response_api_response_fallback_to_logging_obj(
@ -174,7 +174,7 @@ class TestManusResponsesAPICostCalculation:
# Verify cost was calculated using model from logging_obj
assert isinstance(result, ResponsesAPIResponse)
assert "response_cost" in result._hidden_params
assert result._hidden_params["response_cost"] == 0.0015
assert result._hidden_params["response_cost"] == pytest.approx(0.0015)
@patch("litellm.llms.openai.responses.transformation.generic_cost_per_token")
def test_cost_calculation_with_missing_usage(self, mock_generic_cost):

View file

@ -2,18 +2,6 @@
Unit tests for Responses API cost calculation functionality.
Tests the fix for issue #26475 - ensuring cost calculation works correctly
across all 60+ providers inheriting from OpenAIResponsesAPIConfig.
"""
import json
import os
import sys
from unittest.mock import MagicMock, patch
import httpx
import pytest
sys.path.insert(
0, os.path.abspath("../../../../..")
) # Adds the parent directory to the system path
from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig
from litellm.types.llms.openai import (

View file

@ -63,7 +63,7 @@ class TestVolcEngineResponsesAPICostCalculation:
# Verify cost was calculated
assert isinstance(result, ResponsesAPIResponse)
assert "response_cost" in result._hidden_params
assert result._hidden_params["response_cost"] == 0.003 # 0.002 + 0.001
assert result._hidden_params["response_cost"] == pytest.approx(0.003) # 0.002 + 0.001
@patch("litellm.llms.openai.responses.transformation.generic_cost_per_token")
def test_transform_response_api_response_with_cached_tokens(
@ -104,7 +104,7 @@ class TestVolcEngineResponsesAPICostCalculation:
# Verify cost was calculated with caching
assert isinstance(result, ResponsesAPIResponse)
assert "response_cost" in result._hidden_params
assert result._hidden_params["response_cost"] == 0.007
assert result._hidden_params["response_cost"] == pytest.approx(0.007)
@patch("litellm.llms.openai.responses.transformation.generic_cost_per_token")
def test_transform_streaming_response_completed_calculates_cost(
@ -142,7 +142,7 @@ class TestVolcEngineResponsesAPICostCalculation:
# Verify cost was calculated and stored in the response object
assert "response_cost" in result.response._hidden_params
assert result.response._hidden_params["response_cost"] == 0.00225
assert result.response._hidden_params["response_cost"] == pytest.approx(0.00225)
def test_transform_streaming_response_non_completed_no_cost(self):
"""Test that non-completed streaming events don't calculate cost"""
@ -197,7 +197,7 @@ class TestVolcEngineResponsesAPICostCalculation:
# Should patch missing output and still calculate cost
assert isinstance(result, ResponseCompletedEvent)
assert "response_cost" in result.response._hidden_params
assert result.response._hidden_params["response_cost"] == 0.0015
assert result.response._hidden_params["response_cost"] == pytest.approx(0.0015)
@patch("litellm.llms.openai.responses.transformation.generic_cost_per_token")
def test_cost_calculation_with_zero_tokens(self, mock_generic_cost):