From 164d5485e94dc0005de3e03b7363249729f5243c Mon Sep 17 00:00:00 2001 From: slhale Date: Mon, 3 Aug 2015 09:25:52 -0700 Subject: [PATCH] [Search] Wrote provider test Completed tests for GenericSearchProvider. Also fixed style in other tests. --- .../search/test/SearchAggregatorSpec.js | 8 +- .../ElasticsearchSearchProviderSpec.js | 42 ++--- .../providers/GenericSearchProviderSpec.js | 150 ++++++++++++++++++ .../test/workers/GenericSearchWorkerSpec.js | 22 +-- 4 files changed, 187 insertions(+), 35 deletions(-) diff --git a/platform/features/search/test/SearchAggregatorSpec.js b/platform/features/search/test/SearchAggregatorSpec.js index 907b06f082..148d149652 100644 --- a/platform/features/search/test/SearchAggregatorSpec.js +++ b/platform/features/search/test/SearchAggregatorSpec.js @@ -65,11 +65,11 @@ define( mockProviderResults.push({ hits: [ { - id: i, + id: i, score: 42 - i - }, + }, { - id: i + 1, + id: i + 1, score: 42 - (2 * i) } ] @@ -92,7 +92,7 @@ define( it("orders results by score", function () { for (i = 1; i < mockAggregatorResults.hits.length; i += 1) { expect(mockAggregatorResults.hits[i].score) - .not.toBeGreaterThan(mockAggregatorResults.hits[i-1].score); + .not.toBeGreaterThan(mockAggregatorResults.hits[i - 1].score); } }); diff --git a/platform/features/search/test/providers/ElasticsearchSearchProviderSpec.js b/platform/features/search/test/providers/ElasticsearchSearchProviderSpec.js index 77a6e23197..e127f63f11 100644 --- a/platform/features/search/test/providers/ElasticsearchSearchProviderSpec.js +++ b/platform/features/search/test/providers/ElasticsearchSearchProviderSpec.js @@ -29,6 +29,11 @@ define( function (ElasticsearchSearchProvider) { "use strict"; + // JSLint doesn't like underscore-prefixed properties, + // so hide them here. + var ID = "_id", + SCORE = "_score"; + describe("The ElasticSearch search provider ", function () { var mockHttp, mockHttpPromise, @@ -72,25 +77,22 @@ define( }); it("gets data from ElasticSearch", function () { - mockProviderResults = mockHttpPromise.then.mostRecentCall.args[0]( - { - data: { - hits: { - hits: [ - { - _id: 1, - _score: 1 - }, - { - _id: 2, - _score: 2 - } - ], - total: 0 - }, - timed_out: false - } - }); + var data = { + hits: { + hits: [ + {}, + {} + ], + total: 0 + }, + timed_out: false + }; + data.hits.hits[0][ID] = 1; + data.hits.hits[0][SCORE] = 1; + data.hits.hits[1][ID] = 2; + data.hits.hits[1][SCORE] = 2; + + mockProviderResults = mockHttpPromise.then.mostRecentCall.args[0]({data: data}); expect( mockObjectPromise.then.mostRecentCall.args[0]({ @@ -105,7 +107,7 @@ define( }); it("returns something when there is an ElasticSearch error", function () { - mockProviderResults = mockHttpPromise.catch.mostRecentCall.args[0](); + mockProviderResults = mockHttpPromise['catch'].mostRecentCall.args[0](); expect(mockProviderResults).toBeDefined(); }); }); diff --git a/platform/features/search/test/providers/GenericSearchProviderSpec.js b/platform/features/search/test/providers/GenericSearchProviderSpec.js index e69de29bb2..00f4e4ba36 100644 --- a/platform/features/search/test/providers/GenericSearchProviderSpec.js +++ b/platform/features/search/test/providers/GenericSearchProviderSpec.js @@ -0,0 +1,150 @@ +/***************************************************************************** + * Open MCT Web, Copyright (c) 2014-2015, United States Government + * as represented by the Administrator of the National Aeronautics and Space + * Administration. All rights reserved. + * + * 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. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * Open MCT Web includes source code licensed under additional open source + * licenses. See the Open Source Licenses file (LICENSES.md) included with + * 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*/ + +/** + * SearchSpec. Created by shale on 07/31/2015. + */ +define( + ["../../src/providers/GenericSearchProvider"], + function (GenericSearchProvider) { + "use strict"; + + describe("The generic search provider ", function () { + var mockQ, + mockDeferred, + mockObjectService, + mockObjectPromise, + mockDomainObjects, + mockComposition, + mockCompositionPromise, + mockWorkerService, + mockWorker, + mockRoots = ['root1', 'root2'], + provider, + mockProviderResults; + + beforeEach(function () { + var i; + + mockQ = jasmine.createSpyObj( + "$q", + [ "defer" ] + ); + mockDeferred = jasmine.createSpyObj( + "deferred", + [ "resolve", "reject"] + ); + mockDeferred.promise = "mock promise"; + mockQ.defer.andReturn(mockDeferred); + + mockObjectService = jasmine.createSpyObj( + "objectService", + [ "getObjects" ] + ); + mockObjectPromise = jasmine.createSpyObj( + "promise", + [ "then", "catch" ] + ); + mockObjectService.getObjects.andReturn(mockObjectPromise); + + + mockWorkerService = jasmine.createSpyObj( + "workerService", + [ "run" ] + ); + mockWorker = jasmine.createSpyObj( + "worker", + [ "postMessage" ] + ); + mockWorkerService.run.andReturn(mockWorker); + + mockDomainObjects = {}; + for (i = 0; i < 4; i += 1) { + mockDomainObjects[i] = ( + jasmine.createSpyObj( + "domainObject", + [ "getId", "getModel", "hasCapability", "getCapability"] + ) + ); + mockDomainObjects[i].getId.andReturn(i); + } + // Give the first object children + mockDomainObjects[0].hasCapability.andReturn(true); + mockComposition = jasmine.createSpyObj( + "composition", + [ "invoke" ] + ); + mockCompositionPromise = jasmine.createSpyObj( + "promise", + [ "then", "catch" ] + ); + mockComposition.invoke.andReturn(mockCompositionPromise); + mockDomainObjects[0].getCapability.andReturn(mockComposition); + + provider = new GenericSearchProvider(mockQ, mockObjectService, mockWorkerService, mockRoots); + }); + + it("indexes tree on initialization", function () { + mockObjectPromise.then.mostRecentCall.args[0](mockDomainObjects); + mockCompositionPromise.then.mostRecentCall.args[0](mockDomainObjects[1]); + + expect(mockObjectService.getObjects).toHaveBeenCalled(); + expect(mockWorker.postMessage).toHaveBeenCalled(); + }); + + it("sends search queries to the worker", function () { + var timestamp = Date.now(); + provider.query(' test "query" ', timestamp, 1, 2); + expect(mockWorker.postMessage).toHaveBeenCalledWith({ + request: "search", + input: ' test "query" ', + timestamp: timestamp, + maxNumber: 1, + timeout: 2 + }); + }); + + it("handles responses from the worker", function () { + var timestamp = Date.now(), + event = { + data: { + request: "search", + results: { + 1: 1, + 2: 2 + }, + total: 2, + timedOut: false, + timestamp: timestamp + } + }; + + provider.query(' test "query" ', timestamp); + mockWorker.onmessage(event); + mockObjectPromise.then.mostRecentCall.args[0](mockDomainObjects); + expect(mockDeferred.resolve).toHaveBeenCalled(); + }); + + }); + } +); \ No newline at end of file diff --git a/platform/features/search/test/workers/GenericSearchWorkerSpec.js b/platform/features/search/test/workers/GenericSearchWorkerSpec.js index 5e93288d50..3f9cb73e11 100644 --- a/platform/features/search/test/workers/GenericSearchWorkerSpec.js +++ b/platform/features/search/test/workers/GenericSearchWorkerSpec.js @@ -19,7 +19,7 @@ * 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,runs,waitsFor,beforeEach,jasmine,Worker*/ /** * SearchSpec. Created by shale on 07/31/2015. @@ -57,7 +57,7 @@ define( resultsLength = 0; // Search something that should return all objects - runs(function() { + runs(function () { worker.postMessage( { request: "search", @@ -67,7 +67,7 @@ define( timeout: 1000 } ); - }); + }); worker.onmessage = function (event) { var id; @@ -79,11 +79,11 @@ define( flag = true; }; - waitsFor(function() { + waitsFor(function () { return flag; }, "The worker should be searching", 1000); - runs(function() { + runs(function () { expect(workerOutput).toBeDefined(); expect(resultsLength).toEqual(numObjects); }); @@ -95,17 +95,17 @@ define( resultsLength = 0; // Search something that should return 1 object - runs(function() { + runs(function () { worker.postMessage( { request: "search", - input: "0", + input: "2", maxNumber: 100, timestamp: Date.now(), timeout: 1000 } ); - }); + }); worker.onmessage = function (event) { var id; @@ -117,14 +117,14 @@ define( flag = true; }; - waitsFor(function() { + waitsFor(function () { return flag; }, "The worker should be searching", 1000); - runs(function() { + runs(function () { expect(workerOutput).toBeDefined(); expect(resultsLength).toEqual(1); - expect(workerOutput.results[0]).toBeDefined(); + expect(workerOutput.results[2]).toBeDefined(); }); }); });