File: test_pydantic_fields.py

package info (click to toggle)
ormar 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,952 kB
  • sloc: python: 24,085; makefile: 34; sh: 14
file content (126 lines) | stat: -rw-r--r-- 3,633 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import random
from typing import Optional

import ormar
import pytest
from pydantic import BaseModel, Field, HttpUrl
from pydantic_extra_types.payment import PaymentCardNumber

from tests.lifespan import init_tests
from tests.settings import create_config

base_ormar_config = create_config()


class ModelTest(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=200)
    url: HttpUrl = "https://www.example.com"  # type: ignore
    number: Optional[PaymentCardNumber] = None


CARD_NUMBERS = [
    "123456789007",
    "123456789015",
    "123456789023",
    "123456789031",
    "123456789049",
]


def get_number():
    return random.choice(CARD_NUMBERS)


class ModelTest2(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=200)
    url: HttpUrl = "https://www.example2.com"  # type: ignore
    number: PaymentCardNumber = Field(default_factory=get_number)


class PydanticTest(BaseModel):
    aa: str
    bb: int


class ModelTest3(ormar.Model):
    ormar_config = base_ormar_config.copy()

    def __init__(self, **kwargs):
        kwargs["number"] = get_number()
        kwargs["pydantic_test"] = PydanticTest(aa="random", bb=42)
        super().__init__(**kwargs)

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=200)
    url: HttpUrl = "https://www.example3.com"  # type: ignore
    number: PaymentCardNumber
    pydantic_test: PydanticTest


create_test_database = init_tests(base_ormar_config)


@pytest.mark.asyncio
async def test_working_with_pydantic_fields():
    async with base_ormar_config.database:
        test = ModelTest(name="Test")
        assert test.name == "Test"
        assert test.url == "https://www.example.com"
        assert test.number is None
        test.number = "123456789015"

        test.url = "https://www.sdta.ada.pt"
        assert test.url == "https://www.sdta.ada.pt"

        await test.save()
        test_check = await ModelTest.objects.get()

        assert test_check.name == "Test"
        assert test_check.url == "https://www.example.com"
        assert test_check.number is None


@pytest.mark.asyncio
async def test_default_factory_for_pydantic_fields():
    async with base_ormar_config.database:
        test = ModelTest2(name="Test2", number="4000000000000002")
        assert test.name == "Test2"
        assert test.url == "https://www.example2.com"
        assert test.number == "4000000000000002"

        test.url = "http://www.sdta.ada.pt"
        assert test.url == "http://www.sdta.ada.pt"

        await test.save()
        test_check = await ModelTest2.objects.get()

        assert test_check.name == "Test2"
        assert test_check.url == "https://www.example2.com"
        assert test_check.number in CARD_NUMBERS
        assert test_check.number != test.number


@pytest.mark.asyncio
async def test_init_setting_for_pydantic_fields():
    async with base_ormar_config.database:
        test = ModelTest3(name="Test3")
        assert test.name == "Test3"
        assert test.url == "https://www.example3.com"
        assert test.pydantic_test.bb == 42

        test.url = "http://www.sdta.ada.pt"
        assert test.url == "http://www.sdta.ada.pt"

        await test.save()
        test_check = await ModelTest3.objects.get()

        assert test_check.name == "Test3"
        assert test_check.url == "https://www.example3.com"
        assert test_check.number in CARD_NUMBERS
        assert test_check.pydantic_test.aa == "random"