Skip to content

Commit

Permalink
test: add columns unit tests (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 13, 2024
1 parent 84fb4bb commit e96f85d
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/operations/tables.ts
Expand Up @@ -481,7 +481,7 @@ export function dropColumns(mOptions: MigrationOptions): DropColumns {

const columnsStr = formatLines(
columns.map(mOptions.literal),
` DROP ${ifExists ? ' IF EXISTS' : ''}`,
` DROP ${ifExists ? 'IF EXISTS ' : ''}`,
`${cascade ? ' CASCADE' : ''},`
);

Expand Down
87 changes: 87 additions & 0 deletions test/operations/columns/addColumns.spec.ts
@@ -0,0 +1,87 @@
import { describe, expect, it } from 'vitest';
import { PgType } from '../../../src';
import { addColumns } from '../../../src/operations/tables';
import { options1 } from '../../utils';

describe('operations', () => {
describe('columns', () => {
describe('addColumns', () => {
const addColumnsFn = addColumns(options1);

it('should return a function', () => {
expect(addColumnsFn).toBeTypeOf('function');
});

it('should return sql statement', () => {
const statement = addColumnsFn('transactions', {
status: {
type: 'varchar(30)',
default: 'old',
},
mtime: {
type: PgType.TIMESTAMP_WITH_TIME_ZONE,
default: 'now()',
},
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(`ALTER TABLE "transactions"
ADD "status" varchar(30) DEFAULT $pga$old$pga$,
ADD "mtime" timestamp with time zone DEFAULT $pga$now()$pga$;`);
});

it('should return sql statement with columnOptions', () => {
const statement = addColumnsFn(
'transactions',
{
status: {
type: 'varchar(30)',
default: 'old',
},
},
{
ifNotExists: true,
}
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(`ALTER TABLE "transactions"
ADD IF NOT EXISTS "status" varchar(30) DEFAULT $pga$old$pga$;`);
});

it('should return sql statement with schema', () => {
const statement = addColumnsFn(
{ schema: 'myschema', name: 'distributors' },
{
status: PgType.VARCHAR,
}
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
`ALTER TABLE "myschema"."distributors"
ADD "status" varchar;`
);
});

describe('reverse', () => {
it('should contain a reverse function', () => {
expect(addColumnsFn.reverse).toBeTypeOf('function');
});

it('should return sql statement', () => {
const statement = addColumnsFn.reverse('transactions', {
status: {
type: 'varchar(30)',
default: 'old',
},
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(`ALTER TABLE "transactions"
DROP "status";`);
});
});
});
});
});
44 changes: 44 additions & 0 deletions test/operations/columns/alterColumn.spec.ts
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest';
import { alterColumn } from '../../../src/operations/tables';
import { options1 } from '../../utils';

describe('operations', () => {
describe('columns', () => {
describe('alterColumn', () => {
const alterColumnFn = alterColumn(options1);

it('should return a function', () => {
expect(alterColumnFn).toBeTypeOf('function');
});

it('should return sql statement', () => {
const statement = alterColumnFn('distributors', 'address', {});

expect(statement).toBeTypeOf('string');
expect(statement).toHaveLength(0);
});

it('should return sql statement with columnOptions', () => {
const statement = alterColumnFn('distributors', 'address', {
default: null,
type: 'varchar(30)',
collation: 'C',
using: 'address::text',
notNull: false,
sequenceGenerated: null,
comment: 'Address of the distributor',
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
`ALTER TABLE "distributors"
ALTER "address" DROP DEFAULT,
ALTER "address" SET DATA TYPE varchar(30) COLLATE C USING address::text,
ALTER "address" DROP NOT NULL,
ALTER "address" DROP IDENTITY;
COMMENT ON COLUMN "distributors"."address" IS $pga$Address of the distributor$pga$;`
);
});
});
});
});
58 changes: 58 additions & 0 deletions test/operations/columns/dropColumns.spec.ts
@@ -0,0 +1,58 @@
import { describe, expect, it } from 'vitest';
import { dropColumns } from '../../../src/operations/tables';
import { options1 } from '../../utils';

describe('operations', () => {
describe('columns', () => {
describe('dropColumns', () => {
const dropColumnsFn = dropColumns(options1);

it('should return a function', () => {
expect(dropColumnsFn).toBeTypeOf('function');
});

it('should return sql statement', () => {
const statement = dropColumnsFn('distributors', 'address');

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
`ALTER TABLE "distributors"
DROP "address";`
);
});

it('should return sql statement with dropOptions', () => {
const statement = dropColumnsFn(
'distributors',
{
address: 'varchar(30)',
},
{
ifExists: true,
cascade: true,
}
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(`ALTER TABLE "distributors"
DROP IF EXISTS "address";`);
});

it('should return sql statement with schema', () => {
const statement = dropColumnsFn(
{
schema: 'myschema',
name: 'distributors',
},
{
address: 'varchar(30)',
}
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(`ALTER TABLE "myschema"."distributors"
DROP "address";`);
});
});
});
});
56 changes: 56 additions & 0 deletions test/operations/columns/renameColumn.spec.ts
@@ -0,0 +1,56 @@
import { describe, expect, it } from 'vitest';
import { renameColumn } from '../../../src/operations/tables';
import { options1 } from '../../utils';

describe('operations', () => {
describe('columns', () => {
describe('renameColumn', () => {
const renameColumnFn = renameColumn(options1);

it('should return a function', () => {
expect(renameColumnFn).toBeTypeOf('function');
});

it('should return sql statement', () => {
const statement = renameColumnFn('distributors', 'address', 'city');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER TABLE "distributors" RENAME "address" TO "city";'
);
});

it('should return sql statement with schema', () => {
const statement = renameColumnFn(
{ name: 'distributors', schema: 'myschema' },
'address',
'city'
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER TABLE "myschema"."distributors" RENAME "address" TO "city";'
);
});

describe('reverse', () => {
it('should contain a reverse function', () => {
expect(renameColumnFn.reverse).toBeTypeOf('function');
});

it('should return sql statement', () => {
const statement = renameColumnFn.reverse(
'distributors',
'address',
'city'
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TABLE "distributors" RENAME "city" TO "address";'
);
});
});
});
});
});

0 comments on commit e96f85d

Please sign in to comment.