Skip to content

Commit

Permalink
test: add operators unit tests (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Mar 8, 2024
1 parent 2a2f53a commit 928773e
Show file tree
Hide file tree
Showing 12 changed files with 704 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/operations/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function dropOperatorFamily(
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';

return `DROP OPERATOR FAMILY ${ifExistsStr} ${operatorFamilyNameStr} USING ${indexMethod}${cascadeStr};`;
return `DROP OPERATOR FAMILY${ifExistsStr} ${operatorFamilyNameStr} USING ${indexMethod}${cascadeStr};`;
};

return _drop;
Expand Down Expand Up @@ -240,7 +240,7 @@ export function dropOperatorClass(
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';

return `DROP OPERATOR CLASS ${ifExistsStr} ${operatorClassNameStr} USING ${indexMethod}${cascadeStr};`;
return `DROP OPERATOR CLASS${ifExistsStr} ${operatorClassNameStr} USING ${indexMethod}${cascadeStr};`;
};

return _drop;
Expand Down
185 changes: 185 additions & 0 deletions test/operations/operators/addToOperatorFamily.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { describe, expect, it } from 'vitest';
import { addToOperatorFamily } from '../../../src/operations/operators';
import { options1 } from '../../utils';

describe('operations', () => {
describe('operators', () => {
describe('addToOperatorFamily', () => {
const addToOperatorFamilyFn = addToOperatorFamily(options1);

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

it.todo('should return sql statement', () => {
const statement = addToOperatorFamilyFn('integer_ops', 'btree', [
{
name: '<',
number: 1,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '<=',
number: 2,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '=',
number: 3,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '>=',
number: 4,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '>',
number: 5,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: 'btint42cmp',
number: 1,
type: 'function',
params: [{ type: 'int4' }, { type: 'int2' }],
},
]);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
`ALTER OPERATOR FAMILY "integer_ops" USING btree ADD
OPERATOR 1 "<"(int4, int2),
OPERATOR 2 "<="(int4, int2),
OPERATOR 3 "="(int4, int2),
OPERATOR 4 ">="(int4, int2),
OPERATOR 5 ">"(int4, int2),
FUNCTION 1 "btint42cmp"(int4, int2);`
);
});

it.todo('should return sql statement with schema', () => {
const statement = addToOperatorFamilyFn(
{ name: 'integer_ops', schema: 'myschema' },
'btree',
[
{
name: '<',
number: 1,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '<=',
number: 2,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '=',
number: 3,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '>=',
number: 4,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '>',
number: 5,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: 'btint42cmp',
number: 1,
type: 'function',
params: [{ type: 'int4' }, { type: 'int2' }],
},
]
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
`ALTER OPERATOR FAMILY "myschema"."integer_ops" USING btree ADD
OPERATOR 1 "<"(int4, int2),
OPERATOR 2 "<="(int4, int2),
OPERATOR 3 "="(int4, int2),
OPERATOR 4 ">="(int4, int2),
OPERATOR 5 ">"(int4, int2),
FUNCTION 1 "btint42cmp"(int4, int2);`
);
});

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

it.todo('should return sql statement', () => {
const statement = addToOperatorFamilyFn.reverse(
'integer_ops',
'btree',
[
{
name: '<',
number: 1,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '<=',
number: 2,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '=',
number: 3,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '>=',
number: 4,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: '>',
number: 5,
type: 'operator',
params: [{ type: 'int4' }, { type: 'int2' }],
},
{
name: 'btint42cmp',
number: 1,
type: 'function',
params: [{ type: 'int4' }, { type: 'int2' }],
},
]
);

expect(statement).toBeTypeOf('string');
expect(statement).toBe(
`ALTER OPERATOR FAMILY "integer_ops" USING btree DROP
OPERATOR 1 "<"(int4, int2),
OPERATOR 2 "<="(int4, int2),
OPERATOR 3 "="(int4, int2),
OPERATOR 4 ">="(int4, int2),
OPERATOR 5 ">"(int4, int2),
FUNCTION 1 "btint42cmp"(int4, int2);`
);
});
});
});
});
});
80 changes: 80 additions & 0 deletions test/operations/operators/createOperator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { describe, expect, it } from 'vitest';
import { createOperator } from '../../../src/operations/operators';
import { options1 } from '../../utils';

describe('operations', () => {
describe('operators', () => {
describe('createOperator', () => {
const createOperatorFn = createOperator(options1);

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

it('should return sql statement', () => {
const statement = createOperatorFn('===', {
left: 'box',
right: 'box',
procedure: 'area_equal_function',
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE OPERATOR === (PROCEDURE = "area_equal_function", LEFTARG = "box", RIGHTARG = "box");'
);
});

it('should return sql statement with operatorOptions', () => {
const statement = createOperatorFn('===', {
left: 'box',
right: 'box',
procedure: 'area_equal_function',
commutator: '===',
negator: '!==',
restrict: 'area_restriction_function',
join: 'area_join_function',
hashes: true,
merges: true,
});

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE OPERATOR === (PROCEDURE = "area_equal_function", LEFTARG = "box", RIGHTARG = "box", COMMUTATOR = ===, NEGATOR = !==, RESTRICT = "area_restriction_function", JOIN = "area_join_function", HASHES, MERGES);'
);
});

it('should return sql statement with schema', () => {
const statement = createOperatorFn(
{ name: '===', schema: 'myschema' },
{
left: 'box',
right: 'box',
procedure: 'area_equal_function',
}
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
'CREATE OPERATOR myschema.=== (PROCEDURE = "area_equal_function", LEFTARG = "box", RIGHTARG = "box");'
);
});

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

it('should return sql statement', () => {
const statement = createOperatorFn.reverse('===', {
left: 'box',
right: 'box',
procedure: 'area_equal_function',
});

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

describe('operations', () => {
describe('operators', () => {
describe('createOperatorClass', () => {
const createOperatorClassFn = createOperatorClass(options1);

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

it.todo('should return sql statement', () => {
const statement = createOperatorClassFn(
'gist__int_ops',
'_int4',
'gist',
[
{
name: '&&',
number: 3,
type: 'operator',
},
],
{
default: true,
}
);

expect(statement).toBeTypeOf('string');
expect(statement).toStrictEqual(
`CREATE OPERATOR CLASS "gist__int_ops" DEFAULT FOR TYPE "_int4" USING "gist" AS
FUNCTION 3 "&&"();`
);
});

it.todo('should return sql statement with operatorOptions', () => {});

it.todo('should return sql statement with schema', () => {});

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

it.todo('should return sql statement', () => {});
});
});
});
});
Loading

0 comments on commit 928773e

Please sign in to comment.