1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
""" Test the pyodata integration with httpx client
- it provided sync, requests like interface
- it provides asyncio interface as well - FOCUS OF THIS TEST MODULE
https://www.python-httpx.org/
"""
import httpx
from httpx import Response
import respx
import pytest
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 = 'http://example.com'
def test_invalid_odata_version():
"""Check handling of request for invalid OData version implementation"""
with pytest.raises(PyODataException) as e_info:
pyodata.Client(SERVICE_URL, httpx, 'INVALID VERSION')
assert str(e_info.value).startswith('No implementation for selected odata version')
def test_create_client_for_local_metadata(metadata):
"""Check client creation for valid use case with local metadata"""
client = pyodata.Client(SERVICE_URL, httpx, metadata=metadata)
assert isinstance(client, pyodata.v2.service.Service)
assert client.schema.is_valid == True
assert len(client.schema.entity_sets) != 0
@pytest.mark.parametrize("content_type", ['application/xml', 'application/atom+xml', 'text/xml'])
def test_create_service_application(respx_mock, metadata, content_type):
"""Check client creation for valid MIME types"""
# Note: respx_mock is provided by respx package as pytest helper
headers = httpx.Headers(
{'Content-Type': content_type}
)
respx_mock.get(f"{SERVICE_URL}/$metadata").mock(
return_value=Response(status_code=200,
content=metadata,
headers=headers,
)
)
client = pyodata.Client(SERVICE_URL, httpx)
assert isinstance(client, pyodata.v2.service.Service)
# one more test for '/' terminated url
client = pyodata.Client(SERVICE_URL + '/', httpx)
assert isinstance(client, pyodata.v2.service.Service)
assert client.schema.is_valid
def test_metadata_not_reachable(respx_mock):
"""Check handling of not reachable service metadata"""
headers = httpx.Headers(
{'Content-Type': 'text/html'}
)
respx_mock.get(f"{SERVICE_URL}/$metadata").mock(
return_value=Response(status_code=404,
headers=headers,
)
)
with pytest.raises(HttpError) as e_info:
pyodata.Client(SERVICE_URL, httpx)
assert str(e_info.value).startswith('Metadata request failed')
def test_metadata_saml_not_authorized(respx_mock):
"""Check handling of not SAML / OAuth unauthorized response"""
headers = httpx.Headers(
{'Content-Type': 'text/html; charset=utf-8'}
)
respx_mock.get(f"{SERVICE_URL}/$metadata").mock(
return_value=Response(status_code=200,
headers=headers,
)
)
with pytest.raises(HttpError) as e_info:
pyodata.Client(SERVICE_URL, httpx)
assert str(e_info.value).startswith('Metadata request did not return XML, MIME type:')
def test_client_custom_configuration(respx_mock,metadata):
"""Check client creation for custom configuration"""
headers = httpx.Headers(
{'Content-Type': 'application/xml'}
)
respx_mock.get(f"{SERVICE_URL}/$metadata").mock(
return_value=Response(status_code=200,
headers=headers,
content=metadata,
)
)
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()
})
with pytest.raises(PyODataException) as e_info:
client = pyodata.Client(SERVICE_URL, httpx, config=custom_config, namespaces=namespaces)
assert str(e_info.value) == 'You cannot pass namespaces and config at the same time'
with pytest.warns(DeprecationWarning,match='Passing namespaces directly is deprecated. Use class Config instead'):
client = pyodata.Client(SERVICE_URL, httpx, namespaces=namespaces)
assert isinstance(client, pyodata.v2.service.Service)
assert client.schema.config.namespaces == namespaces
client = pyodata.Client(SERVICE_URL, httpx, config=custom_config)
assert isinstance(client, pyodata.v2.service.Service)
assert client.schema.config == custom_config
|