Skip to content

Commit

Permalink
test: Add tests for SearchTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-suhas committed Jun 26, 2017
1 parent 7573a0a commit 1d63636
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
97 changes: 97 additions & 0 deletions test/core-test/search-template.test.js
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);
});
3 changes: 3 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ test('misc are exported', t => {
t.truthy(bob.InnerHits);
t.truthy(bob.innerHits);

t.truthy(bob.SearchTemplate);
t.truthy(bob.searchTemplate);

t.truthy(bob.prettyPrint);
});

Expand Down

0 comments on commit 1d63636

Please sign in to comment.