import {describe, expect, it} from 'vitest'; import { convertToDataSourceQueryCollection } from '../../src/backend'; describe('query', () => { it('returns a data source query collection from search params', () => { const q = new URLSearchParams(); const collection = convertToDataSourceQueryCollection(q); expect(collection.expressions).toBeInstanceOf(Array); }); it('coerces a numeric value', () => { const q = new URLSearchParams({ attr: '2', attr2: '2', }); const collection = convertToDataSourceQueryCollection( q, { processEntries: { attr: { type: 'number' } }, }, ); expect(collection).toEqual({ type: 'and', expressions: [ { type: 'or', expressions: [ { lhs: 'attr', operator: '=', rhs: 2, }, ], }, { type: 'or', expressions: [ { lhs: 'attr2', operator: '=', rhs: '2', }, ], }, ], }); }); it('coerces a boolean value', () => { const q = new URLSearchParams({ attr: 'true', attr2: 'false', attr3: 'true', attr4: 'false', }); const collection = convertToDataSourceQueryCollection( q, { processEntries: { attr: { type: 'boolean', }, attr2: { type: 'boolean', } }, }, ); expect(collection).toEqual({ type: 'and', expressions: [ { type: 'or', expressions: [ { lhs: 'attr', operator: '=', rhs: true, }, ], }, { type: 'or', expressions: [ { lhs: 'attr2', operator: '=', rhs: false, }, ], }, { type: 'or', expressions: [ { lhs: 'attr3', operator: '=', rhs: 'true', }, ], }, { type: 'or', expressions: [ { lhs: 'attr4', operator: '=', rhs: 'false', }, ], }, ], }); }); it('returns an equal query', () => { const q = new URLSearchParams({ attr: 'foo' }); const collection = convertToDataSourceQueryCollection(q); expect(collection).toEqual({ type: 'and', expressions: [ { type: 'or', expressions: [ { lhs: 'attr', operator: '=', rhs: 'foo', }, ], }, ], }); }); it('returns an AND operator query', () => { const q = new URLSearchParams([ ['attr', 'foo'], ['attr2', 'bar'], ]); const collection = convertToDataSourceQueryCollection(q); expect(collection).toEqual({ type: 'and', expressions: [ { type: 'or', expressions: [ { lhs: 'attr', operator: '=', rhs: 'foo', }, ], }, { type: 'or', expressions: [ { lhs: 'attr2', operator: '=', rhs: 'bar', }, ], }, ], }); }); it('returns an OR operator query', () => { const q = new URLSearchParams([ ['attr', 'foo'], ['attr', 'bar'], ]); const collection = convertToDataSourceQueryCollection(q); expect(collection).toEqual({ type: 'and', expressions: [ { type: 'or', expressions: [ { lhs: 'attr', operator: '=', rhs: 'foo', }, { lhs: 'attr', operator: '=', rhs: 'bar', } ] }, ], }); }); it('returns an query with appropriate grouping', () => { const q = new URLSearchParams([ ['attr3', 'quux'], ['attr', 'foo'], ['attr4', 'quuux'], ['attr', 'bar'], ['attr2', 'baz'], ]); const collection = convertToDataSourceQueryCollection(q); expect(collection).toEqual({ type: 'and', expressions: [ { type: 'or', expressions: [ { lhs: 'attr3', operator: '=', rhs: 'quux', }, ], }, { type: 'or', expressions: [ { lhs: 'attr', operator: '=', rhs: 'foo', }, { lhs: 'attr', operator: '=', rhs: 'bar', } ] }, { type: 'or', expressions: [ { lhs: 'attr4', operator: '=', rhs: 'quuux', }, ], }, { type: 'or', expressions: [ { lhs: 'attr2', operator: '=', rhs: 'baz', }, ], }, ], }); }); });