Skip to content

Commit 2c3ad13

Browse files
committed
test: run db pull e2e test also for postgres
1 parent 93a400a commit 2c3ad13

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { createProject, runCli } from '../utils';
66
const getSchema = (workDir: string) => fs.readFileSync(path.join(workDir, 'zenstack/schema.zmodel')).toString();
77

88
describe('DB pull', () => {
9-
it('sqlite schema', () => {
10-
const workDir = createProject(`
11-
model User {
9+
it('simple schema', () => {
10+
const workDir = createProject(
11+
`model User {
1212
id String @id @default(cuid())
1313
email String @unique @map("email_address")
1414
name String? @default("Anonymous")

packages/cli/test/utils.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
1-
import { createTestProject } from '@zenstackhq/testtools';
1+
import { createTestProject, getTestDbProvider } from '@zenstackhq/testtools';
2+
import { createHash } from 'node:crypto';
23
import { execSync } from 'node:child_process';
34
import fs from 'node:fs';
45
import path from 'node:path';
6+
import { expect } from 'vitest';
57

6-
const ZMODEL_PRELUDE = `datasource db {
7-
provider = "sqlite"
8-
url = "file:./dev.db"
9-
}`;
8+
const TEST_PG_CONFIG = {
9+
host: process.env['TEST_PG_HOST'] ?? 'localhost',
10+
port: process.env['TEST_PG_PORT'] ? parseInt(process.env['TEST_PG_PORT']) : 5432,
11+
user: process.env['TEST_PG_USER'] ?? 'postgres',
12+
password: process.env['TEST_PG_PASSWORD'] ?? 'postgres',
13+
};
14+
15+
function getTestDbName(provider: string) {
16+
if (provider === 'sqlite') {
17+
return './test.db';
18+
}
19+
const testName = expect.getState().currentTestName ?? 'unnamed';
20+
const testPath = expect.getState().testPath ?? '';
21+
// digest test name
22+
const digest = createHash('md5')
23+
.update(testName + testPath)
24+
.digest('hex');
25+
// compute a database name based on test name
26+
return (
27+
'test_' +
28+
testName
29+
.toLowerCase()
30+
.replace(/[^a-z0-9_]/g, '_')
31+
.replace(/_+/g, '_')
32+
.substring(0, 30) +
33+
digest.slice(0, 6)
34+
);
35+
}
1036

1137
export function createProject(zmodel: string, addPrelude = true) {
38+
const provider = getTestDbProvider() ?? 'sqlite';
39+
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}`;
44+
45+
const ZMODEL_PRELUDE = `datasource db {
46+
provider = "${provider}"
47+
url = "${dbUrl}"
48+
}
49+
`;
1250
const workDir = createTestProject();
1351
fs.mkdirSync(path.join(workDir, 'zenstack'), { recursive: true });
1452
const schemaPath = path.join(workDir, 'zenstack/schema.zmodel');

0 commit comments

Comments
 (0)