-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a few very simple tests... to test
- Loading branch information
1 parent
71de153
commit f0bb9c7
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |