Skip to content

Commit

Permalink
test: add roles unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 11, 2024
1 parent 7d8d9e6 commit 5fc43a6
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/operations/roles/alterRole.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, it } from 'vitest';
import { alterRole } from '../../../src/operations/roles';
import { options1 } from '../../utils';

describe('operations', () => {
describe('roles', () => {
describe('alterRole', () => {
const alterRoleFn = alterRole(options1);

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

it('should return sql statement', () => {
const statement = alterRoleFn('jonathan', {});

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

it('should return sql statement with roleOptions', () => {
const statement = alterRoleFn('jonathan', {
replication: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER ROLE "jonathan" WITH REPLICATION;'
);
});
});
});
});
69 changes: 69 additions & 0 deletions test/operations/roles/createRole.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, expect, it } from 'vitest';
import { createRole } from '../../../src/operations/roles';
import { options1 } from '../../utils';

describe('operations', () => {
describe('roles', () => {
describe('createRole', () => {
const createRoleFn = createRole(options1);

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

it('should return sql statement', () => {
const statement = createRoleFn('jonathan');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE ROLE "jonathan" WITH NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT NOLOGIN NOREPLICATION;'
);
});

it('should return sql statement with roleOptions', () => {
const statement = createRoleFn('miriam', {
login: true,
password: 'jw8s0F4',
valid: '2005-01-01',
superuser: false,
createdb: false,
createrole: false,
inherit: false,
replication: false,
bypassrls: false,
limit: 10,
inRole: 'admin',
role: 'user',
admin: 'admin',
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE ROLE "miriam" WITH NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT LOGIN NOREPLICATION NOBYPASSRLS CONNECTION LIMIT 10 ENCRYPTED PASSWORD $pga$jw8s0F4$pga$ VALID UNTIL $pga$2005-01-01$pga$ IN ROLE admin ROLE user ADMIN admin;'
);
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE ROLE "myschema"."miriam" WITH NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT NOLOGIN NOREPLICATION;'
);
});

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

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

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

describe('operations', () => {
describe('roles', () => {
describe('dropRole', () => {
const dropRoleFn = dropRole(options1);

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

it('should return sql statement', () => {
const statement = dropRoleFn('jonathan');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual('DROP ROLE "jonathan";');
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual('DROP ROLE IF EXISTS "jonathan";');
});
});
});
});
49 changes: 49 additions & 0 deletions test/operations/roles/renameRole.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, expect, it } from 'vitest';
import { renameRole } from '../../../src/operations/roles';
import { options1 } from '../../utils';

describe('operations', () => {
describe('roles', () => {
describe('renameRole', () => {
const renameRoleFn = renameRole(options1);

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

it('should return sql statement', () => {
const statement = renameRoleFn('jonathan', 'davide');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER ROLE "jonathan" RENAME TO "davide";'
);
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER ROLE "myschema"."jonathan" RENAME TO "myschema"."davide";'
);
});

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

it('should return sql statement', () => {
const statement = renameRoleFn.reverse('jonathan', 'davide');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('ALTER ROLE "davide" RENAME TO "jonathan";');
});
});
});
});
});

0 comments on commit 5fc43a6

Please sign in to comment.