|
| 1 | +""" |
| 2 | +Copyright 2026 Telefónica Innovación Digital, S.L. |
| 3 | +This file is part of Toolium. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +""" |
| 17 | + |
| 18 | +import os |
| 19 | +from unittest import mock |
| 20 | + |
| 21 | +import pytest |
| 22 | + |
| 23 | +from toolium.test.utils.ai_utils.common import ( |
| 24 | + configure_default_openai_model, # noqa: F401, fixture needed to set the OpenAI model for all tests in this module |
| 25 | +) |
| 26 | +from toolium.utils.ai_utils.openai import openai_request |
| 27 | + |
| 28 | + |
| 29 | +def _build_mock_completion(content='test response', prompt_tokens=10, completion_tokens=5, total_tokens=15): |
| 30 | + """Build a mock OpenAI completion object with usage data""" |
| 31 | + usage_dump = { |
| 32 | + 'prompt_tokens': prompt_tokens, |
| 33 | + 'completion_tokens': completion_tokens, |
| 34 | + 'total_tokens': total_tokens, |
| 35 | + 'completion_tokens_details': { |
| 36 | + 'accepted_prediction_tokens': 0, |
| 37 | + 'audio_tokens': 0, |
| 38 | + 'reasoning_tokens': 0, |
| 39 | + 'rejected_prediction_tokens': 0, |
| 40 | + }, |
| 41 | + 'prompt_tokens_details': { |
| 42 | + 'audio_tokens': 0, |
| 43 | + 'cached_tokens': 0, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + mock_usage = mock.MagicMock() |
| 48 | + mock_usage.prompt_tokens = prompt_tokens |
| 49 | + mock_usage.completion_tokens = completion_tokens |
| 50 | + mock_usage.total_tokens = total_tokens |
| 51 | + mock_usage.model_dump.return_value = usage_dump |
| 52 | + |
| 53 | + mock_message = mock.MagicMock() |
| 54 | + mock_message.content = content |
| 55 | + |
| 56 | + mock_choice = mock.MagicMock() |
| 57 | + mock_choice.message = mock_message |
| 58 | + |
| 59 | + mock_completion = mock.MagicMock() |
| 60 | + mock_completion.choices = [mock_choice] |
| 61 | + mock_completion.usage = mock_usage |
| 62 | + return mock_completion |
| 63 | + |
| 64 | + |
| 65 | +@mock.patch('toolium.utils.ai_utils.openai.OpenAI') |
| 66 | +def test_openai_request_returns_token_usage(mock_openai_class): |
| 67 | + mock_client = mock.MagicMock() |
| 68 | + mock_openai_class.return_value = mock_client |
| 69 | + mock_client.chat.completions.create.return_value = _build_mock_completion( |
| 70 | + content='hello', prompt_tokens=20, completion_tokens=10, total_tokens=30 |
| 71 | + ) |
| 72 | + |
| 73 | + response, token_usage = openai_request('system', 'user') |
| 74 | + |
| 75 | + assert response == 'hello' |
| 76 | + assert token_usage['prompt_tokens'] == 20 |
| 77 | + assert token_usage['completion_tokens'] == 10 |
| 78 | + assert token_usage['total_tokens'] == 30 |
| 79 | + assert 'completion_tokens_details' in token_usage |
| 80 | + assert 'prompt_tokens_details' in token_usage |
| 81 | + |
| 82 | + |
| 83 | +@mock.patch('toolium.utils.ai_utils.openai.OpenAI') |
| 84 | +def test_openai_request_returns_empty_token_usage_when_no_usage(mock_openai_class): |
| 85 | + mock_client = mock.MagicMock() |
| 86 | + mock_openai_class.return_value = mock_client |
| 87 | + mock_completion = _build_mock_completion() |
| 88 | + mock_completion.usage = None |
| 89 | + mock_client.chat.completions.create.return_value = mock_completion |
| 90 | + |
| 91 | + response, token_usage = openai_request('system', 'user') |
| 92 | + |
| 93 | + assert response == 'test response' |
| 94 | + assert token_usage == {} |
| 95 | + |
| 96 | + |
| 97 | +@mock.patch('toolium.utils.ai_utils.openai.OpenAI') |
| 98 | +def test_openai_request_with_response_format_returns_token_usage(mock_openai_class): |
| 99 | + mock_client = mock.MagicMock() |
| 100 | + mock_openai_class.return_value = mock_client |
| 101 | + |
| 102 | + mock_parsed = mock.MagicMock() |
| 103 | + mock_message = mock.MagicMock() |
| 104 | + mock_message.parsed = mock_parsed |
| 105 | + mock_choice = mock.MagicMock() |
| 106 | + mock_choice.message = mock_message |
| 107 | + mock_completion = _build_mock_completion(prompt_tokens=50, completion_tokens=25, total_tokens=75) |
| 108 | + mock_completion.choices = [mock_choice] |
| 109 | + mock_client.beta.chat.completions.parse.return_value = mock_completion |
| 110 | + |
| 111 | + response, token_usage = openai_request('system', 'user', response_format=mock.MagicMock()) |
| 112 | + |
| 113 | + assert response is mock_parsed |
| 114 | + assert token_usage['prompt_tokens'] == 50 |
| 115 | + assert token_usage['completion_tokens'] == 25 |
| 116 | + assert token_usage['total_tokens'] == 75 |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.skipif(not os.getenv('AZURE_OPENAI_API_KEY'), reason='AZURE_OPENAI_API_KEY environment variable not set') |
| 120 | +def test_openai_request_returns_token_usage_with_azure(): |
| 121 | + response, token_usage = openai_request('You are a helpful assistant.', 'Say hello.', azure=True) |
| 122 | + |
| 123 | + assert isinstance(response, str) |
| 124 | + assert len(response) > 0 |
| 125 | + assert token_usage['prompt_tokens'] > 0 |
| 126 | + assert token_usage['completion_tokens'] > 0 |
| 127 | + assert token_usage['total_tokens'] > 0 |
| 128 | + assert 'completion_tokens_details' in token_usage |
| 129 | + assert 'prompt_tokens_details' in token_usage |
0 commit comments