|
| 1 | +using System.Text.Json.Nodes; |
| 2 | +using AzureSearchEmulator.Indexing; |
| 3 | +using AzureSearchEmulator.Models; |
| 4 | +using AzureSearchEmulator.SearchData; |
| 5 | +using AzureSearchEmulator.Searching; |
| 6 | +using Lucene.Net.Index; |
| 7 | +using Lucene.Net.Store; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace AzureSearchEmulator.UnitTests; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Covers the next page a search offers, which the controller returns as |
| 14 | +/// <c>@search.nextPageParameters</c>. |
| 15 | +/// </summary> |
| 16 | +/// <remarks> |
| 17 | +/// Azure offers one only when the request leaves out <c>top</c>: it then returns 50 results and, |
| 18 | +/// if more match, a continuation. A request that sets <c>top</c> gets no continuation. |
| 19 | +/// </remarks> |
| 20 | +public class SearchPagingTests : IDisposable |
| 21 | +{ |
| 22 | + private const int DocumentCount = 120; |
| 23 | + |
| 24 | + private readonly RAMDirectory _directory = new(); |
| 25 | + private readonly SearchIndex _index; |
| 26 | + private readonly LuceneNetIndexWriterFactory _writerFactory; |
| 27 | + private readonly LuceneNetIndexSearcher _searcher; |
| 28 | + |
| 29 | + public SearchPagingTests() |
| 30 | + { |
| 31 | + _index = new SearchIndex |
| 32 | + { |
| 33 | + Name = "paged", |
| 34 | + Fields = |
| 35 | + [ |
| 36 | + new SearchField { Name = "Id", Type = "Edm.String", Key = true, Searchable = false, Filterable = true, Sortable = true }, |
| 37 | + new SearchField { Name = "Even", Type = "Edm.Boolean", Filterable = true }, |
| 38 | + ] |
| 39 | + }; |
| 40 | + |
| 41 | + var factory = new SharedDirectoryFactory(_directory); |
| 42 | + _writerFactory = new LuceneNetIndexWriterFactory(factory); |
| 43 | + _searcher = new LuceneNetIndexSearcher(factory); |
| 44 | + |
| 45 | + var indexer = new LuceneNetSearchIndexer(_writerFactory, factory); |
| 46 | + |
| 47 | + indexer.IndexDocuments(_index, Enumerable.Range(0, DocumentCount) |
| 48 | + .Select(i => (IndexDocumentAction)new UploadIndexDocumentAction(new JsonObject |
| 49 | + { |
| 50 | + ["Id"] = i.ToString("D3"), |
| 51 | + ["Even"] = i % 2 == 0, |
| 52 | + })) |
| 53 | + .ToList()); |
| 54 | + } |
| 55 | + |
| 56 | + public void Dispose() |
| 57 | + { |
| 58 | + // Writer before directory: it holds an open handle and commits on dispose. |
| 59 | + _writerFactory.Dispose(); |
| 60 | + _directory.Dispose(); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task WithoutTop_ReturnsTheDefaultPageAndTheSkipForTheNextOne() |
| 65 | + { |
| 66 | + var response = await _searcher.Search(_index, new SearchRequest()); |
| 67 | + |
| 68 | + Assert.Equal(SearchRequest.DefaultTop, response.Results.Count); |
| 69 | + Assert.Equal(SearchRequest.DefaultTop, response.NextPageSkip); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// The walk a client makes by following each continuation: 50, 50, then the last 20, with |
| 74 | + /// every document returned once. |
| 75 | + /// </summary> |
| 76 | + [Fact] |
| 77 | + public async Task WithoutTop_FollowingEachNextPage_ReturnsEveryDocumentOnce() |
| 78 | + { |
| 79 | + var ids = new List<string>(); |
| 80 | + var pageSizes = new List<int>(); |
| 81 | + int? skip = 0; |
| 82 | + |
| 83 | + while (skip is { } nextSkip) |
| 84 | + { |
| 85 | + var response = await _searcher.Search(_index, new SearchRequest { Skip = nextSkip, Orderby = "Id" }); |
| 86 | + |
| 87 | + pageSizes.Add(response.Results.Count); |
| 88 | + ids.AddRange(response.Results.Select(r => r["Id"]!.GetValue<string>())); |
| 89 | + skip = response.NextPageSkip; |
| 90 | + } |
| 91 | + |
| 92 | + Assert.Equal([50, 50, 20], pageSizes); |
| 93 | + Assert.Equal(Enumerable.Range(0, DocumentCount).Select(i => i.ToString("D3")), ids); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public async Task WithoutTop_WhenEveryMatchFitsOnOnePage_OffersNoNextPage() |
| 98 | + { |
| 99 | + // 60 even documents, so skip 10 leaves exactly 50. |
| 100 | + var response = await _searcher.Search(_index, new SearchRequest { Filter = "Even eq true", Skip = 10 }); |
| 101 | + |
| 102 | + Assert.Equal(50, response.Results.Count); |
| 103 | + Assert.Null(response.NextPageSkip); |
| 104 | + } |
| 105 | + |
| 106 | + [Theory] |
| 107 | + [InlineData(10)] |
| 108 | + [InlineData(SearchRequest.DefaultTop)] |
| 109 | + [InlineData(1000)] |
| 110 | + public async Task WithTop_OffersNoNextPage(int top) |
| 111 | + { |
| 112 | + var response = await _searcher.Search(_index, new SearchRequest { Top = top }); |
| 113 | + |
| 114 | + Assert.Equal(Math.Min(top, DocumentCount), response.Results.Count); |
| 115 | + Assert.Null(response.NextPageSkip); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// A next page whose skip would pass <see cref="SearchRequest.MaxSkip"/> would be refused when |
| 120 | + /// sent back, so none is offered once the limit is reached. |
| 121 | + /// </summary> |
| 122 | + [Theory] |
| 123 | + [InlineData(SearchRequest.MaxSkip - 50, SearchRequest.MaxSkip)] |
| 124 | + [InlineData(SearchRequest.MaxSkip - 40, null)] |
| 125 | + public async Task WithoutTop_NearTheSkipLimit_OffersANextPageOnlyWithinIt(int skip, int? expectedNextPageSkip) |
| 126 | + { |
| 127 | + using var directory = new RAMDirectory(); |
| 128 | + var factory = new SharedDirectoryFactory(directory); |
| 129 | + using var writerFactory = new LuceneNetIndexWriterFactory(factory); |
| 130 | + |
| 131 | + new LuceneNetSearchIndexer(writerFactory, factory).IndexDocuments(_index, Enumerable.Range(0, SearchRequest.MaxSkip + 100) |
| 132 | + .Select(i => (IndexDocumentAction)new UploadIndexDocumentAction(new JsonObject { ["Id"] = i.ToString("D6") })) |
| 133 | + .ToList()); |
| 134 | + |
| 135 | + var response = await new LuceneNetIndexSearcher(factory).Search(_index, new SearchRequest { Skip = skip }); |
| 136 | + |
| 137 | + Assert.Equal(SearchRequest.DefaultTop, response.Results.Count); |
| 138 | + Assert.Equal(expectedNextPageSkip, response.NextPageSkip); |
| 139 | + } |
| 140 | + |
| 141 | + private class SharedDirectoryFactory(Lucene.Net.Store.Directory directory) : ILuceneDirectoryFactory, ILuceneIndexReaderFactory |
| 142 | + { |
| 143 | + public Lucene.Net.Store.Directory GetDirectory(string indexName) => directory; |
| 144 | + public void ClearCachedDirectory(string indexName) { } |
| 145 | + public IndexReader GetIndexReader(string indexName) => DirectoryReader.Open(directory); |
| 146 | + public IndexReader RefreshReader(string indexName) => GetIndexReader(indexName); |
| 147 | + public void ClearCachedReader(string indexName) { } |
| 148 | + } |
| 149 | +} |
0 commit comments