|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | +package org.elasticsearch.xpack.esql.core.querydsl.query; |
| 8 | + |
| 9 | +import org.elasticsearch.test.ESTestCase; |
| 10 | +import org.elasticsearch.xpack.esql.core.expression.predicate.fulltext.StringQueryPredicate; |
| 11 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 12 | +import org.elasticsearch.xpack.esql.core.tree.SourceTests; |
| 13 | +import org.elasticsearch.xpack.esql.core.util.StringUtils; |
| 14 | +import org.elasticsearch.xpack.kql.query.KqlQueryBuilder; |
| 15 | + |
| 16 | +import java.time.ZoneId; |
| 17 | +import java.time.zone.ZoneRulesException; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Function; |
| 22 | + |
| 23 | +import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode; |
| 24 | +import static org.hamcrest.Matchers.equalTo; |
| 25 | + |
| 26 | +public class KqlQueryTests extends ESTestCase { |
| 27 | + static KqlQuery randomMatchQuery() { |
| 28 | + return new KqlQuery(SourceTests.randomSource(), randomAlphaOfLength(5)); |
| 29 | + // TODO add the options |
| 30 | + } |
| 31 | + |
| 32 | + public void testEqualsAndHashCode() { |
| 33 | + checkEqualsAndHashCode(randomMatchQuery(), KqlQueryTests::copy, KqlQueryTests::mutate); |
| 34 | + } |
| 35 | + |
| 36 | + private static KqlQuery copy(KqlQuery query) { |
| 37 | + return new KqlQuery(query.source(), query.query(), query.options()); |
| 38 | + } |
| 39 | + |
| 40 | + private static KqlQuery mutate(KqlQuery query) { |
| 41 | + List<Function<KqlQuery, KqlQuery>> options = Arrays.asList( |
| 42 | + q -> new KqlQuery(SourceTests.mutate(q.source()), q.query(), q.options()), |
| 43 | + q -> new KqlQuery(q.source(), randomValueOtherThan(q.query(), () -> randomAlphaOfLength(5)), q.options()) |
| 44 | + ); |
| 45 | + // TODO mutate the options |
| 46 | + return randomFrom(options).apply(query); |
| 47 | + } |
| 48 | + |
| 49 | + public void testQueryBuilding() { |
| 50 | + KqlQueryBuilder qb = getBuilder("case_insensitive=false"); |
| 51 | + assertThat(qb.caseInsensitive(), equalTo(false)); |
| 52 | + |
| 53 | + qb = getBuilder("case_insensitive=false;time_zone=UTC;default_field=foo"); |
| 54 | + assertThat(qb.caseInsensitive(), equalTo(false)); |
| 55 | + assertThat(qb.timeZone(), equalTo(ZoneId.of("UTC"))); |
| 56 | + assertThat(qb.defaultField(), equalTo("foo")); |
| 57 | + |
| 58 | + Exception e = expectThrows(IllegalArgumentException.class, () -> getBuilder("pizza=yummy")); |
| 59 | + assertThat(e.getMessage(), equalTo("illegal kql query option [pizza]")); |
| 60 | + |
| 61 | + e = expectThrows(ZoneRulesException.class, () -> getBuilder("time_zone=aoeu")); |
| 62 | + assertThat(e.getMessage(), equalTo("Unknown time-zone ID: aoeu")); |
| 63 | + } |
| 64 | + |
| 65 | + private static KqlQueryBuilder getBuilder(String options) { |
| 66 | + final Source source = new Source(1, 1, StringUtils.EMPTY); |
| 67 | + final StringQueryPredicate predicate = new StringQueryPredicate(source, "eggplant", options); |
| 68 | + final KqlQuery kqlQuery = new KqlQuery(source, "eggplant", predicate.optionMap()); |
| 69 | + return (KqlQueryBuilder) kqlQuery.asBuilder(); |
| 70 | + } |
| 71 | + |
| 72 | + public void testToString() { |
| 73 | + final Source source = new Source(1, 1, StringUtils.EMPTY); |
| 74 | + final KqlQuery kqlQuery = new KqlQuery(source, "eggplant", Map.of()); |
| 75 | + assertEquals("KqlQuery@1:2[eggplant]", kqlQuery.toString()); |
| 76 | + } |
| 77 | +} |
0 commit comments