Coverage for databases/tests/test_enum.py: 100%
41 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1import pytest
2from dirty_equals import IsPartialDict
4from prisma import Prisma
5from prisma.enums import Role
6from prisma.models import Types
7from prisma._compat import PYDANTIC_V2, model_json_schema
10@pytest.mark.asyncio
11async def test_enum_create(client: Prisma) -> None:
12 """Creating a record with an enum value"""
13 record = await client.types.create({})
14 assert record.enum == Role.USER
16 record = await client.types.create({'enum': Role.ADMIN})
17 assert record.enum == Role.ADMIN
19 # ensure consistent format
20 assert str(record.enum) == 'ADMIN'
21 assert f'{record.enum}' == 'ADMIN'
22 assert '%s' % record.enum == 'ADMIN'
24 assert str(Role.ADMIN) == 'ADMIN'
25 assert f'{Role.ADMIN}' == 'ADMIN'
26 assert '%s' % Role.ADMIN == 'ADMIN'
29# TODO: all other actions
32@pytest.mark.asyncio
33async def test_id5(client: Prisma) -> None:
34 """Combined ID constraint with an Enum field"""
35 model = await client.id5.create(
36 data={
37 'name': 'Robert',
38 'role': Role.ADMIN,
39 },
40 )
42 found = await client.id5.find_unique(
43 where={
44 'name_role': {
45 'name': 'Robert',
46 'role': Role.ADMIN,
47 },
48 },
49 )
50 assert found is not None
51 assert found.name == model.name
52 assert found.role == Role.ADMIN
54 found = await client.id5.find_unique(
55 where={
56 'name_role': {
57 'name': 'Robert',
58 'role': Role.USER,
59 },
60 },
61 )
62 assert found is None
65@pytest.mark.asyncio
66async def test_unique6(client: Prisma) -> None:
67 """Combined unique constraint with an Enum field"""
68 model = await client.unique6.create(
69 data={
70 'name': 'Robert',
71 'role': Role.ADMIN,
72 },
73 )
75 found = await client.unique6.find_unique(
76 where={
77 'name_role': {
78 'name': 'Robert',
79 'role': Role.ADMIN,
80 },
81 },
82 )
83 assert found is not None
84 assert found.name == model.name
85 assert found.role == Role.ADMIN
87 found = await client.unique6.find_unique(
88 where={
89 'name_role': {
90 'name': 'Robert',
91 'role': Role.USER,
92 },
93 },
94 )
95 assert found is None
98def test_json_schema() -> None:
99 """Ensure a JSON Schema definition can be created"""
100 defs = {
101 'Role': IsPartialDict(
102 {
103 'title': 'Role',
104 'enum': ['USER', 'ADMIN', 'EDITOR'],
105 'type': 'string',
106 }
107 )
108 }
110 if PYDANTIC_V2:
111 assert model_json_schema(Types) == IsPartialDict(
112 {
113 '$defs': defs,
114 'properties': IsPartialDict(
115 {
116 'enum': {
117 '$ref': '#/$defs/Role',
118 }
119 }
120 ),
121 },
122 )
123 else:
124 assert model_json_schema(Types) == IsPartialDict(
125 definitions=defs,
126 properties=IsPartialDict(
127 {
128 'enum': {
129 '$ref': '#/definitions/Role',
130 }
131 }
132 ),
133 )