11/// <reference types="@types/jest" />
22
3- import { getDMMF } from '@prisma/internals' ;
43import OpenAPIParser from '@readme/openapi-parser' ;
5- import * as fs from 'fs ' ;
4+ import { loadZModelAndDmmf } from '@zenstackhq/testtools ' ;
65import * as tmp from 'tmp' ;
7- import { loadDocument } from 'zenstack/cli/cli-util' ;
8- import prismaPlugin from 'zenstack/plugins/prisma' ;
96import generate from '../src' ;
107
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-
338describe ( 'Open API Plugin Tests' , ( ) => {
349 it ( 'run plugin' , async ( ) => {
3510 const { model, dmmf, modelFile } = await loadZModelAndDmmf ( `
36- plugin openapi {
37- provider = '${ process . cwd ( ) } /dist'
38- }
11+ plugin openapi {
12+ provider = '${ process . cwd ( ) } /dist'
13+ }
3914
40- enum Role {
41- USER
42- ADMIN
43- }
15+ enum Role {
16+ USER
17+ ADMIN
18+ }
4419
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+ }
8341
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+ }
8658
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+ }
8968 ` ) ;
9069
9170 const { name : output } = tmp . fileSync ( { postfix : '.yaml' } ) ;
@@ -102,5 +81,6 @@ describe('Open API Plugin Tests', () => {
10281 expect ( api . paths ?. [ '/post/findMany' ] ) . toBeUndefined ( ) ;
10382
10483 expect ( api . paths ?. [ '/foo/findMany' ] ) . toBeUndefined ( ) ;
84+ expect ( api . paths ?. [ '/bar/findMany' ] ) . toBeUndefined ( ) ;
10585 } ) ;
10686} ) ;
0 commit comments