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 domains unit tests #1007

Merged
merged 8 commits into from
Mar 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/operations/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function createDomain(mOptions: MigrationOptions): CreateDomain {
constraintName,
} = options;

const constraints = [];
const constraints: string[] = [];

if (collation) {
constraints.push(`COLLATE ${collation}`);
Expand Down Expand Up @@ -69,6 +69,7 @@ export function createDomain(mOptions: MigrationOptions): CreateDomain {

_create.reverse = (domainName, type, options) =>
dropDomain(mOptions)(domainName, options);

return _create;
}

Expand All @@ -82,7 +83,7 @@ export function alterDomain(mOptions: MigrationOptions): AlterDomain {
constraintName,
} = options;

const actions = [];
const actions: string[] = [];

if (defaultValue === null) {
actions.push('DROP DEFAULT');
Expand Down
74 changes: 74 additions & 0 deletions test/operations/domains/alterDomain.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, expect, it } from 'vitest';
import { alterDomain } from '../../../src/operations/domains';
import { options1 } from '../../utils';

describe('operations', () => {
describe('alterDomain', () => {
const alterDomainFn = alterDomain(options1);

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

it('should return sql statement with domainOptions default', () => {
const statement = alterDomainFn('zipcode', {
default: '12345',
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER DOMAIN "zipcode" SET DEFAULT $pga$12345$pga$;'
);
});

it('should return sql statement with domainOptions default null', () => {
const statement = alterDomainFn('zipcode', {
default: null,
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe('ALTER DOMAIN "zipcode" DROP DEFAULT;');
});

it('should return sql statement with domainOptions notNull', () => {
const statement = alterDomainFn('zipcode', {
notNull: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe('ALTER DOMAIN "zipcode" SET NOT NULL;');
});

it('should return sql statement with domainOptions allowNull', () => {
const statement = alterDomainFn('zipcode', {
allowNull: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe('ALTER DOMAIN "zipcode" DROP NOT NULL;');
});

it('should return sql statement with domainOptions check', () => {
const statement = alterDomainFn('zipcode', {
check: 'char_length(VALUE) = 5',
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER DOMAIN "zipcode" CHECK (char_length(VALUE) = 5);'
);
});

it('should return sql statement with domainOptions check and constraintName', () => {
const statement = alterDomainFn('zipcode', {
check: 'char_length(VALUE) = 5',
constraintName: 'zipchk',
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'ALTER DOMAIN "zipcode" CONSTRAINT "zipchk" CHECK (char_length(VALUE) = 5);'
);
});
});
});
99 changes: 99 additions & 0 deletions test/operations/domains/createDomain.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { describe, expect, it } from 'vitest';
import { PgType } from '../../../src';
import { createDomain } from '../../../src/operations/domains';
import { options1 } from '../../utils';

describe('operations', () => {
describe('createDomain', () => {
const createDomainFn = createDomain(options1);

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

it('should return sql statement with string', () => {
const statement = createDomainFn('us_postal_code', 'TEXT');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('CREATE DOMAIN "us_postal_code" AS TEXT;');
});

it('should return sql statement with PgType', () => {
const statement = createDomainFn('us_postal_code', PgType.TEXT);

expect(statement).toBeTypeOf('string');
expect(statement).toBe('CREATE DOMAIN "us_postal_code" AS text;');
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'CREATE DOMAIN "myschema"."us_postal_code" AS TEXT;'
);
});

it('should return sql statement with domainOptions collation and default', () => {
const statement = createDomainFn('us_postal_code', 'TEXT', {
collation: 'en_US',
default: '12345',
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'CREATE DOMAIN "us_postal_code" AS TEXT COLLATE en_US DEFAULT $pga$12345$pga$;'
);
});

it('should return sql statement with domainOptions check', () => {
const statement = createDomainFn('us_postal_code', 'TEXT', {
check: "VALUE ~ '^d{5}$'",
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'CREATE DOMAIN "us_postal_code" AS TEXT CHECK (VALUE ~ \'^d{5}$\');'
);
});

it('should return sql statement with domainOptions constraintName and notNull', () => {
const statement = createDomainFn('us_postal_code', 'TEXT', {
constraintName: 'us_postal_code_check',
notNull: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
'CREATE DOMAIN "us_postal_code" AS TEXT CONSTRAINT "us_postal_code_check" NOT NULL;'
);
});

it('should throw when notNull and check are passed', () => {
expect(() =>
createDomainFn('us_postal_code', 'TEXT', {
check: "VALUE ~ '^d{5}$'",
notNull: true,
})
).toThrow(
new Error('"notNull" and "check" can\'t be specified together')
);
});

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

it('should return sql statement', () => {
const statement = createDomainFn.reverse('us_postal_code', 'TEXT');

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

describe('operations', () => {
describe('dropDomain', () => {
const dropDomainFn = dropDomain(options1);

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

it('should return sql statement', () => {
const statement = dropDomainFn('us_postal_code');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('DROP DOMAIN "us_postal_code";');
});

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

expect(statement).toBeTypeOf('string');
expect(statement).toBe('DROP DOMAIN IF EXISTS "us_postal_code" CASCADE;');
});

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

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

describe('operations', () => {
describe('renameDomain', () => {
const renameDomainFn = renameDomain(options1);

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

it('should return sql statement with domainOptions default', () => {
const statement = renameDomainFn('zipcode', 'zip_code');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('ALTER DOMAIN "zipcode" RENAME TO "zip_code";');
});

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

it('should return sql statement', () => {
const statement = renameDomainFn.reverse('zipcode', 'zip_code');

expect(statement).toBeTypeOf('string');
expect(statement).toBe('ALTER DOMAIN "zip_code" RENAME TO "zipcode";');
});
});
});
});