Skip to content

Commit

Permalink
Fix jacoco and add unit tests for coverage
Browse files Browse the repository at this point in the history
Signed-off-by: acarbonetto <andrewc@bitquilltech.com>
  • Loading branch information
acarbonetto committed Aug 22, 2023
1 parent 014a31d commit 0d3652e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,37 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.opensearch.sql.ast.dsl.AstDSL.alias;
import static org.opensearch.sql.ast.dsl.AstDSL.and;
import static org.opensearch.sql.ast.dsl.AstDSL.filter;
import static org.opensearch.sql.ast.dsl.AstDSL.function;
import static org.opensearch.sql.ast.dsl.AstDSL.highlight;
import static org.opensearch.sql.ast.dsl.AstDSL.project;
import static org.opensearch.sql.ast.dsl.AstDSL.qualifiedName;
import static org.opensearch.sql.ast.dsl.AstDSL.relation;
import static org.opensearch.sql.ast.dsl.AstDSL.stringLiteral;
import static org.opensearch.sql.ast.dsl.AstDSL.unresolvedArg;
import static org.opensearch.sql.data.type.ExprCoreType.STRUCT;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.sql.ast.dsl.AstDSL;
import org.opensearch.sql.ast.expression.RelevanceFieldList;
import org.opensearch.sql.ast.expression.ScoreFunction;
import org.opensearch.sql.ast.expression.UnresolvedExpression;
import org.opensearch.sql.ast.tree.UnresolvedPlan;
import org.opensearch.sql.data.model.ExprTupleValue;
import org.opensearch.sql.exception.SemanticCheckException;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.function.BuiltinFunctionRepository;
import org.opensearch.sql.expression.function.FunctionName;
import org.opensearch.sql.expression.function.OpenSearchFunction;
Expand Down Expand Up @@ -124,28 +141,14 @@ public void analyze_filter_visit_score_function_with_unsupported_boost_SemanticC

@Test
public void analyze_relevance_field_list() {
// setup
// setup
OpenSearchFunction scoreFunction = new OpenSearchFunction(
new FunctionName("match_phrase_prefix"), List.of());
when(builtinFunctionRepository.compile(any(), any(), any())).thenReturn(scoreFunction);

UnresolvedPlan unresolvedPlan =
AstDSL.filter(
AstDSL.relation("schema"),
new ScoreFunction(
AstDSL.function(
"match_phrase_prefix",
AstDSL.unresolvedArg("field", stringLiteral("field_value1")),
AstDSL.unresolvedArg("query", stringLiteral("search query"))),
AstDSL.doubleLiteral(1.0)));

// test
LogicalPlan logicalPlan = analyze(unresolvedPlan);
OpenSearchFunction relevanceQuery =
(OpenSearchFunction) ((LogicalFilter) logicalPlan).getCondition();

// verify
assertEquals(true, relevanceQuery.isScoreTracked());
LinkedHashMap tuple = new LinkedHashMap(Map.of("Title", 1.0F, "Body", 4.2F, "Tags", 1.5F));

UnresolvedExpression unresolvedPlan = new RelevanceFieldList(tuple);
Expression relevanceFieldList = unresolvedPlan.accept(expressionAnalyzer, analysisContext);
assertEquals(STRUCT, relevanceFieldList.type());
assertTrue(relevanceFieldList.valueOf() instanceof ExprTupleValue);
assertEquals(tuple.get("Tags"), relevanceFieldList.valueOf().tupleValue().get("Tags").floatValue());
assertEquals(tuple.get("Body"), relevanceFieldList.valueOf().tupleValue().get("Body").floatValue());
assertEquals(tuple.get("Title"), relevanceFieldList.valueOf().tupleValue().get("Title").floatValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,16 @@ public void testCreateDataSourceMetadata() {
Mockito.verify(client.threadPool().getThreadContext(), Mockito.times(2)).stashContext();
}

@Test
public void testCreateDataSourceMetadataUninitialized() {
Mockito.when(clusterService.getClusterApplierService().isInitialClusterStateSet()).thenReturn(false);

List<DataSourceMetadata> dataSourceMetadataList =
openSearchDataSourceMetadataStorage.getDataSourceMetadata();

Assertions.assertEquals(0, dataSourceMetadataList.size());
}

@Test
public void testCreateDataSourceMetadataWithOutCreatingIndex() {
Mockito.when(clusterService.state().routingTable().hasIndex(DATASOURCE_INDEX_NAME))
Expand Down

0 comments on commit 0d3652e

Please sign in to comment.