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 indexes unit tests #1012

Merged
merged 2 commits into from
Mar 6, 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
76 changes: 76 additions & 0 deletions test/operations/indexes/createIndex.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, expect, it } from 'vitest';
import { createIndex } from '../../../src/operations/indexes';
import { options1 } from '../../utils';

describe('operations', () => {
describe('indexes', () => {
describe('createIndex', () => {
const createIndexFn = createIndex(options1);

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

it('should return sql statement', () => {
const statement = createIndexFn('films', ['title'], {
name: 'title_idx',
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
`CREATE INDEX "title_idx" ON "films" ("title");`
);
});

it('should return sql statement with indexOptions', () => {
const statement = createIndexFn('films', 'title', {
unique: true,
concurrently: true,
ifNotExists: true,
include: ['director', 'rating'],
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "films_title_unique_index" ON "films" ("title") INCLUDE ("director", "rating");'
);
});

it('should return sql statement with schema', () => {
const statement = createIndexFn({ name: 'films', schema: 'myschema' }, [
{ name: 'title', sort: 'ASC' },
]);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE INDEX "films_title_index" ON "myschema"."films" ("title" ASC);'
);
});

it('should throw when passing opclass as last column', () => {
expect(() =>
createIndexFn('films', [{ name: 'title', opclass: 'op' }], {
opclass: 'op',
})
).toThrow(
new Error(
"There is already defined opclass on column, can't override it with global one"
)
);
});

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

it('should return sql statement', () => {
const statement = createIndexFn.reverse('films', ['title']);

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

describe('operations', () => {
describe('indexes', () => {
describe('dropIndex', () => {
const dropIndexFn = dropIndex(options1);

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

it('should return sql statement', () => {
const statement = dropIndexFn('title_idx', []);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual('DROP INDEX "title_idx__index";');
});

it('should return sql statement with dropOptions', () => {
const statement = dropIndexFn('title_idx', [], {
concurrently: true,
ifExists: true,
cascade: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'DROP INDEX CONCURRENTLY IF EXISTS "title_idx__index" CASCADE;'
);
});
});
});
});