Updated test
This commit is contained in:
62
test/dsl/fluent-query.test.ts
Normal file
62
test/dsl/fluent-query.test.ts
Normal file
@ -0,0 +1,62 @@
|
||||
'use strict'
|
||||
|
||||
import { test } from 'tap'
|
||||
/* eslint-disable no-unused-vars */
|
||||
import * as types from '../../dsl/lib/types'
|
||||
/* eslint-enable no-unused-vars */
|
||||
import { F } from '../../dsl'
|
||||
|
||||
test('match', t => {
|
||||
t.deepEqual(new F()
|
||||
.match('foo', 'bar')
|
||||
.match('foo', 'baz')
|
||||
.build(), {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{ match: { foo: 'bar' } },
|
||||
{ match: { foo: 'baz' } }
|
||||
]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.deepEqual(new F()
|
||||
.match('foo', ['bar', 'baz'])
|
||||
.build(), {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{ match: { foo: 'bar' } },
|
||||
{ match: { foo: 'baz' } }
|
||||
]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('matchPhrase', t => {
|
||||
t.deepEqual(new F()
|
||||
.matchPhrase('foo', 'bar')
|
||||
.build(), {
|
||||
query: {
|
||||
match_phrase: { foo: 'bar' },
|
||||
}
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('matchPhrasePrefix', t => {
|
||||
t.deepEqual(new F()
|
||||
.matchPhrasePrefix('foo', 'bar')
|
||||
.build(), {
|
||||
query: {
|
||||
match_phrase_prefix: { foo: 'bar' },
|
||||
}
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
@ -59,10 +59,24 @@ test('Q is a function that creates the final query object', t => {
|
||||
[randomTopLevelKey]: 42
|
||||
})
|
||||
|
||||
t.deepEqual(Q({ match: { foo: 'bar' } }, { [randomTopLevelKey]: 42 }), {
|
||||
query: {
|
||||
match: { foo: 'bar' }
|
||||
},
|
||||
[randomTopLevelKey]: 42
|
||||
})
|
||||
|
||||
t.deepEqual(Q({ function_score: { foo: 'bar' } }, { [randomTopLevelKey]: 42 }), {
|
||||
query: {
|
||||
function_score: { foo: 'bar' }
|
||||
},
|
||||
[randomTopLevelKey]: 42
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Compile a query', t => {
|
||||
test('Compile a query (safe)', t => {
|
||||
t.type(Q.param, 'function')
|
||||
t.type(Q.compile, 'function')
|
||||
|
||||
@ -94,6 +108,38 @@ test('Compile a query', t => {
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Compile a query (unsafe)', t => {
|
||||
t.type(Q.param, 'function')
|
||||
t.type(Q.compile, 'function')
|
||||
|
||||
const query = Q(
|
||||
{ a: Q.param('a') },
|
||||
{ b: Q.param('b') },
|
||||
{ c: Q.param('c') }
|
||||
)
|
||||
|
||||
interface Input {
|
||||
a: number,
|
||||
b: number,
|
||||
c: number
|
||||
}
|
||||
const compiledQuery = Q.compileUnsafe<Input>(query)
|
||||
|
||||
t.deepEqual(compiledQuery({ a: 1, b: 2, c: 3 }), {
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{ a: 1 },
|
||||
{ b: 2 },
|
||||
{ c: 3 }
|
||||
]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('match returns a match query', t => {
|
||||
t.type(Q.match, 'function')
|
||||
|
||||
Reference in New Issue
Block a user