Skip to content

Commit

Permalink
test: add materialized views unit tests (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 2, 2024
1 parent acaf6d7 commit 38fa3c3
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/operations/viewsMaterialized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function createMaterializedView(
const columnsStr = columnNames ? `(${columnNames})` : '';
const withOptionsStr = withOptions ? ` WITH (${withOptions})` : '';
const tablespaceStr = tablespace
? `TABLESPACE ${mOptions.literal(tablespace)}`
? ` TABLESPACE ${mOptions.literal(tablespace)}`
: '';
const dataStr = dataClause(data);
const viewNameStr = mOptions.literal(viewName);
Expand Down
58 changes: 58 additions & 0 deletions test/operations/materializedViews/alterMaterializedView.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, expect, it } from 'vitest';
import { alterMaterializedView } from '../../../src/operations/viewsMaterialized';
import { options1 } from '../../utils';

describe('operations', () => {
describe('materializedViews', () => {
describe('alterMaterializedView', () => {
const alterMaterializedViewFn = alterMaterializedView(options1);

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

// TODO @Shinigami92 2024-04-02: This should throw an error
it('should return sql statement', () => {
const statement = alterMaterializedViewFn('a_mview', {});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(`ALTER MATERIALIZED VIEW "a_mview"
;`);
});

it('should return sql statement with materializedOptions', () => {
const statement = alterMaterializedViewFn('a_mview', {
cluster: 'a_cluster',
extension: 'a_extension',
storageParameters: {
fillfactor: 70,
fillfactor2: 50,
reset1: null,
reset2: null,
},
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
`ALTER MATERIALIZED VIEW "a_mview"
CLUSTER ON "a_cluster",
DEPENDS ON EXTENSION "a_extension",
SET (fillfactor = 70, fillfactor2 = 50),
RESET (reset1, reset2);`
);
});

it('should return sql statement without cluster', () => {
const statement = alterMaterializedViewFn('a_mview', {
cluster: null,
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
`ALTER MATERIALIZED VIEW "a_mview"
SET WITHOUT CLUSTER;`
);
});
});
});
});
77 changes: 77 additions & 0 deletions test/operations/materializedViews/createMaterializedView.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, it } from 'vitest';
import { createMaterializedView } from '../../../src/operations/viewsMaterialized';
import { options1 } from '../../utils';

describe('operations', () => {
describe('materializedViews', () => {
describe('createMaterializedView', () => {
const createMaterializedViewFn = createMaterializedView(options1);

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

// TODO @Shinigami92 2024-04-02: This should throw an error
it.todo('should return sql statement', () => {
const statement = createMaterializedViewFn('order_summary', {}, '');

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

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

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

it('should return sql statement with schema', () => {
const statement = createMaterializedViewFn(
{ name: 'comedies', schema: 'myschema' },
{
ifNotExists: true,
columns: 'kind',
storageParameters: { fillfactor: 70 },
tablespace: 'mytablespace',
data: true,
// TODO @Shinigami92 2024-04-02: Remove these from the options interface
// ifExists: true,
// cascade: true,
},
"SELECT * FROM films WHERE kind = 'Comedy'"
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'CREATE MATERIALIZED VIEW IF NOT EXISTS "myschema"."comedies"("kind") WITH (fillfactor = 70) TABLESPACE "mytablespace" AS SELECT * FROM films WHERE kind = \'Comedy\' WITH DATA;'
);
});

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

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

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

describe('operations', () => {
describe('materializedViews', () => {
describe('dropMaterializedView', () => {
const dropMaterializedViewFn = dropMaterializedView(options1);

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

it('should return sql statement', () => {
const statement = dropMaterializedViewFn('order_summary');

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

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

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

describe('operations', () => {
describe('materializedViews', () => {
describe('refreshMaterializedView', () => {
const refreshMaterializedViewFn = refreshMaterializedView(options1);

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

it('should return sql statement', () => {
const statement = refreshMaterializedViewFn('order_summary');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('REFRESH MATERIALIZED VIEW "order_summary";');
});

it('should return sql statement with materializedViewRefreshOptions', () => {
const statement = refreshMaterializedViewFn('annual_statistics_basis', {
concurrently: true,
data: false,
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'REFRESH MATERIALIZED VIEW CONCURRENTLY "annual_statistics_basis" WITH NO DATA;'
);
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'REFRESH MATERIALIZED VIEW "myschema"."order_summary";'
);
});

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

it('should return sql statement', () => {
const statement = refreshMaterializedViewFn.reverse('order_summary');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('REFRESH MATERIALIZED VIEW "order_summary";');
});
});
});
});
});
51 changes: 51 additions & 0 deletions test/operations/materializedViews/renameMaterializedView.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it } from 'vitest';
import { renameMaterializedView } from '../../../src/operations/viewsMaterialized';
import { options1 } from '../../utils';

describe('operations', () => {
describe('materializedViews', () => {
describe('renameMaterializedView', () => {
const renameMaterializedViewFn = renameMaterializedView(options1);

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

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

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

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

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

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

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

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER MATERIALIZED VIEW "bar" RENAME TO "foo";'
);
});
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, expect, it } from 'vitest';
import { renameMaterializedViewColumn } from '../../../src/operations/viewsMaterialized';
import { options1 } from '../../utils';

describe('operations', () => {
describe('materializedViews', () => {
describe('renameMaterializedViewColumn', () => {
const renameMaterializedViewColumnFn =
renameMaterializedViewColumn(options1);

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

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

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

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

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

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

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

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER MATERIALIZED VIEW "a_mview" RENAME COLUMN "bar" TO "foo";'
);
});
});
});
});
});
6 changes: 3 additions & 3 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default defineConfig({
include: ['src'],
reportOnFailure: true,
thresholds: {
lines: 85,
statements: 85,
functions: 85,
lines: 90,
statements: 90,
functions: 90,
branches: 85,
},
},
Expand Down

0 comments on commit 38fa3c3

Please sign in to comment.