Skip to content

Commit 74dffda

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

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
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: 28 additions & 11 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,27 @@ 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);
40-
const dbUrl =
41-
provider === 'sqlite'
42-
? `file:${dbName}`
43-
: `postgres://${TEST_PG_CONFIG.user}:${TEST_PG_CONFIG.password}@${TEST_PG_CONFIG.host}:${TEST_PG_CONFIG.port}/${dbName}`;
54+
const testPg = `postgres://${TEST_PG_CONFIG.user}:${TEST_PG_CONFIG.password}@${TEST_PG_CONFIG.host}:${TEST_PG_CONFIG.port}`;
55+
const dbUrl = provider === 'sqlite' ? `file:${dbName}` : `${testPg}/${dbName}`;
4456

57+
if (provider === 'postgresql') {
58+
const pg = new PostgresClient({
59+
connectionString: `${testPg}/postgres`,
60+
});
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}"
4868
}
4969
`;
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;
70+
71+
return createProject(addPrelude ? `${ZMODEL_PRELUDE}\n${zmodel}` : zmodel, false);
5572
}
5673

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

0 commit comments

Comments
 (0)