Coverage for tests/test_generation/test_assumptions.py: 100%
40 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 subprocess
3import pytest
5from prisma.utils import temp_env_update
7from ..utils import Testdir
10def test_one_datasource_allowed(testdir: Testdir) -> None:
11 """Prisma only allows one datasource"""
12 schema = (
13 testdir.SCHEMA_HEADER
14 + """
15 datasource db2 {{
16 provider = "sqlite"
17 url = "file:dev-2.db"
18 }}
19 """
20 )
22 with pytest.raises(subprocess.CalledProcessError) as exc:
23 testdir.generate(schema=schema)
25 output = exc.value.output.decode('utf-8')
26 assert (
27 'You defined more than one datasource. '
28 'This is not allowed yet because support for multiple databases '
29 'has not been implemented yet' in output
30 )
33def test_can_generate_from_env_var(testdir: Testdir) -> None:
34 """Prisma generator output can be resolved from an env variable"""
35 schema = """
36 datasource db {{
37 provider = "sqlite"
38 url = "file:dev.db"
39 }}
41 // default output: {output}
42 generator db {{
43 provider = "coverage run -m prisma"
44 output = env("PRISMA_TEST_ASSUMPTIONS_OUTPUT")
45 {options}
46 }}
48 model User {{
49 id Int @id @default(autoincrement())
50 email String @unique
51 name String?
52 }}
53 """
55 with temp_env_update({'PRISMA_TEST_ASSUMPTIONS_OUTPUT': str(testdir.path / 'prisma')}):
56 testdir.generate(schema=schema)
59def test_relational_field_cannot_be_unique(testdir: Testdir) -> None:
60 """Prisma does not allow relational fields to be unique"""
61 schema = (
62 testdir.SCHEMA_HEADER
63 + """
64 model User {{
65 id Int @id @default(autoincrement())
66 name String
67 posts Post[]
68 }}
70 model Post {{
71 id Int @id @default(autoincrement())
72 author User @relation(fields: [author_id], references: [id]) @unique
73 author_id Int
74 }}
75 """
76 )
78 with pytest.raises(subprocess.CalledProcessError) as exc:
79 testdir.generate(schema=schema)
81 output = exc.value.output.decode('utf-8')
82 assert 'The field `author` is a relation field and cannot be marked with `unique`.' in output
85def test_enum_same_name_as_model_disallowed(testdir: Testdir) -> None:
86 """Ensure an Enum cannot be defined with the same name as a model"""
87 schema = (
88 testdir.SCHEMA_HEADER
89 + """
90 model User {{
91 id Int @id @default(autoincrement())
92 name String
93 }}
95 enum User {{
96 FOO
97 BAR
98 }}
99 """
100 )
102 with pytest.raises(subprocess.CalledProcessError) as exc:
103 testdir.generate(schema=schema)
105 output = exc.value.output.decode('utf-8')
106 assert 'The enum "User" cannot be defined because a model with that name already exists.' in output
109def test_multiple_compund_ids_disallowed(testdir: Testdir) -> None:
110 """Multiple @@id() annotations are not allowed on the same model"""
111 schema = (
112 testdir.SCHEMA_HEADER
113 + """
114 model User {{
115 name String
116 surname String
117 points Int
118 email String
120 @@id([name, surname])
121 @@id([points, email])
122 }}
123 """
124 )
126 with pytest.raises(subprocess.CalledProcessError) as exc:
127 testdir.generate(schema=schema)
129 output = exc.value.output.decode('utf-8')
130 assert 'Attribute "@id" can only be defined once.' in output
133def test_compound_unique_constraint_implicit_field_shaddowing(
134 testdir: Testdir,
135) -> None:
136 """Compound unique constraints cannot implicitly have the same name as an already defined field
138 https://github.com/prisma/prisma/issues/10456
139 """
140 schema = (
141 testdir.SCHEMA_HEADER
142 + """
143 model User {{
144 name String
145 surname String
146 name_surname String
148 @@unique([name, surname])
149 }}
150 """
151 )
152 with pytest.raises(subprocess.CalledProcessError) as exc:
153 testdir.generate(schema=schema)
155 assert (
156 'The field `name_surname` clashes with the `@@unique` name. '
157 'Please resolve the conflict by providing a custom id name: `@@unique([...], name: "custom_name")`'
158 ) in str(exc.value.output, 'utf-8')
161def test_compound_id_implicit_field_shaddowing(testdir: Testdir) -> None:
162 """Compound IDs cannot implicitly have the same name as an already defined field
164 https://github.com/prisma/prisma/issues/10456
165 """
166 schema = (
167 testdir.SCHEMA_HEADER
168 + """
169 model User {{
170 name String
171 surname String
172 name_surname String
174 @@id([name, surname])
175 }}
176 """
177 )
178 with pytest.raises(subprocess.CalledProcessError) as exc:
179 testdir.generate(schema=schema)
181 assert (
182 "The field `name_surname` clashes with the `@@id` attribute's name. "
183 'Please resolve the conflict by providing a custom id name: `@@id([...], name: "custom_name")`'
184 ) in str(exc.value.output, 'utf-8')