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 6fd1cc3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
5 changes: 2 additions & 3 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,11 +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
53 changes: 48 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,14 +460,47 @@ 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)
def test_delete_index_library_block(self, mock_meilisearch):
"""
Expand Down

0 comments on commit 6fd1cc3

Please sign in to comment.