11import pytest
2- import responses # https://github.com/getsentry/responses
2+ import responses # https://github.com/getsentry/responses
33from unittest .mock import Mock
4- from datacrunch .http_client import http_client
54from datacrunch .exceptions import APIException
65
76INVALID_REQUEST = 'invalid_request'
109UNAUTHORIZED_REQUEST = 'unauthorized_request'
1110UNAUTHORIZED_REQUEST_MESSAGE = 'Access token is missing or invalid'
1211
12+
1313class TestHttpClient :
1414 def test_add_base_url (self , http_client ):
1515 # arrange
1616 path = "/test"
1717 base = http_client ._base_url
18-
18+
1919 # act
2020 url = http_client ._add_base_url (path )
2121
@@ -31,14 +31,14 @@ def test_generate_bearer_header(self, http_client):
3131 assert bearer_string == f'Bearer { access_token } '
3232
3333 def test_generate_user_agent (self , http_client ):
34- # arrange
34+ # arrange
3535 version = http_client ._version
3636 client_id_truncated = http_client ._auth_service ._client_id [0 :10 ]
3737
3838 # act
3939 user_agent_string = http_client ._generate_user_agent ()
4040
41- # assert
41+ # assert
4242 assert type (user_agent_string ) == str
4343 assert user_agent_string == f'datacrunch-python-v{ version } -{ client_id_truncated } '
4444
@@ -47,7 +47,7 @@ def test_generate_headers(self, http_client):
4747 headers = http_client ._generate_headers ()
4848 authorization_string = http_client ._generate_bearer_header ()
4949 user_agent_string = http_client ._generate_user_agent ()
50-
50+
5151 # assert
5252 assert type (headers ) == dict
5353 assert type (headers ['Content-Type' ]) == str
@@ -91,7 +91,7 @@ def test_get_successful(self, http_client):
9191 response = http_client .get ('/test' )
9292
9393 # assert
94- assert response .ok == True
94+ assert response .ok is True
9595 assert response .status_code == 200
9696 assert response .text == '{}'
9797 assert response .headers ['Content-Type' ] == 'application/json'
@@ -111,7 +111,7 @@ def test_post_successful(self, http_client):
111111 response = http_client .post ('/test' , params = {})
112112
113113 # assert
114- assert response .ok == True
114+ assert response .ok is True
115115 assert response .status_code == 200
116116 assert response .text == '{}'
117117 assert response .headers ['Content-Type' ] == 'application/json'
@@ -131,7 +131,7 @@ def test_delete_successful(self, http_client):
131131 response = http_client .delete ('/test' )
132132
133133 # assert
134- assert response .ok == True
134+ assert response .ok is True
135135 assert response .status_code == 200
136136 assert response .headers ['Content-Type' ] == 'application/json'
137137 assert responses .assert_call_count (http_client ._base_url + '/test' , 1 ) is True
@@ -141,10 +141,11 @@ def test_get_failed(self, http_client):
141141 responses .add (
142142 method = responses .GET ,
143143 url = (http_client ._base_url + '/test' ),
144- status = 401 ,
144+ status = 401 ,
145145 json = {'code' : UNAUTHORIZED_REQUEST , 'message' : UNAUTHORIZED_REQUEST_MESSAGE },
146146 content_type = 'application/json'
147147 )
148+ error_str = f'error code: { UNAUTHORIZED_REQUEST } \n message: { UNAUTHORIZED_REQUEST_MESSAGE } '
148149
149150 # act
150151 with pytest .raises (APIException ) as excinfo :
@@ -153,13 +154,14 @@ def test_get_failed(self, http_client):
153154 # assert
154155 assert excinfo .value .code == UNAUTHORIZED_REQUEST
155156 assert excinfo .value .message == UNAUTHORIZED_REQUEST_MESSAGE
157+ assert excinfo .value .__str__ () == error_str
156158
157159 def test_post_failed (self , http_client ):
158160 # arrange - add response mock
159161 responses .add (
160162 method = responses .POST ,
161163 url = (http_client ._base_url + '/test' ),
162- status = 400 ,
164+ status = 400 ,
163165 json = {'code' : INVALID_REQUEST , 'message' : INVALID_REQUEST_MESSAGE },
164166 content_type = 'application/json'
165167 )
@@ -177,7 +179,7 @@ def test_delete_failed(self, http_client):
177179 responses .add (
178180 method = responses .DELETE ,
179181 url = (http_client ._base_url + '/test' ),
180- status = 400 ,
182+ status = 400 ,
181183 json = {'code' : INVALID_REQUEST , 'message' : INVALID_REQUEST_MESSAGE },
182184 content_type = 'application/json'
183185 )
@@ -188,4 +190,4 @@ def test_delete_failed(self, http_client):
188190
189191 # assert
190192 assert excinfo .value .code == INVALID_REQUEST
191- assert excinfo .value .message == INVALID_REQUEST_MESSAGE
193+ assert excinfo .value .message == INVALID_REQUEST_MESSAGE
0 commit comments