Skip to content

Commit 17edb48

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

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,55 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { describe, expect, it } from 'vitest';
44
import { createProject, runCli } from '../utils';
5+
import { getTestDbProvider } from '@zenstackhq/testtools';
6+
import { createHash } from 'node:crypto';
57

68
const 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-z0-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+
846
describe('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+
}
1154
model 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

Comments
 (0)