-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(view): Add generic match leaf view
This view can be instantiated many times
- Loading branch information
1 parent
05d7c93
commit b650df8
Showing
4 changed files
with
124 additions
and
1 deletion.
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
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
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,86 @@ | ||
const VariableStore = require('../../../lib/VariableStore'); | ||
const match = require('../../../view/leaf/match'); | ||
|
||
module.exports.tests = {}; | ||
|
||
module.exports.tests.base_usage = function(test, common) { | ||
test('input and field specified', function(t) { | ||
const vs = new VariableStore(); | ||
|
||
vs.var('match:example:input', 'input value'); | ||
vs.var('match:example:field', 'field value'); | ||
|
||
const view = match('example')(vs); | ||
|
||
const actual = JSON.parse(JSON.stringify(view)); | ||
|
||
const expected = { | ||
match: { | ||
['field value']: { | ||
query: 'input value' | ||
} | ||
} | ||
}; | ||
|
||
t.deepEqual(actual, expected, 'match view rendered as expected'); | ||
t.end(); | ||
}); | ||
|
||
test('optional parameters specified', function(t) { | ||
const vs = new VariableStore(); | ||
|
||
vs.var('match:example2:input', 'input value'); | ||
vs.var('match:example2:field', 'field value'); | ||
vs.var('match:example2:fuzziness', 2); | ||
vs.var('match:example2:cutoff_frequency', 0.01); | ||
vs.var('match:example2:analyzer', 'customAnalyzer'); | ||
|
||
const view = match('example2')(vs); | ||
|
||
const actual = JSON.parse(JSON.stringify(view)); | ||
|
||
const expected = { | ||
match: { | ||
['field value']: { | ||
query: 'input value', | ||
fuzziness: 2, | ||
cutoff_frequency: 0.01, | ||
analyzer: 'customAnalyzer' | ||
} | ||
} | ||
}; | ||
|
||
t.deepEqual(actual, expected, 'match view rendered with optional parameters'); | ||
t.end(); | ||
}); | ||
}; | ||
|
||
module.exports.tests.incorrect_usage = function(test, common) { | ||
test('no variables specified', function(t) { | ||
const vs = new VariableStore(); | ||
|
||
const view = match('broken_example')(vs); | ||
|
||
t.equal(view, null, 'should return null'); | ||
t.end(); | ||
}); | ||
|
||
test('no input specified', function(t) { | ||
const vs = new VariableStore(); | ||
vs.var('match:broken_example:field', 'field value'); | ||
|
||
const view = match('broken_example')(vs); | ||
|
||
t.equal(view, null, 'should return null'); | ||
t.end(); | ||
}); | ||
}; | ||
|
||
module.exports.all = function (tape, common) { | ||
function test(name, testFunction) { | ||
return tape('view/leaf/match ' + name, testFunction); | ||
} | ||
for( var testCase in module.exports.tests ){ | ||
module.exports.tests[testCase](test, common); | ||
} | ||
}; |
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,35 @@ | ||
const match = require('../../lib/leaf/match'); | ||
|
||
module.exports = function( prefix ){ | ||
return function( vs ){ | ||
const input_variable = `match:${prefix}:input`; | ||
const field_variable = `match:${prefix}:field`; | ||
|
||
if( !vs.isset(input_variable) || | ||
!vs.isset(field_variable) ) { | ||
return null; | ||
} | ||
|
||
const options = { }; | ||
|
||
const optional_params = ['boost', | ||
'operator', | ||
'analyzer', | ||
'cutoff_frequency', | ||
'fuzziness', | ||
'max_expansions', | ||
'prefix_length', | ||
'fuzzy_transpositions', | ||
'minimum_should_match', | ||
'zero_terms_query']; | ||
|
||
optional_params.forEach(function(param) { | ||
const variable_name = `match:${prefix}:${param}`; | ||
if (vs.isset(variable_name)) { | ||
options[param] = vs.var(variable_name); | ||
} | ||
}); | ||
|
||
return match(vs.var(field_variable), vs.var(input_variable), options); | ||
}; | ||
}; |