@@ -2,12 +2,55 @@ import fs from 'node:fs';
22import path from 'node:path' ;
33import { describe , expect , it } from 'vitest' ;
44import { createProject , runCli } from '../utils' ;
5+ import { getTestDbProvider } from '@zenstackhq/testtools' ;
6+ import { createHash } from 'node:crypto' ;
57
68const getSchema = ( workDir : string ) => fs . readFileSync ( path . join ( workDir , 'zenstack/schema.zmodel' ) ) . toString ( ) ;
79
10+ const TEST_PG_CONFIG = {
11+ host : process . env [ 'TEST_PG_HOST' ] ?? 'localhost' ,
12+ port : process . env [ 'TEST_PG_PORT' ] ? parseInt ( process . env [ 'TEST_PG_PORT' ] ) : 5432 ,
13+ user : process . env [ 'TEST_PG_USER' ] ?? 'postgres' ,
14+ password : process . env [ 'TEST_PG_PASSWORD' ] ?? 'postgres' ,
15+ } ;
16+
17+ function getTestDbName ( provider : string ) {
18+ if ( provider === 'sqlite' ) {
19+ return './test.db' ;
20+ }
21+ const testName = expect . getState ( ) . currentTestName ?? 'unnamed' ;
22+ const testPath = expect . getState ( ) . testPath ?? '' ;
23+ // digest test name
24+ const digest = createHash ( 'md5' )
25+ . update ( testName + testPath )
26+ . digest ( 'hex' ) ;
27+ // compute a database name based on test name
28+ return (
29+ 'test_' +
30+ testName
31+ . toLowerCase ( )
32+ . replace ( / [ ^ a - z 0 - 9 _ ] / g, '_' )
33+ . replace ( / _ + / g, '_' )
34+ . substring ( 0 , 30 ) +
35+ digest . slice ( 0 , 6 )
36+ ) ;
37+ }
38+
39+ const provider = getTestDbProvider ( ) ?? 'sqlite' ;
40+ const dbName = getTestDbName ( provider ) ;
41+ const dbUrl =
42+ provider === 'sqlite'
43+ ? `file:${ dbName } `
44+ : `postgres://${ TEST_PG_CONFIG . user } :${ TEST_PG_CONFIG . password } @${ TEST_PG_CONFIG . host } :${ TEST_PG_CONFIG . port } /${ dbName } ` ;
45+
846describe ( 'DB pull' , ( ) => {
9- it ( 'sqlite schema' , ( ) => {
10- const workDir = createProject ( `
47+ it ( 'simple schema' , ( ) => {
48+ const workDir = createProject (
49+ `
50+ datasource db {
51+ provider = "${ provider } "
52+ url = "${ dbUrl } "
53+ }
1154model User {
1255 id String @id @default(cuid())
1356 email String @unique @map("email_address")
@@ -85,7 +128,9 @@ enum Role {
85128 USER
86129 ADMIN
87130 MODERATOR
88- }` ) ;
131+ }` ,
132+ false ,
133+ ) ;
89134 runCli ( 'format' , workDir ) ;
90135 runCli ( 'db push' , workDir ) ;
91136
0 commit comments