-
-
Notifications
You must be signed in to change notification settings - Fork 300
Embed topics and content logic #4588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
akolson
merged 5 commits into
learningequality:search-recommendations
from
akolson:embed-topics-and-content-logic
Jun 14, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,15 +1,18 @@ | ||
|
|
||
| class ConnectionError(Exception): | ||
| pass | ||
| pass | ||
|
|
||
|
|
||
| class TimeoutError(Exception): | ||
| pass | ||
| pass | ||
|
|
||
|
|
||
| class HttpError(Exception): | ||
| pass | ||
| pass | ||
|
|
||
|
|
||
| class InvalidRequest(Exception): | ||
| pass | ||
| pass | ||
|
|
||
|
|
||
| class InvalidResponse(Exception): | ||
| pass | ||
| pass |
83 changes: 83 additions & 0 deletions
83
contentcuration/contentcuration/tests/utils/test_recommendations.py
This file contains hidden or 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 |
|---|---|---|
| @@ -1,10 +1,93 @@ | ||
| from automation.utils.appnexus import errors | ||
| from django.test import TestCase | ||
| from mock import MagicMock | ||
| from mock import patch | ||
|
|
||
| from contentcuration.models import ContentNode | ||
| from contentcuration.utils.recommendations import EmbeddingsResponse | ||
| from contentcuration.utils.recommendations import Recommendations | ||
| from contentcuration.utils.recommendations import RecommendationsAdapter | ||
|
|
||
|
|
||
| class RecommendationsTestCase(TestCase): | ||
| def test_backend_initialization(self): | ||
| recomendations = Recommendations() | ||
| self.assertIsNotNone(recomendations) | ||
| self.assertIsInstance(recomendations, Recommendations) | ||
|
|
||
|
|
||
| class RecommendationsAdapterTestCase(TestCase): | ||
| def setUp(self): | ||
| self.adapter = RecommendationsAdapter(MagicMock()) | ||
| self.topic = { | ||
| 'id': 'topic_id', | ||
| 'title': 'topic_title', | ||
| 'description': 'topic_description', | ||
| 'language': 'en', | ||
| 'ancestors': [ | ||
| { | ||
| 'id': 'ancestor_id', | ||
| 'title': 'ancestor_title', | ||
| 'description': 'ancestor_description', | ||
| } | ||
| ] | ||
| } | ||
| self.resources = [ | ||
| MagicMock(spec=ContentNode), | ||
| ] | ||
|
|
||
| def test_adapter_initialization(self): | ||
| self.assertIsNotNone(self.adapter) | ||
| self.assertIsInstance(self.adapter, RecommendationsAdapter) | ||
|
|
||
| @patch('contentcuration.utils.recommendations.EmbedTopicsRequest') | ||
| def test_embed_topics_backend_connect_success(self, embed_topics_request_mock): | ||
| self.adapter.backend.connect.return_value = True | ||
| self.adapter.backend.make_request.return_value = MagicMock(spec=EmbeddingsResponse) | ||
| response = self.adapter.embed_topics(self.topic) | ||
| self.adapter.backend.connect.assert_called_once() | ||
| self.adapter.backend.make_request.assert_called_once() | ||
| self.assertIsInstance(response, EmbeddingsResponse) | ||
|
|
||
| def test_embed_topics_backend_connect_failure(self): | ||
| self.adapter.backend.connect.return_value = False | ||
| with self.assertRaises(errors.ConnectionError): | ||
| self.adapter.embed_topics(self.topic) | ||
| self.adapter.backend.connect.assert_called_once() | ||
| self.adapter.backend.make_request.assert_not_called() | ||
|
|
||
| @patch('contentcuration.utils.recommendations.EmbedTopicsRequest') | ||
| def test_embed_topics_make_request_exception(self, embed_topics_request_mock): | ||
| self.adapter.backend.connect.return_value = True | ||
| self.adapter.backend.make_request.side_effect = Exception("Mocked exception") | ||
| response = self.adapter.embed_topics(self.topic) | ||
| self.adapter.backend.connect.assert_called_once() | ||
| self.adapter.backend.make_request.assert_called_once() | ||
| self.assertIsInstance(response, EmbeddingsResponse) | ||
| self.assertEqual(str(response.error), "Mocked exception") | ||
|
|
||
| @patch('contentcuration.utils.recommendations.EmbedContentRequest') | ||
| def test_embed_content_backend_connect_success(self, embed_content_request_mock): | ||
| self.adapter.backend.connect.return_value = True | ||
| self.adapter.backend.make_request.return_value = MagicMock(spec=EmbeddingsResponse) | ||
| response = self.adapter.embed_content(self.resources) | ||
| self.adapter.backend.connect.assert_called_once() | ||
| self.adapter.backend.make_request.assert_called_once() | ||
| self.assertIsInstance(response, EmbeddingsResponse) | ||
|
|
||
| def test_embed_content_backend_connect_failure(self): | ||
| self.adapter.backend.connect.return_value = False | ||
| with self.assertRaises(errors.ConnectionError): | ||
| self.adapter.embed_content(self.resources) | ||
| self.adapter.backend.connect.assert_called_once() | ||
| self.adapter.backend.make_request.assert_not_called() | ||
|
|
||
| @patch('contentcuration.utils.recommendations.EmbedContentRequest') | ||
| def test_embed_content_make_request_exception(self, embed_content_request_mock): | ||
| self.adapter.backend.connect.return_value = True | ||
| self.adapter.backend.make_request.side_effect = Exception("Mocked exception") | ||
| response = self.adapter.embed_content(self.resources) | ||
| self.adapter.backend.connect.assert_called_once() | ||
| self.adapter.backend.make_request.assert_called_once() | ||
| self.assertIsInstance(response, EmbeddingsResponse) | ||
| self.assertEqual(str(response.error), "Mocked exception") |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.