From c8ed64b3fc60b4bdb5a9b382763fce452fc15ea5 Mon Sep 17 00:00:00 2001 From: Markov Andrey Date: Tue, 30 Jun 2026 09:23:37 +0000 Subject: [PATCH] Add new file --- core/tests/test_intent_classify.py | 121 +++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 core/tests/test_intent_classify.py diff --git a/core/tests/test_intent_classify.py b/core/tests/test_intent_classify.py new file mode 100644 index 0000000..02edcc0 --- /dev/null +++ b/core/tests/test_intent_classify.py @@ -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 \ No newline at end of file