Skip to content

Commit

Permalink
Rework on DSL tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Jun 6, 2022
1 parent 9814fa4 commit 2ac159f
Showing 1 changed file with 71 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.stream.Stream;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -370,41 +369,6 @@ void match_invalid_parameter() {
assertEquals("Parameter invalid_parameter is invalid for match function.", msg);
}

@Test
// `buildQuery` calls OpenSearch's (not plugin's) `toString()` function which prints
// - fields' weights inside quotes
// - `flags` as an integer, but not as an enum
// - parameters not in alphabetical order
@Disabled
void should_build_simple_query_string_query_with_default_parameters() {
assertJsonEquals(
"{\n"
+ " \"simple_query_string\" : {\n"
+ " \"query\" : \"search query\",\n"
+ " \"fields\" : [\n"
+ " \"field1^1.0\",\n"
+ " \"field2^0.3\"\n"
+ " ],\n"
+ " \"default_operator\" : \"OR\",\n"
+ " \"analyze_wildcard\" : false,\n"
+ " \"auto_generate_synonyms_phrase_query\" : true,\n"
+ " \"flags\" : \"ALL\",\n"
+ " \"fuzzy_max_expansions\" : 50,\n"
+ " \"fuzzy_prefix_length\" : 0,\n"
+ " \"fuzzy_transpositions\" : true,\n"
+ " \"lenient\" : false,\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ "}",
buildQuery(
dsl.simple_query_string(
dsl.namedArgument("fields", DSL.literal(new ExprTupleValue(
new LinkedHashMap<>(ImmutableMap.of(
"field1", ExprValueUtils.floatValue(1.F),
"field2", ExprValueUtils.floatValue(.3F)))))),
dsl.namedArgument("query", literal("search query")))));
}

@Test
void should_build_match_phrase_query_with_default_parameters() {
assertJsonEquals(
Expand All @@ -425,34 +389,83 @@ void should_build_match_phrase_query_with_default_parameters() {
}

@Test
// Test is disabled because DSL because of:
// 1) DSL reverses order of the `fields`
// 2) `flags` are printed by OpenSearch (not by the plugin) as an integer
// 3) parameters are printed -//- not in lexicographical order
@Disabled
void should_build_simple_query_string_query_with_custom_parameters() {
assertJsonEquals(
"{\n"
// Notes for following three tests:
// 1) OpenSearch (not the plugin) might change order of fields
// 2) `flags` are printed by OpenSearch as an integer
// 3) `minimum_should_match` printed as a string
void should_build_simple_query_string_query_with_default_parameters_single_field() {
assertJsonEquals("{\n"
+ " \"simple_query_string\" : {\n"
+ " \"query\" : \"search query\",\n"
+ " \"fields\" : [\n"
+ " \"field1^1.0\",\n"
+ " \"field2^0.3\"\n"
+ " \"field1^1.0\"\n"
+ " ],\n"
+ " \"default_operator\" : \"or\",\n"
+ " \"analyze_wildcard\" : false,\n"
+ " \"auto_generate_synonyms_phrase_query\" : true,\n"
+ " \"flags\" : -1,\n"
+ " \"fuzzy_max_expansions\" : 50,\n"
+ " \"fuzzy_prefix_length\" : 0,\n"
+ " \"fuzzy_transpositions\" : true,\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ "}",
buildQuery(dsl.simple_query_string(
dsl.namedArgument("fields", DSL.literal(new ExprTupleValue(
new LinkedHashMap<>(ImmutableMap.of(
"field1", ExprValueUtils.floatValue(1.F)))))),
dsl.namedArgument("query", literal("search query")))));
}

void should_build_simple_query_string_query_with_default_parameters_multiple_fields() {
var expected = "{\n"
+ " \"simple_query_string\" : {\n"
+ " \"query\" : \"search query\",\n"
+ " \"fields\" : [%s],\n"
+ " \"default_operator\" : \"or\",\n"
+ " \"analyze_wildcard\" : false,\n"
+ " \"auto_generate_synonyms_phrase_query\" : true,\n"
+ " \"flags\" : -1,\n"
+ " \"fuzzy_max_expansions\" : 50,\n"
+ " \"fuzzy_prefix_length\" : 0,\n"
+ " \"fuzzy_transpositions\" : true,\n"
+ " \"boost\" : 1.0\n"
+ " }\n"
+ "}";
var actual = buildQuery(dsl.simple_query_string(
dsl.namedArgument("fields", DSL.literal(new ExprTupleValue(
new LinkedHashMap<>(ImmutableMap.of(
"field1", ExprValueUtils.floatValue(1.F),
"field2", ExprValueUtils.floatValue(.3F)))))),
dsl.namedArgument("query", literal("search query"))));

var ex1 = String.format(expected, "\"field1^1.0\", \"field2^0.3\"");
var ex2 = String.format(expected, "\"field2^0.3\", \"field1^1.0\"");
assertTrue(new JSONObject(ex1).similar(new JSONObject(actual))
|| new JSONObject(ex2).similar(new JSONObject(actual)),
StringUtils.format("Actual %s doesn't match neither expected %s nor %s", actual, ex1, ex2));
}

@Test
void should_build_simple_query_string_query_with_custom_parameters() {
var expected = "{\n"
+ " \"simple_query_string\" : {\n"
+ " \"query\" : \"search query\",\n"
+ " \"fields\" : [%s],\n"
+ " \"analyze_wildcard\" : true,\n"
+ " \"analyzer\" : \"keyword\",\n"
+ " \"auto_generate_synonyms_phrase_query\" : false,\n"
+ " \"default_operator\" : \"AND\",\n"
+ " \"flags\" : \"AND\",\n"
+ " \"default_operator\" : \"and\",\n"
+ " \"flags\" : 1,\n"
+ " \"fuzzy_max_expansions\" : 10,\n"
+ " \"fuzzy_prefix_length\" : 2,\n"
+ " \"fuzzy_transpositions\" : false,\n"
+ " \"lenient\" : false,\n"
+ " \"minimum_should_match\" : 3,\n"
+ " \"minimum_should_match\" : \"3\",\n"
+ " \"boost\" : 2.0\n"
+ " }\n"
+ "}",
buildQuery(
+ "}";
var actual = buildQuery(
dsl.simple_query_string(
dsl.namedArgument("fields", DSL.literal(
ExprValueUtils.tupleValue(ImmutableMap.of("field1", 1.F, "field2", .3F)))),
Expand All @@ -467,7 +480,13 @@ void should_build_simple_query_string_query_with_custom_parameters() {
dsl.namedArgument("fuzzy_transpositions", literal("false")),
dsl.namedArgument("lenient", literal("false")),
dsl.namedArgument("minimum_should_match", literal("3")),
dsl.namedArgument("boost", literal("2.0")))));
dsl.namedArgument("boost", literal("2.0"))));

var ex1 = String.format(expected, "\"field1^1.0\", \"field2^0.3\"");
var ex2 = String.format(expected, "\"field2^0.3\", \"field1^1.0\"");
assertTrue(new JSONObject(ex1).similar(new JSONObject(actual))
|| new JSONObject(ex2).similar(new JSONObject(actual)),
StringUtils.format("Actual %s doesn't match neither expected %s nor %s", actual, ex1, ex2));
}

@Test
Expand Down

0 comments on commit 2ac159f

Please sign in to comment.