|
13 | 13 | )
|
14 | 14 | from learning_resources.models import LearningResource
|
15 | 15 | from learning_resources.serializers import LearningResourceMetadataDisplaySerializer
|
| 16 | +from learning_resources_search.constants import ( |
| 17 | + CONTENT_FILE_TYPE, |
| 18 | +) |
16 | 19 | from learning_resources_search.serializers import (
|
17 | 20 | serialize_bulk_content_files,
|
18 | 21 | serialize_bulk_learning_resources,
|
@@ -806,3 +809,44 @@ def test_update_content_file_payload_only_includes_existing_keys(
|
806 | 809 | )
|
807 | 810 | else:
|
808 | 811 | mock_retrieve.assert_not_called()
|
| 812 | + |
| 813 | + |
| 814 | +@pytest.mark.django_db |
| 815 | +def test_embed_learning_resources_contentfile_summarization_filter(mocker): |
| 816 | + """ |
| 817 | + Test that the summarizer runs for a content file if another content file |
| 818 | + in the parent learning run also has a summary. |
| 819 | + """ |
| 820 | + settings.OPENAI_API_KEY = "test" |
| 821 | + settings.QDRANT_ENABLE_INDEXING_PLUGIN_HOOKS = True |
| 822 | + mock_content_summarizer = mocker.patch( |
| 823 | + "learning_resources.content_summarizer.ContentSummarizer.summarize_content_files_by_ids" |
| 824 | + ) |
| 825 | + mock_chat_llm = mocker.patch( |
| 826 | + "learning_resources.content_summarizer.ChatLiteLLM", autospec=True |
| 827 | + ) |
| 828 | + mock_instance = mock_chat_llm.return_value |
| 829 | + mock_summary_response = mocker.MagicMock() |
| 830 | + mock_summary_response.content = "mocked summary" |
| 831 | + mock_instance.invoke.return_value = mock_summary_response |
| 832 | + mock_instance.with_structured_output.return_value.invoke.return_value = { |
| 833 | + "flashcards": [ |
| 834 | + { |
| 835 | + "question": "Generated Question", |
| 836 | + "answer": "Generated Answer", |
| 837 | + } |
| 838 | + ] |
| 839 | + } |
| 840 | + |
| 841 | + run = LearningResourceRunFactory.create(published=True) |
| 842 | + ContentFileFactory.create_batch( |
| 843 | + 2, content="test content", summary="summary text", run=run |
| 844 | + ) |
| 845 | + new_content_files = ContentFileFactory.create_batch( |
| 846 | + 2, content="new content", summary="", run=run |
| 847 | + ) |
| 848 | + cf_ids = [cf.id for cf in new_content_files] |
| 849 | + embed_learning_resources(cf_ids, resource_type=CONTENT_FILE_TYPE, overwrite=False) |
| 850 | + |
| 851 | + # Assert that the summarizer was called with the correct content file IDs |
| 852 | + assert sorted(mock_content_summarizer.mock_calls[0].args[0]) == sorted(cf_ids) |
0 commit comments