From f91617c7ec08b2ad77103ac6cbaa8e63bd3630be Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sat, 16 Oct 2021 00:32:04 +0200 Subject: [PATCH] Update tests --- .../cavallium/dbengine/ExpectedQueryType.java | 11 +- .../dbengine/TestLuceneSearches.java | 103 +++++++++--------- 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/src/test/java/it/cavallium/dbengine/ExpectedQueryType.java b/src/test/java/it/cavallium/dbengine/ExpectedQueryType.java index b9d2f30..8d36984 100644 --- a/src/test/java/it/cavallium/dbengine/ExpectedQueryType.java +++ b/src/test/java/it/cavallium/dbengine/ExpectedQueryType.java @@ -1,3 +1,12 @@ package it.cavallium.dbengine; -record ExpectedQueryType(boolean shard, boolean sorted, boolean sortedByScore, boolean complete, boolean onlyCount) {} +import it.cavallium.dbengine.client.MultiSort; +import it.cavallium.dbengine.client.SearchResultKey; +import it.cavallium.dbengine.client.query.BasicType; + +record ExpectedQueryType(boolean shard, boolean sorted, boolean sortedByScore, boolean complete, boolean onlyCount) { + + public ExpectedQueryType(boolean shard, MultiSort> multiSort, boolean complete, boolean onlyCount) { + this(shard, multiSort.isSorted(), multiSort.getQuerySort().getBasicType$() == BasicType.ScoreSort, complete, onlyCount); + } +} diff --git a/src/test/java/it/cavallium/dbengine/TestLuceneSearches.java b/src/test/java/it/cavallium/dbengine/TestLuceneSearches.java index fbe5406..e3f0f34 100644 --- a/src/test/java/it/cavallium/dbengine/TestLuceneSearches.java +++ b/src/test/java/it/cavallium/dbengine/TestLuceneSearches.java @@ -251,59 +251,69 @@ public class TestLuceneSearches { } } - @ParameterizedTest - @MethodSource("provideQueryArgumentsScoreModeAndSort") - public void testSearchNoDocs(boolean shards, MultiSort> multiSort) throws Throwable { - var sorted = multiSort.isSorted(); - var sortedByScore = multiSort.getQuerySort().getBasicType$() == BasicType.ScoreSort; - runSearchers(new ExpectedQueryType(shards, sorted, sortedByScore, true, false), searcher -> { - var luceneIndex = getLuceneIndex(shards, searcher); - ClientQueryParamsBuilder> queryBuilder = ClientQueryParams.builder(); - queryBuilder.query(new MatchNoDocsQuery()); - queryBuilder.snapshot(null); - queryBuilder.complete(true); - queryBuilder.sort(multiSort); - var query = queryBuilder.build(); + public void testSearch(ClientQueryParamsBuilder> queryParamsBuilder, + ExpectedQueryType expectedQueryType) throws Throwable { + + runSearchers(expectedQueryType, searcher -> { + var luceneIndex = getLuceneIndex(expectedQueryType.shard(), searcher); + var query = queryParamsBuilder.build(); try (var results = run(luceneIndex.search(query)).receive()) { var hits = results.totalHitsCount(); - assertExactHits(searcher, query, 0, hits); - var keys = getResults(results); - assertEquals(List.of(), keys); + if (hits.exact()) { + Assertions.assertEquals(keys.size(), hits.value()); + } else { + Assertions.assertTrue(keys.size() >= hits.value()); + } + + var officialSearcher = new OfficialSearcher(); + luceneIndex = getLuceneIndex(expectedQueryType.shard(), officialSearcher); + var officialQuery = queryParamsBuilder.limit(ELEMENTS.size() * 2L).build(); + try (var officialResults = run(luceneIndex.search(officialQuery)).receive()) { + var officialHits = officialResults.totalHitsCount(); + var officialKeys = getResults(officialResults).stream().toList(); + if (officialHits.exact()) { + Assertions.assertEquals(officialKeys.size(), officialHits.value()); + } else { + Assertions.assertTrue(officialKeys.size() >= officialHits.value()); + } + + if (hits.exact() && officialHits.exact()) { + assertExactHits(officialHits.value(), hits); + } + + Assertions.assertEquals(officialKeys.size(), keys.size()); + + assertResults(officialKeys, keys, expectedQueryType.sorted(), expectedQueryType.sortedByScore()); + } } }); } + @ParameterizedTest + @MethodSource("provideQueryArgumentsScoreModeAndSort") + public void testSearchNoDocs(boolean shards, MultiSort> multiSort) throws Throwable { + ClientQueryParamsBuilder> queryBuilder = ClientQueryParams.builder(); + queryBuilder.query(new MatchNoDocsQuery()); + queryBuilder.snapshot(null); + queryBuilder.complete(true); + queryBuilder.sort(multiSort); + + ExpectedQueryType expectedQueryType = new ExpectedQueryType(shards, multiSort, true, false); + testSearch(queryBuilder, expectedQueryType); + } + @ParameterizedTest @MethodSource("provideQueryArgumentsScoreModeAndSort") public void testSearchAllDocs(boolean shards, MultiSort> multiSort) throws Throwable { - var sorted = multiSort.isSorted(); - var sortedByScore = multiSort.getQuerySort().getBasicType$() == BasicType.ScoreSort; - runSearchers(new ExpectedQueryType(shards, sorted, sortedByScore, true, false), (LocalSearcher searcher) -> { - var luceneIndex = getLuceneIndex(shards, searcher); - ClientQueryParamsBuilder> queryBuilder = ClientQueryParams.builder(); - queryBuilder.query(new MatchAllDocsQuery()); - queryBuilder.snapshot(null); - queryBuilder.complete(true); - queryBuilder.sort(multiSort); - var query = queryBuilder.build(); - try (var results = run(luceneIndex.search(query)).receive()) { - var hits = results.totalHitsCount(); - assertHitsIfPossible(ELEMENTS.size(), hits); + ClientQueryParamsBuilder> queryBuilder = ClientQueryParams.builder(); + queryBuilder.query(new MatchAllDocsQuery()); + queryBuilder.snapshot(null); + queryBuilder.complete(true); + queryBuilder.sort(multiSort); - var keys = getResults(results); - - var officialSearcher = new OfficialSearcher(); - luceneIndex = getLuceneIndex(shards, officialSearcher); - var officialQuery = queryBuilder.limit(ELEMENTS.size() * 2L).build(); - try (var officialResults = run(luceneIndex.search(officialQuery)).receive()) { - var officialKeys = getResults(officialResults).stream().toList(); - - assertResults(officialKeys, keys, sorted, sortedByScore); - } - - } - }); + ExpectedQueryType expectedQueryType = new ExpectedQueryType(shards, multiSort, true, false); + testSearch(queryBuilder, expectedQueryType); } private void assertResults(List expectedKeys, List resultKeys, boolean sorted, boolean sortedByScore) { @@ -335,13 +345,8 @@ public class TestLuceneSearches { } } - private void assertExactHits(LocalSearcher searcher, - ClientQueryParams> query, - long expectedCount, - TotalHitsCount hits) { - if (supportsPreciseHitsCount(searcher, query)) { - assertEquals(new TotalHitsCount(expectedCount, true), hits); - } + private void assertExactHits(long expectedCount, TotalHitsCount hits) { + assertEquals(new TotalHitsCount(expectedCount, true), hits); } private List getResults(SearchResultKeys results) {