Skip to content

Commit

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

describe('operations', () => {
describe('sequences', () => {
describe('alterSequence', () => {
const alterSequenceFn = alterSequence(options1);

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

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

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(`ALTER SEQUENCE "serial"
;`);
});

it('should return sql statement with sequenceOptions', () => {
const statement = alterSequenceFn('serial', {
restart: 105,
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(`ALTER SEQUENCE "serial"
RESTART WITH 105;`);
});
});
});
});
76 changes: 76 additions & 0 deletions test/operations/sequences/createSequence.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, expect, it } from 'vitest';
import { createSequence } from '../../../src/operations/sequences';
import { options1 } from '../../utils';

describe('operations', () => {
describe('sequences', () => {
describe('createSequence', () => {
const createSequenceFn = createSequence(options1);

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

it('should return sql statement', () => {
const statement = createSequenceFn('serial');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(`CREATE SEQUENCE "serial"
;`);
});

it('should return sql statement with sequenceOptions', () => {
const statement = createSequenceFn('serial', {
temporary: true,
ifNotExists: true,
type: 'bigint',
increment: 2,
minvalue: 0,
maxvalue: 1000,
start: 1,
cache: 1,
cycle: true,
owner: 'mytable.mycol',
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
`CREATE TEMPORARY SEQUENCE IF NOT EXISTS "serial"
AS bigint
INCREMENT BY 2
MAXVALUE 1000
START WITH 1
CACHE 1
CYCLE
OWNED BY mytable.mycol;`
);
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
`CREATE SEQUENCE "myschema"."serial"
;`
);
});

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

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

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

describe('operations', () => {
describe('sequences', () => {
describe('dropSequence', () => {
const dropSequenceFn = dropSequence(options1);

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

it('should return sql statement', () => {
const statement = dropSequenceFn('serial');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual('DROP SEQUENCE "serial";');
});

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

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

describe('operations', () => {
describe('sequences', () => {
describe('renameSequence', () => {
const renameSequenceFn = renameSequence(options1);

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

it('should return sql statement', () => {
const statement = renameSequenceFn('serial', 'serial2');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER SEQUENCE "serial" RENAME TO "serial2";'
);
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER SEQUENCE "myschema"."serial" RENAME TO "myschema"."serial2";'
);
});

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

it('should return sql statement', () => {
const statement = renameSequenceFn.reverse('serial', 'serial2');

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER SEQUENCE "serial2" RENAME TO "serial";'
);
});
});
});
});
});
8 changes: 4 additions & 4 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export default defineConfig({
include: ['src'],
reportOnFailure: true,
thresholds: {
lines: 57,
statements: 57,
functions: 57,
branches: 82,
lines: 60,
statements: 60,
functions: 60,
branches: 84,
},
},
reporters: process.env.CI_PREFLIGHT
Expand Down

0 comments on commit 05e3c06

Please sign in to comment.