File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33#
44# Copyright (c) 2021 Ingram Micro. All Rights Reserved.
55#
6- from connect .client .testing .fluent import AsyncConnectClientMocker , ConnectClientMocker # noqa
6+ from connect .client .testing .fluent import ( # noqa
7+ AsyncConnectClientMocker ,
8+ ConnectClientMocker ,
9+ get_httpx_mocker ,
10+ get_requests_mocker ,
11+ )
Original file line number Diff line number Diff line change 11import pytest
22
3- from connect .client .testing import AsyncConnectClientMocker , ConnectClientMocker
3+ from connect .client .testing import (
4+ AsyncConnectClientMocker ,
5+ ConnectClientMocker ,
6+ get_httpx_mocker ,
7+ get_requests_mocker ,
8+ )
49
510
611@pytest .fixture
@@ -43,3 +48,25 @@ def _finalizer():
4348
4449 request .addfinalizer (_finalizer )
4550 return _wrapper
51+
52+
53+ @pytest .fixture
54+ def requests_mocker ():
55+ """
56+ This fixture allows you to mock http calls made using the `requests` library
57+ when they are made in conjunction with calls made with the `ConnectClient`.
58+ The returned mocker is the one provided by the
59+ [responses](https://github.com/getsentry/responses) library.
60+ """
61+ return get_requests_mocker ()
62+
63+
64+ @pytest .fixture
65+ def httpx_mocker ():
66+ """
67+ This fixture allows you to mock http calls made using the `httpx` library
68+ when they are made in conjunction with calls made with the `AsyncConnectClient`.
69+ The returned mocker is the one provided by the
70+ [pytest-httpx](https://colin-b.github.io/pytest_httpx/) library.
71+ """
72+ return get_httpx_mocker ()
Original file line number Diff line number Diff line change @@ -232,3 +232,23 @@ def mock(
232232 kwargs ['match_content' ] = match_body
233233
234234 _async_mocker .add_response (** kwargs )
235+
236+
237+ def get_requests_mocker ():
238+ """
239+ Returns a mocker object to mock http calls made using the `requests` library
240+ when they are made in conjunction with calls made with the `ConnectClient`.
241+ The returned mocker is the one provided by the
242+ [responses](https://github.com/getsentry/responses) library.
243+ """
244+ return _mocker
245+
246+
247+ def get_httpx_mocker ():
248+ """
249+ Returns a mocker object to mock http calls made using the `httpx` library
250+ when they are made in conjunction with calls made with the `AsyncConnectClient`.
251+ The returned mocker is the one provided by the
252+ [pytest-httpx](https://colin-b.github.io/pytest_httpx/) library.
253+ """
254+ return _async_mocker
Original file line number Diff line number Diff line change 1+ import httpx
12import pytest
23
34from connect .client import AsyncConnectClient
@@ -10,3 +11,21 @@ async def test_client_mocker_factory(async_client_mocker_factory):
1011
1112 client = AsyncConnectClient ('api_key' , endpoint = 'http://example.com' )
1213 assert await client .products .create (payload = {}) == {'id' : 'PRD-000' }
14+
15+
16+ @pytest .mark .asyncio
17+ async def test_httpx_mocker (async_client_mocker_factory , httpx_mocker ):
18+ mocker = async_client_mocker_factory ('http://example.com' )
19+ mocker .products .create (return_value = {'id' : 'PRD-000' })
20+ httpx_mocker .add_response (
21+ method = 'GET' ,
22+ url = 'https://test.com' ,
23+ json = [{"key1" : "value1" , "key2" : "value2" }],
24+ )
25+
26+ client = AsyncConnectClient ('api_key' , endpoint = 'http://example.com' )
27+ assert await client .products .create (payload = {}) == {'id' : 'PRD-000' }
28+
29+ async with httpx .AsyncClient () as client :
30+ response = await client .get ('https://test.com' )
31+ assert response .json () == [{"key1" : "value1" , "key2" : "value2" }]
Original file line number Diff line number Diff line change 55
66from connect .client import AsyncConnectClient , ClientError
77from connect .client .rql import R
8- from connect .client .testing .fluent import AsyncConnectClientMocker
8+ from connect .client .testing .fluent import AsyncConnectClientMocker , _async_mocker , get_httpx_mocker
99
1010
1111@pytest .mark .asyncio
@@ -400,3 +400,7 @@ async def test_exclude(mocker, exclude):
400400 async with httpx .AsyncClient () as client :
401401 r = await client .get ('https://www.google.com' )
402402 assert r .status_code == 200
403+
404+
405+ def test_get_httpx_mocker ():
406+ assert get_httpx_mocker () == _async_mocker
Original file line number Diff line number Diff line change 1+ import requests
2+
13from connect .client import ConnectClient
24
35
@@ -15,3 +17,13 @@ def test_client_mocker_factory_default_base_url(client_mocker_factory):
1517
1618 client = ConnectClient ('api_key' , endpoint = 'https://example.org/public/v1' )
1719 assert client .products .create (payload = {}) == {'id' : 'PRD-000' }
20+
21+
22+ def test_requests_mocker (client_mocker_factory , requests_mocker ):
23+ mocker = client_mocker_factory ('http://example.com' )
24+ mocker .products .create (return_value = {'id' : 'PRD-000' })
25+ requests_mocker .get ('https://test.com' , json = {'a' : 'b' })
26+
27+ client = ConnectClient ('api_key' , endpoint = 'http://example.com' )
28+ assert client .products .create (payload = {}) == {'id' : 'PRD-000' }
29+ assert requests .get ('https://test.com' ).json () == {'a' : 'b' }
Original file line number Diff line number Diff line change 44
55from connect .client import ClientError , ConnectClient
66from connect .client .rql import R
7- from connect .client .testing .fluent import ConnectClientMocker
7+ from connect .client .testing .fluent import ConnectClientMocker , _mocker , get_requests_mocker
88
99
1010def test_create ():
@@ -391,3 +391,7 @@ def test_exclude(mocker, exclude):
391391
392392 for idx , item in enumerate (excluded ):
393393 assert mocked_add_passthru .mock_calls [idx ][1 ][0 ] == item
394+
395+
396+ def test_get_requests_mocker ():
397+ assert get_requests_mocker () == _mocker
Original file line number Diff line number Diff line change 77from connect .client .testing .fixtures import ( # noqa
88 async_client_mocker_factory ,
99 client_mocker_factory ,
10+ httpx_mocker ,
11+ requests_mocker ,
1012)
1113from tests .fixtures .client_models import ( # noqa
1214 action_factory ,
You can’t perform that action at this time.
0 commit comments