File: test_typing_utils.py

package info (click to toggle)
python-beanie 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: python: 14,427; makefile: 7; sh: 6
file content (47 lines) | stat: -rw-r--r-- 1,417 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
from typing import Optional, Union

import pytest
from pydantic import BaseModel
from typing_extensions import Annotated

from beanie import Document, Link
from beanie.odm.fields import Indexed
from beanie.odm.utils.pydantic import get_model_fields
from beanie.odm.utils.typing import extract_id_class, get_index_attributes


class Lock(Document):
    k: int


class TestTyping:
    def test_extract_id_class(self):
        # Union
        assert extract_id_class(Union[str, int]) is str
        assert extract_id_class(Union[str, None]) is str
        assert extract_id_class(Union[str, None, int]) is str
        # Optional
        assert extract_id_class(Optional[str]) is str
        # Link
        assert extract_id_class(Link[Lock]) == Lock

    @pytest.mark.parametrize(
        "type,result",
        (
            (str, None),
            (Indexed(str), (1, {})),
            (Indexed(str, "text", unique=True), ("text", {"unique": True})),
            (Annotated[str, Indexed()], (1, {})),
            (
                Annotated[str, "other metadata", Indexed(unique=True)],
                (1, {"unique": True}),
            ),
            (Annotated[str, "other metadata"], None),
        ),
    )
    def test_get_index_attributes(self, type, result):
        class Foo(BaseModel):
            bar: type

        field = get_model_fields(Foo)["bar"]
        assert get_index_attributes(field) == result