[Search] Update spec for Generic Search Worker
This commit is contained in:
@@ -4,12 +4,12 @@
|
|||||||
* Administration. All rights reserved.
|
* Administration. All rights reserved.
|
||||||
*
|
*
|
||||||
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
||||||
* "License"); you may not use this file except in compliance with the License.
|
* 'License'); you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0.
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
* distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
|
||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
* License for the specific language governing permissions and limitations
|
* License for the specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
@@ -19,114 +19,205 @@
|
|||||||
* 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,runs,waitsFor,beforeEach,jasmine,Worker,require*/
|
/*global define,describe,it,expect,runs,waitsFor,beforeEach,jasmine,Worker,
|
||||||
|
require,afterEach*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SearchSpec. Created by shale on 07/31/2015.
|
* SearchSpec. Created by shale on 07/31/2015.
|
||||||
*/
|
*/
|
||||||
define(
|
define([
|
||||||
[],
|
|
||||||
function () {
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
describe("The generic search worker ", function () {
|
], function (
|
||||||
|
|
||||||
|
) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
describe('GenericSearchWorker', function () {
|
||||||
// If this test fails, make sure this path is correct
|
// If this test fails, make sure this path is correct
|
||||||
var worker = new Worker(require.toUrl('platform/search/src/services/GenericSearchWorker.js')),
|
var worker,
|
||||||
numObjects = 5;
|
objectX,
|
||||||
|
objectY,
|
||||||
|
objectZ,
|
||||||
|
itemsToIndex,
|
||||||
|
onMessage,
|
||||||
|
data,
|
||||||
|
waitForResult;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
var i;
|
worker = new Worker(
|
||||||
for (i = 0; i < numObjects; i += 1) {
|
require.toUrl('platform/search/src/services/GenericSearchWorker.js')
|
||||||
worker.postMessage(
|
|
||||||
{
|
|
||||||
request: "index",
|
|
||||||
id: i,
|
|
||||||
model: {
|
|
||||||
name: "object " + i,
|
|
||||||
id: i,
|
|
||||||
type: "something"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("searches can reach all objects", function () {
|
objectX = {
|
||||||
var flag = false,
|
id: 'x',
|
||||||
workerOutput,
|
model: {name: 'object xx'}
|
||||||
resultsLength = 0;
|
|
||||||
|
|
||||||
// Search something that should return all objects
|
|
||||||
runs(function () {
|
|
||||||
worker.postMessage(
|
|
||||||
{
|
|
||||||
request: "search",
|
|
||||||
input: "object",
|
|
||||||
maxResults: 100,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
timeout: 1000
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
worker.onmessage = function (event) {
|
|
||||||
var id;
|
|
||||||
|
|
||||||
workerOutput = event.data;
|
|
||||||
for (id in workerOutput.results) {
|
|
||||||
resultsLength += 1;
|
|
||||||
}
|
|
||||||
flag = true;
|
|
||||||
};
|
};
|
||||||
|
objectY = {
|
||||||
waitsFor(function () {
|
id: 'y',
|
||||||
return flag;
|
model: {name: 'object yy'}
|
||||||
}, "The worker should be searching", 1000);
|
|
||||||
|
|
||||||
runs(function () {
|
|
||||||
expect(workerOutput).toBeDefined();
|
|
||||||
expect(resultsLength).toEqual(numObjects);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("searches return only matches", function () {
|
|
||||||
var flag = false,
|
|
||||||
workerOutput,
|
|
||||||
resultsLength = 0;
|
|
||||||
|
|
||||||
// Search something that should return 1 object
|
|
||||||
runs(function () {
|
|
||||||
worker.postMessage(
|
|
||||||
{
|
|
||||||
request: "search",
|
|
||||||
input: "2",
|
|
||||||
maxResults: 100,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
timeout: 1000
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
worker.onmessage = function (event) {
|
|
||||||
var id;
|
|
||||||
|
|
||||||
workerOutput = event.data;
|
|
||||||
for (id in workerOutput.results) {
|
|
||||||
resultsLength += 1;
|
|
||||||
}
|
|
||||||
flag = true;
|
|
||||||
};
|
};
|
||||||
|
objectZ = {
|
||||||
|
id: 'z',
|
||||||
|
model: {name: 'object zz'}
|
||||||
|
};
|
||||||
|
itemsToIndex = [
|
||||||
|
objectX,
|
||||||
|
objectY,
|
||||||
|
objectZ
|
||||||
|
];
|
||||||
|
|
||||||
|
itemsToIndex.forEach(function (item) {
|
||||||
|
worker.postMessage({
|
||||||
|
request: 'index',
|
||||||
|
id: item.id,
|
||||||
|
model: item.model
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
onMessage = jasmine.createSpy('onMessage');
|
||||||
|
worker.addEventListener('message', onMessage);
|
||||||
|
|
||||||
|
waitForResult = function () {
|
||||||
waitsFor(function () {
|
waitsFor(function () {
|
||||||
return flag;
|
if (onMessage.calls.length > 0) {
|
||||||
}, "The worker should be searching", 1000);
|
data = onMessage.calls[0].args[0].data;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function () {
|
||||||
|
worker.terminate();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns search results for partial term matches', function () {
|
||||||
|
|
||||||
|
worker.postMessage({
|
||||||
|
request: 'search',
|
||||||
|
input: 'obj',
|
||||||
|
maxResults: 100,
|
||||||
|
queryId: 123
|
||||||
|
});
|
||||||
|
|
||||||
|
waitForResult();
|
||||||
|
|
||||||
runs(function () {
|
runs(function () {
|
||||||
expect(workerOutput).toBeDefined();
|
expect(onMessage).toHaveBeenCalled();
|
||||||
expect(resultsLength).toEqual(1);
|
|
||||||
expect(workerOutput.results[2]).toBeDefined();
|
expect(data.request).toBe('search');
|
||||||
|
expect(data.total).toBe(3);
|
||||||
|
expect(data.queryId).toBe(123);
|
||||||
|
expect(data.results.length).toBe(3);
|
||||||
|
expect(data.results[0].item.id).toBe('x');
|
||||||
|
expect(data.results[0].item.model).toEqual(objectX.model);
|
||||||
|
expect(data.results[0].matchCount).toBe(1);
|
||||||
|
expect(data.results[1].item.id).toBe('y');
|
||||||
|
expect(data.results[1].item.model).toEqual(objectY.model);
|
||||||
|
expect(data.results[1].matchCount).toBe(1);
|
||||||
|
expect(data.results[2].item.id).toBe('z');
|
||||||
|
expect(data.results[2].item.model).toEqual(objectZ.model);
|
||||||
|
expect(data.results[2].matchCount).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('scores exact term matches higher', function () {
|
||||||
|
worker.postMessage({
|
||||||
|
request: 'search',
|
||||||
|
input: 'object',
|
||||||
|
maxResults: 100,
|
||||||
|
queryId: 234
|
||||||
|
});
|
||||||
|
|
||||||
|
waitForResult();
|
||||||
|
|
||||||
|
runs(function () {
|
||||||
|
expect(data.queryId).toBe(234);
|
||||||
|
expect(data.results.length).toBe(3);
|
||||||
|
expect(data.results[0].item.id).toBe('x');
|
||||||
|
expect(data.results[0].matchCount).toBe(1.5);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can find partial term matches', function () {
|
||||||
|
worker.postMessage({
|
||||||
|
request: 'search',
|
||||||
|
input: 'x',
|
||||||
|
maxResults: 100,
|
||||||
|
queryId: 345
|
||||||
|
});
|
||||||
|
|
||||||
|
waitForResult();
|
||||||
|
|
||||||
|
runs(function () {
|
||||||
|
expect(data.queryId).toBe(345);
|
||||||
|
expect(data.results.length).toBe(1);
|
||||||
|
expect(data.results[0].item.id).toBe('x');
|
||||||
|
expect(data.results[0].matchCount).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('matches individual terms', function () {
|
||||||
|
worker.postMessage({
|
||||||
|
request: 'search',
|
||||||
|
input: 'x y z',
|
||||||
|
maxResults: 100,
|
||||||
|
queryId: 456
|
||||||
|
});
|
||||||
|
|
||||||
|
waitForResult();
|
||||||
|
|
||||||
|
runs(function () {
|
||||||
|
expect(data.queryId).toBe(456);
|
||||||
|
expect(data.results.length).toBe(3);
|
||||||
|
expect(data.results[0].item.id).toBe('x');
|
||||||
|
expect(data.results[0].matchCount).toBe(1);
|
||||||
|
expect(data.results[1].item.id).toBe('y');
|
||||||
|
expect(data.results[1].matchCount).toBe(1);
|
||||||
|
expect(data.results[2].item.id).toBe('z');
|
||||||
|
expect(data.results[1].matchCount).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('scores exact matches highest', function () {
|
||||||
|
worker.postMessage({
|
||||||
|
request: 'search',
|
||||||
|
input: 'object xx',
|
||||||
|
maxResults: 100,
|
||||||
|
queryId: 567
|
||||||
|
});
|
||||||
|
|
||||||
|
waitForResult();
|
||||||
|
|
||||||
|
runs(function () {
|
||||||
|
expect(data.queryId).toBe(567);
|
||||||
|
expect(data.results.length).toBe(3);
|
||||||
|
expect(data.results[0].item.id).toBe('x');
|
||||||
|
expect(data.results[0].matchCount).toBe(103);
|
||||||
|
expect(data.results[1].matchCount).toBe(1.5);
|
||||||
|
expect(data.results[2].matchCount).toBe(1.5);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('scores multiple term match above single match', function () {
|
||||||
|
worker.postMessage({
|
||||||
|
request: 'search',
|
||||||
|
input: 'obj x',
|
||||||
|
maxResults: 100,
|
||||||
|
queryId: 678
|
||||||
|
});
|
||||||
|
|
||||||
|
waitForResult();
|
||||||
|
|
||||||
|
runs(function () {
|
||||||
|
expect(data.queryId).toBe(678);
|
||||||
|
expect(data.results.length).toBe(3);
|
||||||
|
expect(data.results[0].item.id).toBe('x');
|
||||||
|
expect(data.results[0].matchCount).toBe(2);
|
||||||
|
expect(data.results[1].matchCount).toBe(1);
|
||||||
|
expect(data.results[2].matchCount).toBe(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user