Skip to content

Commit

Permalink
fix: update index with empty collection list
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Sep 11, 2024
1 parent bd44874 commit c33c3fa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
6 changes: 2 additions & 4 deletions openedx/core/djangoapps/content/search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def _collections_for_content_object(object_id: UsageKey | LearningContextKey) ->
"collections": ["COL_A", "COL_B"],
}
Returns an empty dict if the object is not in any collections.
Returns an empty list if the object is not in any collections.
"""
# Gather the collections associated with this object
result = {}
Expand All @@ -247,12 +247,10 @@ def _collections_for_content_object(object_id: UsageKey | LearningContextKey) ->
component.learning_package_id,
component.key,
).values_list("key", flat=True)
result[Fields.collections] = list(collections)
except ObjectDoesNotExist:
log.warning(f"No component found for {object_id}")

if collections:
result[Fields.collections] = list(collections)

return result


Expand Down
51 changes: 46 additions & 5 deletions openedx/core/djangoapps/content/search/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,22 @@ def test_reindex_meilisearch(self, mock_meilisearch):
doc_vertical["tags"] = {}
doc_problem1 = copy.deepcopy(self.doc_problem1)
doc_problem1["tags"] = {}
doc_problem1["collections"] = []
doc_problem2 = copy.deepcopy(self.doc_problem2)
doc_problem2["tags"] = {}
doc_problem2["collections"] = []

api.rebuild_index()
self.assertEqual(
mock_meilisearch.return_value.index.return_value.add_documents.call_count,
3,
)
mock_meilisearch.return_value.index.return_value.add_documents.assert_has_calls(
[
call([doc_sequential, doc_vertical]),
call([doc_problem1, doc_problem2]),
call([self.collection_dict]),
call([doc_sequential, doc_vertical]),
],
any_order=True,
)

@override_settings(MEILISEARCH_ENABLED=True)
Expand Down Expand Up @@ -254,6 +259,7 @@ def test_reindex_meilisearch_library_block_error(self, mock_meilisearch):
doc_vertical["tags"] = {}
doc_problem2 = copy.deepcopy(self.doc_problem2)
doc_problem2["tags"] = {}
doc_problem2["collections"] = []

orig_from_component = library_api.LibraryXBlockMetadata.from_component

Expand All @@ -271,13 +277,17 @@ def mocked_from_component(lib_key, component):
):
api.rebuild_index()

self.assertEqual(
mock_meilisearch.return_value.index.return_value.add_documents.call_count,
3,
)
mock_meilisearch.return_value.index.return_value.add_documents.assert_has_calls(
[
call([doc_sequential, doc_vertical]),
# Problem 1 should not be indexed
call([doc_problem2]),
call([self.collection_dict]),
call([doc_sequential, doc_vertical]),
],
any_order=True,
)

# Check that the sorting-related settings were updated to support sorting on the expected fields
Expand Down Expand Up @@ -450,12 +460,43 @@ def test_index_library_block_collections(self, mock_meilisearch):
"collections": [collection1.key, collection2.key],
}

self.assertEqual(
mock_meilisearch.return_value.index.return_value.update_documents.call_count,
2,
)
mock_meilisearch.return_value.index.return_value.update_documents.assert_has_calls(
[
call([doc_problem_with_collection2]),
call([doc_problem_with_collection1]),
],
any_order=True,
)

# Remove Problem1 from both Collections
mock_meilisearch.return_value.index.return_value.update_documents.reset_mock()
doc_problem_without_collections = {
"id": self.doc_problem1["id"],
"collections": [],
}

for collection in (collection1, collection2):
library_api.update_library_collection_components(
self.library.key,
collection_key=collection.key,
usage_keys=[
self.problem1.usage_key,
],
remove=True,
)

self.assertEqual(
mock_meilisearch.return_value.index.return_value.update_documents.call_count,
2,
)
mock_meilisearch.return_value.index.return_value.update_documents.assert_has_calls(
[
call([doc_problem_with_collection2]),
call([doc_problem_without_collections]),
],
)

@override_settings(MEILISEARCH_ENABLED=True)
Expand Down

0 comments on commit c33c3fa

Please sign in to comment.