File: test_pydantic_calculated_fields.py

package info (click to toggle)
flask-openapi3 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,976 kB
  • sloc: python: 4,754; sh: 17; makefile: 15; javascript: 5
file content (41 lines) | stat: -rw-r--r-- 926 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# -*- coding: utf-8 -*-
# @Author  : llc
# @Time    : 2024/1/28 16:38
from functools import cached_property

import pytest
from pydantic import BaseModel, Field, computed_field

from flask_openapi3 import OpenAPI

app = OpenAPI(__name__)
app.config["TESTING"] = True


@pytest.fixture
def client():
    client = app.test_client()

    return client


class User(BaseModel):
    firstName: str = Field(title="First Name")
    lastName: str

    @computed_field(title="Display Name")
    @cached_property
    def display_name(self) -> str:
        return f"{self.firstName} {self.lastName}"  # pragma: no cover


@app.get("/user", responses={200: User})
def get_book():
    return "ok"  # pragma: no cover


def test_openapi(client):
    resp = client.get("/openapi/openapi.json")
    import pprint
    pprint.pprint(resp.json)
    assert resp.json["components"]["schemas"]["User"]["properties"].get("display_name") is not None