File: test_main.py

package info (click to toggle)
python-djantic 0.7.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: python: 2,599; sh: 19; makefile: 14
file content (66 lines) | stat: -rw-r--r-- 1,492 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
import pytest
from pydantic.v1 import ConfigError
from testapp.models import User

from djantic import ModelSchema


@pytest.mark.django_db
def test_config_errors():
    """
    Test the model config error exceptions.
    """

    with pytest.raises(
        ConfigError, match="(Is `Config` class defined?)"
    ):

        class InvalidModelErrorSchema(ModelSchema):
            pass

    with pytest.raises(
        ConfigError, match="(Is `Config.model` a valid Django model class?)"
    ):

        class InvalidModelErrorSchema(ModelSchema):
            class Config:
                model = "Ok"

    with pytest.raises(
        ConfigError,
        match="Only one of 'include' or 'exclude' should be set in configuration.",
    ):

        class IncludeExcludeErrorSchema(ModelSchema):
            class Config:
                model = User
                include = ["id"]
                exclude = ["first_name"]


@pytest.mark.django_db
def test_get_field_names():
    """
    Test retrieving the field names for a model.
    """

    class UserSchema(ModelSchema):
        class Config:
            model = User
            include = ["id"]

    assert UserSchema.get_field_names() == ["id"]

    class UserSchema(ModelSchema):
        class Config:
            model = User
            exclude = ["id"]

    assert UserSchema.get_field_names() == [
        "profile",
        "first_name",
        "last_name",
        "email",
        "created_at",
        "updated_at",
    ]