Skip to content

Commit

Permalink
Get chat and sync chat tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Dec 17, 2024
1 parent 85eab8d commit 0957521
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 111 deletions.
5 changes: 2 additions & 3 deletions chat/test/handlers/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
sys.path.append('./src')

from handlers.chat import handler
from handlers.model import set_model_override
from helpers.apitoken import ApiToken
from langchain_core.language_models.fake_chat_models import FakeListChatModel
from langgraph.checkpoint.memory import MemorySaver
Expand Down Expand Up @@ -37,8 +36,8 @@ def test_handler_unauthorized(self, mock_create_saver, mock_is_logged_in):

@patch.object(ApiToken, 'is_logged_in', return_value=True)
@patch('agent.search_agent.checkpoint_saver', return_value=MemorySaver())
def test_handler_success(self, mock_create_saver, mock_is_logged_in):
set_model_override(FakeListChatModel(responses=["fake response"]))
@patch('handlers.chat.chat_model', return_value=FakeListChatModel(responses=["fake response"]))
def test_handler_success(self, mock_chat_model, mock_create_saver, mock_is_logged_in):
event = {
"socket": Websocket(client=MockClient(), endpoint_url="test", connection_id="test", ref="test"),
"body": '{"question": "Question?"}'
Expand Down
38 changes: 25 additions & 13 deletions chat/test/handlers/test_chat_sync.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# ruff: noqa: E402

import json
import os
import sys
from langchain_core.language_models.fake_chat_models import FakeListChatModel
from langgraph.checkpoint.memory import MemorySaver

sys.path.append('./src')

from unittest import mock, TestCase
from unittest import TestCase
from unittest.mock import patch
from handlers.chat_sync import handler
from helpers.apitoken import ApiToken
Expand All @@ -14,22 +17,31 @@ class MockContext:
def __init__(self):
self.log_stream_name = 'test'

@mock.patch.dict(
os.environ,
{
"AZURE_OPENAI_RESOURCE_NAME": "test",
},
)
class TestHandler(TestCase):
def test_handler_unauthorized(self):
self.assertEqual(handler({"body": '{ "question": "Question?"}'}, MockContext()), {'body': 'Unauthorized', 'statusCode': 401})

@patch.object(ApiToken, 'is_logged_in')
@patch.object(ApiToken, 'is_logged_in', return_value = True)
def test_no_question(self, mock_is_logged_in):
mock_is_logged_in.return_value = True
self.assertEqual(handler({"body": '{ "question": ""}'}, MockContext()), {'statusCode': 400, 'body': 'Question cannot be blank'})

@patch.object(ApiToken, 'is_logged_in')
def test_handler_success(self, mock_is_logged_in):
mock_is_logged_in.return_value = True
self.assertEqual(handler({"body": '{"question": "Question?"}'}, MockContext()), {'statusCode': 200})
@patch.object(ApiToken, 'is_logged_in', return_value = True)
@patch("agent.search_agent.checkpoint_saver", return_value=MemorySaver())
@patch('handlers.chat_sync.chat_model', return_value=FakeListChatModel(responses=["fake response"]))
def test_handler_success(self, mock_chat_model, mock_create_saver, mock_is_logged_in):
expected_body = {
"answer": ["fake response"],
"is_dev_team": False,
"is_superuser": False,
"k": 40,
"model": "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
"question": "Question?",
"ref": "test_ref",
"artifacts": [],
"token_counts": {}
}
response = handler({"body": '{"question": "Question?", "ref": "test_ref"}'}, MockContext())

self.assertEqual(json.loads(response.get("body")), expected_body)
self.assertEqual(response.get("statusCode"), 200)
self.assertEqual(response.get("headers", {}).get("Content-Type"), "application/json")
95 changes: 0 additions & 95 deletions chat/test/helpers/test_http_event_config.py

This file was deleted.

0 comments on commit 0957521

Please sign in to comment.