Skip to content

Commit

Permalink
Improving tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret committed Nov 20, 2024
1 parent b5b59ae commit 5c0083c
Showing 1 changed file with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,40 @@
import java.time.ZoneId;
import java.time.zone.ZoneRulesException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
import static org.hamcrest.Matchers.equalTo;

public class KqlQueryTests extends ESTestCase {
static KqlQuery randomMatchQuery() {
return new KqlQuery(SourceTests.randomSource(), randomAlphaOfLength(5));
// TODO add the options
static KqlQuery randomKqkQueryQuery() {
Map<String, String> options = new HashMap<>();

if (randomBoolean()) {
options.put(KqlQueryBuilder.CASE_INSENSITIVE_FIELD.getPreferredName(), String.valueOf(randomBoolean()));
}

if (randomBoolean()) {
options.put(KqlQueryBuilder.DEFAULT_FIELD_FIELD.getPreferredName(), randomIdentifier());
}

if (randomBoolean()) {
options.put(KqlQueryBuilder.TIME_ZONE_FIELD.getPreferredName(), randomZone().getId());
}

return new KqlQuery(SourceTests.randomSource(), randomAlphaOfLength(5), Collections.unmodifiableMap(options));
}

public void testEqualsAndHashCode() {
checkEqualsAndHashCode(randomMatchQuery(), KqlQueryTests::copy, KqlQueryTests::mutate);
for (int runs = 0; runs < 100; runs++) {
checkEqualsAndHashCode(randomKqkQueryQuery(), KqlQueryTests::copy, KqlQueryTests::mutate);
}
}

private static KqlQuery copy(KqlQuery query) {
Expand All @@ -40,12 +59,57 @@ private static KqlQuery copy(KqlQuery query) {
private static KqlQuery mutate(KqlQuery query) {
List<Function<KqlQuery, KqlQuery>> options = Arrays.asList(
q -> new KqlQuery(SourceTests.mutate(q.source()), q.query(), q.options()),
q -> new KqlQuery(q.source(), randomValueOtherThan(q.query(), () -> randomAlphaOfLength(5)), q.options())
q -> new KqlQuery(q.source(), randomValueOtherThan(q.query(), () -> randomAlphaOfLength(5)), q.options()),
q -> new KqlQuery(q.source(), q.query(), mutateOptions(q.options()))
);
// TODO mutate the options

return randomFrom(options).apply(query);
}

private static Map<String, String> mutateOptions(Map<String, String> options) {
Map<String, String> mutatedOptions = new HashMap<>(options);
if (options.isEmpty() == false && randomBoolean()) {
mutatedOptions = options.entrySet()
.stream()
.filter(entry -> randomBoolean())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

while (mutatedOptions.equals(options)) {
if (randomBoolean()) {
mutatedOptions = mutateOption(
mutatedOptions,
KqlQueryBuilder.CASE_INSENSITIVE_FIELD.getPreferredName(),
() -> String.valueOf(randomBoolean())
);
}

if (randomBoolean()) {
mutatedOptions = mutateOption(
mutatedOptions,
KqlQueryBuilder.DEFAULT_FIELD_FIELD.getPreferredName(),
() -> randomIdentifier()
);
}

if (randomBoolean()) {
mutatedOptions = mutateOption(
mutatedOptions,
KqlQueryBuilder.TIME_ZONE_FIELD.getPreferredName(),
() -> randomZone().getId()
);
}
}

return Collections.unmodifiableMap(mutatedOptions);
}

private static Map<String, String> mutateOption(Map<String, String> options, String optionName, Supplier<String> valueSupplier) {
options = new HashMap<>(options);
options.put(optionName, randomValueOtherThan(options.get(optionName), valueSupplier));
return options;
}

public void testQueryBuilding() {
KqlQueryBuilder qb = getBuilder("case_insensitive=false");
assertThat(qb.caseInsensitive(), equalTo(false));
Expand Down

0 comments on commit 5c0083c

Please sign in to comment.