From 8a87e454a447c2e3d6d24a6283fe5eeef8d2fdb6 Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 7 Sep 2020 10:00:52 +0200 Subject: [PATCH] Updated test --- test/dsl/bool-optimize.test.ts | 4 +- test/dsl/boolean-and-helpers.test.ts | 28 ++++++ test/dsl/fluent-query.test.ts | 85 +++++++++++------- test/dsl/query.test.ts | 126 +++++++++++++++++++++++++-- 4 files changed, 201 insertions(+), 42 deletions(-) diff --git a/test/dsl/bool-optimize.test.ts b/test/dsl/bool-optimize.test.ts index a28fd2283..ac5aec7f9 100644 --- a/test/dsl/bool-optimize.test.ts +++ b/test/dsl/bool-optimize.test.ts @@ -596,7 +596,7 @@ test('nested with only should and minimum_should_match / 2', t => { Q.match('1', '2'), Q.term('3', '4') ), - Q.minShouldMatch(2) + Q.minShouldMatch(1) ), Q.should(Q.match('5', '6')) ) @@ -610,7 +610,7 @@ test('nested with only should and minimum_should_match / 2', t => { { match: { 1: '2' } }, { term: { 3: '4' } } ], - minimum_should_match: 2 + minimum_should_match: 1 } }], should: [{ diff --git a/test/dsl/boolean-and-helpers.test.ts b/test/dsl/boolean-and-helpers.test.ts index 23eb213b8..e4916726f 100644 --- a/test/dsl/boolean-and-helpers.test.ts +++ b/test/dsl/boolean-and-helpers.test.ts @@ -11,6 +11,12 @@ test('AND', t => { Q.bool(Q.filter(Q.term('baz', 'faz'))) ) + noShouldClausesWithName( + t, + Q.bool(Q.must(Q.match('foo', 'bar')), Q.name('name')), + Q.bool(Q.filter(Q.term('baz', 'faz'))) + ) + shouldClauses( t, Q.bool(Q.must(Q.match('foo', 'bar'))), @@ -241,6 +247,28 @@ test('AND', t => { }) } + function noShouldClausesWithName (t, query1, query2) { + t.test('No should clauses with name', t => { + t.deepEqual(Q.and(query1, query2), { + query: { + bool: { + must: [{ + bool: { + must: [{ match: { foo: 'bar' } }], + _name: 'name' + } + }], + filter: [{ term: { baz: 'faz' } }] + } + } + }) + + t.deepEqual(Q.and(query1, query2), Q.and(query2, query1)) + + t.end() + }) + } + function shouldClauses (t, query1, query2) { t.test('Should clauses', t => { t.deepEqual(Q.and(query1, query2), { diff --git a/test/dsl/fluent-query.test.ts b/test/dsl/fluent-query.test.ts index 03201ff8d..4a52e10d8 100644 --- a/test/dsl/fluent-query.test.ts +++ b/test/dsl/fluent-query.test.ts @@ -4,35 +4,26 @@ 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' +import { F, Q } from '../../dsl' test('match', t => { t.deepEqual(F() .match('foo', 'bar') .match('foo', 'baz') - .build(), { - query: { - bool: { - must: [ - { match: { foo: 'bar' } }, - { match: { foo: 'baz' } } - ] - } - } - }) + .build(), + Q( + Q.match('foo', 'bar'), + Q.match('foo', 'baz') + ) + ) t.deepEqual(F() .match('foo', ['bar', 'baz']) - .build(), { - query: { - bool: { - must: [ - { match: { foo: 'bar' } }, - { match: { foo: 'baz' } } - ] - } - } - }) + .build(), + Q( + Q.match('foo', ['bar', 'baz']) + ) + ) t.end() }) @@ -40,11 +31,9 @@ test('match', t => { test('matchPhrase', t => { t.deepEqual(F() .matchPhrase('foo', 'bar') - .build(), { - query: { - match_phrase: { foo: 'bar' }, - } - }) + .build(), + Q(Q.matchPhrase('foo', 'bar')) + ) t.end() }) @@ -52,11 +41,45 @@ test('matchPhrase', t => { test('matchPhrasePrefix', t => { t.deepEqual(F() .matchPhrasePrefix('foo', 'bar') - .build(), { - query: { - match_phrase_prefix: { foo: 'bar' }, - } - }) + .build(), + Q(Q.matchPhrasePrefix('foo', 'bar')) + ) + + t.end() +}) + +test('multiMatch', t => { + t.deepEqual(F() + .multiMatch(['foo1', 'foo2'], 'bar') + .build(), + Q(Q.multiMatch(['foo1', 'foo2'], 'bar')) + ) + + t.end() +}) + +test('matchAll', t => { + t.deepEqual(F() + .matchAll() + .build(), + Q(Q.matchAll()) + ) + + t.deepEqual(F() + .matchAll({ boost: 1 }) + .build(), + Q(Q.matchAll({ boost: 1 })) + ) + + t.end() +}) + +test('matchNone', t => { + t.deepEqual(F() + .matchNone() + .build(), + Q(Q.matchNone()) + ) t.end() }) diff --git a/test/dsl/query.test.ts b/test/dsl/query.test.ts index 9981dacad..58c7a3e6a 100644 --- a/test/dsl/query.test.ts +++ b/test/dsl/query.test.ts @@ -112,6 +112,12 @@ test('Compile a query (safe)', t => { } }) + try { + Q.compile(Q.bool()) + } catch (err) { + t.is(err.message, 'The query does not contain any use of `Q.params`') + } + t.end() }) @@ -144,6 +150,12 @@ test('Compile a query (unsafe)', t => { } }) + try { + Q.compileUnsafe(Q.bool()) + } catch (err) { + t.is(err.message, 'The query does not contain any use of `Q.params`') + } + t.end() }) @@ -329,6 +341,11 @@ test('terms returns a terms query', t => { terms: { foo: ['bar', 'baz'] } }) + t.deepEqual( + Q.terms('foo', ['bar', 'baz']), + Q.term('foo', ['bar', 'baz']) + ) + t.end() }) @@ -364,6 +381,12 @@ test('exists returns a exists query', t => { exists: { field: 'foo' } }) + t.deepEqual(Q.exists(['foo', 'bar']), [{ + exists: { field: 'foo' } + }, { + exists: { field: 'bar' } + }]) + t.end() }) @@ -743,6 +766,91 @@ test('bool returns a bool query block', t => { }) t.deepEqual(Q.bool(), { query: { bool: {} } }) + t.deepEqual(Q.bool(undefined), { query: { bool: {} } }) + + t.deepEqual(Q.bool(Q.must(c('foo')), Q.should(c('bar')), Q.mustNot(c('foz')), Q.filter(c('baz'), c('faz'))), { + query: { + bool: { + must: [c('foo')], + must_not: [c('foz')], + should: [c('bar')], + filter: [c('baz'), c('faz')] + } + } + }) + + t.deepEqual(Q.bool( + c('foo'), + Q.bool( + Q.name('name'), + Q.must(c('faz')) + ), + Q.should(c('bar')), + Q.filter(c('baz')) + ), { + query: { + bool: { + must: [ + c('foo'), + { bool: { must: [c('faz')], _name: 'name' } } + ], + should: [c('bar')], + filter: [c('baz')] + } + } + }) + + t.deepEqual(Q.bool( + Q.must(Q.term('hello', 'world')), + Q.match('foo', 'bar'), + Q.match('baz', 'faz'), + Q.minShouldMatch(1) + ), { + query: { + bool: { + must: [ + { term: { hello: 'world' } } + ], + should: [ + { match: { foo: 'bar' } }, + { match: { baz: 'faz' } } + ], + minimum_should_match: 1 + } + } + }) + + t.deepEqual(Q.bool( + Q.must(Q.term('hello', 'world')), + Q.match('foo', 'bar'), + Q.match('baz', 'faz'), + Q.minShouldMatch(2) + ), { + query: { + bool: { + must: [ + { term: { hello: 'world' } }, + { match: { foo: 'bar' } }, + { match: { baz: 'faz' } } + ] + } + } + }) + + t.deepEqual(Q.bool( + Q.match('foo', 'bar'), + Q.match('baz', 'faz'), + Q.minShouldMatch(2) + ), { + query: { + bool: { + must: [ + { match: { foo: 'bar' } }, + { match: { baz: 'faz' } } + ] + } + } + }) t.end() }) @@ -840,15 +948,15 @@ test('sort returns a sort block', t => { t.end() }) - t.test('multiple sorts', t => { - t.deepEqual(Q.sort([{ foo: { order: 'asc' } }, { bar: { order: 'desc' } }]), { - sort: [ - { foo: { order: 'asc' } }, - { bar: { order: 'desc' } } - ] - }) - t.end() - }) + // t.test('multiple sorts', t => { + // t.deepEqual(Q.sort([{ foo: { order: 'asc' } }, { bar: { order: 'desc' } }]), { + // sort: [ + // { foo: { order: 'asc' } }, + // { bar: { order: 'desc' } } + // ] + // }) + // t.end() + // }) t.end() })