@@ -14,14 +14,21 @@ class TestGeminiAI(unittest.TestCase):
14
14
@patch ("src.app.gemini_ai.genai.configure" )
15
15
def setUp (self , mock_configure , mock_model ):
16
16
self .api_key = "test_api_key"
17
+ self .mock_model_instance = MagicMock ()
18
+ mock_model .return_value = self .mock_model_instance
17
19
self .gemini_ai = GeminiAI (api_key = self .api_key )
18
20
self .transcript_text = "This is a test transcript."
19
- self .mock_model = mock_model
20
21
21
- @patch ("src.app.gemini_ai.genai.GenerativeModel" )
22
- def test_initialization (self , mock_model ):
23
- self .assertIsNotNone (self .gemini_ai .model )
22
+ def test_initialization_with_key (self ):
23
+ with patch ("src.app.gemini_ai.genai.configure" ) as mock_configure , \
24
+ patch ("src.app.gemini_ai.genai.GenerativeModel" ) as mock_model :
25
+
26
+ gemini_ai = GeminiAI (api_key = "test_key" )
27
+ mock_configure .assert_called_once_with (api_key = "test_key" )
28
+ mock_model .assert_called_once_with ("gemini-1.5-flash" )
29
+ self .assertIsNotNone (gemini_ai .model )
24
30
31
+ def test_initialization_without_key (self ):
25
32
gemini_ai_no_key = GeminiAI (api_key = None )
26
33
self .assertIsNone (gemini_ai_no_key .model )
27
34
@@ -32,29 +39,109 @@ def test_is_configured(self):
32
39
self .assertFalse (gemini_ai_no_key .is_configured ())
33
40
34
41
def test_generate_summary_success (self ):
35
- self .mock_model .return_value .generate_content .return_value = MagicMock (
36
- text = "This is a summary."
37
- )
42
+ mock_response = MagicMock ()
43
+ mock_response .text = "This is a summary."
44
+ self .mock_model_instance .generate_content .return_value = mock_response
45
+
38
46
summary = self .gemini_ai .generate_summary (self .transcript_text )
39
47
self .assertEqual (summary , "This is a summary." )
48
+ self .mock_model_instance .generate_content .assert_called_once ()
40
49
41
50
def test_generate_summary_failure (self ):
42
- self .mock_model .return_value .generate_content .side_effect = Exception (
43
- "API Error"
44
- )
45
-
51
+ self .mock_model_instance .generate_content .side_effect = Exception ("API Error" )
52
+
46
53
summary = self .gemini_ai .generate_summary (self .transcript_text )
47
54
self .assertIn ("Error generating summary" , summary )
55
+ self .assertIn ("API Error" , summary )
56
+
57
+ def test_extract_key_quotes_success (self ):
58
+ mock_response = MagicMock ()
59
+ mock_response .text = "These are key quotes."
60
+ self .mock_model_instance .generate_content .return_value = mock_response
61
+
62
+ quotes = self .gemini_ai .extract_key_quotes (self .transcript_text )
63
+ self .assertEqual (quotes , "These are key quotes." )
64
+
65
+ def test_extract_key_quotes_failure (self ):
66
+ self .mock_model_instance .generate_content .side_effect = Exception ("API Error" )
67
+
68
+ quotes = self .gemini_ai .extract_key_quotes (self .transcript_text )
69
+ self .assertIn ("Error extracting quotes" , quotes )
70
+
71
+ def test_create_study_guide_success (self ):
72
+ mock_response = MagicMock ()
73
+ mock_response .text = "This is a study guide."
74
+ self .mock_model_instance .generate_content .return_value = mock_response
75
+
76
+ study_guide = self .gemini_ai .create_study_guide (self .transcript_text )
77
+ self .assertEqual (study_guide , "This is a study guide." )
78
+
79
+ def test_create_study_guide_failure (self ):
80
+ self .mock_model_instance .generate_content .side_effect = Exception ("API Error" )
81
+
82
+ study_guide = self .gemini_ai .create_study_guide (self .transcript_text )
83
+ self .assertIn ("Error creating study guide" , study_guide )
84
+
85
+ def test_generate_qa_success (self ):
86
+ mock_response = MagicMock ()
87
+ mock_response .text = "Q: Test question?\n A: Test answer."
88
+ self .mock_model_instance .generate_content .return_value = mock_response
89
+
90
+ qa = self .gemini_ai .generate_qa (self .transcript_text )
91
+ self .assertEqual (qa , "Q: Test question?\n A: Test answer." )
92
+
93
+ def test_generate_qa_failure (self ):
94
+ self .mock_model_instance .generate_content .side_effect = Exception ("API Error" )
95
+
96
+ qa = self .gemini_ai .generate_qa (self .transcript_text )
97
+ self .assertIn ("Error generating Q&A" , qa )
98
+
99
+ def test_create_flashcards_success (self ):
100
+ mock_response = MagicMock ()
101
+ mock_response .text = "FRONT: Test term\n BACK: Test definition\n ---"
102
+ self .mock_model_instance .generate_content .return_value = mock_response
103
+
104
+ flashcards = self .gemini_ai .create_flashcards (self .transcript_text )
105
+ self .assertEqual (flashcards , "FRONT: Test term\n BACK: Test definition\n ---" )
106
+
107
+ def test_create_flashcards_failure (self ):
108
+ self .mock_model_instance .generate_content .side_effect = Exception ("API Error" )
109
+
110
+ flashcards = self .gemini_ai .create_flashcards (self .transcript_text )
111
+ self .assertIn ("Error creating flashcards" , flashcards )
112
+
113
+ def test_highlight_insights_success (self ):
114
+ mock_response = MagicMock ()
115
+ mock_response .text = "🔍 Key Insights: Test insights"
116
+ self .mock_model_instance .generate_content .return_value = mock_response
117
+
118
+ insights = self .gemini_ai .highlight_insights (self .transcript_text )
119
+ self .assertEqual (insights , "🔍 Key Insights: Test insights" )
120
+
121
+ def test_highlight_insights_failure (self ):
122
+ self .mock_model_instance .generate_content .side_effect = Exception ("API Error" )
123
+
124
+ insights = self .gemini_ai .highlight_insights (self .transcript_text )
125
+ self .assertIn ("Error extracting insights" , insights )
48
126
49
127
def test_chat_with_transcript_success (self ):
50
- self .mock_model .return_value .generate_content .return_value = MagicMock (
51
- text = "This is an answer."
52
- )
128
+ mock_response = MagicMock ()
129
+ mock_response .text = "This is an answer."
130
+ self .mock_model_instance .generate_content .return_value = mock_response
131
+
53
132
answer = self .gemini_ai .chat_with_transcript (
54
133
self .transcript_text , "What is this?"
55
134
)
56
135
self .assertEqual (answer , "This is an answer." )
57
136
137
+ def test_chat_with_transcript_failure (self ):
138
+ self .mock_model_instance .generate_content .side_effect = Exception ("API Error" )
139
+
140
+ answer = self .gemini_ai .chat_with_transcript (
141
+ self .transcript_text , "What is this?"
142
+ )
143
+ self .assertIn ("Error in chat" , answer )
144
+
58
145
59
146
if __name__ == "__main__" :
60
- unittest .main ()
147
+ unittest .main ()
0 commit comments