Skip to content

Commit

Permalink
Added Tests For Full Code Coverage
Browse files Browse the repository at this point in the history
Signed-off-by: GabeFernandez310 <[email protected]>
  • Loading branch information
GabeFernandez310 committed Oct 26, 2022
1 parent 451ec4c commit f2d77c6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2987,7 +2987,7 @@ Description

``query("[query_expression]+" ,[, option=<option_value>]*)``

The query function maps to the query_string query used in search engine, to return the documents that match a provided text, number, date or boolean value with a given field or fields.
This is an alternative syntax to the query_string function. The query function maps to the query_string query used in search engine, to return the documents that match a provided text, number, date or boolean value with a given field or fields.
``query_expression`` must be a string provided in Lucene query string syntax. Please refer to examples below:

| ``query('Tags:taste OR Body:taste', ...)``
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class QueryTest {
private static final DSL dsl = new ExpressionConfig()
.dsl(new ExpressionConfig().functionRepository());
private final QueryQuery queryQuery = new QueryQuery();
private final FunctionName queryFunc = FunctionName.of("query_string");
private final FunctionName queryFunc = FunctionName.of("query");
private static final LiteralExpression query_value = DSL.literal("title:query_value");

static Stream<List<Expression>> generateValidData() {
Expand Down Expand Up @@ -104,6 +104,16 @@ public void test_SemanticCheckException_when_invalid_parameter() {
() -> queryQuery.build(new QueryExpression(arguments)));
}

@Test
public void test_SemanticCheckException_when_sending_parameter_multiple_times() {
List<Expression> arguments = List.of(
namedArgument("query", query_value),
namedArgument("allow_leading_wildcard", DSL.literal("true")),
namedArgument("allow_leading_wildcard", DSL.literal("true")));
Assertions.assertThrows(SemanticCheckException.class,
() -> queryQuery.build(new QueryExpression(arguments)));
}

private NamedArgumentExpression namedArgument(String name, String value) {
return dsl.namedArgument(name, DSL.literal(value));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.opensearch.storage.script.filter.lucene.relevance;

import com.google.common.collect.ImmutableMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.opensearch.sql.data.model.ExprValueUtils;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.expression.LiteralExpression;
import org.opensearch.sql.expression.config.ExpressionConfig;

import java.util.List;
import java.util.Map;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class NoFieldQueryTest {
NoFieldQuery query;
private final DSL dsl = new ExpressionConfig().dsl(new ExpressionConfig().functionRepository());
private final String testQueryName = "test_query";
private final Map<String, RelevanceQuery.QueryBuilderStep> actionMap
= ImmutableMap.of("paramA", (o, v) -> o);

@BeforeEach
void setUp() {
query = mock(NoFieldQuery.class,
Mockito.withSettings().useConstructor(actionMap)
.defaultAnswer(Mockito.CALLS_REAL_METHODS));
when(query.getQueryName()).thenReturn(testQueryName);
}

@Test
void createQueryBuilderTest() {
String sampleQuery = "field:query";

query.createQueryBuilder(List.of(
dsl.namedArgument("query",
new LiteralExpression(ExprValueUtils.stringValue(sampleQuery)))));

verify(query).createBuilder(eq(sampleQuery));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,30 @@ public void can_parse_query_relevance_function() {
"SELECT id FROM test WHERE query(`address:query`)"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query(`address:query OR notes:query`)"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query('*:query')"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query(\"*:query\")"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query(`*:query`)"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query('address:*uery OR notes:?uery')"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query(\"address:*uery OR notes:?uery\")"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query(`address:*uery OR notes:?uery`)"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query('address:qu*ry OR notes:qu?ry')"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query(\"address:qu*ry OR notes:qu?ry\")"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query(`address:qu*ry OR notes:qu?ry`)"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query('address:query notes:query')"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query(\"address:query notes:query\")"));
assertNotNull(parser.parse(
"SELECT id FROM test WHERE query(`address:query notes:query`)"));
}


Expand Down

0 comments on commit f2d77c6

Please sign in to comment.