|
| 1 | +#docker-compose -f local.yml run --rm django pytest sde_collections/tests/api_tests.py |
| 2 | +import unittest |
| 3 | +from unittest.mock import patch, Mock |
| 4 | +from requests import HTTPError |
| 5 | +from ..sinequa_api import Api |
| 6 | + |
| 7 | +class TestApi(unittest.TestCase): |
| 8 | + def setUp(self): |
| 9 | + # Set up an instance of the Api class with parameters for testing |
| 10 | + self.api = Api(server_name="test", user="test_user", password="test_password", token="test_token") |
| 11 | + |
| 12 | + @patch('requests.post') |
| 13 | + def test_process_response_success(self, mock_post): |
| 14 | + # This test checks the process_response method when the HTTP request is successful |
| 15 | + mock_response = Mock() |
| 16 | + mock_response.status_code = 200 |
| 17 | + mock_response.json.return_value = {"key": "value"} |
| 18 | + mock_post.return_value = mock_response |
| 19 | + |
| 20 | + response = self.api.process_response("http://example.com/api", payload={"test": "data"}) |
| 21 | + self.assertEqual(response, {"key": "value"}) |
| 22 | + mock_post.assert_called_once() |
| 23 | + |
| 24 | + @patch('requests.post') |
| 25 | + def test_process_response_failure(self, mock_post): |
| 26 | + # Create a mock response object with a 500 status code |
| 27 | + mock_response = Mock() |
| 28 | + mock_response.status_code = 500 |
| 29 | + mock_response.json.return_value = {"error": "Internal Server Error"} |
| 30 | + mock_post.return_value = mock_response |
| 31 | + |
| 32 | + def raise_for_status(): |
| 33 | + if mock_response.status_code != 200: |
| 34 | + raise HTTPError(f"{mock_response.status_code} Server Error: Internal Server Error for url: http://example.com/api") |
| 35 | + |
| 36 | + mock_response.raise_for_status = raise_for_status |
| 37 | + |
| 38 | + # Attempt to process the response and check if it correctly handles the HTTP error |
| 39 | + with self.assertRaises(HTTPError): |
| 40 | + self.api.process_response("http://example.com/api", payload={"test": "data"}) |
| 41 | + |
| 42 | + @patch('requests.post') |
| 43 | + def test_query(self, mock_post): |
| 44 | + """ |
| 45 | + The test ensures that the query method constructs the correct URL and payload based on input parameters, |
| 46 | + and processes a successful API response to return the expected data |
| 47 | + """ |
| 48 | + mock_response = Mock() |
| 49 | + mock_response.status_code = 200 |
| 50 | + mock_response.json.return_value = {"key": "value"} |
| 51 | + mock_post.return_value = mock_response |
| 52 | + |
| 53 | + response = self.api.query(page=1, collection_config_folder="sample_folder") |
| 54 | + self.assertEqual(response, {"key": "value"}) |
| 55 | + |
| 56 | + expected_url = "https://sciencediscoveryengine.test.nasa.gov/api/v1/search.query" |
| 57 | + expected_payload = { |
| 58 | + "app": "nasa-sba-smd", |
| 59 | + "query": { |
| 60 | + "name": "query-smd-primary", |
| 61 | + "text": "", |
| 62 | + "page": 1, |
| 63 | + "pageSize": 1000, |
| 64 | + "advanced": {"collection": "/SDE/sample_folder/"}, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + mock_post.assert_called_once_with( |
| 69 | + expected_url, |
| 70 | + headers=None, |
| 71 | + json=expected_payload, |
| 72 | + data=None, |
| 73 | + verify=False |
| 74 | + ) |
| 75 | + |
| 76 | + @patch('requests.post') |
| 77 | + def test_sql_query(self, mock_post): |
| 78 | + # Mock response for the `sql_query` function with token-based authentication |
| 79 | + mock_response = Mock() |
| 80 | + mock_response.status_code = 200 |
| 81 | + mock_response.json.return_value = {"Rows": [["http://example.com", "sample text", "sample title"]]} |
| 82 | + mock_post.return_value = mock_response |
| 83 | + |
| 84 | + sql = "SELECT url1, text, title FROM sde_index WHERE collection = '/SDE/sample_folder/'" |
| 85 | + response = self.api.sql_query(sql) |
| 86 | + self.assertEqual(response, {"Rows": [["http://example.com", "sample text", "sample title"]]}) |
| 87 | + |
| 88 | + @patch('requests.post') |
| 89 | + def test_get_full_texts(self, mock_post): |
| 90 | + # Mock response for the `get_full_texts` method |
| 91 | + mock_response = Mock() |
| 92 | + mock_response.status_code = 200 |
| 93 | + mock_response.json.return_value = { |
| 94 | + "Rows": [ |
| 95 | + ["http://example.com/article1", "Here is the full text of the first article...", "Article One Title"], |
| 96 | + ["http://example.com/article2", "Here is the full text of the second article...", "Article Two Title"] |
| 97 | + ] |
| 98 | + } |
| 99 | + mock_post.return_value = mock_response |
| 100 | + |
| 101 | + result = self.api.get_full_texts(collection_config_folder="sample_folder") |
| 102 | + expected = [ |
| 103 | + { |
| 104 | + "url": "http://example.com/article1", |
| 105 | + "full_text": "Here is the full text of the first article...", |
| 106 | + "title": "Article One Title" |
| 107 | + }, |
| 108 | + { |
| 109 | + "url": "http://example.com/article2", |
| 110 | + "full_text": "Here is the full text of the second article...", |
| 111 | + "title": "Article Two Title" |
| 112 | + } |
| 113 | + ] |
| 114 | + self.assertEqual(result, expected) |
| 115 | + |
| 116 | + def test_missing_token_for_sql_query(self): |
| 117 | + # To test when token is missing for sql_query |
| 118 | + api = Api(server_name="test", token=None) |
| 119 | + with self.assertRaises(ValueError): |
| 120 | + api.sql_query("SELECT * FROM test_table") |
| 121 | + |
| 122 | + |
| 123 | + def test_process_full_text_response(self): |
| 124 | + # Test `_process_full_text_response` parsing functionality |
| 125 | + raw_response = { |
| 126 | + "Rows": [ |
| 127 | + ["http://example.com/article1", "Full text for article 1", "Title 1"], |
| 128 | + ["http://example.com/article2", "Full text for article 2", "Title 2"] |
| 129 | + ] |
| 130 | + } |
| 131 | + processed_response = Api._process_full_text_response(raw_response) |
| 132 | + expected = [ |
| 133 | + {"url": "http://example.com/article1", "full_text": "Full text for article 1", "title": "Title 1"}, |
| 134 | + {"url": "http://example.com/article2", "full_text": "Full text for article 2", "title": "Title 2"}, |
| 135 | + ] |
| 136 | + self.assertEqual(processed_response, expected) |
| 137 | + |
| 138 | +if __name__ == '__main__': |
| 139 | + unittest.main() |
0 commit comments