Skip to content

Commit

Permalink
Merge pull request #656 from jr01/bugfix/650-odata-filter
Browse files Browse the repository at this point in the history
fix(backend): oData queries with input filter
  • Loading branch information
ghiscoding authored Jan 5, 2021
2 parents 7f06d40 + d9aab76 commit 48b4ebc
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,62 @@ describe('GridOdataService', () => {
expect(query).toBe(expectation);
});

it('should return a query to filter a search value between an inclusive range of numbers using the 2 dots (..) separator, the "RangeInclusive" operator and the range has an unbounded end', () => {
const expectation = `$top=10&$filter=(Duration ge 5)`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.number } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['5..'], operator: 'RangeInclusive' },
} as ColumnFilters;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(query).toBe(expectation);
});

it('should return a query to filter a search value between an inclusive range of numbers using the 2 dots (..) separator, the "RangeInclusive" operator and the range has an unbounded begin', () => {
const expectation = `$top=10&$filter=(Duration le 5)`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.number } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['..5'], operator: 'RangeInclusive' },
} as ColumnFilters;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(query).toBe(expectation);
});

it('should return a query to filter a search value between an inclusive range of numbers using the 2 dots (..) separator, the "RangeExclusive" operator and the range has an unbounded end', () => {
const expectation = `$top=10&$filter=(Duration gt 5)`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.number } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['5..'], operator: 'RangeExclusive' },
} as ColumnFilters;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(query).toBe(expectation);
});

it('should return a query to filter a search value between an inclusive range of numbers using the 2 dots (..) separator, the "RangeExclusive" operator and the range has an unbounded begin', () => {
const expectation = `$top=10&$filter=(Duration lt 5)`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.number } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['..5'], operator: 'RangeExclusive' },
} as ColumnFilters;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(query).toBe(expectation);
});

it('should return a query to filter a search value between an exclusive range of numbers using 2 search terms and the "RangeExclusive" operator', () => {
const expectation = `$top=10&$filter=(substringof('abc', Company) and (Duration gt 5 and Duration lt 22))`;
const mockColumnCompany = { id: 'company', field: 'company' } as Column;
Expand Down Expand Up @@ -1423,9 +1479,9 @@ describe('GridOdataService', () => {
expect(currentFilters).toEqual(presetFilters);
});

it('should return a query with a filter with range of numbers with decimals when the preset is a filter range with 3 dots (...) separator', () => {
it('should return a query with a filter with range of numbers with decimals when the preset is a filter range with 2 dots (..) separator and range ends with a fraction', () => {
serviceOptions.columnDefinitions = [{ id: 'company', field: 'company' }, { id: 'gender', field: 'gender' }, { id: 'duration', field: 'duration', type: FieldType.number }];
const expectation = `$top=10&$filter=(Duration ge 0.5 and Duration le .88)`;
const expectation = `$top=10&$filter=(Duration ge 0.5 and Duration le 0.88)`;
const presetFilters = [
{ columnId: 'duration', searchTerms: ['0.5...88'] },
] as CurrentFilter[];
Expand Down Expand Up @@ -1539,6 +1595,62 @@ describe('GridOdataService', () => {
expect(query).toBe(expectation);
expect(currentFilters).toEqual(presetFilters);
});

it('should return a query to filter a search value with a fraction of a number that is missing a leading 0', () => {
const expectation = `$filter=(Duration eq 0.22)`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.number } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['.22'] },
} as ColumnFilters;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(query).toBe(expectation);
});

it('should return a query without invalid characters to filter a search value with a number that contains invalid characters', () => {
const expectation = `$filter=(Duration eq -22)`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.number } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['-2a2'] },
} as ColumnFilters;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(query).toBe(expectation);
});

it('should return a query without invalid characters to filter a search value with an integer that contains invalid characters', () => {
const expectation = `$filter=(Duration eq 22)`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.integer } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['22;'] },
} as ColumnFilters;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(query).toBe(expectation);
});

it('should return a query with 0 to filter a search value when the search value contains a minus', () => {
const expectation = `$filter=(Duration eq 0)`;
const mockColumnDuration = { id: 'duration', field: 'duration', type: FieldType.float } as Column;
const mockColumnFilters = {
duration: { columnId: 'duration', columnDef: mockColumnDuration, searchTerms: ['-'] },
} as ColumnFilters;

service.init(serviceOptions, paginationOptions, gridStub);
service.updateFilters(mockColumnFilters, false);
const query = service.buildQuery();

expect(query).toBe(expectation);
});
});
});

Expand Down
Loading

0 comments on commit 48b4ebc

Please sign in to comment.