Skip to content

Commit

Permalink
Minor enhancements to compatible test tranformations task (#112840)
Browse files Browse the repository at this point in the history
This commit adds support to transform the value of the value field in the close_to assertion.
For example, with the following configuration:

tasks.named("yamlRestCompatTestTransform").configure({ task ->
   task.replaceValueInCloseTo("get.fields._routing", 9.5, "my test name")
})

will transform the following in "my test name" from:

 close_to:   { get.fields._routing: { value: 5.1, error: 0.00001 } }

to

 close_to:   { get.fields._routing: { value: 9.5, error: 0.00001 } }

This commit also adds supports to specify a specific test name to apply the replaceIsTrue task configuration.
Before this commit, you could replace the values in the is_true, but it only supported doing so for all tests subject to the configuration.
  • Loading branch information
jakelandis committed Sep 30, 2024
1 parent d799fec commit 2eb9274
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.gradle.VersionProperties;
import org.elasticsearch.gradle.internal.test.rest.transform.RestTestTransform;
import org.elasticsearch.gradle.internal.test.rest.transform.RestTestTransformer;
import org.elasticsearch.gradle.internal.test.rest.transform.close_to.ReplaceValueInCloseTo;
import org.elasticsearch.gradle.internal.test.rest.transform.do_.ReplaceKeyInDo;
import org.elasticsearch.gradle.internal.test.rest.transform.headers.InjectHeaders;
import org.elasticsearch.gradle.internal.test.rest.transform.length.ReplaceKeyInLength;
Expand Down Expand Up @@ -253,7 +254,30 @@ public void replaceKeyInMatch(String oldKeyName, String newKeyName) {
}

/**
* Replaces all the values of a is_true assertion for all project REST tests.
* Replaces the value of the `value` of a close_to assertion for a given REST tests.
* For example: close_to: { get.fields._routing: { value: 5.1, error: 0.00001 } }
* to close_to: { get.fields._routing: { value: 9.5, error: 0.00001 } }
* @param subKey the key name directly under close_to to replace. For example "get.fields._routing"
* @param newValue the value used in the replacement. For example 9.5
* @param testName the testName to apply replacement
*/
public void replaceValueInCloseTo(String subKey, double newValue, String testName) {
getTransformations().add(new ReplaceValueInCloseTo(subKey, MAPPER.convertValue(newValue, NumericNode.class), testName));
}

/**
* Replaces the value of the `value` of a close_to assertion for all project REST tests.
* For example: close_to: { get.fields._routing: { value: 5.1, error: 0.00001 } }
* to close_to: { get.fields._routing: { value: 9.5, error: 0.00001 } }
* @param subKey the key name directly under close_to to replace. For example "get.fields._routing"
* @param newValue the value used in the replacement. For example 9.5
*/
public void replaceValueInCloseTo(String subKey, double newValue) {
getTransformations().add(new ReplaceValueInCloseTo(subKey, MAPPER.convertValue(newValue, NumericNode.class)));
}

/**
* Replaces all the values of is_true assertion for all project REST tests.
* For example "is_true": "value_to_replace" to "is_true": "value_replaced"
*
* @param oldValue the value that has to match and will be replaced
Expand All @@ -263,6 +287,18 @@ public void replaceIsTrue(String oldValue, Object newValue) {
getTransformations().add(new ReplaceIsTrue(oldValue, MAPPER.convertValue(newValue, TextNode.class)));
}

/**
* Replaces all the values of is_true assertion for given REST test.
* For example "is_true": "value_to_replace" to "is_true": "value_replaced"
*
* @param oldValue the value that has to match and will be replaced
* @param newValue the value used in the replacement
* @param testName the testName to apply replacement
*/
public void replaceIsTrue(String oldValue, Object newValue, String testName) {
getTransformations().add(new ReplaceIsTrue(oldValue, MAPPER.convertValue(newValue, TextNode.class), testName));
}

/**
* Replaces all the values of a is_false assertion for all project REST tests.
* For example "is_false": "value_to_replace" to "is_false": "value_replaced"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.gradle.internal.test.rest.transform.close_to;

import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import org.elasticsearch.gradle.internal.test.rest.transform.ReplaceByKey;
import org.gradle.api.tasks.Internal;

/**
* Replaces the value of the `value` of a close_to assertion for a given sub-node.
* For example: close_to: { get.fields._routing: { value: 5.1, error: 0.00001 } }
* to close_to: { get.fields._routing: { value: 9.5, error: 0.00001 } }
*/
public class ReplaceValueInCloseTo extends ReplaceByKey {

public ReplaceValueInCloseTo(String replaceKey, NumericNode replacementNode) {
this(replaceKey, replacementNode, null);
}

public ReplaceValueInCloseTo(String replaceKey, NumericNode replacementNode, String testName) {
super(replaceKey, replaceKey, replacementNode, testName);
}

@Override
@Internal
public String getKeyToFind() {
return "close_to";
}

@Override
public void transformTest(ObjectNode matchParent) {
ObjectNode closeToNode = (ObjectNode) matchParent.get(getKeyToFind());
ObjectNode subNode = (ObjectNode) closeToNode.get(requiredChildKey());
subNode.remove("value");
subNode.set("value", getReplacementNode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public class ReplaceIsTrue extends ReplaceTextual {
public ReplaceIsTrue(String valueToBeReplaced, TextNode replacementNode) {
super("is_true", valueToBeReplaced, replacementNode);
}

public ReplaceIsTrue(String valueToBeReplaced, TextNode replacementNode, String testName) {
super("is_true", valueToBeReplaced, replacementNode, testName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

package org.elasticsearch.gradle.internal.test.rest.transform.close_to;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import org.elasticsearch.gradle.internal.test.rest.transform.AssertObjectNodes;
import org.elasticsearch.gradle.internal.test.rest.transform.TransformTests;
import org.junit.Test;

import java.util.Collections;
import java.util.List;

public class ReplaceValueInCloseToTests extends TransformTests {

private static final YAMLFactory YAML_FACTORY = new YAMLFactory();
private static final ObjectMapper MAPPER = new ObjectMapper(YAML_FACTORY);

@Test
public void testReplaceValue() throws Exception {
String test_original = "/rest/transform/close_to/close_to_replace_original.yml";
List<ObjectNode> tests = getTests(test_original);

String test_transformed = "/rest/transform/close_to/close_to_replace_transformed_value.yml";
List<ObjectNode> expectedTransformation = getTests(test_transformed);

NumericNode replacementNode = MAPPER.convertValue(99.99, NumericNode.class);

List<ObjectNode> transformedTests = transformTests(
tests,
Collections.singletonList(new ReplaceValueInCloseTo("aggregations.tsids.buckets.0.voltage.value", replacementNode, null))
);

AssertObjectNodes.areEqual(transformedTests, expectedTransformation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
close_to test:
- do:
search:
index: test
body:
size: 0
aggs:
tsids:
terms:
field: _tsid
order:
_key: asc
aggs:
voltage:
avg:
field: voltage

- match: {hits.total.value: 4}
- length: {aggregations.tsids.buckets: 2}
- match: {aggregations.tsids.buckets.0.key: "KDODRmbj7vu4rLWvjrJbpUuaET_vOYoRw6ImzKEcF4sEaGKnXSaKfM0" }
- match: {aggregations.tsids.buckets.0.doc_count: 2 }
- close_to: {aggregations.tsids.buckets.0.voltage.value: { value: 6.7, error: 0.01 }}
- match: { aggregations.tsids.buckets.1.key: "KDODRmbj7vu4rLWvjrJbpUvcUWJEddqA4Seo8jbBBBFxwC0lrefCb6A" }
- match: {aggregations.tsids.buckets.1.doc_count: 2 }
- close_to: {aggregations.tsids.buckets.1.voltage.value: { value: 7.30, error: 0.01 }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
close_to test:
- do:
search:
index: test
body:
size: 0
aggs:
tsids:
terms:
field: _tsid
order:
_key: asc
aggs:
voltage:
avg:
field: voltage

- match: {hits.total.value: 4}
- length: {aggregations.tsids.buckets: 2}
- match: {aggregations.tsids.buckets.0.key: "KDODRmbj7vu4rLWvjrJbpUuaET_vOYoRw6ImzKEcF4sEaGKnXSaKfM0" }
- match: {aggregations.tsids.buckets.0.doc_count: 2 }
- close_to: {aggregations.tsids.buckets.0.voltage.value: { value: 99.99, error: 0.01 }}
- match: { aggregations.tsids.buckets.1.key: "KDODRmbj7vu4rLWvjrJbpUvcUWJEddqA4Seo8jbBBBFxwC0lrefCb6A" }
- match: {aggregations.tsids.buckets.1.doc_count: 2 }
- close_to: {aggregations.tsids.buckets.1.voltage.value: { value: 7.30, error: 0.01 }}

0 comments on commit 2eb9274

Please sign in to comment.