|
|
import unittest |
|
|
from unittest.mock import patch, MagicMock |
|
|
import sys |
|
|
import os |
|
|
import json |
|
|
|
|
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src')) |
|
|
|
|
|
|
|
|
from app import app, ChatRequest, ChatResponse |
|
|
|
|
|
class TestChatbotAPI(unittest.TestCase): |
|
|
|
|
|
@patch('app.qa_chain') |
|
|
def test_basic_query_response(self, mock_qa_chain): |
|
|
|
|
|
mock_qa_chain.return_value = { |
|
|
"query": "What is qualitative research?", |
|
|
"result": "Qualitative research focuses on thoughts, concepts, or experiences. The data collected often comes in narrative form and concentrates on unearthing insights that can lead to testable hypotheses.", |
|
|
"source_documents": [ |
|
|
MagicMock( |
|
|
page_content="Qualitative research focuses on thoughts, concepts, or experiences. The data collected often comes in narrative form and concentrates on unearthing insights that can lead to testable hypotheses.", |
|
|
metadata={"source": "American University. (2020, July 23). Qualitative vs. quantitative research: Comparing the methods and strategies for education research.", "page": "1"} |
|
|
) |
|
|
] |
|
|
} |
|
|
|
|
|
|
|
|
request = ChatRequest( |
|
|
message="What is qualitative research?", |
|
|
conversation_history=[] |
|
|
) |
|
|
|
|
|
|
|
|
response = app.chat(request) |
|
|
|
|
|
|
|
|
self.assertIsInstance(response, ChatResponse) |
|
|
self.assertIn("Qualitative research focuses on", response.response) |
|
|
self.assertEqual(len(response.citations), 1) |
|
|
self.assertEqual(response.citations[0]["text"], "American University. (2020, July 23). Qualitative vs. quantitative research: Comparing the methods and strategies for education research.") |
|
|
|
|
|
@patch('app.qa_chain') |
|
|
def test_method_recommendation(self, mock_qa_chain): |
|
|
|
|
|
mock_qa_chain.return_value = { |
|
|
"query": "Which research method should I use to study student engagement in online learning?", |
|
|
"result": "For studying student engagement in online learning, a mixed methods approach would be most appropriate. This would allow you to collect both quantitative data (such as participation metrics, time spent on tasks) and qualitative data (such as student experiences and perceptions).", |
|
|
"source_documents": [ |
|
|
MagicMock( |
|
|
page_content="Mixed methods research combines the elements of two types of research: quantitative and qualitative. By blending both quantitative and qualitative data, mixed methods research allows for a more thorough exploration of a research question.", |
|
|
metadata={"source": "Dovetail. (2023, February 20). Mixed methods research guide with examples.", "page": "1"} |
|
|
) |
|
|
] |
|
|
} |
|
|
|
|
|
|
|
|
request = ChatRequest( |
|
|
message="Which research method should I use to study student engagement in online learning?", |
|
|
conversation_history=[] |
|
|
) |
|
|
|
|
|
|
|
|
response = app.chat(request) |
|
|
|
|
|
|
|
|
self.assertIsInstance(response, ChatResponse) |
|
|
self.assertIn("mixed methods approach", response.response.lower()) |
|
|
self.assertEqual(len(response.citations), 1) |
|
|
self.assertEqual(response.citations[0]["text"], "Dovetail. (2023, February 20). Mixed methods research guide with examples.") |
|
|
|
|
|
@patch('app.qa_chain') |
|
|
def test_comparison_query(self, mock_qa_chain): |
|
|
|
|
|
mock_qa_chain.return_value = { |
|
|
"query": "What are the differences between qualitative and quantitative research methods?", |
|
|
"result": "Qualitative research focuses on thoughts, concepts, or experiences, with data collected in narrative form. Quantitative research is expressed in numbers and measurements, aiming to find data to confirm or test a hypothesis.", |
|
|
"source_documents": [ |
|
|
MagicMock( |
|
|
page_content="Qualitative research focuses on thoughts, concepts, or experiences. The data collected often comes in narrative form.", |
|
|
metadata={"source": "American University. (2020, July 23). Qualitative vs. quantitative research: Comparing the methods and strategies for education research.", "page": "1"} |
|
|
), |
|
|
MagicMock( |
|
|
page_content="Quantitative research in education is expressed in numbers and measurements. This type of research aims to find data to confirm or test a hypothesis.", |
|
|
metadata={"source": "American University. (2020, July 23). Qualitative vs. quantitative research: Comparing the methods and strategies for education research.", "page": "2"} |
|
|
) |
|
|
] |
|
|
} |
|
|
|
|
|
|
|
|
request = ChatRequest( |
|
|
message="What are the differences between qualitative and quantitative research methods?", |
|
|
conversation_history=[] |
|
|
) |
|
|
|
|
|
|
|
|
response = app.chat(request) |
|
|
|
|
|
|
|
|
self.assertIsInstance(response, ChatResponse) |
|
|
self.assertIn("Qualitative research focuses on", response.response) |
|
|
self.assertIn("Quantitative research is expressed in", response.response) |
|
|
self.assertEqual(len(response.citations), 2) |
|
|
|
|
|
@patch('app.qa_chain') |
|
|
def test_follow_up_question(self, mock_qa_chain): |
|
|
|
|
|
mock_qa_chain.return_value = { |
|
|
"query": "Conversation history:\nUser: Tell me about mixed methods research\nAssistant: Mixed methods research combines the elements of two types of research: quantitative and qualitative. By blending both quantitative and qualitative data, mixed methods research allows for a more thorough exploration of a research question.\n\nCurrent question: What are the different types of mixed methods designs?", |
|
|
"result": "There are four main types of mixed methods designs: 1) Convergent parallel design, 2) Embedded design, 3) Explanatory sequential design, and 4) Exploratory sequential design.", |
|
|
"source_documents": [ |
|
|
MagicMock( |
|
|
page_content="Designing a mixed methods study can be broken down into four types: convergent parallel, embedded, explanatory sequential, and exploratory sequential.", |
|
|
metadata={"source": "Dovetail. (2023, February 20). Mixed methods research guide with examples.", "page": "3"} |
|
|
) |
|
|
] |
|
|
} |
|
|
|
|
|
|
|
|
request = ChatRequest( |
|
|
message="What are the different types of mixed methods designs?", |
|
|
conversation_history=[ |
|
|
{ |
|
|
"message": "Tell me about mixed methods research", |
|
|
"response": "Mixed methods research combines the elements of two types of research: quantitative and qualitative. By blending both quantitative and qualitative data, mixed methods research allows for a more thorough exploration of a research question." |
|
|
} |
|
|
] |
|
|
) |
|
|
|
|
|
|
|
|
response = app.chat(request) |
|
|
|
|
|
|
|
|
self.assertIsInstance(response, ChatResponse) |
|
|
self.assertIn("four main types", response.response.lower()) |
|
|
self.assertEqual(len(response.citations), 1) |
|
|
|
|
|
@patch('app.qa_chain') |
|
|
def test_empty_input(self, mock_qa_chain): |
|
|
|
|
|
request = ChatRequest( |
|
|
message="", |
|
|
conversation_history=[] |
|
|
) |
|
|
|
|
|
|
|
|
with self.assertRaises(Exception): |
|
|
app.chat(request) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
unittest.main() |
|
|
|