Skip to content

Commit 9c19b4b

Browse files
committed
improve yaml assertion
1 parent 807ae63 commit 9c19b4b

File tree

9 files changed

+76
-2
lines changed

9 files changed

+76
-2
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/scroll/10_basic.yml

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- set: {_scroll_id: scroll_id}
3434
- match: {hits.total: 2 }
3535
- length: {hits.hits: 1 }
36+
- match: { hits.max_score: null }
3637
- match: {hits.hits.0._id: "42" }
3738

3839
- do:
@@ -52,6 +53,7 @@
5253
- match: {hits.total: 2 }
5354
- length: {hits.hits: 1 }
5455
- match: {hits.hits.0._id: "43" }
56+
- match: { hits.max_score: null }
5557

5658
- do:
5759
scroll:
@@ -60,6 +62,7 @@
6062

6163
- match: {hits.total: 2 }
6264
- length: {hits.hits: 0 }
65+
- is_false: hits.max_score
6366

6467
- do:
6568
clear_scroll:
@@ -105,6 +108,7 @@
105108
- set: {_scroll_id: scroll_id}
106109
- match: {hits.total: 2 }
107110
- length: {hits.hits: 1 }
111+
- match: { hits.max_score: null }
108112
- match: {hits.hits.0._id: "42" }
109113

110114
- do:
@@ -123,6 +127,7 @@
123127

124128
- match: {hits.total: 2 }
125129
- length: {hits.hits: 1 }
130+
- match: { hits.max_score: null }
126131
- match: {hits.hits.0._id: "43" }
127132

128133
- do:
@@ -132,6 +137,7 @@
132137

133138
- match: {hits.total: 2 }
134139
- length: {hits.hits: 0 }
140+
- match: { hits.max_score: null }
135141

136142
- do:
137143
clear_scroll:

rest-api-spec/src/main/resources/rest-api-spec/test/search/110_field_collapsing.yml

+1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ setup:
243243

244244
- match: { hits.total: 6 }
245245
- length: { hits.hits: 0 }
246+
- match: { hits.max_score: null }
246247

247248
---
248249
"field collapsing and multiple inner_hits":

rest-api-spec/src/main/resources/rest-api-spec/test/search/120_batch_reduce_size.yml

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ setup:
5353

5454
- match: { num_reduce_phases: 4 }
5555
- match: { hits.total: 3 }
56+
- length: { hits.hits: 0 }
57+
- match: { hits.max_score: null }
5658
- length: { aggregations.str_terms.buckets: 2 }
5759
- match: { aggregations.str_terms.buckets.0.key: "abc" }
5860
- is_false: aggregations.str_terms.buckets.0.key_as_string

rest-api-spec/src/main/resources/rest-api-spec/test/search/140_pre_filter_search_shards.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ setup:
7979
- match: { _shards.skipped: 0 }
8080
- match: { _shards.failed: 0 }
8181
- match: { hits.total: 2 }
82+
- length: { hits.hits: 0 }
83+
- match: { hits.max_score: null }
8284

8385
# this is the case where we have an empty body and don't skip anything since it's match_all
8486
- do:
@@ -102,6 +104,8 @@ setup:
102104
- match: { _shards.skipped : 1}
103105
- match: { _shards.failed: 0 }
104106
- match: { hits.total: 2 }
107+
- length: { hits.hits: 0 }
108+
- match: { hits.max_score: null }
105109

106110
# this case we skip all except of one since we need a real result
107111
- do:
@@ -115,6 +119,8 @@ setup:
115119
- match: { _shards.skipped : 2}
116120
- match: { _shards.failed: 0 }
117121
- match: { hits.total: 0 }
122+
- length: { hits.hits: 0 }
123+
- match: { hits.max_score: null }
118124

119125
- do:
120126
search:
@@ -126,9 +132,10 @@ setup:
126132
- match: { _shards.skipped : 0 }
127133
- match: { _shards.failed: 0 }
128134
- match: { hits.total: 2 }
135+
- length: { hits.hits: 0 }
136+
- match: { hits.max_score: null }
129137
- match: { aggregations.some_agg.doc_count: 3 }
130138

131-
132139
- do:
133140
search:
134141
pre_filter_shard_size: 1
@@ -139,6 +146,8 @@ setup:
139146
- match: { _shards.skipped : 0 }
140147
- match: { _shards.failed: 0 }
141148
- match: { hits.total: 2 }
149+
- length: { hits.hits: 0 }
150+
- match: { hits.max_score: null }
142151
- length: { aggregations.idx_terms.buckets: 3 }
143152

144153
- do:
@@ -151,6 +160,8 @@ setup:
151160
- match: { _shards.skipped : 1 }
152161
- match: { _shards.failed: 0 }
153162
- match: { hits.total: 2 }
163+
- length: { hits.hits: 0 }
164+
- match: { hits.max_score: null }
154165
- length: { aggregations.idx_terms.buckets: 2 }
155166

156167

rest-api-spec/src/main/resources/rest-api-spec/test/search/150_rewrite_on_coordinator.yml

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
- match: { _shards.skipped: 0 }
7070
- match: { _shards.failed: 0 }
7171
- match: { hits.total: 2 }
72+
- length: { hits.hits: 0 }
73+
- match: { hits.max_score: null }
7274

7375
- do:
7476
indices.stats: { index: 'lookup_index', "metric": "get"}

rest-api-spec/src/main/resources/rest-api-spec/test/search/190_index_prefix_search.yml

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ setup:
3939
df: text
4040

4141
- match: {hits.total: 1}
42+
- match: {hits.max_score: 1}
4243
- match: {hits.hits.0._score: 1}
4344

4445
- do:
@@ -52,6 +53,7 @@ setup:
5253
boost: 2
5354

5455
- match: {hits.total: 1}
56+
- match: {hits.max_score: 2}
5557
- match: {hits.hits.0._score: 2}
5658

5759
- do:
@@ -61,6 +63,7 @@ setup:
6163
df: text
6264

6365
- match: {hits.total: 1}
66+
- match: {hits.max_score: 1}
6467
- match: {hits.hits.0._score: 1}
6568

6669
---

rest-api-spec/src/main/resources/rest-api-spec/test/search/210_rescore_explain.yml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
query_weight: 5
3030
rescore_query_weight: 10
3131

32+
- match: {hits.max_score: 15}
3233
- match: { hits.hits.0._score: 15 }
3334
- match: { hits.hits.0._explanation.value: 15 }
3435

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/MatchAssertion.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static org.hamcrest.Matchers.equalTo;
3333
import static org.hamcrest.Matchers.instanceOf;
3434
import static org.junit.Assert.assertNotNull;
35+
import static org.junit.Assert.assertNull;
3536
import static org.junit.Assert.assertThat;
3637

3738
/**
@@ -70,8 +71,13 @@ protected void doAssert(Object actualValue, Object expectedValue) {
7071
}
7172
}
7273

73-
assertNotNull("field [" + getField() + "] is null", actualValue);
7474
logger.trace("assert that [{}] matches [{}] (field [{}])", actualValue, expectedValue, getField());
75+
if (expectedValue == null) {
76+
assertNull("field [" + getField() + "] should be null but was [" + actualValue + "]", actualValue);
77+
return;
78+
}
79+
assertNotNull("field [" + getField() + "] is null", actualValue);
80+
7581
if (actualValue.getClass().equals(safeClass(expectedValue)) == false) {
7682
if (actualValue instanceof Number && expectedValue instanceof Number) {
7783
//Double 1.0 is equal to Integer 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.test.rest.yaml.section;
20+
21+
import org.elasticsearch.common.xcontent.XContentLocation;
22+
import org.elasticsearch.test.ESTestCase;
23+
24+
public class MatchAssertionTests extends ESTestCase {
25+
26+
public void testNull() {
27+
XContentLocation xContentLocation = new XContentLocation(0, 0);
28+
{
29+
MatchAssertion matchAssertion = new MatchAssertion(xContentLocation, "field", null);
30+
matchAssertion.doAssert(null, null);
31+
expectThrows(AssertionError.class, () -> matchAssertion.doAssert("non-null", null));
32+
}
33+
{
34+
MatchAssertion matchAssertion = new MatchAssertion(xContentLocation, "field", "non-null");
35+
expectThrows(AssertionError.class, () -> matchAssertion.doAssert(null, "non-null"));
36+
}
37+
{
38+
MatchAssertion matchAssertion = new MatchAssertion(xContentLocation, "field", "/exp/");
39+
expectThrows(AssertionError.class, () -> matchAssertion.doAssert(null, "/exp/"));
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)