public async Task Search_WhereSearchTermPartiallyMatches_ReturnsResultsWithMatchingValues() { // Arrange List <TestableSearchResult> fakeResults = new List <TestableSearchResult>() { new TestableSearchResult() { Name = "XXX 1" }, new TestableSearchResult() { Name = "Result 2" }, new TestableSearchResult() { Name = "Result 3" } }; ISearchResultProvider <TestableSearchResult> provider = A.Fake <ISearchResultProvider <TestableSearchResult> >(); A.CallTo(() => provider.FetchAll()) .Returns(fakeResults); TestableSimpleSearcher searcher = new TestableSimpleSearcher(provider); // Act IList <TestableSearchResult> results = await searcher.Search("Result", 100, false); //Assert Assert.NotNull(results); Assert.DoesNotContain(results, r => r.Name == "XXX 1"); Assert.Contains(results, r => r.Name == "Result 2"); Assert.Contains(results, r => r.Name == "Result 3"); }
public async Task Search_WithMaximumOfOne_ReturnsOneResult() { // Arrange List <TestableSearchResult> fakeResults = new List <TestableSearchResult>() { new TestableSearchResult() { Name = "Result 1" }, new TestableSearchResult() { Name = "Result 2" } }; ISearchResultProvider <TestableSearchResult> provider = A.Fake <ISearchResultProvider <TestableSearchResult> >(); A.CallTo(() => provider.FetchAll()) .Returns(fakeResults); TestableSimpleSearcher searcher = new TestableSimpleSearcher(provider); // Act IList <TestableSearchResult> results = await searcher.Search("Result", 1, false); //Assert Assert.NotNull(results); Assert.Equal(1, results.Count); }
/// <summary> /// Returns results where the result name contains the whole search term, /// using a simple invariant string match. /// </summary> /// <param name="searchTerm">The string that must be contained.</param> /// <param name="maxResults">The maximum number of results to return. This must be at least 1.</param> /// <returns></returns> public async Task <IList <T> > Search(string searchTerm, int maxResults, bool asYouType) { if (maxResults < 1) { throw new ArgumentOutOfRangeException("maxResults", "The maximum number of results returned must be at least one."); } if (string.IsNullOrEmpty(searchTerm)) { return(new List <T>()); } IList <T> list = await searchResultProvider.FetchAll(); string invariantSearchTerm = ToInvariant(searchTerm); IEnumerable <T> results = list .Where(i => ToInvariant(ResultToString(i)).Contains(invariantSearchTerm)); IOrderedEnumerable <T> orderedResults = OrderResults(results); return(orderedResults .Take(maxResults) .ToList()); }
private async Task <IList <T> > FetchResults() { IList <T> results = await searchResultProvider.FetchAll(); foreach (T result in results) { if (result.GetMetadata <ResultMetadata>(Algorithm) == null) { ResultMetadata resultMetadata = ParseResult(result); result.AddMetadata(Algorithm, resultMetadata); } } return(results); }
public async Task Search_WithValidSearch_OrdersResults() { // Arrange List <TestableSearchResult> fakeResults = new List <TestableSearchResult>() { new TestableSearchResult() { Name = "Result C" }, new TestableSearchResult() { Name = "Result A" }, new TestableSearchResult() { Name = "Result B" } }; ISearchResultProvider <TestableSearchResult> provider = A.Fake <ISearchResultProvider <TestableSearchResult> >(); A.CallTo(() => provider.FetchAll()) .Returns(fakeResults); TestableSimpleSearcher searcher = new TestableSimpleSearcher(provider); // Act IList <TestableSearchResult> results = await searcher.Search("Result", 100, false); //Assert Assert.NotNull(results); Assert.Collection(results, r1 => Assert.Equal("Result A", r1.Name), r2 => Assert.Equal("Result B", r2.Name), r3 => Assert.Equal("Result C", r3.Name)); }