Skip to content

Commit e87ee8e

Browse files
committed
fix: unify OpenAI config in unittests
1 parent ec6b87b commit e87ee8e

6 files changed

Lines changed: 49 additions & 46 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ cobertura.ser
6565
*.vi
6666
*~
6767
*.sass-cache
68+
__pycache__
6869

6970
# Dreamweaver added files
7071
_notes
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 pytest
19+
20+
from toolium.driver_wrappers_pool import DriverWrappersPool
21+
22+
23+
@pytest.fixture(scope='module', autouse=True)
24+
def configure_default_openai_model():
25+
"""
26+
Configure OpenAI model once for all tests in the module
27+
"""
28+
config = DriverWrappersPool.get_default_wrapper().config
29+
try:
30+
config.add_section('AI')
31+
except Exception:
32+
pass
33+
config.set('AI', 'openai_model', 'gpt-4.1-mini')

toolium/test/utils/ai_utils/test_ai_agent.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
import pytest
2323

24+
from toolium.test.utils.ai_utils.common import (
25+
configure_default_openai_model, # noqa: F401, fixture needed to set the OpenAI model for all tests in this module
26+
)
2427
from toolium.utils.ai_utils.ai_agent import create_react_agent, execute_agent
2528

2629
# Global variable to keep track of mock responses in the agent
@@ -68,9 +71,7 @@ def tv_recommendations(user_question): # noqa: ARG001
6871

6972
@pytest.mark.skipif(not os.getenv('AZURE_OPENAI_API_KEY'), reason='AZURE_OPENAI_API_KEY environment variable not set')
7073
def test_react_agent():
71-
agent = create_react_agent(
72-
TV_CONTENT_SYSTEM_MESSAGE, tool_method=tv_recommendations, provider='azure', model_name='gpt-4o-mini'
73-
)
74+
agent = create_react_agent(TV_CONTENT_SYSTEM_MESSAGE, tool_method=tv_recommendations, provider='azure')
7475
agent_results = execute_agent(agent)
7576

7677
# Check if the agent's final response contains a valid JSON with the expected structure and analyze the result

toolium/test/utils/ai_utils/test_answer_evaluation.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
Field = None
2727

2828
from toolium.driver_wrappers_pool import DriverWrappersPool
29+
from toolium.test.utils.ai_utils.common import (
30+
configure_default_openai_model, # noqa: F401, fixture needed to set the OpenAI model for all tests in this module
31+
)
2932
from toolium.utils.ai_utils.evaluate_answer import assert_answer_evaluation, get_answer_evaluation_with_azure_openai
3033

3134
test_data_get = [
@@ -68,9 +71,8 @@
6871
test_data_get,
6972
)
7073
def test_get_answer_evaluation_no_format_with_azure_openai(llm_answer, reference_answer, question):
71-
model = 'gpt4o'
7274
similarity, response = get_answer_evaluation_with_azure_openai(
73-
llm_answer=llm_answer, reference_answer=reference_answer, question=question, model_name=model
75+
llm_answer=llm_answer, reference_answer=reference_answer, question=question
7476
)
7577
assert isinstance(similarity, float), 'Similarity should be a float'
7678
assert isinstance(response['explanation'], str), 'Explanation should be a string'
@@ -90,12 +92,10 @@ class SimilarityEvaluation(BaseModel):
9092
similarity: float = Field(description='Similarity score between 0.0 and 1.0', ge=0.0, le=1.0)
9193
explanation: str = Field(description='Brief justification for the similarity score')
9294

93-
model = 'gpt4o'
9495
similarity, response = get_answer_evaluation_with_azure_openai(
9596
llm_answer=llm_answer,
9697
reference_answer=reference_answer,
9798
question=question,
98-
model_name=model,
9999
response_format=SimilarityEvaluation,
100100
)
101101
assert isinstance(similarity, float), 'Similarity should be a float'
@@ -133,12 +133,10 @@ class AnswerEval(BaseModel):
133133
'Only answer 5 if the answer is completely relevant to the question and gives no additional information.'
134134
)
135135

136-
model = 'gpt4o'
137136
similarity, response = get_answer_evaluation_with_azure_openai(
138137
llm_answer=llm_answer,
139138
reference_answer=reference_answer,
140139
question=question,
141-
model_name=model,
142140
response_format=AnswerEval,
143141
)
144142
assert isinstance(similarity, float), 'Similarity should be a float'
@@ -178,12 +176,10 @@ class AnswerEval(BaseModel):
178176
'Only answer 5 if the answer is completely relevant to the question and gives no additional information.'
179177
)
180178

181-
model = 'gpt4o'
182179
similarity, response = get_answer_evaluation_with_azure_openai(
183180
llm_answer=llm_answer,
184181
reference_answer=reference_answer,
185182
question=question,
186-
model_name=model,
187183
response_format=AnswerEval,
188184
)
189185
assert isinstance(similarity, float), 'Similarity should be a float'
@@ -197,13 +193,11 @@ class AnswerEval(BaseModel):
197193
test_data_assert,
198194
)
199195
def test_assert_answer_with_azure_openai(llm_answer, reference_answer, question, expected_low):
200-
model = 'gpt4o'
201-
provider = 'azure_openai'
196+
provider = 'azure'
202197
assert_answer_evaluation(
203198
llm_answer=llm_answer,
204199
reference_answers=reference_answer,
205200
question=question,
206-
model_name=model,
207201
threshold=expected_low,
208202
provider=provider,
209203
)
@@ -221,11 +215,9 @@ def test_assert_answer_from_config(llm_answer, reference_answer, question, expec
221215
except Exception:
222216
pass
223217
config.set('AI', 'provider', 'azure')
224-
model = 'gpt4o'
225218
assert_answer_evaluation(
226219
llm_answer=llm_answer,
227220
reference_answers=reference_answer,
228221
question=question,
229-
model_name=model,
230222
threshold=expected_low,
231223
)

toolium/test/utils/ai_utils/test_text_analysis.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,11 @@
2020

2121
import pytest
2222

23-
from toolium.driver_wrappers_pool import DriverWrappersPool
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+
)
2426
from toolium.utils.ai_utils.text_analysis import get_text_criteria_analysis
2527

26-
27-
def configure_default_openai_model():
28-
"""
29-
Configure OpenAI model used in unit tests
30-
"""
31-
config = DriverWrappersPool.get_default_wrapper().config
32-
try:
33-
config.add_section('AI')
34-
except Exception:
35-
pass
36-
config.set('AI', 'openai_model', 'gpt-4.1-mini')
37-
38-
3928
get_analysis_examples = (
4029
('How are you today?', ['is a greeting phrase', 'is a question'], 0.7, 1),
4130
('Today is sunny', ['is an affirmation', 'talks about the weather'], 0.7, 1),

toolium/test/utils/ai_utils/test_text_similarity.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,16 @@
2121
import pytest
2222

2323
from toolium.driver_wrappers_pool import DriverWrappersPool
24+
from toolium.test.utils.ai_utils.common import (
25+
configure_default_openai_model, # noqa: F401, fixture needed to set the OpenAI model for all tests in this module
26+
)
2427
from toolium.utils.ai_utils.text_similarity import (
2528
assert_text_similarity,
2629
get_text_similarity_with_azure_openai,
2730
get_text_similarity_with_sentence_transformers,
2831
get_text_similarity_with_spacy,
2932
)
3033

31-
32-
def configure_default_openai_model():
33-
"""
34-
Configure OpenAI model used in unit tests
35-
"""
36-
config = DriverWrappersPool.get_default_wrapper().config
37-
try:
38-
config.add_section('AI')
39-
except Exception:
40-
pass
41-
config.set('AI', 'openai_model', 'gpt-4o-mini')
42-
43-
4434
get_similarity_examples = (
4535
('Today it will be sunny', 'Today it will be sunny', 0.9, 1),
4636
('Today is sunny', 'Today it will be sunny', 0.7, 1),
@@ -78,7 +68,6 @@ def test_get_text_similarity_with_sentence_transformers(input_text, expected_tex
7868
get_openai_similarity_examples,
7969
)
8070
def test_get_text_similarity_with_azure_openai(input_text, expected_text, expected_low, expected_high):
81-
configure_default_openai_model()
8271
similarity = get_text_similarity_with_azure_openai(input_text, expected_text)
8372
assert expected_low <= similarity <= expected_high
8473

@@ -104,7 +93,6 @@ def test_assert_text_similarity_with_sentence_transformers_passed(input_text, ex
10493
@pytest.mark.skipif(not os.getenv('AZURE_OPENAI_API_KEY'), reason='AZURE_OPENAI_API_KEY environment variable not set')
10594
@pytest.mark.parametrize(('input_text', 'expected_text', 'threshold'), assert_similarity_passed_examples)
10695
def test_assert_text_similarity_with_openai_passed(input_text, expected_text, threshold):
107-
configure_default_openai_model()
10896
assert_text_similarity(input_text, expected_text, threshold=threshold, similarity_method='azure_openai')
10997

11098

@@ -143,7 +131,6 @@ def test_assert_text_similarity_with_sentence_transformers_failed(input_text, ex
143131
@pytest.mark.skipif(not os.getenv('AZURE_OPENAI_API_KEY'), reason='AZURE_OPENAI_API_KEY environment variable not set')
144132
@pytest.mark.parametrize(('input_text', 'expected_text', 'threshold'), assert_openai_similarity_failed_examples)
145133
def test_assert_text_similarity_with_openai_failed(input_text, expected_text, threshold):
146-
configure_default_openai_model()
147134
with pytest.raises(AssertionError) as excinfo:
148135
assert_text_similarity(input_text, expected_text, threshold=threshold, similarity_method='azure_openai')
149136
assert str(excinfo.value).startswith('Similarity between received and expected texts is below threshold')

0 commit comments

Comments
 (0)