From f032440632c6164395c29e3a7745602c164a22f7 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Sat, 9 Mar 2024 11:42:47 +0100 Subject: [PATCH] test: add policies unit tests (#1026) --- test/operations/policies/alterPolicy.spec.ts | 36 +++++++++++ test/operations/policies/createPolicy.spec.ts | 64 +++++++++++++++++++ test/operations/policies/dropPolicy.spec.ts | 33 ++++++++++ test/operations/policies/renamePolicy.spec.ts | 52 +++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 test/operations/policies/alterPolicy.spec.ts create mode 100644 test/operations/policies/createPolicy.spec.ts create mode 100644 test/operations/policies/dropPolicy.spec.ts create mode 100644 test/operations/policies/renamePolicy.spec.ts diff --git a/test/operations/policies/alterPolicy.spec.ts b/test/operations/policies/alterPolicy.spec.ts new file mode 100644 index 00000000..1883d46a --- /dev/null +++ b/test/operations/policies/alterPolicy.spec.ts @@ -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);' + ); + }); + }); + }); +}); diff --git a/test/operations/policies/createPolicy.spec.ts b/test/operations/policies/createPolicy.spec.ts new file mode 100644 index 00000000..dcc30b28 --- /dev/null +++ b/test/operations/policies/createPolicy.spec.ts @@ -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";'); + }); + }); + }); + }); +}); diff --git a/test/operations/policies/dropPolicy.spec.ts b/test/operations/policies/dropPolicy.spec.ts new file mode 100644 index 00000000..9dfeb9f7 --- /dev/null +++ b/test/operations/policies/dropPolicy.spec.ts @@ -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";' + ); + }); + }); + }); +}); diff --git a/test/operations/policies/renamePolicy.spec.ts b/test/operations/policies/renamePolicy.spec.ts new file mode 100644 index 00000000..f8298fa8 --- /dev/null +++ b/test/operations/policies/renamePolicy.spec.ts @@ -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";' + ); + }); + }); + }); + }); +});