[Search] Add spec for ElasticSearchProvider

Add spec coverage for ElasticSearchProvider.  Also remove
unneeded guards for max number of results, as the aggregator
will always provide a max number of results.
This commit is contained in:
Pete Richards 2015-10-20 16:01:42 -07:00
parent 77d81f899b
commit 9a63e99710
3 changed files with 147 additions and 17 deletions

View File

@ -13,7 +13,7 @@
"provides": "searchService",
"type": "provider",
"implementation": "ElasticSearchProvider.js",
"depends": [ "$http", "objectService", "ELASTIC_ROOT" ]
"depends": [ "$http", "ELASTIC_ROOT" ]
}
],
"constants": [

View File

@ -31,8 +31,7 @@ define([
) {
"use strict";
var DEFAULT_MAX_RESULTS = 100,
ID_PROPERTY = '_id',
var ID_PROPERTY = '_id',
SOURCE_PROPERTY = '_source',
SCORE_PROPERTY = '_score';
@ -42,14 +41,11 @@ define([
*
* @constructor
* @param $http Angular's $http service, for working with urls.
* @param {ObjectService} objectService the service from which
* domain objects can be gotten.
* @param ROOT the constant `ELASTIC_ROOT` which allows us to
* interact with ElasticSearch.
*/
function ElasticSearchProvider($http, objectService, ROOT) {
function ElasticSearchProvider($http, ROOT) {
this.$http = $http;
this.objectService = objectService;
this.root = ROOT;
}
@ -65,10 +61,6 @@ define([
params = {},
provider = this;
if (!maxResults) {
maxResults = DEFAULT_MAX_RESULTS;
}
searchTerm = this.cleanTerm(searchTerm);
searchTerm = this.fuzzyMatchUnquotedTerms(searchTerm);
@ -102,7 +94,7 @@ define([
* @returns {string} search terms cleaned of excess whitespace.
*/
ElasticSearchProvider.prototype.cleanTerm = function (term) {
return term.trim().replace(/ +/, ' ');
return term.trim().replace(/ +/g, ' ');
};
/**
@ -121,7 +113,8 @@ define([
return query
.replace(matcher, '~ ')
.replace('"~', '"');
.replace(/$/, '~')
.replace(/"~+/, '"');
};
/**

View File

@ -19,14 +19,151 @@
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define,describe,it,expect,beforeEach,jasmine*/
/*global define,describe,it,expect,beforeEach,jasmine,spyOn,Promise,waitsFor*/
/**
* SearchSpec. Created by shale on 07/31/2015.
*/
define([
"../src/ElasticSearchProvider"
], function (ElasticSearchProvider) {
"use strict";
'../src/ElasticSearchProvider'
], function (
ElasticSearchProvider
) {
'use strict';
describe('ElasticSearchProvider', function () {
var $http,
ROOT,
provider;
beforeEach(function () {
$http = jasmine.createSpy('$http');
ROOT = 'http://localhost:9200';
provider = new ElasticSearchProvider($http, ROOT);
});
describe('query', function () {
beforeEach(function () {
spyOn(provider, 'cleanTerm').andReturn('cleanedTerm');
spyOn(provider, 'fuzzyMatchUnquotedTerms').andReturn('fuzzy');
spyOn(provider, 'parseResponse').andReturn('parsedResponse');
$http.andReturn(Promise.resolve({}));
});
it('cleans terms and adds fuzzyness', function () {
provider.query('hello', 10);
expect(provider.cleanTerm).toHaveBeenCalledWith('hello');
expect(provider.fuzzyMatchUnquotedTerms)
.toHaveBeenCalledWith('cleanedTerm');
});
it('calls through to $http', function () {
provider.query('hello', 10);
expect($http).toHaveBeenCalledWith({
method: 'GET',
params: {
q: 'fuzzy',
size: 10
},
url: 'http://localhost:9200/_search/'
});
});
it('gracefully fails when http fails', function () {
var promiseChainResolved = false;
$http.andReturn(Promise.reject());
provider
.query('hello', 10)
.then(function (results) {
expect(results).toEqual({
hits: [],
total: 0
});
promiseChainResolved = true;
});
waitsFor(function () {
return promiseChainResolved;
});
});
it('parses and returns when http succeeds', function () {
var promiseChainResolved = false;
$http.andReturn(Promise.resolve('successResponse'));
provider
.query('hello', 10)
.then(function (results) {
expect(provider.parseResponse)
.toHaveBeenCalledWith('successResponse');
expect(results).toBe('parsedResponse');
promiseChainResolved = true;
});
waitsFor(function () {
return promiseChainResolved;
});
});
});
it('can clean terms', function () {
expect(provider.cleanTerm(' asdhs ')).toBe('asdhs');
expect(provider.cleanTerm(' and some words'))
.toBe('and some words');
expect(provider.cleanTerm('Nice input')).toBe('Nice input');
});
it('can create fuzzy term matchers', function () {
expect(provider.fuzzyMatchUnquotedTerms('pwr dvc 43'))
.toBe('pwr~ dvc~ 43~');
expect(provider.fuzzyMatchUnquotedTerms(
'hello welcome "to quoted village" have fun'
)).toBe(
'hello~ welcome~ "to quoted village" have~ fun~'
);
});
it('can parse responses', function () {
var elasticSearchResponse = {
data: {
hits: {
total: 2,
hits: [
{
'_id': 'hit1Id',
'_source': 'hit1Model',
'_score': 0.56
},
{
'_id': 'hit2Id',
'_source': 'hit2Model',
'_score': 0.34
}
]
}
}
};
expect(provider.parseResponse(elasticSearchResponse))
.toEqual({
hits: [
{
id: 'hit1Id',
model: 'hit1Model',
score: 0.56
},
{
id: 'hit2Id',
model: 'hit2Model',
score: 0.34
}
],
total: 2
});
});
});
});