Skip to content

Commit

Permalink
test: add tables unit tests (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 12, 2024
1 parent d62a769 commit 84fb4bb
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/operations/tables/alterTable.spec.ts
@@ -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
@@ -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
@@ -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
@@ -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";'
);
});
});
});
});
});

0 comments on commit 84fb4bb

Please sign in to comment.