Spaces:
Sleeping
Sleeping
| import unittest | |
| from unittest.mock import patch, MagicMock | |
| from src.search.service import SearchService | |
| from src.utils.constants import Constants | |
| class TestSearchService(unittest.TestCase): | |
| """ | |
| Test cases for the SearchService class. | |
| """ | |
| def test_init(self, mock_image_processor, mock_vector_search): | |
| """ | |
| Test initialization of SearchService. | |
| """ | |
| # Setup mocks | |
| mock_vector_search_instance = MagicMock() | |
| mock_image_processor_instance = MagicMock() | |
| mock_vector_search.return_value = mock_vector_search_instance | |
| mock_image_processor.return_value = mock_image_processor_instance | |
| # Create instance | |
| service = SearchService() | |
| # Assertions | |
| mock_vector_search.assert_called_once() | |
| mock_image_processor.assert_called_once() | |
| self.assertEqual(service.vector_search, mock_vector_search_instance) | |
| self.assertEqual(service.image_processor, mock_image_processor_instance) | |
| def test_customer_search_text_only(self, mock_image_processor, mock_vector_search): | |
| """ | |
| Test customer_search method with text query only. | |
| """ | |
| # Setup mocks | |
| mock_vector_search_instance = MagicMock() | |
| mock_image_processor_instance = MagicMock() | |
| mock_vector_search.return_value = mock_vector_search_instance | |
| mock_image_processor.return_value = mock_image_processor_instance | |
| # Mock search results | |
| mock_results = [ | |
| { | |
| "id": "1", | |
| "title": "Test Title 1", | |
| "text": "Test text 1. This is a longer description that will be shortened.", | |
| "price": "100€", | |
| "thumbnail": "http://example.com/image1.jpg", | |
| "product_page_url": "http://example.com/product1" | |
| } | |
| ] | |
| mock_vector_search_instance.search.return_value = mock_results | |
| # Create instance and call method | |
| service = SearchService() | |
| results = service.customer_search(text_query="test query") | |
| # Assertions | |
| mock_image_processor_instance.describe_image.assert_not_called() | |
| mock_vector_search_instance.search.assert_called_once_with("test query", limit=Constants.CUSTOMER_SEARCH_LIMIT) | |
| self.assertEqual(len(results), 1) | |
| self.assertEqual(results[0]["id"], "1") | |
| self.assertEqual(results[0]["title"], "Test Title 1") | |
| self.assertEqual(results[0]["description"], "Test text 1. This is a longer description that will be shortened.") | |
| self.assertEqual(results[0]["full_text"], "Test text 1. This is a longer description that will be shortened.") | |
| self.assertEqual(results[0]["price"], "100€") | |
| self.assertEqual(results[0]["thumbnail"], "http://example.com/image1.jpg") | |
| self.assertEqual(results[0]["product_page_url"], "http://example.com/product1") | |
| def test_customer_search_image_only(self, mock_image_processor, mock_vector_search): | |
| """ | |
| Test customer_search method with image only. | |
| """ | |
| # Setup mocks | |
| mock_vector_search_instance = MagicMock() | |
| mock_image_processor_instance = MagicMock() | |
| mock_vector_search.return_value = mock_vector_search_instance | |
| mock_image_processor.return_value = mock_image_processor_instance | |
| # Mock image description | |
| mock_image_processor_instance.describe_image.return_value = "Image description" | |
| # Mock search results | |
| mock_results = [ | |
| { | |
| "id": "1", | |
| "title": "Test Title 1", | |
| "text": "Test text 1", | |
| "price": "100€", | |
| "thumbnail": "http://example.com/image1.jpg", | |
| "product_page_url": "http://example.com/product1" | |
| } | |
| ] | |
| mock_vector_search_instance.search.return_value = mock_results | |
| # Create instance and call method | |
| service = SearchService() | |
| results = service.customer_search(image_data=b'test_image_data') | |
| # Assertions | |
| mock_image_processor_instance.describe_image.assert_called_once_with(b'test_image_data') | |
| mock_vector_search_instance.search.assert_called_once_with("Image description", limit=Constants.CUSTOMER_SEARCH_LIMIT) | |
| self.assertEqual(len(results), 1) | |
| def test_customer_search_text_and_image(self, mock_image_processor, mock_vector_search): | |
| """ | |
| Test customer_search method with both text and image. | |
| """ | |
| # Setup mocks | |
| mock_vector_search_instance = MagicMock() | |
| mock_image_processor_instance = MagicMock() | |
| mock_vector_search.return_value = mock_vector_search_instance | |
| mock_image_processor.return_value = mock_image_processor_instance | |
| # Mock image description | |
| mock_image_processor_instance.describe_image.return_value = "Image description" | |
| # Mock search results | |
| mock_results = [ | |
| { | |
| "id": "1", | |
| "title": "Test Title 1", | |
| "text": "Test text 1", | |
| "price": "100€", | |
| "thumbnail": "http://example.com/image1.jpg", | |
| "product_page_url": "http://example.com/product1" | |
| } | |
| ] | |
| mock_vector_search_instance.search.return_value = mock_results | |
| # Create instance and call method | |
| service = SearchService() | |
| results = service.customer_search(text_query="test query", image_data=b'test_image_data') | |
| # Assertions | |
| mock_image_processor_instance.describe_image.assert_called_once_with(b'test_image_data') | |
| mock_vector_search_instance.search.assert_called_once_with("test query Image description", limit=Constants.CUSTOMER_SEARCH_LIMIT) | |
| self.assertEqual(len(results), 1) | |
| def test_customer_search_empty_inputs(self, mock_image_processor, mock_vector_search): | |
| """ | |
| Test customer_search method with empty inputs. | |
| """ | |
| # Setup mocks | |
| mock_vector_search_instance = MagicMock() | |
| mock_image_processor_instance = MagicMock() | |
| mock_vector_search.return_value = mock_vector_search_instance | |
| mock_image_processor.return_value = mock_image_processor_instance | |
| # Create instance and call method | |
| service = SearchService() | |
| results = service.customer_search() | |
| # Assertions | |
| mock_image_processor_instance.describe_image.assert_not_called() | |
| mock_vector_search_instance.search.assert_not_called() | |
| self.assertEqual(results, []) | |
| def test_staff_search(self, mock_image_processor, mock_vector_search): | |
| """ | |
| Test staff_search method. | |
| """ | |
| # Setup mocks | |
| mock_vector_search_instance = MagicMock() | |
| mock_image_processor_instance = MagicMock() | |
| mock_vector_search.return_value = mock_vector_search_instance | |
| mock_image_processor.return_value = mock_image_processor_instance | |
| # Mock image description | |
| mock_image_processor_instance.describe_image.return_value = "Image description" | |
| # Mock search results | |
| mock_results = [ | |
| { | |
| "id": "1", | |
| "title": "Test Title 1", | |
| "text": "Test text 1", | |
| "price": "100€", | |
| "thumbnail": "http://example.com/image1.jpg", | |
| "product_page_url": "http://example.com/product1" | |
| } | |
| ] | |
| mock_vector_search_instance.search.return_value = mock_results | |
| # Create instance and call method | |
| service = SearchService() | |
| result = service.staff_search(b'test_image_data') | |
| # Assertions | |
| mock_image_processor_instance.describe_image.assert_called_once_with(b'test_image_data') | |
| mock_vector_search_instance.search.assert_called_once_with("Image description", limit=Constants.STAFF_SEARCH_LIMIT) | |
| self.assertIsNotNone(result) | |
| self.assertEqual(result["id"], "1") | |
| self.assertEqual(result["title"], "Test Title 1") | |
| def test_staff_search_empty_input(self, mock_image_processor, mock_vector_search): | |
| """ | |
| Test staff_search method with empty input. | |
| """ | |
| # Setup mocks | |
| mock_vector_search_instance = MagicMock() | |
| mock_image_processor_instance = MagicMock() | |
| mock_vector_search.return_value = mock_vector_search_instance | |
| mock_image_processor.return_value = mock_image_processor_instance | |
| # Create instance and call method | |
| service = SearchService() | |
| result = service.staff_search(None) | |
| # Assertions | |
| mock_image_processor_instance.describe_image.assert_not_called() | |
| mock_vector_search_instance.search.assert_not_called() | |
| self.assertIsNone(result) | |
| def test_create_short_description(self, mock_image_processor, mock_vector_search): | |
| """ | |
| Test _create_short_description method. | |
| """ | |
| # Setup mocks | |
| mock_vector_search_instance = MagicMock() | |
| mock_image_processor_instance = MagicMock() | |
| mock_vector_search.return_value = mock_vector_search_instance | |
| mock_image_processor.return_value = mock_image_processor_instance | |
| # Create instance | |
| service = SearchService() | |
| # Test with short text | |
| short_text = "This is a short text." | |
| result = service._create_short_description(short_text) | |
| self.assertEqual(result, short_text) | |
| # Test with long text ending with sentence | |
| long_text = "This is a longer text. It has multiple sentences. We want to cut at the end of a sentence." | |
| result = service._create_short_description(long_text, max_length=50) | |
| self.assertEqual(result, "This is a longer text. It has multiple sentences.") | |
| # Test with long text without sentence ending | |
| long_text_no_sentence = "This is a longer text without proper sentence endings it just keeps going and going" | |
| result = service._create_short_description(long_text_no_sentence, max_length=50) | |
| self.assertEqual(result, "This is a longer text without proper sentence...") | |
| def test_close(self, mock_image_processor, mock_vector_search): | |
| """ | |
| Test close method. | |
| """ | |
| # Setup mocks | |
| mock_vector_search_instance = MagicMock() | |
| mock_image_processor_instance = MagicMock() | |
| mock_vector_search.return_value = mock_vector_search_instance | |
| mock_image_processor.return_value = mock_image_processor_instance | |
| # Create instance and call method | |
| service = SearchService() | |
| service.close() | |
| # Assertions | |
| mock_vector_search_instance.close.assert_called_once() | |
| if __name__ == '__main__': | |
| unittest.main() |