mirror of
https://github.com/nasa/openmct.git
synced 2025-06-17 06:38:17 +00:00
[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:
@ -13,7 +13,7 @@
|
|||||||
"provides": "searchService",
|
"provides": "searchService",
|
||||||
"type": "provider",
|
"type": "provider",
|
||||||
"implementation": "ElasticSearchProvider.js",
|
"implementation": "ElasticSearchProvider.js",
|
||||||
"depends": [ "$http", "objectService", "ELASTIC_ROOT" ]
|
"depends": [ "$http", "ELASTIC_ROOT" ]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"constants": [
|
"constants": [
|
||||||
|
@ -31,8 +31,7 @@ define([
|
|||||||
) {
|
) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var DEFAULT_MAX_RESULTS = 100,
|
var ID_PROPERTY = '_id',
|
||||||
ID_PROPERTY = '_id',
|
|
||||||
SOURCE_PROPERTY = '_source',
|
SOURCE_PROPERTY = '_source',
|
||||||
SCORE_PROPERTY = '_score';
|
SCORE_PROPERTY = '_score';
|
||||||
|
|
||||||
@ -42,14 +41,11 @@ define([
|
|||||||
*
|
*
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param $http Angular's $http service, for working with urls.
|
* @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
|
* @param ROOT the constant `ELASTIC_ROOT` which allows us to
|
||||||
* interact with ElasticSearch.
|
* interact with ElasticSearch.
|
||||||
*/
|
*/
|
||||||
function ElasticSearchProvider($http, objectService, ROOT) {
|
function ElasticSearchProvider($http, ROOT) {
|
||||||
this.$http = $http;
|
this.$http = $http;
|
||||||
this.objectService = objectService;
|
|
||||||
this.root = ROOT;
|
this.root = ROOT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,10 +61,6 @@ define([
|
|||||||
params = {},
|
params = {},
|
||||||
provider = this;
|
provider = this;
|
||||||
|
|
||||||
if (!maxResults) {
|
|
||||||
maxResults = DEFAULT_MAX_RESULTS;
|
|
||||||
}
|
|
||||||
|
|
||||||
searchTerm = this.cleanTerm(searchTerm);
|
searchTerm = this.cleanTerm(searchTerm);
|
||||||
searchTerm = this.fuzzyMatchUnquotedTerms(searchTerm);
|
searchTerm = this.fuzzyMatchUnquotedTerms(searchTerm);
|
||||||
|
|
||||||
@ -102,7 +94,7 @@ define([
|
|||||||
* @returns {string} search terms cleaned of excess whitespace.
|
* @returns {string} search terms cleaned of excess whitespace.
|
||||||
*/
|
*/
|
||||||
ElasticSearchProvider.prototype.cleanTerm = function (term) {
|
ElasticSearchProvider.prototype.cleanTerm = function (term) {
|
||||||
return term.trim().replace(/ +/, ' ');
|
return term.trim().replace(/ +/g, ' ');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -121,7 +113,8 @@ define([
|
|||||||
|
|
||||||
return query
|
return query
|
||||||
.replace(matcher, '~ ')
|
.replace(matcher, '~ ')
|
||||||
.replace('"~', '"');
|
.replace(/$/, '~')
|
||||||
|
.replace(/"~+/, '"');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,14 +19,151 @@
|
|||||||
* this source code distribution or the Licensing information page available
|
* this source code distribution or the Licensing information page available
|
||||||
* at runtime from the About dialog for additional information.
|
* 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.
|
* SearchSpec. Created by shale on 07/31/2015.
|
||||||
*/
|
*/
|
||||||
define([
|
define([
|
||||||
"../src/ElasticSearchProvider"
|
'../src/ElasticSearchProvider'
|
||||||
], function (ElasticSearchProvider) {
|
], function (
|
||||||
"use strict";
|
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
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user