Skip to content

Commit ec8e7a2

Browse files
authored
Merge pull request #60 from cloudblue/add_fixtures_for_mocking_other_calls
Add fixures for mocking http calls done with requests/httpx
2 parents 2b2f407 + 5b65fe6 commit ec8e7a2

8 files changed

Lines changed: 97 additions & 4 deletions

File tree

connect/client/testing/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@
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+
)

connect/client/testing/fixtures.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import 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()

connect/client/testing/fluent.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff 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

tests/async_client/test_fixtures.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import httpx
12
import pytest
23

34
from 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"}]

tests/async_client/test_testing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from connect.client import AsyncConnectClient, ClientError
77
from 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

tests/client/test_fixtures.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import requests
2+
13
from 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'}

tests/client/test_testing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from connect.client import ClientError, ConnectClient
66
from 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

1010
def 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

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from connect.client.testing.fixtures import ( # noqa
88
async_client_mocker_factory,
99
client_mocker_factory,
10+
httpx_mocker,
11+
requests_mocker,
1012
)
1113
from tests.fixtures.client_models import ( # noqa
1214
action_factory,

0 commit comments

Comments
 (0)