Spaces:
Runtime error
Runtime error
| import unittest | |
| from unittest.mock import patch, MagicMock | |
| import sys | |
| import os | |
| import json | |
| # Add the src directory to the path | |
| sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'src')) | |
| # Import the app | |
| from app import app, ChatRequest, ChatResponse | |
| class TestChatbotAPI(unittest.TestCase): | |
| def test_basic_query_response(self, mock_qa_chain): | |
| # Mock the QA chain response | |
| 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"} | |
| ) | |
| ] | |
| } | |
| # Create a test request | |
| request = ChatRequest( | |
| message="What is qualitative research?", | |
| conversation_history=[] | |
| ) | |
| # Call the chat endpoint | |
| response = app.chat(request) | |
| # Assert the response | |
| 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.") | |
| def test_method_recommendation(self, mock_qa_chain): | |
| # Mock the QA chain response | |
| 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"} | |
| ) | |
| ] | |
| } | |
| # Create a test request | |
| request = ChatRequest( | |
| message="Which research method should I use to study student engagement in online learning?", | |
| conversation_history=[] | |
| ) | |
| # Call the chat endpoint | |
| response = app.chat(request) | |
| # Assert the response | |
| 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.") | |
| def test_comparison_query(self, mock_qa_chain): | |
| # Mock the QA chain response | |
| 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"} | |
| ) | |
| ] | |
| } | |
| # Create a test request | |
| request = ChatRequest( | |
| message="What are the differences between qualitative and quantitative research methods?", | |
| conversation_history=[] | |
| ) | |
| # Call the chat endpoint | |
| response = app.chat(request) | |
| # Assert the response | |
| 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) | |
| def test_follow_up_question(self, mock_qa_chain): | |
| # Mock the QA chain response | |
| 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"} | |
| ) | |
| ] | |
| } | |
| # Create a test request with conversation history | |
| 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." | |
| } | |
| ] | |
| ) | |
| # Call the chat endpoint | |
| response = app.chat(request) | |
| # Assert the response | |
| self.assertIsInstance(response, ChatResponse) | |
| self.assertIn("four main types", response.response.lower()) | |
| self.assertEqual(len(response.citations), 1) | |
| def test_empty_input(self, mock_qa_chain): | |
| # Create a test request with empty message | |
| request = ChatRequest( | |
| message="", | |
| conversation_history=[] | |
| ) | |
| # Call the chat endpoint and expect an exception | |
| with self.assertRaises(Exception): | |
| app.chat(request) | |
| if __name__ == '__main__': | |
| unittest.main() | |