From d066ccef97158bdd641cea70c899a5b3229ec71c Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 13 Nov 2024 20:00:10 -0600 Subject: [PATCH 1/4] Fixes issue #1088 --- sde_collections/tests/api_tests.py | 139 +++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 sde_collections/tests/api_tests.py diff --git a/sde_collections/tests/api_tests.py b/sde_collections/tests/api_tests.py new file mode 100644 index 00000000..430b076b --- /dev/null +++ b/sde_collections/tests/api_tests.py @@ -0,0 +1,139 @@ +#docker-compose -f local.yml run --rm django pytest sde_collections/tests/api_tests.py +import unittest +from unittest.mock import patch, Mock +from requests import HTTPError +from ..sinequa_api import Api + +class TestApi(unittest.TestCase): + def setUp(self): + # Set up an instance of the Api class with parameters for testing + self.api = Api(server_name="test", user="test_user", password="test_password", token="test_token") + + @patch('requests.post') + def test_process_response_success(self, mock_post): + # This test checks the process_response method when the HTTP request is successful + mock_response = Mock() + mock_response.status_code = 200 + mock_response.json.return_value = {"key": "value"} + mock_post.return_value = mock_response + + response = self.api.process_response("http://example.com/api", payload={"test": "data"}) + self.assertEqual(response, {"key": "value"}) + mock_post.assert_called_once() + + @patch('requests.post') + def test_process_response_failure(self, mock_post): + # Create a mock response object with a 500 status code + mock_response = Mock() + mock_response.status_code = 500 + mock_response.json.return_value = {"error": "Internal Server Error"} + mock_post.return_value = mock_response + + def raise_for_status(): + if mock_response.status_code != 200: + raise HTTPError(f"{mock_response.status_code} Server Error: Internal Server Error for url: http://example.com/api") + + mock_response.raise_for_status = raise_for_status + + # Attempt to process the response and check if it correctly handles the HTTP error + with self.assertRaises(HTTPError): + self.api.process_response("http://example.com/api", payload={"test": "data"}) + + @patch('requests.post') + def test_query(self, mock_post): + """ + The test ensures that the query method constructs the correct URL and payload based on input parameters, + and processes a successful API response to return the expected data + """ + mock_response = Mock() + mock_response.status_code = 200 + mock_response.json.return_value = {"key": "value"} + mock_post.return_value = mock_response + + response = self.api.query(page=1, collection_config_folder="sample_folder") + self.assertEqual(response, {"key": "value"}) + + expected_url = "https://sciencediscoveryengine.test.nasa.gov/api/v1/search.query" + expected_payload = { + "app": "nasa-sba-smd", + "query": { + "name": "query-smd-primary", + "text": "", + "page": 1, + "pageSize": 1000, + "advanced": {"collection": "/SDE/sample_folder/"}, + }, + } + + mock_post.assert_called_once_with( + expected_url, + headers=None, + json=expected_payload, + data=None, + verify=False + ) + + @patch('requests.post') + def test_sql_query(self, mock_post): + # Mock response for the `sql_query` function with token-based authentication + mock_response = Mock() + mock_response.status_code = 200 + mock_response.json.return_value = {"Rows": [["http://example.com", "sample text", "sample title"]]} + mock_post.return_value = mock_response + + sql = "SELECT url1, text, title FROM sde_index WHERE collection = '/SDE/sample_folder/'" + response = self.api.sql_query(sql) + self.assertEqual(response, {"Rows": [["http://example.com", "sample text", "sample title"]]}) + + @patch('requests.post') + def test_get_full_texts(self, mock_post): + # Mock response for the `get_full_texts` method + mock_response = Mock() + mock_response.status_code = 200 + mock_response.json.return_value = { + "Rows": [ + ["http://example.com/article1", "Here is the full text of the first article...", "Article One Title"], + ["http://example.com/article2", "Here is the full text of the second article...", "Article Two Title"] + ] + } + mock_post.return_value = mock_response + + result = self.api.get_full_texts(collection_config_folder="sample_folder") + expected = [ + { + "url": "http://example.com/article1", + "full_text": "Here is the full text of the first article...", + "title": "Article One Title" + }, + { + "url": "http://example.com/article2", + "full_text": "Here is the full text of the second article...", + "title": "Article Two Title" + } + ] + self.assertEqual(result, expected) + + def test_missing_token_for_sql_query(self): + # To test when token is missing for sql_query + api = Api(server_name="test", token=None) + with self.assertRaises(ValueError): + api.sql_query("SELECT * FROM test_table") + + + def test_process_full_text_response(self): + # Test `_process_full_text_response` parsing functionality + raw_response = { + "Rows": [ + ["http://example.com/article1", "Full text for article 1", "Title 1"], + ["http://example.com/article2", "Full text for article 2", "Title 2"] + ] + } + processed_response = Api._process_full_text_response(raw_response) + expected = [ + {"url": "http://example.com/article1", "full_text": "Full text for article 1", "title": "Title 1"}, + {"url": "http://example.com/article2", "full_text": "Full text for article 2", "title": "Title 2"}, + ] + self.assertEqual(processed_response, expected) + +if __name__ == '__main__': + unittest.main() From 0dc65dd426f009a643e916e6978c528a1b56465e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Nov 2024 02:14:04 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sde_collections/tests/api_tests.py | 45 +++++++++++++++--------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/sde_collections/tests/api_tests.py b/sde_collections/tests/api_tests.py index 430b076b..9944ad85 100644 --- a/sde_collections/tests/api_tests.py +++ b/sde_collections/tests/api_tests.py @@ -1,15 +1,18 @@ -#docker-compose -f local.yml run --rm django pytest sde_collections/tests/api_tests.py +# docker-compose -f local.yml run --rm django pytest sde_collections/tests/api_tests.py import unittest -from unittest.mock import patch, Mock +from unittest.mock import Mock, patch + from requests import HTTPError + from ..sinequa_api import Api + class TestApi(unittest.TestCase): def setUp(self): # Set up an instance of the Api class with parameters for testing self.api = Api(server_name="test", user="test_user", password="test_password", token="test_token") - @patch('requests.post') + @patch("requests.post") def test_process_response_success(self, mock_post): # This test checks the process_response method when the HTTP request is successful mock_response = Mock() @@ -21,7 +24,7 @@ def test_process_response_success(self, mock_post): self.assertEqual(response, {"key": "value"}) mock_post.assert_called_once() - @patch('requests.post') + @patch("requests.post") def test_process_response_failure(self, mock_post): # Create a mock response object with a 500 status code mock_response = Mock() @@ -31,7 +34,9 @@ def test_process_response_failure(self, mock_post): def raise_for_status(): if mock_response.status_code != 200: - raise HTTPError(f"{mock_response.status_code} Server Error: Internal Server Error for url: http://example.com/api") + raise HTTPError( + f"{mock_response.status_code} Server Error: Internal Server Error for url: http://example.com/api" + ) mock_response.raise_for_status = raise_for_status @@ -39,7 +44,7 @@ def raise_for_status(): with self.assertRaises(HTTPError): self.api.process_response("http://example.com/api", payload={"test": "data"}) - @patch('requests.post') + @patch("requests.post") def test_query(self, mock_post): """ The test ensures that the query method constructs the correct URL and payload based on input parameters, @@ -65,15 +70,9 @@ def test_query(self, mock_post): }, } - mock_post.assert_called_once_with( - expected_url, - headers=None, - json=expected_payload, - data=None, - verify=False - ) + mock_post.assert_called_once_with(expected_url, headers=None, json=expected_payload, data=None, verify=False) - @patch('requests.post') + @patch("requests.post") def test_sql_query(self, mock_post): # Mock response for the `sql_query` function with token-based authentication mock_response = Mock() @@ -85,7 +84,7 @@ def test_sql_query(self, mock_post): response = self.api.sql_query(sql) self.assertEqual(response, {"Rows": [["http://example.com", "sample text", "sample title"]]}) - @patch('requests.post') + @patch("requests.post") def test_get_full_texts(self, mock_post): # Mock response for the `get_full_texts` method mock_response = Mock() @@ -93,7 +92,7 @@ def test_get_full_texts(self, mock_post): mock_response.json.return_value = { "Rows": [ ["http://example.com/article1", "Here is the full text of the first article...", "Article One Title"], - ["http://example.com/article2", "Here is the full text of the second article...", "Article Two Title"] + ["http://example.com/article2", "Here is the full text of the second article...", "Article Two Title"], ] } mock_post.return_value = mock_response @@ -103,29 +102,28 @@ def test_get_full_texts(self, mock_post): { "url": "http://example.com/article1", "full_text": "Here is the full text of the first article...", - "title": "Article One Title" + "title": "Article One Title", }, { "url": "http://example.com/article2", "full_text": "Here is the full text of the second article...", - "title": "Article Two Title" - } + "title": "Article Two Title", + }, ] self.assertEqual(result, expected) def test_missing_token_for_sql_query(self): - # To test when token is missing for sql_query + # To test when token is missing for sql_query api = Api(server_name="test", token=None) with self.assertRaises(ValueError): api.sql_query("SELECT * FROM test_table") - def test_process_full_text_response(self): # Test `_process_full_text_response` parsing functionality raw_response = { "Rows": [ ["http://example.com/article1", "Full text for article 1", "Title 1"], - ["http://example.com/article2", "Full text for article 2", "Title 2"] + ["http://example.com/article2", "Full text for article 2", "Title 2"], ] } processed_response = Api._process_full_text_response(raw_response) @@ -135,5 +133,6 @@ def test_process_full_text_response(self): ] self.assertEqual(processed_response, expected) -if __name__ == '__main__': + +if __name__ == "__main__": unittest.main() From 7e1bfe5b8279fb298840750fc9c68e8c34c04785 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 14 Nov 2024 00:55:30 -0600 Subject: [PATCH 3/4] Issue #1088 --- sde_collections/tests/api_tests.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sde_collections/tests/api_tests.py b/sde_collections/tests/api_tests.py index 9944ad85..768d9fb8 100644 --- a/sde_collections/tests/api_tests.py +++ b/sde_collections/tests/api_tests.py @@ -1,12 +1,9 @@ # docker-compose -f local.yml run --rm django pytest sde_collections/tests/api_tests.py import unittest from unittest.mock import Mock, patch - from requests import HTTPError - from ..sinequa_api import Api - class TestApi(unittest.TestCase): def setUp(self): # Set up an instance of the Api class with parameters for testing @@ -69,8 +66,7 @@ def test_query(self, mock_post): "advanced": {"collection": "/SDE/sample_folder/"}, }, } - - mock_post.assert_called_once_with(expected_url, headers=None, json=expected_payload, data=None, verify=False) + mock_post.assert_called_once_with(expected_url, headers={}, json=expected_payload, verify=False) @patch("requests.post") def test_sql_query(self, mock_post): @@ -133,6 +129,5 @@ def test_process_full_text_response(self): ] self.assertEqual(processed_response, expected) - if __name__ == "__main__": unittest.main() From 0085df79119c7f24c65121ca16e92d8e8fa66117 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Nov 2024 06:56:03 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sde_collections/tests/api_tests.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sde_collections/tests/api_tests.py b/sde_collections/tests/api_tests.py index 768d9fb8..73f2a77a 100644 --- a/sde_collections/tests/api_tests.py +++ b/sde_collections/tests/api_tests.py @@ -1,9 +1,12 @@ # docker-compose -f local.yml run --rm django pytest sde_collections/tests/api_tests.py import unittest from unittest.mock import Mock, patch + from requests import HTTPError + from ..sinequa_api import Api + class TestApi(unittest.TestCase): def setUp(self): # Set up an instance of the Api class with parameters for testing @@ -129,5 +132,6 @@ def test_process_full_text_response(self): ] self.assertEqual(processed_response, expected) + if __name__ == "__main__": unittest.main()