-
Notifications
You must be signed in to change notification settings - Fork 95
Add async networking libraries support #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
543d175
feat: add support for async http library aiohttp
mnunzio 00ad1dc
Merge pull request #1 from mnunzio/async_python_no_duplicate_code
Albo90 e5b6ec1
Merge pull request #2 from mnunzio/async_python_no_duplicate_code
Albo90 5f88377
Added: prefix async getattr, atom response
Albo90 a4cc84d
added tests
Albo90 30470be
updated dev-requirements.txt
Albo90 c2d662a
feat: update async_client tests
mnunzio 7892057
Merge pull request #3 from mnunzio/no_duplication_v1
Albo90 398dd24
feat: update dev-requirements.txt
mnunzio bad2e41
Merge pull request #4 from mnunzio/no_duplication_v1
Albo90 9ceb3dd
feat: clean useless code
mnunzio cad0c92
Merge branch 'Albo90:no_duplication_v1' into no_duplication_v1
mnunzio 3bab585
Merge pull request #5 from mnunzio/no_duplication_v1
Albo90 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
requests==2.23.0 | ||
pytest-aiohttp == 1.0.4 | ||
pytest>=4.6.0 | ||
responses>=0.8.1 | ||
setuptools>=38.2.4 | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,130 @@ | ||
"""PyOData Client tests""" | ||
from unittest.mock import patch | ||
|
||
import aiohttp | ||
import pytest | ||
from aiohttp import web | ||
|
||
import pyodata.v2.service | ||
from pyodata import Client | ||
from pyodata.exceptions import PyODataException, HttpError | ||
from pyodata.v2.model import ParserError, PolicyWarning, PolicyFatal, PolicyIgnore, Config | ||
|
||
SERVICE_URL = '' | ||
|
||
|
||
async def test_invalid_odata_version(): | ||
"""Check handling of request for invalid OData version implementation""" | ||
|
||
with pytest.raises(PyODataException) as e_info: | ||
async with aiohttp.ClientSession() as client: | ||
await Client.build_async_client(SERVICE_URL, client, 'INVALID VERSION') | ||
|
||
assert str(e_info.value).startswith('No implementation for selected odata version') | ||
|
||
|
||
async def test_create_client_for_local_metadata(metadata): | ||
"""Check client creation for valid use case with local metadata""" | ||
|
||
async with aiohttp.ClientSession() as client: | ||
service_client = await Client.build_async_client(SERVICE_URL, client, metadata=metadata) | ||
|
||
assert isinstance(service_client, pyodata.v2.service.Service) | ||
assert service_client.schema.is_valid == True | ||
|
||
assert len(service_client.schema.entity_sets) != 0 | ||
|
||
|
||
def generate_metadata_response(headers=None, body=None, status=200): | ||
async def metadata_repsonse(request): | ||
return web.Response(status=status, headers=headers, body=body) | ||
|
||
return metadata_repsonse | ||
|
||
|
||
@pytest.mark.parametrize("content_type", ['application/xml', 'application/atom+xml', 'text/xml']) | ||
async def test_create_service_application(aiohttp_client, metadata, content_type): | ||
"""Check client creation for valid MIME types""" | ||
|
||
app = web.Application() | ||
app.router.add_get('/$metadata', generate_metadata_response(headers={'content-type': content_type}, body=metadata)) | ||
client = await aiohttp_client(app) | ||
|
||
service_client = await Client.build_async_client(SERVICE_URL, client) | ||
|
||
assert isinstance(service_client, pyodata.v2.service.Service) | ||
|
||
# one more test for '/' terminated url | ||
|
||
service_client = await Client.build_async_client(SERVICE_URL + '/', client) | ||
|
||
assert isinstance(service_client, pyodata.v2.service.Service) | ||
assert service_client.schema.is_valid | ||
|
||
|
||
async def test_metadata_not_reachable(aiohttp_client): | ||
"""Check handling of not reachable service metadata""" | ||
|
||
app = web.Application() | ||
app.router.add_get('/$metadata', generate_metadata_response(headers={'content-type': 'text/html'}, status=404)) | ||
client = await aiohttp_client(app) | ||
|
||
with pytest.raises(HttpError) as e_info: | ||
await Client.build_async_client(SERVICE_URL, client) | ||
|
||
assert str(e_info.value).startswith('Metadata request failed') | ||
|
||
|
||
async def test_metadata_saml_not_authorized(aiohttp_client): | ||
"""Check handling of not SAML / OAuth unauthorized response""" | ||
|
||
app = web.Application() | ||
app.router.add_get('/$metadata', generate_metadata_response(headers={'content-type': 'text/html; charset=utf-8'})) | ||
client = await aiohttp_client(app) | ||
|
||
with pytest.raises(HttpError) as e_info: | ||
await Client.build_async_client(SERVICE_URL, client) | ||
|
||
assert str(e_info.value).startswith('Metadata request did not return XML, MIME type:') | ||
|
||
|
||
@patch('warnings.warn') | ||
async def test_client_custom_configuration(mock_warning, aiohttp_client, metadata): | ||
"""Check client creation for custom configuration""" | ||
|
||
namespaces = { | ||
'edmx': "customEdmxUrl.com", | ||
'edm': 'customEdmUrl.com' | ||
} | ||
|
||
custom_config = Config( | ||
xml_namespaces=namespaces, | ||
default_error_policy=PolicyFatal(), | ||
custom_error_policies={ | ||
ParserError.ANNOTATION: PolicyWarning(), | ||
ParserError.ASSOCIATION: PolicyIgnore() | ||
}) | ||
|
||
app = web.Application() | ||
app.router.add_get('/$metadata', | ||
generate_metadata_response(headers={'content-type': 'application/xml'}, body=metadata)) | ||
client = await aiohttp_client(app) | ||
|
||
with pytest.raises(PyODataException) as e_info: | ||
await Client.build_async_client(SERVICE_URL, client, config=custom_config, namespaces=namespaces) | ||
|
||
assert str(e_info.value) == 'You cannot pass namespaces and config at the same time' | ||
|
||
service = await Client.build_async_client(SERVICE_URL, client, namespaces=namespaces) | ||
|
||
mock_warning.assert_called_with( | ||
'Passing namespaces directly is deprecated. Use class Config instead', | ||
DeprecationWarning | ||
) | ||
assert isinstance(service, pyodata.v2.service.Service) | ||
assert service.schema.config.namespaces == namespaces | ||
|
||
service = await Client.build_async_client(SERVICE_URL, client, config=custom_config) | ||
|
||
assert isinstance(service, pyodata.v2.service.Service) | ||
assert service.schema.config == custom_config |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mark at least as TODO to grab more attention in future PR if not done.