Skip to content

Commit c235ae8

Browse files
committed
Add UT
Signed-off-by: xil <[email protected]>
1 parent 3e094a3 commit c235ae8

File tree

4 files changed

+273
-30
lines changed

4 files changed

+273
-30
lines changed

modules/transport-grpc/src/test/java/org/opensearch/transport/grpc/proto/request/search/query/ConstantScoreQueryBuilderProtoUtilsTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,43 @@ public void testFromProtoWithMinimalFields() {
8484
assertNotNull("Inner query should not be null", constantScoreQueryBuilder.innerQuery());
8585
assertNull("Query name should be null", constantScoreQueryBuilder.queryName());
8686
}
87+
88+
public void testFromProtoWithNullRegistry() {
89+
TermQuery termQuery = TermQuery.newBuilder()
90+
.setField("status")
91+
.setValue(FieldValue.newBuilder().setString("active").build())
92+
.build();
93+
QueryContainer innerQueryContainer = QueryContainer.newBuilder().setTerm(termQuery).build();
94+
ConstantScoreQuery constantScoreQuery = ConstantScoreQuery.newBuilder().setFilter(innerQueryContainer).build();
95+
96+
IllegalArgumentException exception = expectThrows(
97+
IllegalArgumentException.class,
98+
() -> ConstantScoreQueryBuilderProtoUtils.fromProto(constantScoreQuery, null)
99+
);
100+
101+
assertEquals("QueryBuilderProtoConverterRegistry cannot be null", exception.getMessage());
102+
}
103+
104+
public void testFromProtoWithMissingFilter() {
105+
ConstantScoreQuery constantScoreQuery = ConstantScoreQuery.newBuilder().build();
106+
107+
IllegalArgumentException exception = expectThrows(
108+
IllegalArgumentException.class,
109+
() -> ConstantScoreQueryBuilderProtoUtils.fromProto(constantScoreQuery, registry)
110+
);
111+
112+
assertEquals("ConstantScore query must have a filter query", exception.getMessage());
113+
}
114+
115+
public void testFromProtoWithNullFilterQuery() {
116+
QueryContainer emptyQueryContainer = QueryContainer.newBuilder().build();
117+
ConstantScoreQuery constantScoreQuery = ConstantScoreQuery.newBuilder().setFilter(emptyQueryContainer).build();
118+
119+
IllegalArgumentException exception = expectThrows(
120+
IllegalArgumentException.class,
121+
() -> ConstantScoreQueryBuilderProtoUtils.fromProto(constantScoreQuery, registry)
122+
);
123+
124+
assertEquals("Filter query cannot be null for constant_score query", exception.getMessage());
125+
}
87126
}

modules/transport-grpc/src/test/java/org/opensearch/transport/grpc/proto/request/search/query/MatchBoolPrefixQueryBuilderProtoUtilsTests.java

Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.opensearch.index.query.Operator;
1414
import org.opensearch.protobufs.MatchBoolPrefixQuery;
1515
import org.opensearch.protobufs.MinimumShouldMatch;
16+
import org.opensearch.protobufs.MultiTermQueryRewrite;
1617
import org.opensearch.test.OpenSearchTestCase;
1718

1819
public class MatchBoolPrefixQueryBuilderProtoUtilsTests extends OpenSearchTestCase {
@@ -88,34 +89,74 @@ public void testFromProtoWithOperatorUnspecified() {
8889
assertEquals("Operator should be default OR", Operator.OR, matchBoolPrefixQueryBuilder.operator());
8990
}
9091

91-
public void testFromProtoWithMinimumShouldMatchInt() {
92-
MinimumShouldMatch minimumShouldMatch = MinimumShouldMatch.newBuilder().setInt32(2).build();
93-
94-
MatchBoolPrefixQuery matchBoolPrefixQuery = MatchBoolPrefixQuery.newBuilder()
92+
public void testFromProtoWithMinimumShouldMatch() {
93+
// Test 1: MinimumShouldMatch with string value
94+
MinimumShouldMatch minimumShouldMatchString = MinimumShouldMatch.newBuilder().setString("75%").build();
95+
MatchBoolPrefixQuery queryString = MatchBoolPrefixQuery.newBuilder()
9596
.setField("message")
9697
.setQuery("test")
97-
.setMinimumShouldMatch(minimumShouldMatch)
98+
.setMinimumShouldMatch(minimumShouldMatchString)
9899
.build();
100+
MatchBoolPrefixQueryBuilder builderString = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryString);
101+
assertNotNull("Builder should not be null (STRING)", builderString);
102+
assertEquals("Minimum should match (STRING)", "75%", builderString.minimumShouldMatch());
99103

100-
MatchBoolPrefixQueryBuilder matchBoolPrefixQueryBuilder = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(matchBoolPrefixQuery);
104+
// Test 2: MinimumShouldMatch with int32 value
105+
MinimumShouldMatch minimumShouldMatchInt = MinimumShouldMatch.newBuilder().setInt32(2).build();
106+
MatchBoolPrefixQuery queryInt = MatchBoolPrefixQuery.newBuilder()
107+
.setField("message")
108+
.setQuery("test")
109+
.setMinimumShouldMatch(minimumShouldMatchInt)
110+
.build();
111+
MatchBoolPrefixQueryBuilder builderInt = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryInt);
112+
assertNotNull("Builder should not be null (INT32)", builderInt);
113+
assertEquals("Minimum should match (INT32)", "2", builderInt.minimumShouldMatch());
101114

102-
assertNotNull("MatchBoolPrefixQueryBuilder should not be null", matchBoolPrefixQueryBuilder);
103-
assertEquals("Minimum should match", "2", matchBoolPrefixQueryBuilder.minimumShouldMatch());
115+
// Test 3: MinimumShouldMatch with empty/neither string nor int32
116+
MinimumShouldMatch minimumShouldMatchEmpty = MinimumShouldMatch.newBuilder().build();
117+
MatchBoolPrefixQuery queryEmpty = MatchBoolPrefixQuery.newBuilder()
118+
.setField("message")
119+
.setQuery("test")
120+
.setMinimumShouldMatch(minimumShouldMatchEmpty)
121+
.build();
122+
MatchBoolPrefixQueryBuilder builderEmpty = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryEmpty);
123+
assertNotNull("Builder should not be null (EMPTY)", builderEmpty);
124+
assertNull("Minimum should match should be null (EMPTY)", builderEmpty.minimumShouldMatch());
104125
}
105126

106-
public void testFromProtoWithFuzzinessInt() {
107-
org.opensearch.protobufs.Fuzziness fuzziness = org.opensearch.protobufs.Fuzziness.newBuilder().setInt32(2).build();
108-
109-
MatchBoolPrefixQuery matchBoolPrefixQuery = MatchBoolPrefixQuery.newBuilder()
127+
public void testFromProtoWithFuzziness() {
128+
// Test 1: Fuzziness with string value
129+
org.opensearch.protobufs.Fuzziness fuzzinessString = org.opensearch.protobufs.Fuzziness.newBuilder().setString("AUTO").build();
130+
MatchBoolPrefixQuery queryString = MatchBoolPrefixQuery.newBuilder()
110131
.setField("message")
111132
.setQuery("test")
112-
.setFuzziness(fuzziness)
133+
.setFuzziness(fuzzinessString)
113134
.build();
135+
MatchBoolPrefixQueryBuilder builderString = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryString);
136+
assertNotNull("Builder should not be null (STRING)", builderString);
137+
assertEquals("Fuzziness should match (STRING)", Fuzziness.AUTO, builderString.fuzziness());
114138

115-
MatchBoolPrefixQueryBuilder matchBoolPrefixQueryBuilder = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(matchBoolPrefixQuery);
139+
// Test 2: Fuzziness with int32 value
140+
org.opensearch.protobufs.Fuzziness fuzzinessInt = org.opensearch.protobufs.Fuzziness.newBuilder().setInt32(2).build();
141+
MatchBoolPrefixQuery queryInt = MatchBoolPrefixQuery.newBuilder()
142+
.setField("message")
143+
.setQuery("test")
144+
.setFuzziness(fuzzinessInt)
145+
.build();
146+
MatchBoolPrefixQueryBuilder builderInt = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryInt);
147+
assertNotNull("Builder should not be null (INT32)", builderInt);
148+
assertEquals("Fuzziness should match (INT32)", Fuzziness.fromEdits(2), builderInt.fuzziness());
116149

117-
assertNotNull("MatchBoolPrefixQueryBuilder should not be null", matchBoolPrefixQueryBuilder);
118-
assertEquals("Fuzziness should match", Fuzziness.fromEdits(2), matchBoolPrefixQueryBuilder.fuzziness());
150+
// Test 3: Fuzziness with empty/neither string nor int32
151+
org.opensearch.protobufs.Fuzziness fuzzinessEmpty = org.opensearch.protobufs.Fuzziness.newBuilder().build();
152+
MatchBoolPrefixQuery queryEmpty = MatchBoolPrefixQuery.newBuilder()
153+
.setField("message")
154+
.setQuery("test")
155+
.setFuzziness(fuzzinessEmpty)
156+
.build();
157+
MatchBoolPrefixQueryBuilder builderEmpty = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryEmpty);
158+
assertNotNull("Builder should not be null (EMPTY)", builderEmpty);
159+
assertNull("Fuzziness should be null (EMPTY)", builderEmpty.fuzziness());
119160
}
120161

121162
public void testFromProtoWithDefaults() {
@@ -132,4 +173,34 @@ public void testFromProtoWithDefaults() {
132173
matchBoolPrefixQueryBuilder.fuzzyTranspositions()
133174
);
134175
}
176+
177+
public void testFromProtoWithFuzzyRewrite() {
178+
// Test 1: Fuzzy rewrite not set (null)
179+
MatchBoolPrefixQuery queryNoRewrite = MatchBoolPrefixQuery.newBuilder().setField("message").setQuery("test").build();
180+
MatchBoolPrefixQueryBuilder builderNoRewrite = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryNoRewrite);
181+
assertNotNull("Builder should not be null (no fuzzyRewrite)", builderNoRewrite);
182+
assertNull("Fuzzy rewrite should be null (not set)", builderNoRewrite.fuzzyRewrite());
183+
184+
// Test 2: Fuzzy rewrite with CONSTANT_SCORE value
185+
MatchBoolPrefixQuery queryWithRewrite = MatchBoolPrefixQuery.newBuilder()
186+
.setField("message")
187+
.setQuery("test")
188+
.setFuzzyRewrite(MultiTermQueryRewrite.MULTI_TERM_QUERY_REWRITE_CONSTANT_SCORE)
189+
.build();
190+
MatchBoolPrefixQueryBuilder builderWithRewrite = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryWithRewrite);
191+
assertNotNull("Builder should not be null (CONSTANT_SCORE)", builderWithRewrite);
192+
assertNotNull("Fuzzy rewrite should not be null (CONSTANT_SCORE)", builderWithRewrite.fuzzyRewrite());
193+
assertEquals("Fuzzy rewrite should match (CONSTANT_SCORE)", "constant_score", builderWithRewrite.fuzzyRewrite());
194+
195+
// Test 3: Fuzzy rewrite with UNSPECIFIED value
196+
MatchBoolPrefixQuery queryUnspecified = MatchBoolPrefixQuery.newBuilder()
197+
.setField("message")
198+
.setQuery("test")
199+
.setFuzzyRewrite(MultiTermQueryRewrite.MULTI_TERM_QUERY_REWRITE_UNSPECIFIED)
200+
.build();
201+
MatchBoolPrefixQueryBuilder builderUnspecified = MatchBoolPrefixQueryBuilderProtoUtils.fromProto(queryUnspecified);
202+
assertNotNull("Builder should not be null (UNSPECIFIED)", builderUnspecified);
203+
assertNull("Fuzzy rewrite should be null (UNSPECIFIED)", builderUnspecified.fuzzyRewrite());
204+
}
205+
135206
}

modules/transport-grpc/src/test/java/org/opensearch/transport/grpc/proto/request/search/query/MatchQueryBuilderProtoUtilsTests.java

Lines changed: 113 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,51 +98,150 @@ public void testFromProtoWithOperatorUnspecified() {
9898
assertEquals("Operator should be default OR", Operator.OR, matchQueryBuilder.operator());
9999
}
100100

101-
public void testFromProtoWithFuzzinessInt() {
101+
public void testFromProtoWithFuzziness() {
102102
FieldValue queryValue = FieldValue.newBuilder().setString("test").build();
103-
org.opensearch.protobufs.Fuzziness fuzziness = org.opensearch.protobufs.Fuzziness.newBuilder().setInt32(2).build();
104103

104+
// Test 1: Fuzziness with string value
105+
org.opensearch.protobufs.Fuzziness fuzzinessString = org.opensearch.protobufs.Fuzziness.newBuilder().setString("AUTO").build();
106+
org.opensearch.protobufs.MatchQuery queryString = org.opensearch.protobufs.MatchQuery.newBuilder()
107+
.setField("message")
108+
.setQuery(queryValue)
109+
.setFuzziness(fuzzinessString)
110+
.build();
111+
MatchQueryBuilder builderString = MatchQueryBuilderProtoUtils.fromProto(queryString);
112+
assertNotNull("Builder should not be null (STRING)", builderString);
113+
assertEquals("Fuzziness should match (STRING)", Fuzziness.AUTO, builderString.fuzziness());
114+
115+
// Test 2: Fuzziness with int32 value
116+
org.opensearch.protobufs.Fuzziness fuzzinessInt = org.opensearch.protobufs.Fuzziness.newBuilder().setInt32(2).build();
117+
org.opensearch.protobufs.MatchQuery queryInt = org.opensearch.protobufs.MatchQuery.newBuilder()
118+
.setField("message")
119+
.setQuery(queryValue)
120+
.setFuzziness(fuzzinessInt)
121+
.build();
122+
MatchQueryBuilder builderInt = MatchQueryBuilderProtoUtils.fromProto(queryInt);
123+
assertNotNull("Builder should not be null (INT32)", builderInt);
124+
assertEquals("Fuzziness should match (INT32)", Fuzziness.fromEdits(2), builderInt.fuzziness());
125+
126+
// Test 3: Fuzziness with empty/neither string nor int32
127+
org.opensearch.protobufs.Fuzziness fuzzinessEmpty = org.opensearch.protobufs.Fuzziness.newBuilder().build();
128+
org.opensearch.protobufs.MatchQuery queryEmpty = org.opensearch.protobufs.MatchQuery.newBuilder()
129+
.setField("message")
130+
.setQuery(queryValue)
131+
.setFuzziness(fuzzinessEmpty)
132+
.build();
133+
MatchQueryBuilder builderEmpty = MatchQueryBuilderProtoUtils.fromProto(queryEmpty);
134+
assertNotNull("Builder should not be null (EMPTY)", builderEmpty);
135+
assertNull("Fuzziness should be null (EMPTY)", builderEmpty.fuzziness());
136+
}
137+
138+
public void testFromProtoWithMinimumShouldMatch() {
139+
FieldValue queryValue = FieldValue.newBuilder().setString("test").build();
140+
141+
// Test 1: MinimumShouldMatch with string value
142+
org.opensearch.protobufs.MinimumShouldMatch minimumShouldMatchString = org.opensearch.protobufs.MinimumShouldMatch.newBuilder()
143+
.setString("75%")
144+
.build();
145+
org.opensearch.protobufs.MatchQuery queryString = org.opensearch.protobufs.MatchQuery.newBuilder()
146+
.setField("message")
147+
.setQuery(queryValue)
148+
.setMinimumShouldMatch(minimumShouldMatchString)
149+
.build();
150+
MatchQueryBuilder builderString = MatchQueryBuilderProtoUtils.fromProto(queryString);
151+
assertNotNull("Builder should not be null (STRING)", builderString);
152+
assertEquals("Minimum should match (STRING)", "75%", builderString.minimumShouldMatch());
153+
154+
// Test 2: MinimumShouldMatch with int32 value
155+
org.opensearch.protobufs.MinimumShouldMatch minimumShouldMatchInt = org.opensearch.protobufs.MinimumShouldMatch.newBuilder()
156+
.setInt32(2)
157+
.build();
158+
org.opensearch.protobufs.MatchQuery queryInt = org.opensearch.protobufs.MatchQuery.newBuilder()
159+
.setField("message")
160+
.setQuery(queryValue)
161+
.setMinimumShouldMatch(minimumShouldMatchInt)
162+
.build();
163+
MatchQueryBuilder builderInt = MatchQueryBuilderProtoUtils.fromProto(queryInt);
164+
assertNotNull("Builder should not be null (INT32)", builderInt);
165+
assertEquals("Minimum should match (INT32)", "2", builderInt.minimumShouldMatch());
166+
167+
// Test 3: MinimumShouldMatch with empty/neither
168+
org.opensearch.protobufs.MinimumShouldMatch minimumShouldMatchEmpty = org.opensearch.protobufs.MinimumShouldMatch.newBuilder()
169+
.build();
170+
org.opensearch.protobufs.MatchQuery queryEmpty = org.opensearch.protobufs.MatchQuery.newBuilder()
171+
.setField("message")
172+
.setQuery(queryValue)
173+
.setMinimumShouldMatch(minimumShouldMatchEmpty)
174+
.build();
175+
MatchQueryBuilder builderEmpty = MatchQueryBuilderProtoUtils.fromProto(queryEmpty);
176+
assertNotNull("Builder should not be null (EMPTY)", builderEmpty);
177+
assertNull("Minimum should match should be null (EMPTY)", builderEmpty.minimumShouldMatch());
178+
}
179+
180+
public void testFromProtoWithFuzzyRewrite() {
181+
FieldValue queryValue = FieldValue.newBuilder().setString("test").build();
182+
183+
// Test 1: FuzzyRewrite with CONSTANT_SCORE value
184+
org.opensearch.protobufs.MatchQuery queryWithRewrite = org.opensearch.protobufs.MatchQuery.newBuilder()
185+
.setField("message")
186+
.setQuery(queryValue)
187+
.setFuzzyRewrite(org.opensearch.protobufs.MultiTermQueryRewrite.MULTI_TERM_QUERY_REWRITE_CONSTANT_SCORE)
188+
.build();
189+
MatchQueryBuilder builderWithRewrite = MatchQueryBuilderProtoUtils.fromProto(queryWithRewrite);
190+
assertNotNull("Builder should not be null (CONSTANT_SCORE)", builderWithRewrite);
191+
assertNotNull("Fuzzy rewrite should not be null (CONSTANT_SCORE)", builderWithRewrite.fuzzyRewrite());
192+
assertEquals("Fuzzy rewrite should match (CONSTANT_SCORE)", "constant_score", builderWithRewrite.fuzzyRewrite());
193+
194+
// Test 2: FuzzyRewrite with UNSPECIFIED value
195+
org.opensearch.protobufs.MatchQuery queryUnspecified = org.opensearch.protobufs.MatchQuery.newBuilder()
196+
.setField("message")
197+
.setQuery(queryValue)
198+
.setFuzzyRewrite(org.opensearch.protobufs.MultiTermQueryRewrite.MULTI_TERM_QUERY_REWRITE_UNSPECIFIED)
199+
.build();
200+
MatchQueryBuilder builderUnspecified = MatchQueryBuilderProtoUtils.fromProto(queryUnspecified);
201+
assertNotNull("Builder should not be null (UNSPECIFIED)", builderUnspecified);
202+
assertNull("Fuzzy rewrite should be null (UNSPECIFIED)", builderUnspecified.fuzzyRewrite());
203+
}
204+
205+
public void testFromProtoWithZeroTermsQueryAll() {
206+
FieldValue queryValue = FieldValue.newBuilder().setString("test").build();
105207
org.opensearch.protobufs.MatchQuery matchQuery = org.opensearch.protobufs.MatchQuery.newBuilder()
106208
.setField("message")
107209
.setQuery(queryValue)
108-
.setFuzziness(fuzziness)
210+
.setZeroTermsQuery(ZeroTermsQuery.ZERO_TERMS_QUERY_ALL)
109211
.build();
110212

111213
MatchQueryBuilder matchQueryBuilder = MatchQueryBuilderProtoUtils.fromProto(matchQuery);
112214

113215
assertNotNull("MatchQueryBuilder should not be null", matchQueryBuilder);
114-
assertEquals("Fuzziness should match", Fuzziness.fromEdits(2), matchQueryBuilder.fuzziness());
216+
assertEquals("Zero terms query should match", MatchQuery.ZeroTermsQuery.ALL, matchQueryBuilder.zeroTermsQuery());
115217
}
116218

117-
public void testFromProtoWithZeroTermsQueryNone() {
219+
public void testFromProtoWithLenient() {
118220
FieldValue queryValue = FieldValue.newBuilder().setString("test").build();
119221
org.opensearch.protobufs.MatchQuery matchQuery = org.opensearch.protobufs.MatchQuery.newBuilder()
120222
.setField("message")
121223
.setQuery(queryValue)
122-
.setZeroTermsQuery(ZeroTermsQuery.ZERO_TERMS_QUERY_NONE)
224+
.setLenient(true)
123225
.build();
124226

125227
MatchQueryBuilder matchQueryBuilder = MatchQueryBuilderProtoUtils.fromProto(matchQuery);
126228

127229
assertNotNull("MatchQueryBuilder should not be null", matchQueryBuilder);
128-
assertEquals("Zero terms query should match", MatchQuery.ZeroTermsQuery.NONE, matchQueryBuilder.zeroTermsQuery());
230+
assertEquals("Lenient should match", true, matchQueryBuilder.lenient());
129231
}
130232

131-
public void testFromProtoWithZeroTermsQueryUnspecified() {
233+
public void testFromProtoWithAutoGenerateSynonymsPhraseQueryFalse() {
132234
FieldValue queryValue = FieldValue.newBuilder().setString("test").build();
133235
org.opensearch.protobufs.MatchQuery matchQuery = org.opensearch.protobufs.MatchQuery.newBuilder()
134236
.setField("message")
135237
.setQuery(queryValue)
136-
.setZeroTermsQuery(ZeroTermsQuery.ZERO_TERMS_QUERY_UNSPECIFIED)
238+
.setAutoGenerateSynonymsPhraseQuery(false)
137239
.build();
138240

139241
MatchQueryBuilder matchQueryBuilder = MatchQueryBuilderProtoUtils.fromProto(matchQuery);
140242

141243
assertNotNull("MatchQueryBuilder should not be null", matchQueryBuilder);
142-
assertEquals(
143-
"Zero terms query should be default when UNSPECIFIED",
144-
MatchQuery.DEFAULT_ZERO_TERMS_QUERY,
145-
matchQueryBuilder.zeroTermsQuery()
146-
);
244+
assertEquals("Auto generate synonyms should match", false, matchQueryBuilder.autoGenerateSynonymsPhraseQuery());
147245
}
246+
148247
}

0 commit comments

Comments
 (0)