Skip to content

Commit 22dbe6d

Browse files
committed
added codecov badge, added exceptions tests
1 parent 0f190b7 commit 22dbe6d

4 files changed

Lines changed: 59 additions & 14 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@
44
<a href='https://github.com/DataCrunch-io/datacrunch-python/actions?query=workflow%3A%22Unit+Tests%22+branch%3Amaster'>
55
<img src='https://github.com/DataCrunch-io/datacrunch-python/workflows/Unit%20Tests/badge.svg'></img>
66
</a>
7+
78
<a href='https://github.com/DataCrunch-io/datacrunch-python/actions?query=workflow%3A%22Code+Style%22+branch%3Amaster'>
89
<img src='https://github.com/DataCrunch-io/datacrunch-python/workflows/Code%20Style/badge.svg'></img>
910
</a>
11+
12+
<a href="https://codecov.io/gh/DataCrunch-io/datacrunch-python">
13+
<img src="https://codecov.io/gh/DataCrunch-io/datacrunch-python/branch/master/graph/badge.svg?token=5X5KTYSSPK"/>
14+
</a>
15+
1016
<a href='https://datacrunch-python.readthedocs.io/en/latest/'>
1117
<img src='https://readthedocs.org/projects/datacrunch-python/badge/?version=latest'></img>
1218
</a>
19+
1320
<a href='https://github.com/DataCrunch-io/datacrunch-python/blob/master/LICENSE'>
1421
<img src='https://img.shields.io/github/license/DataCrunch-io/datacrunch-python'></img>
1522
</a>
23+
1624
<a href='https://pypi.org/project/datacrunch/'>
1725
<img src='https://img.shields.io/pypi/v/datacrunch?logo=python&logoColor=green'></img>
1826
</a>
27+
1928
<a href='https://datacrunch.io'>
2029
<img src='https://img.shields.io/badge/datacrunch-.io-blue'></img>
2130
</a>

tests/unit_tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ACCESS_TOKEN = "test-token"
88
CLIENT_ID = "0123456789xyz"
99

10+
1011
@pytest.fixture
1112
def http_client():
1213
auth_service = Mock()
@@ -15,4 +16,4 @@ def http_client():
1516
auth_service.refresh = Mock(return_value=None)
1617
auth_service._client_id = CLIENT_ID
1718

18-
return HTTPClient(auth_service, BASE_URL)
19+
return HTTPClient(auth_service, BASE_URL)

tests/unit_tests/http_client/test_http_client.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
2-
import responses # https://github.com/getsentry/responses
2+
import responses # https://github.com/getsentry/responses
33
from unittest.mock import Mock
4-
from datacrunch.http_client import http_client
54
from datacrunch.exceptions import APIException
65

76
INVALID_REQUEST = 'invalid_request'
@@ -10,12 +9,13 @@
109
UNAUTHORIZED_REQUEST = 'unauthorized_request'
1110
UNAUTHORIZED_REQUEST_MESSAGE = 'Access token is missing or invalid'
1211

12+
1313
class 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}\nmessage: {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
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
from datacrunch.exceptions import APIException
3+
4+
ERROR_CODE = 'test_code'
5+
ERROR_MESSAGE = "test message"
6+
7+
8+
def test_api_exception_with_code():
9+
# arrange
10+
error_str = f'error code: {ERROR_CODE}\nmessage: {ERROR_MESSAGE}'
11+
12+
# act
13+
with pytest.raises(APIException) as excinfo:
14+
raise APIException(ERROR_CODE, ERROR_MESSAGE)
15+
16+
# assert
17+
assert excinfo.value.code == ERROR_CODE
18+
assert excinfo.value.message == ERROR_MESSAGE
19+
assert excinfo.value.__str__() == error_str
20+
21+
22+
def test_api_exception_without_code():
23+
# arrange
24+
error_str = f'message: {ERROR_MESSAGE}'
25+
26+
# act
27+
with pytest.raises(APIException) as excinfo:
28+
raise APIException(None, ERROR_MESSAGE)
29+
30+
# assert
31+
assert excinfo.value.code is None
32+
assert excinfo.value.message == ERROR_MESSAGE
33+
assert excinfo.value.__str__() == error_str

0 commit comments

Comments
 (0)