Skip to content

Commit

Permalink
chore(utils): allow immutable in utils format (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 4, 2024
1 parent 3e02f32 commit 1a6e80f
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/utils/formatLines.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function formatLines(
lines: string[],
lines: ReadonlyArray<string>,
replace = ' ',
separator = ','
): string {
Expand Down
8 changes: 5 additions & 3 deletions src/utils/formatParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { applyType, escapeValue } from '.';
import type { FunctionParam } from '../operations/functionsTypes';
import type { MigrationOptions } from '../types';

function formatParam(mOptions: MigrationOptions) {
return (param: FunctionParam) => {
function formatParam(
mOptions: MigrationOptions
): (param: FunctionParam) => string {
return (param) => {
const {
mode,
name,
Expand Down Expand Up @@ -34,7 +36,7 @@ function formatParam(mOptions: MigrationOptions) {
}

export function formatParams(
params: FunctionParam[],
params: ReadonlyArray<FunctionParam>,
mOptions: MigrationOptions
): string {
return `(${params.map(formatParam(mOptions)).join(', ')})`;
Expand Down
15 changes: 15 additions & 0 deletions test/utils/formatLines.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ describe('utils', () => {
ADD CONSTRAINT "zipchk" EXCLUDE zipcode WITH = DEFERRABLE INITIALLY IMMEDIATE`);
});

it('should be immutable', () => {
const lines: ReadonlyArray<string> = Object.freeze([
'CONSTRAINT "zipchk" CHECK (char_length(zipcode) = 5) DEFERRABLE INITIALLY IMMEDIATE',
'CONSTRAINT "zipchk" CHECK (zipcode <> 0) DEFERRABLE INITIALLY IMMEDIATE',
'CONSTRAINT "zipchk" EXCLUDE zipcode WITH = DEFERRABLE INITIALLY IMMEDIATE',
]);
const actual = formatLines(lines, ' ADD ');

expect(actual).toBeTypeOf('string');
expect(actual)
.toBe(` ADD CONSTRAINT "zipchk" CHECK (char_length(zipcode) = 5) DEFERRABLE INITIALLY IMMEDIATE,
ADD CONSTRAINT "zipchk" CHECK (zipcode <> 0) DEFERRABLE INITIALLY IMMEDIATE,
ADD CONSTRAINT "zipchk" EXCLUDE zipcode WITH = DEFERRABLE INITIALLY IMMEDIATE`);
});

it('should replace newlines with a whitespace', () => {
const actual = formatLines(['a\r\nb', 'c\rd', 'e\nf']);

Expand Down
102 changes: 102 additions & 0 deletions test/utils/formatParam.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { describe, expect, it } from 'vitest';
import type { FunctionParam } from '../../src';
import { PgType } from '../../src';
import type { MigrationOptions } from '../../src/types';
import { formatParams } from '../../src/utils';
import { options1 } from '../presetMigrationOptions';

describe('utils', () => {
describe('formatParams', () => {
// TODO @Shinigami92 2024-04-04: Should this throw an error if empty array was passed?
it('should return empty braces', () => {
const actual = formatParams([], options1);

expect(actual).toBeTypeOf('string');
expect(actual).toBe('()');
});

it('should apply default shorthand', () => {
const actual = formatParams(
[
{ type: 'int' },
{ type: 'string' },
{ type: 'float' },
{ type: 'double' },
{ type: 'datetime' },
{ type: 'bool' },
],
options1
);

expect(actual).toBeTypeOf('string');
expect(actual).toBe(
'(integer, text, real, double precision, timestamp, boolean)'
);
});

it('should consume PgType value', () => {
const actual = formatParams([{ type: PgType.BYTEA }], options1);

expect(actual).toBeTypeOf('string');
expect(actual).toBe('(bytea)');
});

it('should consume simple string type params', () => {
const actual = formatParams(['int', 'bigint'], options1);

expect(actual).toBeTypeOf('string');
expect(actual).toBe('(integer, bigint)');
});

it('should consume complex object params', () => {
const actual = formatParams(
[
{
mode: 'OUT',
name: 'test',
type: PgType.BIGINT,
default: true,
},
{
mode: 'INOUT',
name: 'test2',
type: PgType.BOX,
default: false,
},
],
options1
);

expect(actual).toBeTypeOf('string');
expect(actual).toBe(
'(OUT "test" bigint DEFAULT true, INOUT "test2" box)'
);
});

it('should be immutable', () => {
const params: ReadonlyArray<FunctionParam> = Object.freeze([
'int',
PgType.BIGINT,
{
mode: 'OUT',
name: 'test',
type: 'bigint',
default: true,
},
{
mode: 'VARIADIC',
name: 'test2',
type: PgType.BOX,
default: false,
},
]);
const mOptions: Readonly<MigrationOptions> = Object.freeze(options1);
const actual = formatParams(params, mOptions);

expect(actual).toBeTypeOf('string');
expect(actual).toBe(
'(integer, bigint, OUT "test" bigint DEFAULT true, VARIADIC "test2" box)'
);
});
});
});

0 comments on commit 1a6e80f

Please sign in to comment.