File: utils.py

package info (click to toggle)
fastapi 0.118.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,212 kB
  • sloc: python: 69,848; javascript: 369; sh: 18; makefile: 17
file content (34 lines) | stat: -rw-r--r-- 1,097 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
import sys

import pytest
from fastapi._compat import PYDANTIC_V2
from inline_snapshot import Snapshot

needs_py39 = pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9+")
needs_py310 = pytest.mark.skipif(
    sys.version_info < (3, 10), reason="requires python3.10+"
)
needs_pydanticv2 = pytest.mark.skipif(not PYDANTIC_V2, reason="requires Pydantic v2")
needs_pydanticv1 = pytest.mark.skipif(PYDANTIC_V2, reason="requires Pydantic v1")


def pydantic_snapshot(
    *,
    v2: Snapshot,
    v1: Snapshot,  # TODO: remove v1 argument when deprecating Pydantic v1
):
    """
    This function should be used like this:

    >>> assert value == pydantic_snapshot(v2=snapshot(),v1=snapshot())

    inline-snapshot will create the snapshots when pytest is executed for each versions of pydantic.

    It is also possible to use the function inside snapshots for version-specific values.

    >>> assert value == snapshot({
        "data": "some data",
        "version_specific": pydantic_snapshot(v2=snapshot(),v1=snapshot()),
    })
    """
    return v2 if PYDANTIC_V2 else v1