Skip to content

Commit 0dc65dd

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent d066cce commit 0dc65dd

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

sde_collections/tests/api_tests.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
#docker-compose -f local.yml run --rm django pytest sde_collections/tests/api_tests.py
1+
# docker-compose -f local.yml run --rm django pytest sde_collections/tests/api_tests.py
22
import unittest
3-
from unittest.mock import patch, Mock
3+
from unittest.mock import Mock, patch
4+
45
from requests import HTTPError
6+
57
from ..sinequa_api import Api
68

9+
710
class TestApi(unittest.TestCase):
811
def setUp(self):
912
# Set up an instance of the Api class with parameters for testing
1013
self.api = Api(server_name="test", user="test_user", password="test_password", token="test_token")
1114

12-
@patch('requests.post')
15+
@patch("requests.post")
1316
def test_process_response_success(self, mock_post):
1417
# This test checks the process_response method when the HTTP request is successful
1518
mock_response = Mock()
@@ -21,7 +24,7 @@ def test_process_response_success(self, mock_post):
2124
self.assertEqual(response, {"key": "value"})
2225
mock_post.assert_called_once()
2326

24-
@patch('requests.post')
27+
@patch("requests.post")
2528
def test_process_response_failure(self, mock_post):
2629
# Create a mock response object with a 500 status code
2730
mock_response = Mock()
@@ -31,15 +34,17 @@ def test_process_response_failure(self, mock_post):
3134

3235
def raise_for_status():
3336
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")
37+
raise HTTPError(
38+
f"{mock_response.status_code} Server Error: Internal Server Error for url: http://example.com/api"
39+
)
3540

3641
mock_response.raise_for_status = raise_for_status
3742

3843
# Attempt to process the response and check if it correctly handles the HTTP error
3944
with self.assertRaises(HTTPError):
4045
self.api.process_response("http://example.com/api", payload={"test": "data"})
4146

42-
@patch('requests.post')
47+
@patch("requests.post")
4348
def test_query(self, mock_post):
4449
"""
4550
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):
6570
},
6671
}
6772

68-
mock_post.assert_called_once_with(
69-
expected_url,
70-
headers=None,
71-
json=expected_payload,
72-
data=None,
73-
verify=False
74-
)
73+
mock_post.assert_called_once_with(expected_url, headers=None, json=expected_payload, data=None, verify=False)
7574

76-
@patch('requests.post')
75+
@patch("requests.post")
7776
def test_sql_query(self, mock_post):
7877
# Mock response for the `sql_query` function with token-based authentication
7978
mock_response = Mock()
@@ -85,15 +84,15 @@ def test_sql_query(self, mock_post):
8584
response = self.api.sql_query(sql)
8685
self.assertEqual(response, {"Rows": [["http://example.com", "sample text", "sample title"]]})
8786

88-
@patch('requests.post')
87+
@patch("requests.post")
8988
def test_get_full_texts(self, mock_post):
9089
# Mock response for the `get_full_texts` method
9190
mock_response = Mock()
9291
mock_response.status_code = 200
9392
mock_response.json.return_value = {
9493
"Rows": [
9594
["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"]
95+
["http://example.com/article2", "Here is the full text of the second article...", "Article Two Title"],
9796
]
9897
}
9998
mock_post.return_value = mock_response
@@ -103,29 +102,28 @@ def test_get_full_texts(self, mock_post):
103102
{
104103
"url": "http://example.com/article1",
105104
"full_text": "Here is the full text of the first article...",
106-
"title": "Article One Title"
105+
"title": "Article One Title",
107106
},
108107
{
109108
"url": "http://example.com/article2",
110109
"full_text": "Here is the full text of the second article...",
111-
"title": "Article Two Title"
112-
}
110+
"title": "Article Two Title",
111+
},
113112
]
114113
self.assertEqual(result, expected)
115114

116115
def test_missing_token_for_sql_query(self):
117-
# To test when token is missing for sql_query
116+
# To test when token is missing for sql_query
118117
api = Api(server_name="test", token=None)
119118
with self.assertRaises(ValueError):
120119
api.sql_query("SELECT * FROM test_table")
121120

122-
123121
def test_process_full_text_response(self):
124122
# Test `_process_full_text_response` parsing functionality
125123
raw_response = {
126124
"Rows": [
127125
["http://example.com/article1", "Full text for article 1", "Title 1"],
128-
["http://example.com/article2", "Full text for article 2", "Title 2"]
126+
["http://example.com/article2", "Full text for article 2", "Title 2"],
129127
]
130128
}
131129
processed_response = Api._process_full_text_response(raw_response)
@@ -135,5 +133,6 @@ def test_process_full_text_response(self):
135133
]
136134
self.assertEqual(processed_response, expected)
137135

138-
if __name__ == '__main__':
136+
137+
if __name__ == "__main__":
139138
unittest.main()

0 commit comments

Comments
 (0)