6
6
PlatformType ,
7
7
)
8
8
from learning_resources .content_summarizer import ContentSummarizer
9
+ from learning_resources .exceptions import (
10
+ FlashcardsGenerationError ,
11
+ SummaryGenerationError ,
12
+ )
9
13
from learning_resources .factories import (
10
14
ContentFileFactory ,
11
15
ContentSummarizerConfigurationFactory ,
@@ -242,9 +246,9 @@ def test_get_unprocessed_content_files_with_platform_and_config(
242
246
def test_summarize_content_files_by_ids (
243
247
processable_content_files , mock_summarize_single_content_file
244
248
):
245
- """The summarizer should process content files that are processable"""
249
+ """The summarizer should process content files that are processable and return the status results """
246
250
summarizer = ContentSummarizer ()
247
- summarizer .summarize_content_files_by_ids (
251
+ results = summarizer .summarize_content_files_by_ids (
248
252
overwrite = False ,
249
253
content_file_ids = [
250
254
content_file .id for content_file in processable_content_files
@@ -253,6 +257,8 @@ def test_summarize_content_files_by_ids(
253
257
assert mock_summarize_single_content_file .call_count == len (
254
258
processable_content_files
255
259
)
260
+ assert isinstance (results , list )
261
+ assert len (results ) == len (processable_content_files )
256
262
257
263
258
264
def test_summarize_single_content_file (mocker , processable_content_files ):
@@ -332,3 +338,77 @@ def test_process_single_file_calls_llm_summary(
332
338
assert mock_instance .with_structured_output .call_count == 1
333
339
elif has_flashcards :
334
340
assert mock_instance .invoke .call_count == 1
341
+
342
+
343
+ @pytest .mark .parametrize (
344
+ ("process_type" , "expected_exception" ),
345
+ [("summary" , SummaryGenerationError ), ("flashcards" , FlashcardsGenerationError )],
346
+ )
347
+ def test_generate_summary_flashcards_exception (
348
+ mocker , processable_content_files , settings , process_type , expected_exception
349
+ ):
350
+ """Test the exception handling in the generate_summary and generate_flashcards methods"""
351
+ settings .OPENAI_API_KEY = "test"
352
+ summarizer = ContentSummarizer ()
353
+ content_file = processable_content_files [0 ]
354
+ content_file .save ()
355
+
356
+ # Mock the ChatLiteLLM class and its methods
357
+ mock_chat_llm = mocker .patch (
358
+ "learning_resources.content_summarizer.ChatLiteLLM" , autospec = True
359
+ )
360
+ mock_instance = mock_chat_llm .return_value
361
+
362
+ # Mock the response for _generate_summary to raise an exception
363
+ mock_instance .invoke .side_effect = Exception ("Test exception" )
364
+ # Mock the response for _generate_flashcards to raise an exception
365
+ mock_instance .with_structured_output .return_value .invoke .side_effect = Exception (
366
+ "INVALID_FORMAT"
367
+ )
368
+
369
+ if process_type == "summary" :
370
+ with pytest .raises (expected_exception ):
371
+ summarizer ._generate_summary ( # noqa: SLF001
372
+ llm_model = "llm_model" , content = content_file .content
373
+ )
374
+ else :
375
+ with pytest .raises (expected_exception ):
376
+ summarizer ._generate_flashcards ( # noqa: SLF001
377
+ llm_model = "llm_model" , content = content_file .content
378
+ )
379
+
380
+
381
+ def test_summarize_single_content_file_with_exception (
382
+ mocker , processable_content_files , settings
383
+ ):
384
+ """Test the exception handling in the summarize_single_content_file method"""
385
+ settings .OPENAI_API_KEY = "test"
386
+ summarizer = ContentSummarizer ()
387
+ content_file = processable_content_files [0 ]
388
+
389
+ # Mock the ChatLiteLLM class and its methods
390
+ mock_chat_llm = mocker .patch (
391
+ "learning_resources.content_summarizer.ChatLiteLLM" , autospec = True
392
+ )
393
+ mock_instance = mock_chat_llm .return_value
394
+
395
+ # Mock the response for _generate_summary to raise an exception
396
+ mock_instance .invoke .side_effect = Exception ("Test exception" )
397
+ # Mock the response for _generate_flashcards to raise an exception
398
+ mock_instance .with_structured_output .return_value .invoke .side_effect = Exception (
399
+ "INVALID_FORMAT"
400
+ )
401
+
402
+ error = summarizer .summarize_single_content_file (content_file .id , overwrite = False )
403
+ assert (
404
+ error
405
+ == f"Summary generation failed for CONTENT_FILE_ID: { content_file .id } \n Error: Test exception\n \n "
406
+ )
407
+ content_file .summary = "Test summary"
408
+ content_file .save ()
409
+ content_file .refresh_from_db ()
410
+ error = summarizer .summarize_single_content_file (content_file .id , overwrite = False )
411
+ assert (
412
+ error
413
+ == f"Flashcards generation failed for CONTENT_FILE_ID: { content_file .id } \n Error: INVALID_FORMAT\n \n "
414
+ )
0 commit comments