Skip to content

Commit 8071c89

Browse files
authored
Add a test case for XGBoostJsonParser (#511)
1 parent 1d8956c commit 8071c89

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/test/java/com/o19s/es/ltr/ranker/parser/XGBoostJsonParserTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.lucene.tests.util.LuceneTestCase;
2828
import org.elasticsearch.common.ParsingException;
2929
import org.elasticsearch.common.io.Streams;
30+
import org.elasticsearch.xcontent.XContentParseException;
3031
import org.hamcrest.CoreMatchers;
3132

3233
import java.io.ByteArrayOutputStream;
@@ -40,6 +41,7 @@
4041
import static com.o19s.es.ltr.LtrTestUtils.randomFeature;
4142
import static com.o19s.es.ltr.LtrTestUtils.randomFeatureSet;
4243
import static java.util.Collections.singletonList;
44+
import static org.hamcrest.Matchers.instanceOf;
4345

4446
public class XGBoostJsonParserTests extends LuceneTestCase {
4547
private final XGBoostJsonParser parser = new XGBoostJsonParser();
@@ -251,6 +253,28 @@ public void testMissingFeat() throws IOException {
251253
CoreMatchers.containsString("Unknown feature [feat2]"));
252254
}
253255

256+
public void testInvalidLeaf() throws IOException {
257+
// The leaf nodes are missing nodeid field.
258+
String model = "[{" +
259+
"\"nodeid\": 0," +
260+
"\"split\":\"feat1\"," +
261+
"\"depth\":0," +
262+
"\"split_condition\":0.123," +
263+
"\"yes\":1," +
264+
"\"no\": 2," +
265+
"\"missing\":1,"+
266+
"\"children\": [" +
267+
" {\"depth\": 1, \"leaf\": 0.5}," +
268+
" {\"depth\": 1, \"leaf\": 0.2}" +
269+
"]}]";
270+
FeatureSet set = new StoredFeatureSet("set", singletonList(randomFeature("feat1")));
271+
// In this test case, the ParsingException is wrapped in an XContentParseException, because the
272+
// ParsingException that occurs while parsing the invalid leaf node happens within the ObjectParser.
273+
Throwable e = expectThrows(XContentParseException.class, () -> parser.parse(set, model)).getCause();
274+
assertThat(e, instanceOf(ParsingException.class));
275+
assertThat(e.getMessage(), CoreMatchers.containsString("This leaf does not have all the required fields"));
276+
}
277+
254278
public void testComplexModel() throws Exception {
255279
String model = readModel("/models/xgboost-wmf.json");
256280
List<StoredFeature> features = new ArrayList<>();

0 commit comments

Comments
 (0)