Skip to content

Commit f860999

Browse files
committed
simplified the examples
1 parent 0670246 commit f860999

File tree

4 files changed

+63
-59
lines changed

4 files changed

+63
-59
lines changed

README.md

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,50 +44,27 @@ async def some_service_mock(
4444
settings.EXTERNAL_SERVICE_URL = old_url
4545
```
4646

47-
### 2. mock specific api
47+
### 2. mock and test it
4848

49-
You don’t need to follow this pattern exactly —
50-
this is just an example where the fixture is responsible for mocking
51-
a specific route.
52-
53-
```python
54-
@pytest.fixture
55-
def some_service_mock_api(
56-
some_service_mock: AddMockDataFunc,
57-
) -> Callable[
58-
[web.Response | ResponseHandler],
59-
List[dict[str, Any]],
60-
]:
61-
"""An example of a fixture where a specific API is mocked"""
62-
63-
def _create_mock(
64-
response: web.Response
65-
| Callable[[web.Request], web.Response | Awaitable[web.Response]],
66-
) -> List[dict[str, Any]]:
67-
return some_service_mock(MockData("POST", "/some_api", response))
68-
69-
return _create_mock
70-
```
71-
72-
### 3. test it
49+
#### static mock
7350

7451
```python
7552
import pytest
7653
from http import HTTPStatus
77-
54+
from async_pytest_httpserver import (
55+
MockData,
56+
)
7857
from aiohttp.web import json_response, Request, Response
7958

80-
# example of static mock
8159

8260
@pytest.mark.asyncio
83-
async def test_static_mock(client, some_service_mock_api):
61+
async def test_static_mock(client, some_service_mock):
8462
# Arrange
85-
calls_info = some_service_mock_api(
86-
json_response(
87-
{"result": "some_result"},
88-
status=HTTPStatus.OK,
89-
)
63+
response = json_response(
64+
{"result": "some_result"},
65+
status=HTTPStatus.OK,
9066
)
67+
calls_info = some_service_mock(MockData("POST", "/some_api", response))
9168

9269
# Act
9370
response = await client.post(
@@ -103,20 +80,32 @@ async def test_static_mock(client, some_service_mock_api):
10380
assert len(calls_info) == 1
10481
call_info = calls_info[0]
10582
assert call_info["json"] == {"text": "text"}
83+
```
84+
85+
#### dynamic async mock
10686

87+
```python
88+
import pytest
89+
from http import HTTPStatus
90+
from async_pytest_httpserver import (
91+
MockData,
92+
)
93+
from aiohttp.web import json_response, Request, Response
10794

108-
# example of dynamic async mock
10995

11096
async def async_mock_handler(request: Request) -> Response:
11197
return json_response(
112-
{"result": "some_result"},
113-
status=HTTPStatus.OK,
114-
)
98+
{"result": "some_result"},
99+
status=HTTPStatus.OK,
100+
)
101+
115102

116103
@pytest.mark.asyncio
117-
async def test_async_handler(client, some_service_mock_api):
104+
async def test_async_handler(client, some_service_mock):
118105
# Arrange
119-
calls_info = some_service_mock_api(async_mock_handler)
106+
calls_info = some_service_mock(
107+
MockData("POST", "/some_api", async_mock_handler)
108+
)
120109

121110
# Act
122111
response = await client.post(
@@ -132,21 +121,31 @@ async def test_async_handler(client, some_service_mock_api):
132121
assert len(calls_info) == 1
133122
call_info = calls_info[0]
134123
assert call_info["json"] == {"text": "text"}
124+
```
135125

126+
#### dynamic sync mock
136127

137-
# example of dynamic sync mock
138-
128+
```python
129+
import pytest
130+
from http import HTTPStatus
131+
from async_pytest_httpserver import (
132+
MockData,
133+
)
134+
from aiohttp.web import json_response, Request, Response
139135

140136
def sync_mock_handler(request: Request) -> Response:
141137
return json_response(
142-
{"result": "some_result"},
143-
status=HTTPStatus.OK,
144-
)
138+
{"result": "some_result"},
139+
status=HTTPStatus.OK,
140+
)
141+
145142

146143
@pytest.mark.asyncio
147-
async def test_sync_handler(client, some_service_mock_api):
144+
async def test_sync_handler(client, some_service_mock):
148145
# Arrange
149-
calls_info = some_service_mock_api(sync_mock_handler)
146+
calls_info = some_service_mock(
147+
MockData("POST", "/some_api", sync_mock_handler)
148+
)
150149

151150
# Act
152151
response = await client.post(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "async-pytest-httpserver"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
description = "Async mock HTTP server for pytest, built on top of aiohttp."
55
readme = "README.md"
66
requires-python = ">=3.13"

tests/test_example.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import pytest
22
from http import HTTPStatus
3-
3+
from async_pytest_httpserver import (
4+
MockData,
5+
)
46
from aiohttp.web import json_response, Request, Response
57
from . import settings
68

79

810
@pytest.mark.asyncio
9-
async def test_static_mock(client, some_service_mock_api):
11+
async def test_static_mock(client, some_service_mock):
1012
# Arrange
11-
calls_info = some_service_mock_api(
12-
json_response(
13-
{"result": "some_result"},
14-
status=HTTPStatus.OK,
15-
)
13+
response = json_response(
14+
{"result": "some_result"},
15+
status=HTTPStatus.OK,
1616
)
17+
calls_info = some_service_mock(MockData("POST", "/some_api", response))
1718

1819
# Act
1920
response = await client.post(
@@ -39,9 +40,11 @@ async def async_mock_handler(request: Request) -> Response:
3940

4041

4142
@pytest.mark.asyncio
42-
async def test_async_handler(client, some_service_mock_api):
43+
async def test_async_handler(client, some_service_mock):
4344
# Arrange
44-
calls_info = some_service_mock_api(async_mock_handler)
45+
calls_info = some_service_mock(
46+
MockData("POST", "/some_api", async_mock_handler)
47+
)
4548

4649
# Act
4750
response = await client.post(
@@ -67,9 +70,11 @@ def sync_mock_handler(request: Request) -> Response:
6770

6871

6972
@pytest.mark.asyncio
70-
async def test_sync_handler(client, some_service_mock_api):
73+
async def test_sync_handler(client, some_service_mock):
7174
# Arrange
72-
calls_info = some_service_mock_api(sync_mock_handler)
75+
calls_info = some_service_mock(
76+
MockData("POST", "/some_api", sync_mock_handler)
77+
)
7378

7479
# Act
7580
response = await client.post(

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)