Skip to content

Commit bb15e1b

Browse files
committed
made some doc changes
1 parent 4182427 commit bb15e1b

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

docs/basics/testing.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,16 @@ from project_name.apps.car.schemas import CreateCarSerializer, CarListFilter
322322
from project_name.apps.car.services import CarRepository
323323

324324

325+
class MockCarRepository(CarRepository):
326+
def get_all(self):
327+
return [dict(id=2, model='CLS',name='Mercedes', year=2023)]
328+
329+
325330
class TestCarControllerE2E:
326331
def setup(self):
327332
test_module = Test.create_test_module(
328333
controllers=[CarController,],
329-
providers=[ProviderConfig(CarRepository, use_class=CarRepository)],
334+
providers=[ProviderConfig(CarRepository, use_class=MockCarRepository)],
330335
config_module=dict(
331336
REDIRECT_SLASHES=True
332337
)
@@ -346,8 +351,7 @@ class TestCarControllerE2E:
346351
"year": 2022,
347352
}
348353

349-
@patch.object(CarRepository, 'get_all', return_value=[dict(id=2, model='CLS',name='Mercedes', year=2023)])
350-
def test_get_all_action(self, mock_get_all):
354+
def test_get_all_action(self):
351355
res = self.client.get('/car?offset=0&limit=10')
352356
assert res.status_code == 200
353357
assert res.json() == {

docs/websockets/index.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,13 @@ The `ws_route` offers more than just defining a websocket route. It can also be
185185
By setting `use_extra_handler=True` in `ws_route` decorator, we activate an in-built handler that gives the ability to
186186
manage different sessions of websocket differently like `on_connect`, `on_message` and `on_disconnect`
187187

188-
- `on_connect(websocket, **kwargs)`: handler to handles client connection with the server.
189-
- `on_message(websocket, data)`: handler for messages received from the client
190-
- `on_disconnect(websocket, close_code)`: handler that handles client disconnecting from websocket server
188+
- `on_connect(websocket, **kwargs)`: handles client connection with the server.
189+
- `on_message(websocket, data)`: handles messages sent from the client
190+
- `on_disconnect(websocket, close_code)`: handles server disconnecting from client
191191

192-
This approach also enables message data type validation using `WsBody`.
193-
`WsBody` is similar to [`Body`](../parsing-inputs/body) but for websockets.
192+
!!! info
193+
This approach also enables message data type validation using `WsBody`.
194+
`WsBody` is similar to [`Body`](../parsing-inputs/body) but for websockets.
194195

195196
Let's rewrite the previous example, `/live-support` websocket route.
196197
```python
@@ -224,22 +225,24 @@ class CarController(ControllerBase):
224225
await websocket.close(code)
225226
```
226227
In the construct above, we created `def live_support_connect` to handle connection to the `'/live-support'` websocket route and
227-
`def live_support_disconnect` to handle disconnection from it. `def live_support_connect` and `def live_support_disconnect` takes `websocket` instance as only parameter and must be an asynchronous function.
228+
`def live_support_disconnect` to handle disconnection from it. `def live_support_connect` and `def live_support_disconnect`
229+
takes `websocket` instance as only parameter and must be an **asynchronous** function.
228230

229-
On the other hand,`def live_support` function is now a **message receiver handler** and so, there is need to define a parameter with `WsBody`, in this case `data:str = WsBody()`. And message sent from client will be passed to `data` parameter and validated as `str` data type.
231+
On the other hand,`def live_support` function is now a **message receiver handler** and so, there is need to define a parameter with `WsBody`,
232+
in this case `data:str = WsBody()`. Message sent from client will be passed to `data` parameter after validation and procession by `WsBody`.
230233
If validation fails, an error will be sent to the client and connection will be destroyed.
231234

232-
The **`encoding` = 'text'** states the **message** data structure expected from client to the server.
233-
There are 3 different **`encoding`** types.
235+
The **`encoding` = 'text'** states the **message** data structure that is required of the client when sending messages to the server.
236+
There are other **`encoding`** types supported:
234237

235238
- `text`: allows only simple text messages as in the case above, e.g. `@ws_route('/path', use_extra_handler=True, encoding='text')`
236239
- `json`: allows json messages e.g. `@ws_route('/path', use_extra_handler=True, encoding='json')`
237240
- `bytes`: allows byte messages e.g. `@ws_route('/path', use_extra_handler=True, encoding='bytes')`
238241

239242
**Simplifying the example above**
240243

241-
We can further simplify the example above by getting rid of the `live_support_connect` and `live_support_disconnect`
242-
and let the inbuilt handler manage that for us as shown below:
244+
We can further simplify the example above by getting rid of the `live_support_connect` and `live_support_disconnect`
245+
and let the inbuilt handler apply the default `connection` and `disconnection` actions.
243246

244247
```python
245248
# project_name/apps/car/controller.py

0 commit comments

Comments
 (0)