Skip to content

Commit c495027

Browse files
committed
fix: update test environment creation
1 parent 2c3ad13 commit c495027

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

packages/cli/test/db/pull.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { describe, expect, it } from 'vitest';
4-
import { createProject, runCli } from '../utils';
4+
import { createTestEnvironment, runCli } from '../utils';
55

66
const getSchema = (workDir: string) => fs.readFileSync(path.join(workDir, 'zenstack/schema.zmodel')).toString();
77

88
describe('DB pull', () => {
9-
it('simple schema', () => {
10-
const workDir = createProject(
11-
`model User {
9+
it('simple schema', async () => {
10+
const workDir = await createTestEnvironment(
11+
`model User {
1212
id String @id @default(cuid())
1313
email String @unique @map("email_address")
1414
name String? @default("Anonymous")
@@ -85,7 +85,8 @@ enum Role {
8585
USER
8686
ADMIN
8787
MODERATOR
88-
}`);
88+
}`,
89+
);
8990
runCli('format', workDir);
9091
runCli('db push', workDir);
9192

packages/cli/test/utils.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
import { createTestProject, getTestDbProvider } from '@zenstackhq/testtools';
2-
import { createHash } from 'node:crypto';
32
import { execSync } from 'node:child_process';
43
import fs from 'node:fs';
54
import path from 'node:path';
65
import { expect } from 'vitest';
6+
import { Client as PostgresClient } from 'pg';
7+
import { createHash } from 'node:crypto';
8+
9+
const ZMODEL_PRELUDE = `datasource db {
10+
provider = "sqlite"
11+
url = "file:./dev.db"
12+
}
13+
`;
14+
export function createProject(zmodel: string, addPrelude = true) {
15+
const workDir = createTestProject();
16+
fs.mkdirSync(path.join(workDir, 'zenstack'), { recursive: true });
17+
const schemaPath = path.join(workDir, 'zenstack/schema.zmodel');
18+
fs.writeFileSync(schemaPath, addPrelude ? `${ZMODEL_PRELUDE}\n\n${zmodel}` : zmodel);
19+
return workDir;
20+
}
721

822
const TEST_PG_CONFIG = {
923
host: process.env['TEST_PG_HOST'] ?? 'localhost',
@@ -34,24 +48,26 @@ function getTestDbName(provider: string) {
3448
);
3549
}
3650

37-
export function createProject(zmodel: string, addPrelude = true) {
51+
export async function createTestEnvironment(zmodel: string, addPrelude = true) {
3852
const provider = getTestDbProvider() ?? 'sqlite';
3953
const dbName = getTestDbName(provider);
4054
const dbUrl =
4155
provider === 'sqlite'
4256
? `file:${dbName}`
4357
: `postgres://${TEST_PG_CONFIG.user}:${TEST_PG_CONFIG.password}@${TEST_PG_CONFIG.host}:${TEST_PG_CONFIG.port}/${dbName}`;
4458

59+
if (provider === 'postgresql') {
60+
const pg = new PostgresClient({ connectionString: dbUrl });
61+
await pg.connect();
62+
await pg.query(`CREATE DATABASE ${dbName};`);
63+
await pg.end();
64+
}
4565
const ZMODEL_PRELUDE = `datasource db {
4666
provider = "${provider}"
4767
url = "${dbUrl}"
48-
}
49-
`;
50-
const workDir = createTestProject();
51-
fs.mkdirSync(path.join(workDir, 'zenstack'), { recursive: true });
52-
const schemaPath = path.join(workDir, 'zenstack/schema.zmodel');
53-
fs.writeFileSync(schemaPath, addPrelude ? `${ZMODEL_PRELUDE}\n${zmodel}` : zmodel);
54-
return workDir;
68+
}`;
69+
70+
return createProject(addPrelude ? `${ZMODEL_PRELUDE}\n${zmodel}` : zmodel, false);
5571
}
5672

5773
export function runCli(command: string, cwd: string) {

0 commit comments

Comments
 (0)