From c9ada839e3b8fc009bfaf1ad7aadaaf7677c4db2 Mon Sep 17 00:00:00 2001 From: Varun Jain Date: Wed, 27 Sep 2023 15:38:08 -0700 Subject: [PATCH] Added Visitor test cases and addressed comments Signed-off-by: Varun Jain --- .../processor/NeuralQueryProcessorIT.java | 7 ++- .../NeuralSearchQueryVisitorTests.java | 57 +++++++++++++++++++ .../util/NeuralSearchClusterUtilTests.java | 6 +- 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/opensearch/neuralsearch/query/visitor/NeuralSearchQueryVisitorTests.java diff --git a/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryProcessorIT.java b/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryProcessorIT.java index 5c7a58346..584388957 100644 --- a/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryProcessorIT.java +++ b/src/test/java/org/opensearch/neuralsearch/processor/NeuralQueryProcessorIT.java @@ -24,9 +24,9 @@ public class NeuralQueryProcessorIT extends BaseNeuralSearchIT { - public static final String index = "my-nlp-index"; - public static final String search_pipeline = "search-pipeline"; - public static final String ingest_pipeline = "nlp-pipeline"; + private static final String index = "my-nlp-index"; + private static final String search_pipeline = "search-pipeline"; + private static final String ingest_pipeline = "nlp-pipeline"; private static final String TEST_KNN_VECTOR_FIELD_NAME_1 = "test-knn-vector-1"; private static final int TEST_DIMENSION = 768; private static final SpaceType TEST_SPACE_TYPE = SpaceType.L2; @@ -47,6 +47,7 @@ public void tearDown() { findDeployedModels().forEach(this::deleteModel); } + @SneakyThrows public void testNeuralQueryProcessor() throws Exception { initializeIndexIfNotExist(); String modelId = getDeployedModelId(); diff --git a/src/test/java/org/opensearch/neuralsearch/query/visitor/NeuralSearchQueryVisitorTests.java b/src/test/java/org/opensearch/neuralsearch/query/visitor/NeuralSearchQueryVisitorTests.java new file mode 100644 index 000000000..7570ece54 --- /dev/null +++ b/src/test/java/org/opensearch/neuralsearch/query/visitor/NeuralSearchQueryVisitorTests.java @@ -0,0 +1,57 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.neuralsearch.query.visitor; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.lucene.search.BooleanClause; +import org.opensearch.neuralsearch.query.NeuralQueryBuilder; +import org.opensearch.test.OpenSearchTestCase; + +public class NeuralSearchQueryVisitorTests extends OpenSearchTestCase { + + public void testAccept_whenNeuralQueryBuilderWithoutModelId_thenSetModelId() { + String modelId = "bdcvjkcdjvkddcjxdjsc"; + NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder(); + neuralQueryBuilder.fieldName("passage_text"); + neuralQueryBuilder.k(768); + + NeuralSearchQueryVisitor neuralSearchQueryVisitor = new NeuralSearchQueryVisitor(modelId, null); + neuralSearchQueryVisitor.accept(neuralQueryBuilder); + + assertEquals(modelId, neuralQueryBuilder.modelId()); + } + + public void testAccept_whenNeuralQueryBuilderWithoutFieldModelId_thenSetFieldModelId() { + Map neuralInfoMap = new HashMap<>(); + neuralInfoMap.put("passage_text", "bdcvjkcdjvkddcjxdjsc"); + NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder(); + neuralQueryBuilder.fieldName("passage_text"); + neuralQueryBuilder.k(768); + + NeuralSearchQueryVisitor neuralSearchQueryVisitor = new NeuralSearchQueryVisitor(null, neuralInfoMap); + neuralSearchQueryVisitor.accept(neuralQueryBuilder); + + assertEquals("bdcvjkcdjvkddcjxdjsc", neuralQueryBuilder.modelId()); + } + + public void testAccept_whenNullValuesInVisitor_thenFail() { + NeuralQueryBuilder neuralQueryBuilder = new NeuralQueryBuilder(); + NeuralSearchQueryVisitor neuralSearchQueryVisitor = new NeuralSearchQueryVisitor(null, null); + + expectThrows(IllegalArgumentException.class, () -> neuralSearchQueryVisitor.accept(neuralQueryBuilder)); + } + + public void testGetChildVisitor() { + NeuralSearchQueryVisitor neuralSearchQueryVisitor = new NeuralSearchQueryVisitor(null, null); + + NeuralSearchQueryVisitor subVisitor = (NeuralSearchQueryVisitor) neuralSearchQueryVisitor.getChildVisitor(BooleanClause.Occur.MUST); + + assertEquals(subVisitor, neuralSearchQueryVisitor); + + } +} diff --git a/src/test/java/org/opensearch/neuralsearch/util/NeuralSearchClusterUtilTests.java b/src/test/java/org/opensearch/neuralsearch/util/NeuralSearchClusterUtilTests.java index 9f619d4c2..548f651e8 100644 --- a/src/test/java/org/opensearch/neuralsearch/util/NeuralSearchClusterUtilTests.java +++ b/src/test/java/org/opensearch/neuralsearch/util/NeuralSearchClusterUtilTests.java @@ -15,7 +15,7 @@ public class NeuralSearchClusterUtilTests extends OpenSearchTestCase { - public void testSingleNodeCluster() { + public void testMinNodeVersion_whenSingleNodeCluster_thenSuccess() { ClusterService clusterService = mockClusterService(Version.V_2_4_0); final NeuralSearchClusterUtil neuralSearchClusterUtil = NeuralSearchClusterUtil.instance(); @@ -26,7 +26,7 @@ public void testSingleNodeCluster() { assertTrue(Version.V_2_4_0.equals(minVersion)); } - public void testMultipleNodesCluster() { + public void testMinNodeVersion_whenMultipleNodesCluster_thenSuccess() { ClusterService clusterService = mockClusterService(Version.V_2_3_0); final NeuralSearchClusterUtil neuralSearchClusterUtil = NeuralSearchClusterUtil.instance(); @@ -37,7 +37,7 @@ public void testMultipleNodesCluster() { assertTrue(Version.V_2_3_0.equals(minVersion)); } - public void testWhenErrorOnClusterStateDiscover() { + public void testMinNodeVersion_WhenErrorOnClusterState_thenMatchCurrentVersion() { ClusterService clusterService = mock(ClusterService.class); when(clusterService.state()).thenThrow(new RuntimeException("Cluster state is not ready"));