Skip to content

Commit

Permalink
testes: increase coverage for +90%
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanildoBarauna committed Jun 16, 2024
1 parent c91d96f commit 8d21675
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 4 deletions.
81 changes: 80 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ requests = "^2.32.3"
poetry-dynamic-versioning = "^1.3.0"
pytest = "^8.2.2"
coverage = "^7.5.3"
responses = "^0.25.3"


[build-system]
Expand Down
8 changes: 7 additions & 1 deletion src/api_to_dataframe/models/get_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@

class GetData:
@staticmethod
def get_response(endpoint: str, headers: dict, retry_strategies: RetryStrategies, timeout: int):
def get_response(endpoint: str,
headers: dict,
retry_strategies: RetryStrategies,
timeout: int):
try:
response = requests.get(endpoint, timeout=timeout, headers=headers)
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
raise http_err
except Timeout as timeout_err:
print(f'Timeout error occurred: {timeout_err}')
raise timeout_err
except RequestException as req_err:
print(f'Error occurred: {req_err}')
raise req_err

Check warning on line 25 in src/api_to_dataframe/models/get_data.py

View check run for this annotation

Codecov / codecov/patch

src/api_to_dataframe/models/get_data.py#L23-L25

Added lines #L23 - L25 were not covered by tests
else:
return response

Expand Down
4 changes: 2 additions & 2 deletions tests/test_run.py → tests/test_controller_client_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pandas as pd
import requests

from api_to_dataframe import ClientBuilder
from api_to_dataframe import ClientBuilder, RetryStrategies


@pytest.fixture()
Expand Down Expand Up @@ -36,4 +36,4 @@ def test_response_to_json(setup):

def test_to_dataframe(response_setup):
df = ClientBuilder.api_to_dataframe(response_setup)
assert isinstance(df, pd.DataFrame)
assert isinstance(df, pd.DataFrame)
78 changes: 78 additions & 0 deletions tests/test_models_get_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import pytest
import requests
import responses

from api_to_dataframe.models.get_data import GetData
from api_to_dataframe.controller.client_builder import ClientBuilder
from api_to_dataframe.common.utils.retry_strategies import RetryStrategies


def test_get_response():
assert True


def test_to_dataframe():
with pytest.raises(TypeError):
GetData.to_dataframe("")


@responses.activate
def test_to_emp_dataframe():
endpoint = "https://api.exemplo.com"
expected_response = {}

responses.add(responses.GET, endpoint,
json=expected_response, status=200)

client = ClientBuilder(endpoint=endpoint)
response = client.get_api_data()

with pytest.raises(ValueError):
GetData.to_dataframe(response)


@responses.activate
def test_http_error():
endpoint = "https://api.exemplo.com"
expected_response = {}

responses.add(responses.GET, endpoint,
json=expected_response, status=400)

with ((pytest.raises(requests.exceptions.HTTPError))):
GetData.get_response(
endpoint=endpoint,
retry_strategies=RetryStrategies.NoRetryStrategy,
headers={},
timeout=10)


@responses.activate
def test_timeout_error():
endpoint = "https://api.exemplo.com"

responses.add(responses.GET, endpoint, body=requests.exceptions.Timeout())

with pytest.raises(requests.exceptions.Timeout):
GetData.get_response(
endpoint=endpoint,
retry_strategies=RetryStrategies.NoRetryStrategy,
headers={},
timeout=10)


@responses.activate
def test_request_exception():
endpoint = "https://api.exemplo.com"

expected_response = {}

responses.add(responses.GET, endpoint,
json=expected_response, status=500)

with pytest.raises(requests.exceptions.RequestException):
GetData.get_response(
endpoint=endpoint,
retry_strategies=RetryStrategies.NoRetryStrategy,
headers={},
timeout=10)

0 comments on commit 8d21675

Please sign in to comment.