-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7573a0a
commit 1d63636
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import test from 'ava'; | ||
import sinon from 'sinon'; | ||
import { SearchTemplate, searchTemplate, matchQuery, termQuery } from '../../src'; | ||
import { makeSetsOptionMacro } from '../_macros'; | ||
|
||
const setsOption = makeSetsOptionMacro(searchTemplate); | ||
|
||
test(setsOption, 'inline', { | ||
param: { | ||
query: matchQuery('{{my_field}}', '{{my_value}}'), | ||
size: '{{my_size}}' | ||
} | ||
}); | ||
test(setsOption, 'file', { param: 'storedTemplate' }); | ||
test(setsOption, 'id', { param: 'indexedTemplate' }); | ||
test(setsOption, 'indexed', { param: 'indexedTemplate', keyName: 'id' }); | ||
test(setsOption, 'params', { | ||
param: { | ||
my_field: 'message', | ||
my_value: 'some message', | ||
my_size: 5 | ||
} | ||
}); | ||
|
||
test('constructor sets arguments', t => { | ||
let valueA = new SearchTemplate( | ||
'inline', | ||
'{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}' | ||
).toJSON(); | ||
let valueB = new SearchTemplate() | ||
.inline('{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}') | ||
.toJSON(); | ||
t.deepEqual(valueA, valueB); | ||
|
||
let expected = { | ||
inline: '{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}' | ||
}; | ||
t.deepEqual(valueA, expected); | ||
|
||
valueA = new SearchTemplate('file', 'storedTemplate').toJSON(); | ||
valueB = new SearchTemplate().file('storedTemplate').toJSON(); | ||
t.deepEqual(valueA, valueB); | ||
|
||
expected = { | ||
file: 'storedTemplate' | ||
}; | ||
t.deepEqual(valueA, expected); | ||
|
||
valueA = new SearchTemplate('id', 'indexedTemplate').toJSON(); | ||
valueB = new SearchTemplate().id('indexedTemplate').toJSON(); | ||
t.deepEqual(valueA, valueB); | ||
|
||
expected = { | ||
id: 'indexedTemplate' | ||
}; | ||
t.deepEqual(valueA, expected); | ||
|
||
const err = t.throws(() => new SearchTemplate('invalid_script_type', 'src'), Error); | ||
t.is(err.message, '`type` must be one of `inline`, `id`, `indexed`, `file`'); | ||
}); | ||
|
||
test.serial('mixed representaion', t => { | ||
const spy = sinon.spy(console, 'warn'); | ||
|
||
const value = new SearchTemplate().file('storedTemplate').id('indexedTemplate').toJSON(); | ||
const expected = { | ||
id: 'indexedTemplate' | ||
}; | ||
t.deepEqual(value, expected); | ||
|
||
t.true(spy.calledTwice); | ||
t.true( | ||
spy.firstCall.calledWith( | ||
'[SearchTemplate] Search template source(`inline`/`id`/`file`) was already specified!' | ||
) | ||
); | ||
t.true(spy.secondCall.calledWith('[SearchTemplate] Overwriting.')); | ||
console.warn.restore(); | ||
}); | ||
|
||
test('toJSON can handle elastic-builder objs', t => { | ||
const value = new SearchTemplate( | ||
'inline', | ||
'{ "query": { "bool": { "must": {{#toJson}}clauses{{/toJson}} } } }' | ||
) | ||
.params({ | ||
clauses: [termQuery('user', 'foo'), termQuery('user', 'bar')] | ||
}) | ||
.toJSON(); | ||
const expected = { | ||
inline: '{ "query": { "bool": { "must": {{#toJson}}clauses{{/toJson}} } } }', | ||
params: { | ||
clauses: [{ term: { user: 'foo' } }, { term: { user: 'bar' } }] | ||
} | ||
}; | ||
t.deepEqual(value, expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters