File size: 8,205 Bytes
5cf374f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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):
    
    @patch('app.qa_chain')
    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.")
    
    @patch('app.qa_chain')
    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.")
    
    @patch('app.qa_chain')
    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)
    
    @patch('app.qa_chain')
    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)
    
    @patch('app.qa_chain')
    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()