-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
test_enum.py
133 lines (114 loc) · 3.21 KB
/
test_enum.py
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
127
128
129
130
131
132
133
import pytest
from dirty_equals import IsPartialDict
from prisma import Prisma
from prisma.enums import Role
from prisma.models import Types
from prisma._compat import PYDANTIC_V2, model_json_schema
@pytest.mark.asyncio
async def test_enum_create(client: Prisma) -> None:
"""Creating a record with an enum value"""
record = await client.types.create({})
assert record.enum == Role.USER
record = await client.types.create({'enum': Role.ADMIN})
assert record.enum == Role.ADMIN
# ensure consistent format
assert str(record.enum) == 'ADMIN'
assert f'{record.enum}' == 'ADMIN'
assert '%s' % record.enum == 'ADMIN'
assert str(Role.ADMIN) == 'ADMIN'
assert f'{Role.ADMIN}' == 'ADMIN'
assert '%s' % Role.ADMIN == 'ADMIN'
# TODO: all other actions
@pytest.mark.asyncio
async def test_id5(client: Prisma) -> None:
"""Combined ID constraint with an Enum field"""
model = await client.id5.create(
data={
'name': 'Robert',
'role': Role.ADMIN,
},
)
found = await client.id5.find_unique(
where={
'name_role': {
'name': 'Robert',
'role': Role.ADMIN,
},
},
)
assert found is not None
assert found.name == model.name
assert found.role == Role.ADMIN
found = await client.id5.find_unique(
where={
'name_role': {
'name': 'Robert',
'role': Role.USER,
},
},
)
assert found is None
@pytest.mark.asyncio
async def test_unique6(client: Prisma) -> None:
"""Combined unique constraint with an Enum field"""
model = await client.unique6.create(
data={
'name': 'Robert',
'role': Role.ADMIN,
},
)
found = await client.unique6.find_unique(
where={
'name_role': {
'name': 'Robert',
'role': Role.ADMIN,
},
},
)
assert found is not None
assert found.name == model.name
assert found.role == Role.ADMIN
found = await client.unique6.find_unique(
where={
'name_role': {
'name': 'Robert',
'role': Role.USER,
},
},
)
assert found is None
def test_json_schema() -> None:
"""Ensure a JSON Schema definition can be created"""
defs = {
'Role': IsPartialDict(
{
'title': 'Role',
'enum': ['USER', 'ADMIN', 'EDITOR'],
'type': 'string',
}
)
}
if PYDANTIC_V2:
assert model_json_schema(Types) == IsPartialDict(
{
'$defs': defs,
'properties': IsPartialDict(
{
'enum': {
'$ref': '#/$defs/Role',
}
}
),
},
)
else:
assert model_json_schema(Types) == IsPartialDict(
definitions=defs,
properties=IsPartialDict(
{
'enum': {
'$ref': '#/definitions/Role',
}
}
),
)