Skip to content

Commit 8d21675

Browse files
testes: increase coverage for +90%
1 parent c91d96f commit 8d21675

File tree

5 files changed

+168
-4
lines changed

5 files changed

+168
-4
lines changed

poetry.lock

Lines changed: 80 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ requests = "^2.32.3"
3131
poetry-dynamic-versioning = "^1.3.0"
3232
pytest = "^8.2.2"
3333
coverage = "^7.5.3"
34+
responses = "^0.25.3"
3435

3536

3637
[build-system]

src/api_to_dataframe/models/get_data.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@
77

88
class GetData:
99
@staticmethod
10-
def get_response(endpoint: str, headers: dict, retry_strategies: RetryStrategies, timeout: int):
10+
def get_response(endpoint: str,
11+
headers: dict,
12+
retry_strategies: RetryStrategies,
13+
timeout: int):
1114
try:
1215
response = requests.get(endpoint, timeout=timeout, headers=headers)
1316
response.raise_for_status()
1417
except HTTPError as http_err:
1518
print(f'HTTP error occurred: {http_err}')
19+
raise http_err
1620
except Timeout as timeout_err:
1721
print(f'Timeout error occurred: {timeout_err}')
22+
raise timeout_err
1823
except RequestException as req_err:
1924
print(f'Error occurred: {req_err}')
25+
raise req_err
2026
else:
2127
return response
2228

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pandas as pd
33
import requests
44

5-
from api_to_dataframe import ClientBuilder
5+
from api_to_dataframe import ClientBuilder, RetryStrategies
66

77

88
@pytest.fixture()
@@ -36,4 +36,4 @@ def test_response_to_json(setup):
3636

3737
def test_to_dataframe(response_setup):
3838
df = ClientBuilder.api_to_dataframe(response_setup)
39-
assert isinstance(df, pd.DataFrame)
39+
assert isinstance(df, pd.DataFrame)

tests/test_models_get_data.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import pytest
2+
import requests
3+
import responses
4+
5+
from api_to_dataframe.models.get_data import GetData
6+
from api_to_dataframe.controller.client_builder import ClientBuilder
7+
from api_to_dataframe.common.utils.retry_strategies import RetryStrategies
8+
9+
10+
def test_get_response():
11+
assert True
12+
13+
14+
def test_to_dataframe():
15+
with pytest.raises(TypeError):
16+
GetData.to_dataframe("")
17+
18+
19+
@responses.activate
20+
def test_to_emp_dataframe():
21+
endpoint = "https://api.exemplo.com"
22+
expected_response = {}
23+
24+
responses.add(responses.GET, endpoint,
25+
json=expected_response, status=200)
26+
27+
client = ClientBuilder(endpoint=endpoint)
28+
response = client.get_api_data()
29+
30+
with pytest.raises(ValueError):
31+
GetData.to_dataframe(response)
32+
33+
34+
@responses.activate
35+
def test_http_error():
36+
endpoint = "https://api.exemplo.com"
37+
expected_response = {}
38+
39+
responses.add(responses.GET, endpoint,
40+
json=expected_response, status=400)
41+
42+
with ((pytest.raises(requests.exceptions.HTTPError))):
43+
GetData.get_response(
44+
endpoint=endpoint,
45+
retry_strategies=RetryStrategies.NoRetryStrategy,
46+
headers={},
47+
timeout=10)
48+
49+
50+
@responses.activate
51+
def test_timeout_error():
52+
endpoint = "https://api.exemplo.com"
53+
54+
responses.add(responses.GET, endpoint, body=requests.exceptions.Timeout())
55+
56+
with pytest.raises(requests.exceptions.Timeout):
57+
GetData.get_response(
58+
endpoint=endpoint,
59+
retry_strategies=RetryStrategies.NoRetryStrategy,
60+
headers={},
61+
timeout=10)
62+
63+
64+
@responses.activate
65+
def test_request_exception():
66+
endpoint = "https://api.exemplo.com"
67+
68+
expected_response = {}
69+
70+
responses.add(responses.GET, endpoint,
71+
json=expected_response, status=500)
72+
73+
with pytest.raises(requests.exceptions.RequestException):
74+
GetData.get_response(
75+
endpoint=endpoint,
76+
retry_strategies=RetryStrategies.NoRetryStrategy,
77+
headers={},
78+
timeout=10)

0 commit comments

Comments
 (0)