Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add types unit tests #1042

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions test/operations/types/createType.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, expect, it } from 'vitest';
import { PgType } from '../../../src';
import { createType } from '../../../src/operations/types';
import { options1 } from '../../utils';

describe('operations', () => {
describe('types', () => {
describe('createType', () => {
const createTypeFn = createType(options1);

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

it('should return sql statement', () => {
const statement = createTypeFn('compfoo', {
f1: 'int',
f2: PgType.TEXT,
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(`CREATE TYPE "compfoo" AS (
"f1" integer,
"f2" text
);`);
});

// TODO @Shinigami92 2024-03-18: The typeOptions are buggy
it.todo('should return sql statement with typeOptions');

it('should return sql statement with schema', () => {
const statement = createTypeFn(
{
name: 'box',
schema: 'myschema',
},
['cstring']
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE TYPE "myschema"."box" AS ENUM ($pga$cstring$pga$);'
);
});

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

it('should return sql statement', () => {
const statement = createTypeFn.reverse('compfoo', {
f1: 'int',
f2: PgType.TEXT,
});

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

describe('operations', () => {
describe('types', () => {
describe('dropType', () => {
const dropTypeFn = dropType(options1);

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

it('should return sql statement', () => {
const statement = dropTypeFn('box');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual('DROP TYPE "box";');
});

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

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

describe('operations', () => {
describe('types', () => {
describe('renameType', () => {
const renameTypeFn = renameType(options1);

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

it('should return sql statement', () => {
const statement = renameTypeFn('electronic_mail', 'email');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER TYPE "electronic_mail" RENAME TO "email";'
);
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'ALTER TYPE "myschema"."electronic_mail" RENAME TO "myschema"."email";'
);
});

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

it('should return sql statement', () => {
const statement = renameTypeFn.reverse('electronic_mail', 'email');

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "email" RENAME TO "electronic_mail";'
);
});
});
});
});
});