90 lines
No EOL
3.4 KiB
JavaScript
90 lines
No EOL
3.4 KiB
JavaScript
import { describe, it } from 'node:test'
|
|
import assert from '../assert.js'
|
|
import { and, eq, expr, gt, gte, lt, lte, not, or, params, whereClause, xor } from '../../src/query-parser/where.js'
|
|
import { And, Equal, Filter, GreaterThan, GreaterThanEqual, LesserThan, LesserThanEqual, Literal, Or } from '../../src/query-parser/types.js'
|
|
|
|
const l = x => new Literal(x)
|
|
describe('where operators', () => {
|
|
it('or', () => {
|
|
assert.parseOk(or, 'OR', ([actual]) => {
|
|
assert.strictEqual(actual.apply([true, true]), true)
|
|
assert.strictEqual(actual.apply([false, true]), true)
|
|
assert.strictEqual(actual.apply([true, false]), true)
|
|
assert.strictEqual(actual.apply([false, false]), false)
|
|
})
|
|
})
|
|
|
|
it('and', () => {
|
|
assert.parseOk(and, 'AND', ([actual]) => {
|
|
assert.strictEqual(actual.apply([true, true]), true)
|
|
assert.strictEqual(actual.apply([false, true]), false)
|
|
assert.strictEqual(actual.apply([true, false]), false)
|
|
assert.strictEqual(actual.apply([false, false]), false)
|
|
})
|
|
})
|
|
|
|
it('xor', () => {
|
|
assert.parseOk(xor, 'XOR', ([actual]) => {
|
|
assert.strictEqual(actual.apply([true, true]), false)
|
|
assert.strictEqual(actual.apply([false, true]), true)
|
|
assert.strictEqual(actual.apply([true, false]), true)
|
|
assert.strictEqual(actual.apply([false, false]), false)
|
|
})
|
|
})
|
|
|
|
it('not', () => {
|
|
assert.parseOk(not, 'NOT', ([actual]) => {
|
|
assert.strictEqual(actual.apply([true]), false)
|
|
assert.strictEqual(actual.apply([false]), true)
|
|
})
|
|
})
|
|
|
|
it('gt', () => {
|
|
assert.parseOk(gt, '>', ([actual]) => {
|
|
assert.strictEqual(actual.apply([1, 2]), false)
|
|
assert.strictEqual(actual.apply([2, 1]), true)
|
|
assert.strictEqual(actual.apply([1, 1]), false)
|
|
})
|
|
})
|
|
|
|
it('gte', () => {
|
|
assert.parseOk(gte, '>=', ([actual]) => {
|
|
assert.strictEqual(actual.apply([1, 2]), false)
|
|
assert.strictEqual(actual.apply([2, 1]), true)
|
|
assert.strictEqual(actual.apply([1, 1]), true)
|
|
})
|
|
})
|
|
|
|
it('lt', () => {
|
|
assert.parseOk(lt, '<', ([actual]) => {
|
|
assert.strictEqual(actual.apply([1, 2]), true)
|
|
assert.strictEqual(actual.apply([2, 1]), false)
|
|
assert.strictEqual(actual.apply([1, 1]), false)
|
|
})
|
|
})
|
|
|
|
it('lte', () => {
|
|
assert.parseOk(lte, '<=', ([actual]) => {
|
|
assert.strictEqual(actual.apply([1, 2]), true)
|
|
assert.strictEqual(actual.apply([2, 1]), false)
|
|
assert.strictEqual(actual.apply([1, 1]), true)
|
|
})
|
|
})
|
|
|
|
it('eq', () => {
|
|
assert.parseOk(eq, '=', ([actual]) => {
|
|
assert.strictEqual(actual.apply([1, 2]), false)
|
|
assert.strictEqual(actual.apply([2, 1]), false)
|
|
assert.strictEqual(actual.apply([1, 1]), true)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('WHERE keyword', () => {
|
|
it('handles complex filters', () => {
|
|
assert.parseOk(whereClause, 'WHERE 1.1 > 5 AND (1 = 500 OR 2 > 3) OR 5 <= 2', ([actual]) => {
|
|
const expected = new Filter(l(1.1), GreaterThan, l(5), And, l('('), l(1), Equal, l(500), Or, l(2), GreaterThan, l(3), l(')'), Or, l(5), LesserThanEqual, l(2))
|
|
assert.deepEqual(actual, expected)
|
|
})
|
|
})
|
|
}) |