diff --git a/contentcuration/contentcuration/tests/utils/test_recommendations.py b/contentcuration/contentcuration/tests/utils/test_recommendations.py index 4f5ceb8ff1..3ef11e43e5 100644 --- a/contentcuration/contentcuration/tests/utils/test_recommendations.py +++ b/contentcuration/contentcuration/tests/utils/test_recommendations.py @@ -3,6 +3,7 @@ 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 @@ -31,6 +32,9 @@ def setUp(self): } ] } + self.resources = [ + MagicMock(spec=ContentNode), + ] def test_adapter_initialization(self): self.assertIsNotNone(self.adapter) @@ -61,3 +65,29 @@ def test_embed_topics_make_request_exception(self, embed_topics_request_mock): 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")