d42 Schemas
The jj-district42 package allows you to use d42 schemas with JJ mock.
Installation
To install the package, use the following command:
$ pip3 install jj-district42
Usage
Server Side
First you need to start the remote mock server. To do this, run the following command:
- CLI
- Docker
- Python
$ jj --port 8080
$ docker run -p 8080:80 vedrouniverse/jj
import jj
from jj.mock import Mock
jj.serve(Mock(), port=8080)
Client Side
Then you can use jj-district42
to define the schema for the response and validate it against the actual response.
import jj
import httpx
from jj.mock import mocked
from jj_district42 import HistorySchema
from d42 import validate_or_fail
# Define the matcher and the response for the request
matcher = jj.match("GET", "/users")
response = jj.Response(status=200, json=[])
# Use the `mocked` context manager to capture the request and response data
with mocked(matcher, response) as mock:
resp = httpx.get("http://localhost:8080/users", params={"user_id": 1})
# Validate the mock history against the schema
assert validate_or_fail(
HistorySchema % [
{
"request": {
"method": "GET",
"path": "/users",
"params": {"user_id": "1"},
}
}
],
mock.history
)
This code will validate the mock history against the HistorySchema
and raise a ValidationException
if the validation fails.
info
For more examples of using jj-district42
, see the examples directory