Skip to content

Commit

Permalink
test: add policies unit tests (#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Mar 9, 2024
1 parent f111be5 commit f032440
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/operations/policies/alterPolicy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from 'vitest';
import { alterPolicy } from '../../../src/operations/policies';
import { options1 } from '../../utils';

describe('operations', () => {
describe('policies', () => {
describe('alterPolicy', () => {
const alterPolicyFn = alterPolicy(options1);

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

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

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual('ALTER POLICY "p1" ON "my_table" ;');
});

it('should return sql statement with policyOptions', () => {
const statement = alterPolicyFn('my_table', 'p1', {
role: 'CURRENT_USER',
check: 'true',
using: 'true',
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER POLICY "p1" ON "my_table" TO CURRENT_USER USING (true) WITH CHECK (true);'
);
});
});
});
});
64 changes: 64 additions & 0 deletions test/operations/policies/createPolicy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { describe, expect, it } from 'vitest';
import { createPolicy } from '../../../src/operations/policies';
import { options1 } from '../../utils';

describe('operations', () => {
describe('policies', () => {
describe('createPolicy', () => {
const createPolicyFn = createPolicy(options1);

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

it('should return sql statement', () => {
const statement = createPolicyFn('my_table', 'p1');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE POLICY "p1" ON "my_table" FOR ALL TO PUBLIC;'
);
});

it('should return sql statement with policyOptions', () => {
const statement = createPolicyFn('my_table', 'p1', {
role: 'CURRENT_USER',
check: 'true',
using: 'true',
command: 'SELECT',
ifExists: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE POLICY "p1" ON "my_table" FOR SELECT TO CURRENT_USER USING (true) WITH CHECK (true);'
);
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE POLICY "p1" ON "myschema"."my_table" FOR ALL TO PUBLIC;'
);
});

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

it('should return sql statement', () => {
const statement = createPolicyFn.reverse('my_table', 'p1');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('DROP POLICY "p1" ON "my_table";');
});
});
});
});
});
33 changes: 33 additions & 0 deletions test/operations/policies/dropPolicy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, it } from 'vitest';
import { dropPolicy } from '../../../src/operations/policies';
import { options1 } from '../../utils';

describe('operations', () => {
describe('policies', () => {
describe('dropPolicy', () => {
const dropPolicyFn = dropPolicy(options1);

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

it('should return sql statement', () => {
const statement = dropPolicyFn('my_table', 'p1');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual('DROP POLICY "p1" ON "my_table";');
});

it('should return sql statement with dropOptions', () => {
const statement = dropPolicyFn('my_table', 'p1', {
ifExists: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'DROP POLICY IF EXISTS "p1" ON "my_table";'
);
});
});
});
});
52 changes: 52 additions & 0 deletions test/operations/policies/renamePolicy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, it } from 'vitest';
import { renamePolicy } from '../../../src/operations/policies';
import { options1 } from '../../utils';

describe('operations', () => {
describe('policies', () => {
describe('renamePolicy', () => {
const renamePolicyFn = renamePolicy(options1);

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

it('should return sql statement', () => {
const statement = renamePolicyFn('my_table', 'p1', 'p2');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER POLICY "p1" ON "my_table" RENAME TO "p2";'
);
});

it('should return sql statement with schema', () => {
const statement = renamePolicyFn(
{ name: 'my_table', schema: 'myschema' },
'p1',
'p2'
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER POLICY "p1" ON "myschema"."my_table" RENAME TO "p2";'
);
});

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

it('should return sql statement', () => {
const statement = renamePolicyFn.reverse('my_table', 'p1', 'p2');

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER POLICY "p2" ON "my_table" RENAME TO "p1";'
);
});
});
});
});
});

0 comments on commit f032440

Please sign in to comment.