/
getDmmf.test.ts
376 lines (332 loc) · 10.1 KB
/
getDmmf.test.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import fs from 'fs'
import path from 'path'
import stripAnsi from 'strip-ansi'
import { getDMMF } from '../..'
import { fixturesPath } from '../__utils__/fixtures'
jest.setTimeout(10_000)
if (process.env.CI) {
// 10s is not always enough for the "big schema" test on macOS CI.
jest.setTimeout(60_000)
}
const describeIf = (condition: boolean) => (condition ? describe : describe.skip)
describeIf(
process.env.PRISMA_CLI_QUERY_ENGINE_TYPE === 'library' || process.env.PRISMA_CLI_QUERY_ENGINE_TYPE === undefined,
)('getDMMF', () => {
test('simple model, no datasource', async () => {
const dmmf = await getDMMF({
datamodel: `model A {
id Int @id
name String
}`,
})
expect(dmmf.datamodel).toMatchSnapshot()
expect(dmmf).toMatchSnapshot()
})
test('simple model, sqlite', async () => {
const dmmf = await getDMMF({
datamodel: `
datasource db {
provider = "sqlite"
url = "file:dev.db"
}
model A {
id Int @id
name String
}`,
})
expect(dmmf.datamodel).toMatchSnapshot()
expect(dmmf).toMatchSnapshot()
})
test('simple model, postgresql', async () => {
const dmmf = await getDMMF({
datamodel: `
datasource db {
provider = "postgresql"
url = env("MY_POSTGRESQL_DB")
}
model A {
id Int @id
name String
}`,
})
expect(dmmf.datamodel).toMatchSnapshot()
expect(dmmf).toMatchSnapshot()
})
test('simple model, mysql', async () => {
const dmmf = await getDMMF({
datamodel: `
datasource db {
provider = "mysql"
url = env("MY_MYSQL_DB")
}
model A {
id Int @id
name String
}`,
})
expect(dmmf.datamodel).toMatchSnapshot()
expect(dmmf).toMatchSnapshot()
})
test('simple model, sql server', async () => {
const dmmf = await getDMMF({
datamodel: `
datasource db {
provider = "sqlserver"
url = env("MY_SQLSERVER_DB")
}
model A {
id Int @id
name String
}`,
})
expect(dmmf.datamodel).toMatchSnapshot()
expect(dmmf).toMatchSnapshot()
})
test('simple model, mongodb', async () => {
const dmmf = await getDMMF({
datamodel: `
datasource db {
provider = "mongodb"
url = "MY_MONGODB_DB"
}
model A {
id Int @id @map("_id")
name String
}`,
})
expect(dmmf.datamodel).toMatchSnapshot()
expect(dmmf).toMatchSnapshot()
})
test('@@map model', async () => {
const dmmf = await getDMMF({
datamodel: `
datasource db {
provider = "postgresql"
url = env("MY_POSTGRESQL_DB")
}
model User {
id Int @default(autoincrement())
email String @unique
@@map("users")
}`,
})
expect(dmmf.datamodel).toMatchSnapshot()
expect(dmmf).toMatchSnapshot()
})
test('model with autoincrement should fail if sqlite', async () => {
expect.assertions(1)
const datamodel = `
datasource db {
provider = "sqlite"
url = "file:dev.db"
}
model User {
id Int @default(autoincrement())
email String @unique
@@map("users")
}`
try {
await getDMMF({ datamodel })
} catch (e) {
expect(stripAnsi(e.message)).toMatchInlineSnapshot(`
"Get DMMF: Schema parsing - Error while interacting with query-engine-node-api library
Error code: P1012
error: Error parsing attribute \\"@default\\": The \`autoincrement()\` default value is used on a non-id field even though the datasource does not support this.
--> schema.prisma:7
|
6 | model User {
7 | id Int @default(autoincrement())
8 | email String @unique
|
error: Error parsing attribute \\"@default\\": The \`autoincrement()\` default value is used on a non-indexed field even though the datasource does not support this.
--> schema.prisma:7
|
6 | model User {
7 | id Int @default(autoincrement())
8 | email String @unique
|
Validation Error Count: 2
Prisma CLI Version : 0.0.0"
`)
}
})
test('model with autoincrement should fail if mysql', async () => {
expect.assertions(1)
const datamodel = `
datasource db {
provider = "mysql"
url = env("MY_MYSQL_DB")
}
model User {
id Int @default(autoincrement())
email String @unique
@@map("users")
}`
try {
await getDMMF({ datamodel })
} catch (e) {
expect(stripAnsi(e.message)).toMatchInlineSnapshot(`
"Get DMMF: Schema parsing - Error while interacting with query-engine-node-api library
Error code: P1012
error: Error parsing attribute \\"@default\\": The \`autoincrement()\` default value is used on a non-indexed field even though the datasource does not support this.
--> schema.prisma:7
|
6 | model User {
7 | id Int @default(autoincrement())
8 | email String @unique
|
Validation Error Count: 1
Prisma CLI Version : 0.0.0"
`)
}
})
test('@@unique model', async () => {
const dmmf = await getDMMF({
datamodel: `
datasource db {
provider = "postgres"
url = env("MY_POSTGRES_DB")
}
// From https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-model#examples-3
// Specify a multi-field unique attribute that includes a relation field
model Post {
id Int @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
title String
published Boolean @default(false)
@@unique([authorId, title])
}
model User {
id Int @id @default(autoincrement())
email String @unique
posts Post[]
}
// Specify a multi-field unique attribute on two String fields
model User1 {
id Int @default(autoincrement())
firstName String
lastName String
isAdmin Boolean @default(false)
@@unique([firstName, lastName])
}
// Specify a multi-field unique attribute on two String fields and one Boolean field
model User2 {
id Int @default(autoincrement())
firstName String
lastName String
isAdmin Boolean @default(false)
@@unique([firstName, lastName, isAdmin])
}
`,
})
expect(dmmf).toMatchSnapshot()
})
test('@@id model', async () => {
const dmmf = await getDMMF({
datamodel: `
datasource db {
provider = "postgres"
url = env("MY_POSTGRES_DB")
}
generator client {
provider = "prisma-client-js"
}
model User1 {
id Int @default(autoincrement())
firstName String
lastName String
isAdmin Boolean @default(false)
@@id(fields: [firstName, lastName], name: "customName") // with name
}
// Specify a multi-field id attribute on two String fields and one Boolean field
model User2 {
id Int @default(autoincrement())
firstName String
lastName String
isAdmin Boolean @default(false)
@@id([firstName, lastName, isAdmin])
}
`,
})
expect(dmmf).toMatchSnapshot()
})
test('chinook introspected schema', async () => {
const file = fs.readFileSync(path.join(fixturesPath, 'chinook.prisma'), 'utf-8')
const dmmf = await getDMMF({
datamodel: file,
})
const str = JSON.stringify(dmmf)
expect(str.length).toMatchSnapshot()
})
test('big schema', async () => {
const file = fs.readFileSync(path.join(fixturesPath, 'bigschema.prisma'), 'utf-8')
const dmmf = await getDMMF({
datamodel: file,
})
const str = JSON.stringify(dmmf)
expect(str.length).toMatchSnapshot()
})
test('with validation errors', async () => {
const datamodel = `generator client {
provider = "prisma-client-js"
}
datasource my_db {
provider = "sqlite"
url = "file:dev.db"
}
model User {
id String @id @default(cuid())
id String @id @default(cuid())
name String
email String @unique
status String @default("")
permissions Permission @default()
permissions Permission @default("")
posts Post[]
posts Post[]
}
model Post {
id String @id @default(cuid())
name String
email String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
enum Permission {
ADMIN
USER
OWNER
COLLABORATOR
}
`
try {
await getDMMF({ datamodel })
} catch (e) {
expect(stripAnsi(e.message)).toMatchInlineSnapshot(`
"Get DMMF: Schema parsing - Error while interacting with query-engine-node-api library
Error code: P1012
error: Field \\"id\\" is already defined on model \\"User\\".
--> schema.prisma:12
|
11 | id String @id @default(cuid())
12 | id String @id @default(cuid())
|
error: Field \\"permissions\\" is already defined on model \\"User\\".
--> schema.prisma:17
|
16 | permissions Permission @default()
17 | permissions Permission @default(\\"\\")
|
error: Field \\"posts\\" is already defined on model \\"User\\".
--> schema.prisma:19
|
18 | posts Post[]
19 | posts Post[]
|
Validation Error Count: 3
Prisma CLI Version : 0.0.0"
`)
}
})
})