-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding tests for a factory class and params
Signed-off-by: Martin Gaievski <[email protected]>
- Loading branch information
1 parent
a07b379
commit cd49545
Showing
3 changed files
with
182 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
src/test/java/org/opensearch/neuralsearch/processor/factory/RRFProcessorFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.neuralsearch.processor.factory; | ||
|
||
import lombok.SneakyThrows; | ||
import org.opensearch.neuralsearch.processor.NormalizationProcessorWorkflow; | ||
import org.opensearch.neuralsearch.processor.RRFProcessor; | ||
import org.opensearch.neuralsearch.processor.combination.ScoreCombinationFactory; | ||
import org.opensearch.neuralsearch.processor.combination.ScoreCombiner; | ||
import org.opensearch.neuralsearch.processor.normalization.ScoreNormalizationFactory; | ||
import org.opensearch.neuralsearch.processor.normalization.ScoreNormalizer; | ||
import org.opensearch.search.pipeline.Processor; | ||
import org.opensearch.search.pipeline.SearchPhaseResultsProcessor; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.mockito.Mockito.mock; | ||
|
||
import static org.opensearch.neuralsearch.processor.factory.RRFProcessorFactory.PARAMETERS; | ||
import static org.opensearch.neuralsearch.processor.factory.RRFProcessorFactory.COMBINATION_CLAUSE; | ||
import static org.opensearch.neuralsearch.processor.factory.RRFProcessorFactory.TECHNIQUE; | ||
|
||
public class RRFProcessorFactoryTests extends OpenSearchTestCase { | ||
|
||
@SneakyThrows | ||
public void testCombinationParams_whenValidValues_thenSuccessful() { | ||
RRFProcessorFactory rrfProcessorFactory = new RRFProcessorFactory( | ||
new NormalizationProcessorWorkflow(new ScoreNormalizer(), new ScoreCombiner()), | ||
new ScoreNormalizationFactory(), | ||
new ScoreCombinationFactory() | ||
); | ||
final Map<String, Processor.Factory<SearchPhaseResultsProcessor>> processorFactories = new HashMap<>(); | ||
String tag = "tag"; | ||
String description = "description"; | ||
boolean ignoreFailure = false; | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put(COMBINATION_CLAUSE, new HashMap<>(Map.of(TECHNIQUE, "rrf", PARAMETERS, new HashMap<>(Map.of("rank_constant", 100))))); | ||
Processor.PipelineContext pipelineContext = mock(Processor.PipelineContext.class); | ||
SearchPhaseResultsProcessor searchPhaseResultsProcessor = rrfProcessorFactory.create( | ||
processorFactories, | ||
tag, | ||
description, | ||
ignoreFailure, | ||
config, | ||
pipelineContext | ||
); | ||
assertNotNull(searchPhaseResultsProcessor); | ||
assertTrue(searchPhaseResultsProcessor instanceof RRFProcessor); | ||
RRFProcessor rrfProcessor = (RRFProcessor) searchPhaseResultsProcessor; | ||
assertEquals("score-ranker-processor", rrfProcessor.getType()); | ||
} | ||
|
||
@SneakyThrows | ||
public void testInvalidCombinationParams_whenRankIsNegative_thenFail() { | ||
RRFProcessorFactory rrfProcessorFactory = new RRFProcessorFactory( | ||
new NormalizationProcessorWorkflow(new ScoreNormalizer(), new ScoreCombiner()), | ||
new ScoreNormalizationFactory(), | ||
new ScoreCombinationFactory() | ||
); | ||
final Map<String, Processor.Factory<SearchPhaseResultsProcessor>> processorFactories = new HashMap<>(); | ||
String tag = "tag"; | ||
String description = "description"; | ||
boolean ignoreFailure = false; | ||
|
||
Map<String, Object> config = new HashMap<>(); | ||
config.put(COMBINATION_CLAUSE, new HashMap<>(Map.of(TECHNIQUE, "rrf", PARAMETERS, new HashMap<>(Map.of("rank_constant", -1))))); | ||
Processor.PipelineContext pipelineContext = mock(Processor.PipelineContext.class); | ||
IllegalArgumentException exception = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> rrfProcessorFactory.create(processorFactories, tag, description, ignoreFailure, config, pipelineContext) | ||
); | ||
assertTrue( | ||
exception.getMessage().contains("rank constant must be in the interval between 1 and 10000, submitted rank constant: -1") | ||
); | ||
} | ||
|
||
@SneakyThrows | ||
public void testInvalidCombinationParams_whenRankIsTooLarge_thenFail() { | ||
RRFProcessorFactory rrfProcessorFactory = new RRFProcessorFactory( | ||
new NormalizationProcessorWorkflow(new ScoreNormalizer(), new ScoreCombiner()), | ||
new ScoreNormalizationFactory(), | ||
new ScoreCombinationFactory() | ||
); | ||
final Map<String, Processor.Factory<SearchPhaseResultsProcessor>> processorFactories = new HashMap<>(); | ||
String tag = "tag"; | ||
String description = "description"; | ||
boolean ignoreFailure = false; | ||
|
||
Map<String, Object> config = new HashMap<>(); | ||
config.put(COMBINATION_CLAUSE, new HashMap<>(Map.of(TECHNIQUE, "rrf", PARAMETERS, new HashMap<>(Map.of("rank_constant", 50_000))))); | ||
Processor.PipelineContext pipelineContext = mock(Processor.PipelineContext.class); | ||
IllegalArgumentException exception = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> rrfProcessorFactory.create(processorFactories, tag, description, ignoreFailure, config, pipelineContext) | ||
); | ||
assertTrue( | ||
exception.getMessage().contains("rank constant must be in the interval between 1 and 10000, submitted rank constant: 50000") | ||
); | ||
} | ||
|
||
@SneakyThrows | ||
public void testInvalidCombinationParams_whenRankIsNotNumeric_thenFail() { | ||
RRFProcessorFactory rrfProcessorFactory = new RRFProcessorFactory( | ||
new NormalizationProcessorWorkflow(new ScoreNormalizer(), new ScoreCombiner()), | ||
new ScoreNormalizationFactory(), | ||
new ScoreCombinationFactory() | ||
); | ||
final Map<String, Processor.Factory<SearchPhaseResultsProcessor>> processorFactories = new HashMap<>(); | ||
String tag = "tag"; | ||
String description = "description"; | ||
boolean ignoreFailure = false; | ||
|
||
Map<String, Object> config = new HashMap<>(); | ||
config.put( | ||
COMBINATION_CLAUSE, | ||
new HashMap<>(Map.of(TECHNIQUE, "rrf", PARAMETERS, new HashMap<>(Map.of("rank_constant", "string")))) | ||
); | ||
Processor.PipelineContext pipelineContext = mock(Processor.PipelineContext.class); | ||
IllegalArgumentException exception = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> rrfProcessorFactory.create(processorFactories, tag, description, ignoreFailure, config, pipelineContext) | ||
); | ||
assertTrue(exception.getMessage().contains("parameter [rank_constant] must be an integer")); | ||
} | ||
|
||
@SneakyThrows | ||
public void testInvalidCombinationName_whenUnsupportedFunction_thenFail() { | ||
RRFProcessorFactory rrfProcessorFactory = new RRFProcessorFactory( | ||
new NormalizationProcessorWorkflow(new ScoreNormalizer(), new ScoreCombiner()), | ||
new ScoreNormalizationFactory(), | ||
new ScoreCombinationFactory() | ||
); | ||
final Map<String, Processor.Factory<SearchPhaseResultsProcessor>> processorFactories = new HashMap<>(); | ||
String tag = "tag"; | ||
String description = "description"; | ||
boolean ignoreFailure = false; | ||
|
||
Map<String, Object> config = new HashMap<>(); | ||
config.put( | ||
COMBINATION_CLAUSE, | ||
new HashMap<>(Map.of(TECHNIQUE, "my_function", PARAMETERS, new HashMap<>(Map.of("rank_constant", 100)))) | ||
); | ||
Processor.PipelineContext pipelineContext = mock(Processor.PipelineContext.class); | ||
IllegalArgumentException exception = expectThrows( | ||
IllegalArgumentException.class, | ||
() -> rrfProcessorFactory.create(processorFactories, tag, description, ignoreFailure, config, pipelineContext) | ||
); | ||
assertTrue(exception.getMessage().contains("provided combination technique is not supported")); | ||
} | ||
} |