Редактировать test_intent_classify.py
This commit is contained in:
121
tests/test_intent_classify.py
Normal file
121
tests/test_intent_classify.py
Normal file
@@ -0,0 +1,121 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Тесты для модуля классификации намерений.
|
||||
Проверяем, что функция корректно распознаёт разные типы запросов.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from core.functions.intent_classify import classify_intent, _intent_cache, _CACHE_TTL
|
||||
import time
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_classify_fact(mock_giga, mock_bot_config, sample_prompt_intent):
|
||||
"""
|
||||
Тест: запрос с конкретным фактом должен возвращать 'FACT'.
|
||||
"""
|
||||
# Настраиваем мок: при вызове chat вернём 'FACT'
|
||||
mock_giga.chat.return_value = "FACT"
|
||||
|
||||
result = await classify_intent(
|
||||
giga=mock_giga,
|
||||
query="Какое тактовое время установлено на линии?",
|
||||
prompt_text=sample_prompt_intent,
|
||||
bot_config=mock_bot_config
|
||||
)
|
||||
assert result == "FACT"
|
||||
# Проверяем, что chat был вызван один раз
|
||||
mock_giga.chat.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_classify_greeting(mock_giga, mock_bot_config, sample_prompt_intent):
|
||||
"""
|
||||
Тест: приветствие должно возвращать 'GREETING'.
|
||||
"""
|
||||
mock_giga.chat.return_value = "GREETING"
|
||||
|
||||
result = await classify_intent(
|
||||
giga=mock_giga,
|
||||
query="Привет!",
|
||||
prompt_text=sample_prompt_intent,
|
||||
bot_config=mock_bot_config
|
||||
)
|
||||
assert result == "GREETING"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_classify_unknown(mock_giga, mock_bot_config, sample_prompt_intent):
|
||||
"""
|
||||
Тест: если модель вернула неизвестный код, возвращается 'GENERAL'.
|
||||
"""
|
||||
mock_giga.chat.return_value = "UNKNOWN_CODE"
|
||||
|
||||
result = await classify_intent(
|
||||
giga=mock_giga,
|
||||
query="Что-то непонятное",
|
||||
prompt_text=sample_prompt_intent,
|
||||
bot_config=mock_bot_config
|
||||
)
|
||||
assert result == "GENERAL"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_classify_cache(mock_giga, mock_bot_config, sample_prompt_intent):
|
||||
"""
|
||||
Тест: проверка работы кэша – при повторном запросе результат берётся из кэша,
|
||||
и chat вызывается только один раз.
|
||||
"""
|
||||
mock_giga.chat.return_value = "METRICS"
|
||||
|
||||
# Первый вызов – должен вызвать chat
|
||||
result1 = await classify_intent(
|
||||
giga=mock_giga,
|
||||
query="Выведи все KPI из отчёта",
|
||||
prompt_text=sample_prompt_intent,
|
||||
bot_config=mock_bot_config
|
||||
)
|
||||
assert result1 == "METRICS"
|
||||
assert mock_giga.chat.call_count == 1
|
||||
|
||||
# Второй вызов с тем же запросом – chat не должен вызываться (кэш)
|
||||
result2 = await classify_intent(
|
||||
giga=mock_giga,
|
||||
query="Выведи все KPI из отчёта",
|
||||
prompt_text=sample_prompt_intent,
|
||||
bot_config=mock_bot_config
|
||||
)
|
||||
assert result2 == "METRICS"
|
||||
assert mock_giga.chat.call_count == 1 # всё ещё 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_classify_cache_expiry(mock_giga, mock_bot_config, sample_prompt_intent):
|
||||
"""
|
||||
Тест: проверка, что кэш истекает после TTL.
|
||||
"""
|
||||
mock_giga.chat.return_value = "FACT"
|
||||
|
||||
# Первый вызов
|
||||
result1 = await classify_intent(
|
||||
giga=mock_giga,
|
||||
query="Какая длительность переналадки?",
|
||||
prompt_text=sample_prompt_intent,
|
||||
bot_config=mock_bot_config
|
||||
)
|
||||
assert result1 == "FACT"
|
||||
assert mock_giga.chat.call_count == 1
|
||||
|
||||
# Искусственно ставим время в кэше так, чтобы оно было старше TTL
|
||||
key = "какая длительность переналадки?"
|
||||
_intent_cache[key] = (time.time() - _CACHE_TTL - 10, "FACT")
|
||||
|
||||
# Второй вызов – должен снова вызвать chat
|
||||
result2 = await classify_intent(
|
||||
giga=mock_giga,
|
||||
query="Какая длительность переналадки?",
|
||||
prompt_text=sample_prompt_intent,
|
||||
bot_config=mock_bot_config
|
||||
)
|
||||
assert result2 == "FACT"
|
||||
assert mock_giga.chat.call_count == 2
|
||||
Reference in New Issue
Block a user