Skip to content

Commit

Permalink
Added more unit tests for search engine
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed May 16, 2018
1 parent dc6e584 commit a19f98c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/tests/unit/search-engine.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { SearchResultItem } from "../../ts/search-result-item";
import { SearchEngine } from "./../../ts/search-engine";
import { CountManager } from "../../ts/count-manager";
import {FakeCountRepository} from "./fake-count-repository";
import { Count } from "../../ts/count";

function getFakeItems(items: string[]): SearchResultItem[] {
return items.map((i): SearchResultItem => {
Expand Down Expand Up @@ -43,5 +46,45 @@ describe("SearchEngine", (): void => {
expect(actual.length).toBeGreaterThan(0);
expect(actual[0].name).toBe("hans");
});

it("should list frequently used items higher", (): void => {
const fakeCount: Count = {
abc: 5,
abcd: 6,
abcde: 7,
};
const fakeCountRepo = new FakeCountRepository(fakeCount);
const countManager = new CountManager(fakeCountRepo);
const fakeItems = getFakeItems(["abc", "abcd", "abcde"]);
const searchEngine = new SearchEngine(fakeItems);
const userInput = "ab";

const result = searchEngine.search(userInput, countManager);

expect(result.length).toBe(3);
expect(result[0].name).toBe("abcde");
expect(result[1].name).toBe("abcd");
expect(result[2].name).toBe("abc");
});

it("should not list frequently used items higher if their count is 4 or lower", (): void => {
const fakeCount: Count = {
abc: 1,
abcd: 2,
abcde: 3,
};
const fakeCountRepo = new FakeCountRepository(fakeCount);
const countManager = new CountManager(fakeCountRepo);
const fakeItems = getFakeItems(["abc", "abcd", "abcde"]);
const searchEngine = new SearchEngine(fakeItems);
const userInput = "ab";

const result = searchEngine.search(userInput, countManager);

expect(result.length).toBe(3);
expect(result[0].name).toBe("abc");
expect(result[1].name).toBe("abcd");
expect(result[2].name).toBe("abcde");
});
});
});
2 changes: 1 addition & 1 deletion src/ts/search-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class SearchEngine {
for (let i = 0; i < searchResults.length; i++) {
const score = count[searchResults[i].item.executionArgument];

if (score !== undefined) {
if (score !== undefined && score > 4) {
searchResults[i].score /= (score * 0.25);
}
}
Expand Down

0 comments on commit a19f98c

Please sign in to comment.