Skip to content

Commit

Permalink
test: add views unit tests (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 20, 2024
1 parent f5528f2 commit acaf6d7
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 0 deletions.
67 changes: 67 additions & 0 deletions test/operations/views/alterView.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { describe, expect, it } from 'vitest';
import { alterView } from '../../../src/operations/views';
import { options1 } from '../../utils';

describe('operations', () => {
describe('views', () => {
describe('alterView', () => {
const alterViewFn = alterView(options1);

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

// TODO @Shinigami92 2024-03-09: This should throw an error
it('should return sql statement', () => {
const statement = alterViewFn('a_view', {});

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

it('should return sql statement with viewOptions', () => {
const statement = alterViewFn('a_view', {
checkOption: 'LOCAL',
options: {
classification: 'PG',
},
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER VIEW "a_view" SET (classification = PG, check_option = LOCAL);'
);
});

it('should return sql statement with options containing null value', () => {
const statement = alterViewFn('a_view', {
checkOption: 'LOCAL',
options: {
classification: null,
},
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
`ALTER VIEW "a_view" SET (check_option = LOCAL);
ALTER VIEW "a_view" RESET (classification);`
);
});

it('should throw error on duplicate checkOption', () => {
expect(() =>
alterViewFn('a_view', {
checkOption: 'LOCAL',
options: {
check_option: 'CASCADED',
},
})
).toThrow(
new Error(
'"options.check_option" and "checkOption" can\'t be specified together'
)
);
});
});
});
});
44 changes: 44 additions & 0 deletions test/operations/views/alterViewColumn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest';
import { alterViewColumn } from '../../../src/operations/views';
import { options1 } from '../../utils';

describe('operations', () => {
describe('views', () => {
describe('alterViewColumn', () => {
const alterViewColumnFn = alterViewColumn(options1);

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

it('should return sql statement', () => {
const statement = alterViewColumnFn('a_view', 'ts', {});

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

it('should return sql statement with viewColumnOptions', () => {
const statement = alterViewColumnFn('a_view', 'ts', {
default: 'now()',
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER VIEW "a_view" ALTER COLUMN "ts" SET DEFAULT $pga$now()$pga$;'
);
});

it('should return sql statement with viewColumnOptions drop default', () => {
const statement = alterViewColumnFn('a_view', 'ts', {
default: null,
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER VIEW "a_view" ALTER COLUMN "ts" DROP DEFAULT;'
);
});
});
});
});
77 changes: 77 additions & 0 deletions test/operations/views/createView.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, it } from 'vitest';
import { createView } from '../../../src/operations/views';
import { options1 } from '../../utils';

describe('operations', () => {
describe('views', () => {
describe('createView', () => {
const createViewFn = createView(options1);

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

// TODO @Shinigami92 2024-03-20: This should throw an error
it.todo('should return sql statement', () => {
const statement = createViewFn('comedies', {}, '');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('CREATE VIEW "comedies" AS ;');
});

it('should return sql statement with viewOptions', () => {
const statement = createViewFn(
'comedies',
{
ifExists: true,
cascade: true,
replace: true,
recursive: true,
columns: ['column1', 'column2'],
options: {
classification: 'PG',
},
checkOption: 'LOCAL',
temporary: true,
},
"SELECT * FROM films WHERE kind = 'Comedy'"
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'CREATE OR REPLACE TEMPORARY RECURSIVE VIEW "comedies"("column1", "column2") WITH (classification = PG) AS SELECT * FROM films WHERE kind = \'Comedy\' WITH LOCAL CHECK OPTION;'
);
});

it('should return sql statement with schema', () => {
const statement = createViewFn(
{ name: 'comedies', schema: 'myschema' },
{},
"SELECT * FROM films WHERE kind = 'Comedy'"
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'CREATE VIEW "myschema"."comedies" AS SELECT * FROM films WHERE kind = \'Comedy\';'
);
});

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

it('should return sql statement', () => {
const statement = createViewFn.reverse(
'comedies',
{},
"SELECT * FROM films WHERE kind = 'Comedy'"
);

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

describe('operations', () => {
describe('views', () => {
describe('dropView', () => {
const dropViewFn = dropView(options1);

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

it('should return sql statement', () => {
const statement = dropViewFn('kinds');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('DROP VIEW "kinds";');
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toBe('DROP VIEW IF EXISTS "kinds" CASCADE;');
});
});
});
});
47 changes: 47 additions & 0 deletions test/operations/views/renameView.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it } from 'vitest';
import { renameView } from '../../../src/operations/views';
import { options1 } from '../../utils';

describe('operations', () => {
describe('views', () => {
describe('renameView', () => {
const renameViewFn = renameView(options1);

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

it('should return sql statement', () => {
const statement = renameViewFn('foo', 'bar');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('ALTER VIEW "foo" RENAME TO "bar";');
});

it('should return sql statement with schema', () => {
const statement = renameViewFn(
{ name: 'foo', schema: 'myschema' },
{ name: 'bar', schema: 'myschema' }
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER VIEW "myschema"."foo" RENAME TO "myschema"."bar";'
);
});

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

it('should return sql statement', () => {
const statement = renameViewFn.reverse('foo', 'bar');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('ALTER VIEW "bar" RENAME TO "foo";');
});
});
});
});
});

0 comments on commit acaf6d7

Please sign in to comment.