Skip to content

Commit

Permalink
test: add more types unit tests (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 19, 2024
1 parent e9ca60c commit 0b5b964
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 10 deletions.
55 changes: 55 additions & 0 deletions test/operations/types/addTypeAttribute.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, expect, it } from 'vitest';
import { PgType } from '../../../src';
import { addTypeAttribute } from '../../../src/operations/types';
import { options1 } from '../../utils';

describe('operations', () => {
describe('types', () => {
describe('addTypeAttribute', () => {
const addTypeAttributeFn = addTypeAttribute(options1);

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

it('should return sql statement', () => {
const statement = addTypeAttributeFn('compfoo', 'f3', PgType.INT);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "compfoo" ADD ATTRIBUTE "f3" integer;'
);
});

it('should return sql statement with schema', () => {
const statement = addTypeAttributeFn(
{ name: 'compfoo', schema: 'myschema' },
'f3',
'int'
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "myschema"."compfoo" ADD ATTRIBUTE "f3" integer;'
);
});

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

it('should return sql statement', () => {
const statement = addTypeAttributeFn.reverse(
'compfoo',
'f3',
PgType.INT
);

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

describe('operations', () => {
describe('types', () => {
describe('addTypeValue', () => {
const addTypeValueFn = addTypeValue(options1);

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

it('should return sql statement', () => {
const statement = addTypeValueFn('colors', 'purple');

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "colors" ADD VALUE $pga$purple$pga$;'
);
});

it('should return sql statement with typeValueOptions', () => {
const statement = addTypeValueFn('colors', 'purple', {
after: 'red',
ifNotExists: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "colors" ADD VALUE IF NOT EXISTS $pga$purple$pga$ AFTER $pga$red$pga$;'
);

const statement2 = addTypeValueFn('colors', 'purple', {
before: 'yellow',
ifNotExists: true,
});

expect(statement2).toBeTypeOf('string');
expect(statement2).toBe(
'ALTER TYPE "colors" ADD VALUE IF NOT EXISTS $pga$purple$pga$ BEFORE $pga$yellow$pga$;'
);
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "myschema"."colors" ADD VALUE $pga$purple$pga$;'
);
});

it('should throw when before and after are specified together', () => {
expect(() =>
addTypeValueFn('colors', 'purple', {
before: 'blue',
after: 'red',
})
).toThrow(
new Error('"before" and "after" can\'t be specified together')
);
});
});
});
});
4 changes: 2 additions & 2 deletions test/operations/types/createType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('operations', () => {
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(`CREATE TYPE "compfoo" AS (
expect(statement).toBe(`CREATE TYPE "compfoo" AS (
"f1" integer,
"f2" text
);`);
Expand All @@ -38,7 +38,7 @@ describe('operations', () => {
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
expect(statement).toBe(
'CREATE TYPE "myschema"."box" AS ENUM ($pga$cstring$pga$);'
);
});
Expand Down
4 changes: 2 additions & 2 deletions test/operations/types/dropType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('operations', () => {
const statement = dropTypeFn('box');

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

it('should return sql statement with dropOptions', () => {
Expand All @@ -25,7 +25,7 @@ describe('operations', () => {
});

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

describe('operations', () => {
describe('types', () => {
describe('dropTypeAttribute', () => {
const dropTypeAttributeFn = dropTypeAttribute(options1);

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

it('should return sql statement', () => {
const statement = dropTypeAttributeFn('compfoo', 'bar', {});

expect(statement).toBeTypeOf('string');
expect(statement).toBe('ALTER TYPE "compfoo" DROP ATTRIBUTE "bar";');
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "compfoo" DROP ATTRIBUTE "bar" IF EXISTS;'
);
});
});
});
});
4 changes: 2 additions & 2 deletions test/operations/types/renameType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('operations', () => {
const statement = renameTypeFn('electronic_mail', 'email');

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
expect(statement).toBe(
'ALTER TYPE "electronic_mail" RENAME TO "email";'
);
});
Expand All @@ -27,7 +27,7 @@ describe('operations', () => {
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
expect(statement).toBe(
'ALTER TYPE "myschema"."electronic_mail" RENAME TO "myschema"."email";'
);
});
Expand Down
56 changes: 56 additions & 0 deletions test/operations/types/renameTypeAttribute.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it } from 'vitest';
import { renameTypeAttribute } from '../../../src/operations/types';
import { options1 } from '../../utils';

describe('operations', () => {
describe('types', () => {
describe('renameTypeAttribute', () => {
const renameTypeAttributeFn = renameTypeAttribute(options1);

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

it('should return sql statement', () => {
const statement = renameTypeAttributeFn('compfoo', 'f3', 'f4');

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "compfoo" RENAME ATTRIBUTE "f3" TO "f4";'
);
});

it('should return sql statement with schema', () => {
const statement = renameTypeAttributeFn(
{ name: 'compfoo', schema: 'myschema' },
'f3',
'f4'
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "myschema"."compfoo" RENAME ATTRIBUTE "f3" TO "f4";'
);
});

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

it('should return sql statement', () => {
const statement = renameTypeAttributeFn.reverse(
'compfoo',
'f3',
'f4'
);

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

describe('operations', () => {
describe('types', () => {
describe('renameTypeValue', () => {
const renameTypeValueFn = renameTypeValue(options1);

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

it('should return sql statement', () => {
const statement = renameTypeValueFn('colors', 'purple', 'mauve');

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "colors" RENAME VALUE $pga$purple$pga$ TO $pga$mauve$pga$;'
);
});

it('should return sql statement with schema', () => {
const statement = renameTypeValueFn(
{ name: 'colors', schema: 'myschema' },
'purple',
'mauve'
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "myschema"."colors" RENAME VALUE $pga$purple$pga$ TO $pga$mauve$pga$;'
);
});

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

it('should return sql statement', () => {
const statement = renameTypeValueFn.reverse(
'colors',
'purple',
'mauve'
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "colors" RENAME VALUE $pga$mauve$pga$ TO $pga$purple$pga$;'
);
});
});
});
});
});
38 changes: 38 additions & 0 deletions test/operations/types/setTypeAttribute.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest';
import { PgType } from '../../../src';
import { setTypeAttribute } from '../../../src/operations/types';
import { options1 } from '../../utils';

describe('operations', () => {
describe('types', () => {
describe('setTypeAttribute', () => {
const setTypeAttributeFn = setTypeAttribute(options1);

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

it('should return sql statement', () => {
const statement = setTypeAttributeFn('compfoo', 'f3', PgType.INT);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "compfoo" ALTER ATTRIBUTE "f3" SET DATA TYPE integer;'
);
});

it('should return sql statement with schema', () => {
const statement = setTypeAttributeFn(
{ name: 'compfoo', schema: 'myschema' },
'f3',
'int'
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER TYPE "myschema"."compfoo" ALTER ATTRIBUTE "f3" SET DATA TYPE integer;'
);
});
});
});
});
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: 70,
statements: 70,
functions: 75,
branches: 83,
lines: 85,
statements: 85,
functions: 85,
branches: 85,
},
},
reporters: process.env.CI_PREFLIGHT
Expand Down

0 comments on commit 0b5b964

Please sign in to comment.