33
44import aiohttp
55import pytest
6- import requests . adapters
6+ from aiohttp import web
77
88import pyodata .v2 .service
99from pyodata import Client
1010from pyodata .exceptions import PyODataException , HttpError
1111from pyodata .v2 .model import ParserError , PolicyWarning , PolicyFatal , PolicyIgnore , Config
1212
13- SERVICE_URL = 'http://example.com '
13+ SERVICE_URL = ''
1414
1515
16- @pytest .mark .asyncio
1716async def test_invalid_odata_version ():
1817 """Check handling of request for invalid OData version implementation"""
1918
@@ -24,7 +23,6 @@ async def test_invalid_odata_version():
2423 assert str (e_info .value ).startswith ('No implementation for selected odata version' )
2524
2625
27- @pytest .mark .asyncio
2826async def test_create_client_for_local_metadata (metadata ):
2927 """Check client creation for valid use case with local metadata"""
3028
@@ -37,68 +35,63 @@ async def test_create_client_for_local_metadata(metadata):
3735 assert len (service_client .schema .entity_sets ) != 0
3836
3937
40- @patch ("pyodata.client._async_fetch_metadata" )
38+ def generate_metadata_response (headers = None , body = None , status = 200 ):
39+ async def metadata_repsonse (request ):
40+ return web .Response (status = status , headers = headers , body = body )
41+
42+ return metadata_repsonse
43+
44+
4145@pytest .mark .parametrize ("content_type" , ['application/xml' , 'application/atom+xml' , 'text/xml' ])
42- @pytest .mark .asyncio
43- async def test_create_service_application (mock_fetch_metadata , metadata , content_type ):
46+ async def test_create_service_application (aiohttp_client , metadata , content_type ):
4447 """Check client creation for valid MIME types"""
45- mock_fetch_metadata .return_value = metadata
4648
47- async with aiohttp .ClientSession () as client :
48- service_client = await Client .build_async_client (SERVICE_URL , client )
49+ app = web .Application ()
50+ app .router .add_get ('/$metadata' , generate_metadata_response (headers = {'content-type' : content_type }, body = metadata ))
51+ client = await aiohttp_client (app )
4952
50- assert isinstance ( service_client , pyodata . v2 . service . Service )
53+ service_client = await Client . build_async_client ( SERVICE_URL , client )
5154
52- # one more test for '/' terminated url
55+ assert isinstance ( service_client , pyodata . v2 . service . Service )
5356
54- service_client = await Client . build_async_client ( SERVICE_URL + '/' , requests )
57+ # one more test for '/' terminated url
5558
56- assert isinstance (service_client , pyodata .v2 .service .Service )
57- assert service_client .schema .is_valid
59+ service_client = await Client .build_async_client (SERVICE_URL + '/' , client )
60+
61+ assert isinstance (service_client , pyodata .v2 .service .Service )
62+ assert service_client .schema .is_valid
5863
5964
60- @patch ("aiohttp.client.ClientSession.get" )
61- @pytest .mark .asyncio
62- async def test_metadata_not_reachable (mock ):
65+ async def test_metadata_not_reachable (aiohttp_client ):
6366 """Check handling of not reachable service metadata"""
6467
65- response = AsyncMock ()
66- response .status = 404
67- response .headers = {'content-type' : 'text/html' }
68- mock .return_value .__aenter__ .return_value = response
68+ app = web .Application ()
69+ app .router .add_get ('/$metadata' , generate_metadata_response (headers = {'content-type' : 'text/html' }, status = 404 ))
70+ client = await aiohttp_client (app )
6971
7072 with pytest .raises (HttpError ) as e_info :
71- async with aiohttp .ClientSession () as client :
72- await Client .build_async_client (SERVICE_URL , client )
73+ await Client .build_async_client (SERVICE_URL , client )
7374
7475 assert str (e_info .value ).startswith ('Metadata request failed' )
7576
7677
77- @patch ("aiohttp.client.ClientSession.get" )
78- @pytest .mark .asyncio
79- async def test_metadata_saml_not_authorized (mock ):
78+ async def test_metadata_saml_not_authorized (aiohttp_client ):
8079 """Check handling of not SAML / OAuth unauthorized response"""
8180
82- response = AsyncMock ()
83- response .status = 200
84- response .headers = {'content-type' : 'text/html; charset=utf-8' }
85- mock .return_value .__aenter__ .return_value = response
81+ app = web .Application ()
82+ app .router .add_get ('/$metadata' , generate_metadata_response (headers = {'content-type' : 'text/html; charset=utf-8' }))
83+ client = await aiohttp_client (app )
8684
8785 with pytest .raises (HttpError ) as e_info :
88- async with aiohttp .ClientSession () as client :
89- await Client .build_async_client (SERVICE_URL , client )
86+ await Client .build_async_client (SERVICE_URL , client )
9087
9188 assert str (e_info .value ).startswith ('Metadata request did not return XML, MIME type:' )
9289
9390
94- @patch ("pyodata.client._async_fetch_metadata" )
9591@patch ('warnings.warn' )
96- @pytest .mark .asyncio
97- async def test_client_custom_configuration (mock_warning , mock_fetch_metadata , metadata ):
92+ async def test_client_custom_configuration (mock_warning , aiohttp_client , metadata ):
9893 """Check client creation for custom configuration"""
9994
100- mock_fetch_metadata .return_value = metadata
101-
10295 namespaces = {
10396 'edmx' : "customEdmxUrl.com" ,
10497 'edm' : 'customEdmUrl.com'
@@ -112,14 +105,16 @@ async def test_client_custom_configuration(mock_warning, mock_fetch_metadata, me
112105 ParserError .ASSOCIATION : PolicyIgnore ()
113106 })
114107
108+ app = web .Application ()
109+ app .router .add_get ('/$metadata' , generate_metadata_response (headers = {'content-type' : 'application/xml' },body = metadata ))
110+ client = await aiohttp_client (app )
111+
115112 with pytest .raises (PyODataException ) as e_info :
116- async with aiohttp .ClientSession () as client :
117- await Client .build_async_client (SERVICE_URL , client , config = custom_config , namespaces = namespaces )
113+ await Client .build_async_client (SERVICE_URL , client , config = custom_config , namespaces = namespaces )
118114
119115 assert str (e_info .value ) == 'You cannot pass namespaces and config at the same time'
120116
121- async with aiohttp .ClientSession () as client :
122- service = await Client .build_async_client (SERVICE_URL , client , namespaces = namespaces )
117+ service = await Client .build_async_client (SERVICE_URL , client , namespaces = namespaces )
123118
124119 mock_warning .assert_called_with (
125120 'Passing namespaces directly is deprecated. Use class Config instead' ,
@@ -128,8 +123,7 @@ async def test_client_custom_configuration(mock_warning, mock_fetch_metadata, me
128123 assert isinstance (service , pyodata .v2 .service .Service )
129124 assert service .schema .config .namespaces == namespaces
130125
131- async with aiohttp .ClientSession () as client :
132- service = await Client .build_async_client (SERVICE_URL , client , config = custom_config )
126+ service = await Client .build_async_client (SERVICE_URL , client , config = custom_config )
133127
134128 assert isinstance (service , pyodata .v2 .service .Service )
135129 assert service .schema .config == custom_config
0 commit comments