Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tables unit tests #1032

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions test/operations/tables/alterTable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest';
import { alterTable } from '../../../src/operations/tables';
import { options1 } from '../../utils';

describe('operations', () => {
describe('tables', () => {
describe('alterTable', () => {
const alterTableFn = alterTable(options1);

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

// TODO @Shinigami92 2024-03-11: This should throw an error
it('should return sql statement', () => {
const statement = alterTableFn(
'films',
// @ts-expect-error: add runtime error
{}
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(`ALTER TABLE "films"
;`);
});

it('should return sql statement with tableOptions', () => {
const statement = alterTableFn('distributors', {
levelSecurity: 'ENABLE',
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(`ALTER TABLE "distributors"
ENABLE ROW LEVEL SECURITY;`);
});
});
});
});
87 changes: 87 additions & 0 deletions test/operations/tables/createTable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { describe, expect, it } from 'vitest';
import { PgType } from '../../../src';
import { createTable } from '../../../src/operations/tables';
import { options1 } from '../../utils';

describe('operations', () => {
describe('tables', () => {
describe('createTable', () => {
const createTableFn = createTable(options1);

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

// TODO @Shinigami92 2024-03-12: This should throw an error when columns are empty
it('should return sql statement', () => {
const statement = createTableFn('films', {});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual('CREATE TABLE "films" (\n \n);');
});

it('should return sql statement with tableOptions', () => {
const statement = createTableFn('films', {
code: {
type: 'char(5)',
primaryKey: true,
},
title: {
type: 'varchar(40)',
notNull: true,
},
did: {
type: PgType.INTEGER,
notNull: true,
},
date_prod: PgType.DATE,
kind: 'varchar(10)',
len: {
type: 'interval hour to minute',
},
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
`CREATE TABLE "films" (
"code" char(5) PRIMARY KEY,
"title" varchar(40) NOT NULL,
"did" integer NOT NULL,
"date_prod" date,
"kind" varchar(10),
"len" interval hour to minute
);`
);
});

// TODO @Shinigami92 2024-03-12: This should throw an error when columns are empty
it('should return sql statement with schema', () => {
const statement = createTableFn(
{
name: 'films',
schema: 'myschema',
},
{}
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE TABLE "myschema"."films" (\n \n);'
);
});

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

it('should return sql statement', () => {
const statement = createTableFn.reverse('films', {});

expect(statement).toBeTypeOf('string');
expect(statement).toBe('DROP TABLE "films";');
});
});
});
});
});
34 changes: 34 additions & 0 deletions test/operations/tables/dropTable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, expect, it } from 'vitest';
import { dropTable } from '../../../src/operations/tables';
import { options1 } from '../../utils';

describe('operations', () => {
describe('tables', () => {
describe('dropTable', () => {
const dropTableFn = dropTable(options1);

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

it('should return sql statement', () => {
const statement = dropTableFn('films');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual('DROP TABLE "films";');
});

it('should return sql statement with dropOptions', () => {
const statement = dropTableFn('films', {
ifExists: true,
cascade: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'DROP TABLE IF EXISTS "films" CASCADE;'
);
});
});
});
});
51 changes: 51 additions & 0 deletions test/operations/tables/renameTable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it } from 'vitest';
import { renameTable } from '../../../src/operations/tables';
import { options1 } from '../../utils';

describe('operations', () => {
describe('tables', () => {
describe('renameTable', () => {
const renameTableFn = renameTable(options1);

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

it('should return sql statement', () => {
const statement = renameTableFn('distributors', 'suppliers');

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

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

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

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

it('should return sql statement', () => {
const statement = renameTableFn.reverse('distributors', 'suppliers');

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