1
1
/// <reference types="@types/jest" />
2
2
3
- import { getDMMF } from '@prisma/internals' ;
4
3
import OpenAPIParser from '@readme/openapi-parser' ;
5
- import * as fs from 'fs ' ;
4
+ import { loadZModelAndDmmf } from '@zenstackhq/testtools ' ;
6
5
import * as tmp from 'tmp' ;
7
- import { loadDocument } from 'zenstack/cli/cli-util' ;
8
- import prismaPlugin from 'zenstack/plugins/prisma' ;
9
6
import generate from '../src' ;
10
7
11
- async function loadZModelAndDmmf ( content : string ) {
12
- const prelude = `
13
- datasource db {
14
- provider = 'postgresql'
15
- url = env('DATABASE_URL')
16
- }
17
- ` ;
18
-
19
- const { name : modelFile } = tmp . fileSync ( { postfix : '.zmodel' } ) ;
20
- fs . writeFileSync ( modelFile , `${ prelude } \n${ content } ` ) ;
21
-
22
- const model = await loadDocument ( modelFile ) ;
23
-
24
- const { name : prismaFile } = tmp . fileSync ( { postfix : '.prisma' } ) ;
25
- await prismaPlugin ( model , { schemaPath : modelFile , output : prismaFile , generateClient : false } ) ;
26
-
27
- const prismaContent = fs . readFileSync ( prismaFile , { encoding : 'utf-8' } ) ;
28
-
29
- const dmmf = await getDMMF ( { datamodel : prismaContent } ) ;
30
- return { model, dmmf, modelFile } ;
31
- }
32
-
33
8
describe ( 'Open API Plugin Tests' , ( ) => {
34
9
it ( 'run plugin' , async ( ) => {
35
10
const { model, dmmf, modelFile } = await loadZModelAndDmmf ( `
36
- plugin openapi {
37
- provider = '${ process . cwd ( ) } /dist'
38
- }
11
+ plugin openapi {
12
+ provider = '${ process . cwd ( ) } /dist'
13
+ }
39
14
40
- enum Role {
41
- USER
42
- ADMIN
43
- }
15
+ enum Role {
16
+ USER
17
+ ADMIN
18
+ }
44
19
45
- model User {
46
- id String @id
47
- createdAt DateTime @default(now())
48
- updatedAt DateTime @updatedAt
49
- email String @unique
50
- role Role @default(USER)
51
- posts Post[]
52
-
53
- @@openapi.meta({
54
- findMany: {
55
- description: 'Find users matching the given conditions'
56
- },
57
- delete: {
58
- method: 'put',
59
- path: 'dodelete',
60
- description: 'Delete a unique user',
61
- summary: 'Delete a user yeah yeah',
62
- tags: ['delete', 'user']
63
- },
64
- })
65
- }
66
-
67
- model Post {
68
- id String @id
69
- createdAt DateTime @default(now())
70
- updatedAt DateTime @updatedAt
71
- title String
72
- author User? @relation(fields: [authorId], references: [id])
73
- authorId String?
74
- published Boolean @default(false)
75
- viewCount Int @default(0)
76
-
77
- @@openapi.meta({
78
- findMany: {
79
- ignore: true
80
- }
81
- })
82
- }
20
+ model User {
21
+ id String @id
22
+ createdAt DateTime @default(now())
23
+ updatedAt DateTime @updatedAt
24
+ email String @unique
25
+ role Role @default(USER)
26
+ posts Post[]
27
+
28
+ @@openapi.meta({
29
+ findMany: {
30
+ description: 'Find users matching the given conditions'
31
+ },
32
+ delete: {
33
+ method: 'put',
34
+ path: 'dodelete',
35
+ description: 'Delete a unique user',
36
+ summary: 'Delete a user yeah yeah',
37
+ tags: ['delete', 'user']
38
+ },
39
+ })
40
+ }
83
41
84
- model Foo {
85
- id String @id
42
+ model Post {
43
+ id String @id
44
+ createdAt DateTime @default(now())
45
+ updatedAt DateTime @updatedAt
46
+ title String
47
+ author User? @relation(fields: [authorId], references: [id])
48
+ authorId String?
49
+ published Boolean @default(false)
50
+ viewCount Int @default(0)
51
+
52
+ @@openapi.meta({
53
+ findMany: {
54
+ ignore: true
55
+ }
56
+ })
57
+ }
86
58
87
- @@openapi.ignore
88
- }
59
+ model Foo {
60
+ id String @id
61
+ @@openapi.ignore
62
+ }
63
+
64
+ model Bar {
65
+ id String @id
66
+ @@ignore
67
+ }
89
68
` ) ;
90
69
91
70
const { name : output } = tmp . fileSync ( { postfix : '.yaml' } ) ;
@@ -102,5 +81,6 @@ describe('Open API Plugin Tests', () => {
102
81
expect ( api . paths ?. [ '/post/findMany' ] ) . toBeUndefined ( ) ;
103
82
104
83
expect ( api . paths ?. [ '/foo/findMany' ] ) . toBeUndefined ( ) ;
84
+ expect ( api . paths ?. [ '/bar/findMany' ] ) . toBeUndefined ( ) ;
105
85
} ) ;
106
86
} ) ;
0 commit comments