Unittest Tool: This is a `flask.test_client_class` I wrote to making the libraries created by `openapi-python-client` work with `flask.test_client()`
Project description
[TOC]
Intro
This is a simple test_client_class for flask that I wrote to make the rest APIs created
with openapi-python-client work with the flask.test_client()s when one is writing unittests
It is a bit hacky, but it is also only meant to be used for unittests.
Example
In the example I am using OpenAPI (flask_openapi3), because it creates an endpoint with a json-file there can be used by openapi_python_client to create the rest-api which is used to call the flask application through the test client.
import json
import flask
from pydantic import BaseModel, Field
from flask_openapi3 import OpenAPI
app = OpenAPI(__name__)
class SumResponse(BaseModel):
the_sum: int = Field(..., description="sum of 2 numbers")
class NumbersRequest(BaseModel):
no_1: int = Field(..., description="1st number")
no_2: int = Field(..., description="2nd number")
@app.post(rule="/multiply-2-numbers", responses={"200": SumResponse})
def multiply_2_numbers(body: NumbersRequest):
resp = flask.Response(json.dumps({"the_sum": body.no_1 * body.no_2}))
resp.headers.set('Content-Type', 'application/json')
resp.status_code = 200
return resp
import unittest
class Testing(unittest.TestCase):
def test_10_generate_rest_lib(self):
from pathlib import Path
from openapi_python_client import GeneratorData, Config, Project, MetaType
config = Config()
with app.test_client() as client:
resp = client.get("/openapi/openapi.json")
openapi = GeneratorData.from_dict(data=resp.json, config=config)
path = Path(__file__).parent.joinpath("test_rest_api")
path.mkdir(exist_ok=True)
project = Project(openapi=openapi, meta=MetaType.NONE, config=config)
project.package_dir = path
project.project_dir = path
project.update()
def test_20_generate_rest_lib(self):
from test_rest_api.api.default.multiply_2_numbers_multiply_2_numbers_post import (
sync_detailed as rest_api_multiply_2_numbers)
from test_rest_api.models.numbers_request import (
NumbersRequest as RestApiNumbersRequest)
from flask_httpx_request_converted_to_flask_test_client_request import ConvertHttpx2FlaskTestClient
app.test_client_class = ConvertHttpx2FlaskTestClient
with app.test_client() as client:
resp = rest_api_multiply_2_numbers(client=client,
json_body=RestApiNumbersRequest(no_1=42, no_2=1337))
assert 200 == resp.status_code
result = resp.parsed
assert 56154 == result.the_sum
if __name__ == '__main__':
ts = unittest.TestSuite()
ts.addTests([
Testing.test_10_generate_rest_lib,
Testing.test_20_generate_rest_lib],
)
ttr = unittest.TextTestRunner(verbosity=2)
ttr.run(ts)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask-httpx-request-converted-to-flask-test-client-request-0.1.5.tar.gz.
File metadata
- Download URL: flask-httpx-request-converted-to-flask-test-client-request-0.1.5.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e4aba774e80a2256f51f14523052884004030aa0170a45518b5d67e4f807cf0
|
|
| MD5 |
ae2a7f2b3abbc6f25dec73b6e348d6bd
|
|
| BLAKE2b-256 |
da802a725606516429243beaeb961f505592f6005dba4627bdd21e79f3e030c6
|
File details
Details for the file flask_httpx_request_converted_to_flask_test_client_request-0.1.5-py3-none-any.whl.
File metadata
- Download URL: flask_httpx_request_converted_to_flask_test_client_request-0.1.5-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4bac97acfa36c167e7ff578c578c971120bf3b9b53d24c118b1322f0948939af
|
|
| MD5 |
fbc824601116b85f7c04cfc31d856923
|
|
| BLAKE2b-256 |
a13ef1b6586c713378731e013c55f3cf74230e93a5503d29b9dbeb7952e76b02
|