-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In production, we sometimes observe the following error on document deletion: lms-1 | File "/openedx/venv/lib/python3.11/site-packages/forum/handlers.py", line 115, in handle_deletion lms-1 | search_backend.delete_document(sender.index_name, document_id) lms-1 | File "/openedx/venv/lib/python3.11/site-packages/forum/search/meilisearch.py", line 93, in delete_document lms-1 | doc_pk = m.id2pk(doc_id) lms-1 | ^^^^^^^^^^^^^^^ lms-1 | File "/openedx/venv/lib/python3.11/site-packages/search/meilisearch.py", line 362, in id2pk lms-1 | return hashlib.sha1(value.encode()).hexdigest() lms-1 | ^^^^^^^^^^^^ lms-1 | AttributeError: 'int' object has no attribute 'encode' This is due to the fact that the doc_id passed to the delete_document method is sometimes an integer, sometimes a str. To reflect that, we updated the base function signature. We also improved the unit test coverage. Not all functions are covered yet, but it's getting better. Note that to reproduce this issue we should use MySQL as a storage backend.
- Loading branch information
Showing
5 changed files
with
96 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
Unit tests for the meilisearch search backend. | ||
""" | ||
|
||
from unittest.mock import patch, Mock | ||
|
||
import search.meilisearch as m | ||
from forum.search import meilisearch | ||
|
||
TEST_ID = "abcd" | ||
TEST_PK = m.id2pk(TEST_ID) | ||
|
||
|
||
def test_create_document() -> None: | ||
assert { | ||
"id": TEST_ID, | ||
m.PRIMARY_KEY_FIELD_NAME: TEST_PK, | ||
} == meilisearch.create_document({}, TEST_ID) | ||
|
||
assert { | ||
"id": TEST_ID, | ||
m.PRIMARY_KEY_FIELD_NAME: TEST_PK, | ||
"body": "Somebody", | ||
} == meilisearch.create_document({"body": "Somebody"}, TEST_ID) | ||
|
||
assert { | ||
"id": TEST_ID, | ||
m.PRIMARY_KEY_FIELD_NAME: TEST_PK, | ||
"course_id": "some/course/id", | ||
} == meilisearch.create_document({"course_id": "some/course/id"}, TEST_ID) | ||
|
||
assert { | ||
"id": TEST_ID, | ||
m.PRIMARY_KEY_FIELD_NAME: TEST_PK, | ||
} == meilisearch.create_document( | ||
{"field_should_not_be_here": "some_value"}, TEST_ID | ||
) | ||
|
||
assert { | ||
"id": TEST_ID, | ||
m.PRIMARY_KEY_FIELD_NAME: TEST_PK, | ||
"body": "Somebody", | ||
} == meilisearch.create_document({"body": "<p>Somebody</p>"}, TEST_ID) | ||
|
||
|
||
def test_index_document() -> None: | ||
backend = meilisearch.MeilisearchDocumentBackend() | ||
with patch.object( | ||
backend, "get_index", return_value=Mock(add_documents=Mock()) | ||
) as mock_get_index: | ||
backend.index_document( | ||
"my_index", | ||
TEST_ID, | ||
{ | ||
"body": "<p>Some body</p>", | ||
"some other field": "some value", | ||
}, | ||
) | ||
mock_get_index.assert_called_once_with("my_index") | ||
mock_get_index().add_documents.assert_called_once_with( | ||
[ | ||
{ | ||
"id": TEST_ID, | ||
"_pk": TEST_PK, | ||
"body": "Some body", | ||
} | ||
] | ||
) | ||
|
||
|
||
def test_delete_document() -> None: | ||
backend = meilisearch.MeilisearchDocumentBackend() | ||
with patch.object( | ||
backend, "get_index", return_value=Mock(add_documents=Mock()) | ||
) as mock_get_index: | ||
backend.delete_document("my_index", TEST_ID) | ||
mock_get_index.assert_called_once_with("my_index") | ||
mock_get_index().delete_document.assert_called_once_with(TEST_PK) |