Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 3482534

Browse files
committed
test(cli): support datasource extras in test utils
Enhances the test utility helpers to allow passing extra datasource properties, such as multi-schema configurations for PostgreSQL. Refactors existing database pull tests to use these extra properties, ensuring the generated ZModel schema correctly reflects multi-schema environments while simplifying assertions.
1 parent 9dcf2ab commit 3482534

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

packages/cli/src/actions/pull/utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ export function getDatasource(model: Model) {
6464

6565
const schemasField = datasource.fields.find((f) => f.name === 'schemas');
6666
const schemas =
67-
(schemasField &&
68-
getLiteralArray(schemasField.value)
69-
?.map(getStringLiteral)
70-
.filter((s) => s !== undefined)) ||
67+
(schemasField &&
68+
getLiteralArray(schemasField.value)
69+
?.filter((s) => s !== undefined)) as string[] ||
7170
[];
72-
71+
7372
return {
7473
name: datasource.name,
7574
provider: getStringLiteral(

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,21 +353,19 @@ model Post {
353353
354354
@@schema("content")
355355
}`,
356-
{ provider: 'postgresql' },
356+
{ provider: 'postgresql', extra:{ schemas: ["public", "content", "auth"] } },
357357
);
358358
runCli('db push', workDir);
359359

360360
const schemaFile = path.join(workDir, 'zenstack/schema.zmodel');
361361
const { model } = await loadSchemaDocument(schemaFile, { returnServices: true });
362362
const expectedSchema = generator.generate(model);
363363

364-
fs.writeFileSync(schemaFile, getDefaultPrelude({ provider: 'postgresql' }));
364+
fs.writeFileSync(schemaFile, getDefaultPrelude({ provider: 'postgresql', extra:{ schemas: ["public", "content", "auth"]} }));
365365
runCli('db pull --indent 4', workDir);
366366

367367
const restoredSchema = getSchema(workDir);
368368
expect(restoredSchema).toEqual(expectedSchema);
369-
expect(restoredSchema).toContain('@@schema("auth")');
370-
expect(restoredSchema).toContain('@@schema("content")');
371369
});
372370

373371
it('should preserve native PostgreSQL enums when schema exists', async ({ skip }) => {
@@ -404,8 +402,6 @@ enum UserRole {
404402
const pulledSchema = getSchema(workDir);
405403

406404
expect(pulledSchema).toEqual(originalSchema);
407-
expect(pulledSchema).toContain('enum UserStatus');
408-
expect(pulledSchema).toContain('enum UserRole');
409405
});
410406

411407
it('should not modify schema with PostgreSQL-specific features', async ({ skip }) => {
@@ -442,7 +438,7 @@ enum UserStatus {
442438
INACTIVE
443439
SUSPENDED
444440
}`,
445-
{ provider: 'postgresql' },
441+
{ provider: 'postgresql', extra:{ schemas: ["public", "content", "auth"] } },
446442
);
447443
runCli('db push', workDir);
448444

packages/cli/test/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function getTestDbName(provider: string) {
4242
);
4343
}
4444

45-
export function getDefaultPrelude(options?: { provider?: 'sqlite' | 'postgresql' | 'mysql' }) {
45+
export function getDefaultPrelude(options?: { provider?: 'sqlite' | 'postgresql' | 'mysql', extra?: Record<string, string | string[]> }) {
4646
const provider = (options?.provider || getTestDbProvider()) ?? 'sqlite';
4747
const dbName = getTestDbName(provider);
4848
let dbUrl: string;
@@ -60,10 +60,10 @@ export function getDefaultPrelude(options?: { provider?: 'sqlite' | 'postgresql'
6060
default:
6161
throw new Error(`Unsupported provider: ${provider}`);
6262
}
63-
63+
const DATASOURCE_EXTRAS = Object.entries(options?.extra || {}).map(([k, v]) => ` ${k} = ${JSON.stringify(v)}`).join('\n');
6464
const ZMODEL_PRELUDE = `datasource db {
6565
provider = "${provider}"
66-
url = "${dbUrl}"
66+
url = "${dbUrl}"${DATASOURCE_EXTRAS.length > 0 ? '\n' + DATASOURCE_EXTRAS : ''}
6767
}`;
6868
return ZMODEL_PRELUDE;
6969
}
@@ -81,9 +81,9 @@ export function createProject(
8181

8282
export async function createFormattedProject(
8383
zmodel: string,
84-
options?: { provider?: 'sqlite' | 'postgresql' | 'mysql' },
84+
options?: { provider?: 'sqlite' | 'postgresql' | 'mysql', extra?: Record<string, string | string[]> },
8585
) {
86-
const fullContent = `${getDefaultPrelude({ provider: options?.provider })}\n\n${zmodel}`;
86+
const fullContent = `${getDefaultPrelude({ provider: options?.provider, extra: options?.extra })}\n\n${zmodel}`;
8787
const formatted = await formatDocument(fullContent);
8888
return createProject(formatted, { customPrelude: true, provider: options?.provider });
8989
}

0 commit comments

Comments
 (0)