@@ -15,4 +15,47 @@ describe('CLI db commands test', () => {
1515 runCli ( 'db push' , workDir ) ;
1616 expect ( fs . existsSync ( path . join ( workDir , 'zenstack/dev.db' ) ) ) . toBe ( true ) ;
1717 } ) ;
18+
19+ it ( 'should seed the database with db seed with seed script' , ( ) => {
20+ const workDir = createProject ( model ) ;
21+ const pkgJson = JSON . parse ( fs . readFileSync ( path . join ( workDir , 'package.json' ) , 'utf8' ) ) ;
22+ pkgJson . zenstack = {
23+ seed : 'node seed.js' ,
24+ } ;
25+ fs . writeFileSync ( path . join ( workDir , 'package.json' ) , JSON . stringify ( pkgJson , null , 2 ) ) ;
26+ fs . writeFileSync (
27+ path . join ( workDir , 'seed.js' ) ,
28+ `
29+ import fs from 'node:fs';
30+ fs.writeFileSync('seed.txt', 'success');
31+ ` ,
32+ ) ;
33+
34+ runCli ( 'db seed' , workDir ) ;
35+ expect ( fs . readFileSync ( path . join ( workDir , 'seed.txt' ) , 'utf8' ) ) . toBe ( 'success' ) ;
36+ } ) ;
37+
38+ it ( 'should seed the database after migrate reset' , ( ) => {
39+ const workDir = createProject ( model ) ;
40+ const pkgJson = JSON . parse ( fs . readFileSync ( path . join ( workDir , 'package.json' ) , 'utf8' ) ) ;
41+ pkgJson . zenstack = {
42+ seed : 'node seed.js' ,
43+ } ;
44+ fs . writeFileSync ( path . join ( workDir , 'package.json' ) , JSON . stringify ( pkgJson , null , 2 ) ) ;
45+ fs . writeFileSync (
46+ path . join ( workDir , 'seed.js' ) ,
47+ `
48+ import fs from 'node:fs';
49+ fs.writeFileSync('seed.txt', 'success');
50+ ` ,
51+ ) ;
52+
53+ runCli ( 'migrate reset --force' , workDir ) ;
54+ expect ( fs . readFileSync ( path . join ( workDir , 'seed.txt' ) , 'utf8' ) ) . toBe ( 'success' ) ;
55+ } ) ;
56+
57+ it ( 'should skip seeding the database without seed script' , ( ) => {
58+ const workDir = createProject ( model ) ;
59+ runCli ( 'db seed' , workDir ) ;
60+ } ) ;
1861} ) ;
0 commit comments