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
2
2
import unittest
3
- from unittest .mock import patch , Mock
3
+ from unittest .mock import Mock , patch
4
+
4
5
from requests import HTTPError
6
+
5
7
from ..sinequa_api import Api
6
8
9
+
7
10
class TestApi (unittest .TestCase ):
8
11
def setUp (self ):
9
12
# Set up an instance of the Api class with parameters for testing
10
13
self .api = Api (server_name = "test" , user = "test_user" , password = "test_password" , token = "test_token" )
11
14
12
- @patch (' requests.post' )
15
+ @patch (" requests.post" )
13
16
def test_process_response_success (self , mock_post ):
14
17
# This test checks the process_response method when the HTTP request is successful
15
18
mock_response = Mock ()
@@ -21,7 +24,7 @@ def test_process_response_success(self, mock_post):
21
24
self .assertEqual (response , {"key" : "value" })
22
25
mock_post .assert_called_once ()
23
26
24
- @patch (' requests.post' )
27
+ @patch (" requests.post" )
25
28
def test_process_response_failure (self , mock_post ):
26
29
# Create a mock response object with a 500 status code
27
30
mock_response = Mock ()
@@ -31,15 +34,17 @@ def test_process_response_failure(self, mock_post):
31
34
32
35
def raise_for_status ():
33
36
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
+ )
35
40
36
41
mock_response .raise_for_status = raise_for_status
37
42
38
43
# Attempt to process the response and check if it correctly handles the HTTP error
39
44
with self .assertRaises (HTTPError ):
40
45
self .api .process_response ("http://example.com/api" , payload = {"test" : "data" })
41
46
42
- @patch (' requests.post' )
47
+ @patch (" requests.post" )
43
48
def test_query (self , mock_post ):
44
49
"""
45
50
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):
65
70
},
66
71
}
67
72
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 )
75
74
76
- @patch (' requests.post' )
75
+ @patch (" requests.post" )
77
76
def test_sql_query (self , mock_post ):
78
77
# Mock response for the `sql_query` function with token-based authentication
79
78
mock_response = Mock ()
@@ -85,15 +84,15 @@ def test_sql_query(self, mock_post):
85
84
response = self .api .sql_query (sql )
86
85
self .assertEqual (response , {"Rows" : [["http://example.com" , "sample text" , "sample title" ]]})
87
86
88
- @patch (' requests.post' )
87
+ @patch (" requests.post" )
89
88
def test_get_full_texts (self , mock_post ):
90
89
# Mock response for the `get_full_texts` method
91
90
mock_response = Mock ()
92
91
mock_response .status_code = 200
93
92
mock_response .json .return_value = {
94
93
"Rows" : [
95
94
["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" ],
97
96
]
98
97
}
99
98
mock_post .return_value = mock_response
@@ -103,29 +102,28 @@ def test_get_full_texts(self, mock_post):
103
102
{
104
103
"url" : "http://example.com/article1" ,
105
104
"full_text" : "Here is the full text of the first article..." ,
106
- "title" : "Article One Title"
105
+ "title" : "Article One Title" ,
107
106
},
108
107
{
109
108
"url" : "http://example.com/article2" ,
110
109
"full_text" : "Here is the full text of the second article..." ,
111
- "title" : "Article Two Title"
112
- }
110
+ "title" : "Article Two Title" ,
111
+ },
113
112
]
114
113
self .assertEqual (result , expected )
115
114
116
115
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
118
117
api = Api (server_name = "test" , token = None )
119
118
with self .assertRaises (ValueError ):
120
119
api .sql_query ("SELECT * FROM test_table" )
121
120
122
-
123
121
def test_process_full_text_response (self ):
124
122
# Test `_process_full_text_response` parsing functionality
125
123
raw_response = {
126
124
"Rows" : [
127
125
["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" ],
129
127
]
130
128
}
131
129
processed_response = Api ._process_full_text_response (raw_response )
@@ -135,5 +133,6 @@ def test_process_full_text_response(self):
135
133
]
136
134
self .assertEqual (processed_response , expected )
137
135
138
- if __name__ == '__main__' :
136
+
137
+ if __name__ == "__main__" :
139
138
unittest .main ()
0 commit comments