Skip to content

Commit

Permalink
Added a few very simple tests... to test
Browse files Browse the repository at this point in the history
  • Loading branch information
rderollepot committed Jun 24, 2024
1 parent 71de153 commit f0bb9c7
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ keywords = ["API", "data", "public transport", "Athens", "timetables"]

[project.urls]
Repository = "https://github.com/rderollepot/oasa_api.git"

[tool.pytest.ini_options]
pythonpath = [
"."
]
8 changes: 8 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest

from oasa_api.exceptions import OASAApiException


def test_oasa_api_exception():
with pytest.raises(OASAApiException):
raise OASAApiException("An error occurred")
29 changes: 29 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

from oasa_api.models import Result
from oasa_api.exceptions import OASAApiException


def test_result_parse_data_list():
data = [{'id': '1', 'name': 'Stop 1'}, {'id': '2', 'name': 'Stop 2'}]
result = Result(status_code=200, data=data)
df_list = result.parse_data()

assert len(df_list) == 2
assert df_list.loc[0, 'id'] == '1'


def test_result_parse_data_dict():
data = {'stops': [{'id': '1', 'name': 'Stop 1'}, {'id': '2', 'name': 'Stop 2'}]}
result = Result(status_code=200, data=data)
df_dict = result.parse_data()

assert len(df_dict['stops']) == 2
assert df_dict['stops'].loc[0, 'id'] == '1'


# noinspection PyTypeChecker
def test_result_parse_data_invalid():
result = Result(status_code=200, data='invalid')
with pytest.raises(OASAApiException):
result.parse_data()
78 changes: 78 additions & 0 deletions tests/test_rest_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import pytest
from unittest.mock import patch, Mock
from requests.exceptions import RequestException

from oasa_api.rest_adapter import RestAdapter, HttpMethod
from oasa_api.exceptions import OASAApiException


@patch('oasa_api.rest_adapter.requests.request')
def test_get_success(mock_get):
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {'data': 'value'}
mock_get.return_value = mock_response

adapter = RestAdapter('https://fakeurl.com')
result = adapter.get(params={'param1': 'value1'})

assert result.data['data'] == 'value'


@patch('oasa_api.rest_adapter.requests.request')
def test_do_get_success(mock_request):
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {'data': 'value'}
mock_request.return_value = mock_response

adapter = RestAdapter('https://fakeurl.com')
result = adapter._do(http_method=HttpMethod.GET, params={'param1': 'value1'})

assert result.data['data'] == 'value'


@patch('oasa_api.rest_adapter.requests.request')
def test_do_post_success(mock_request):
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {'data': 'value'}
mock_request.return_value = mock_response

adapter = RestAdapter('https://fakeurl.com')
result = adapter._do(http_method=HttpMethod.POST, params={'param1': 'value1'}, data={'key': 'value'})

assert result.data['data'] == 'value'


@patch('oasa_api.rest_adapter.requests.request')
def test_do_request_failure(mock_request):
mock_request.side_effect = RequestException()

adapter = RestAdapter('https://fakeurl.com')
with pytest.raises(OASAApiException):
adapter._do(http_method=HttpMethod.GET)


@patch('oasa_api.rest_adapter.requests.request')
def test_do_json_failure(mock_request):
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.side_effect = ValueError()
mock_request.return_value = mock_response

adapter = RestAdapter('https://fakeurl.com')
with pytest.raises(OASAApiException):
adapter._do(http_method=HttpMethod.GET)


@patch('oasa_api.rest_adapter.requests.request')
def test_do_status_code_failure(mock_request):
mock_response = Mock()
mock_response.status_code = 500
mock_response.json.return_value = {'data': 'value'}
mock_request.return_value = mock_response

adapter = RestAdapter('https://fakeurl.com')
with pytest.raises(OASAApiException):
adapter._do(http_method=HttpMethod.GET)

0 comments on commit f0bb9c7

Please sign in to comment.