Skip to content

Commit dd9b343

Browse files
authored
[Transform] Add test assertion for terms.order field (#104849)
1 parent 5b7b142 commit dd9b343

File tree

1 file changed

+35
-31
lines changed
  • x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration

1 file changed

+35
-31
lines changed

x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121

2222
import java.io.IOException;
2323
import java.time.Instant;
24-
import java.util.Arrays;
25-
import java.util.Collections;
26-
import java.util.HashMap;
27-
import java.util.HashSet;
2824
import java.util.List;
2925
import java.util.Map;
3026
import java.util.Set;
@@ -63,7 +59,7 @@ protected boolean preserveIndicesUponCompletion() {
6359
@Before
6460
public void createIndexes() throws IOException {
6561
setupDataAccessRole(DATA_ACCESS_ROLE, REVIEWS_INDEX_NAME);
66-
setupUser(TEST_USER_NAME, Arrays.asList("transform_admin", DATA_ACCESS_ROLE));
62+
setupUser(TEST_USER_NAME, List.of("transform_admin", DATA_ACCESS_ROLE));
6763

6864
// it's not possible to run it as @BeforeClass as clients aren't initialized then, so we need this little hack
6965
if (indicesCreated) {
@@ -894,6 +890,15 @@ public void testPivotWithTermsAgg() throws Exception {
894890
}
895891
}
896892
},
893+
"common_users_desc": {
894+
"terms": {
895+
"field": "user_id",
896+
"size": 3,
897+
"order": {
898+
"_key": "desc"
899+
}
900+
}
901+
},
897902
"rare_users": {
898903
"rare_terms": {
899904
"field": "user_id"
@@ -922,33 +927,32 @@ public void testPivotWithTermsAgg() throws Exception {
922927
"hits.hits._source.common_users",
923928
searchResult
924929
)).get(0);
930+
assertThat(commonUsers, is(not(nullValue())));
931+
assertThat(
932+
commonUsers,
933+
equalTo(
934+
Map.of(
935+
"user_10",
936+
Map.of("common_businesses", Map.of("business_12", 6, "business_9", 4)),
937+
"user_0",
938+
Map.of("common_businesses", Map.of("business_0", 35))
939+
)
940+
)
941+
);
942+
Map<String, Integer> commonUsersDesc = (Map<String, Integer>) ((List<?>) XContentMapValues.extractValue(
943+
"hits.hits._source.common_users_desc",
944+
searchResult
945+
)).get(0);
946+
assertThat(commonUsersDesc, is(not(nullValue())));
947+
// 3 user names latest in lexicographic order (user_7, user_8, user_9) are selected properly but their order is not preserved.
948+
// See https://github.com/elastic/elasticsearch/issues/104847 for more information.
949+
assertThat(commonUsersDesc, equalTo(Map.of("user_7", 6, "user_9", 2, "user_8", 8)));
925950
Map<String, Integer> rareUsers = (Map<String, Integer>) ((List<?>) XContentMapValues.extractValue(
926951
"hits.hits._source.rare_users",
927952
searchResult
928953
)).get(0);
929-
assertThat(commonUsers, is(not(nullValue())));
930-
assertThat(commonUsers, equalTo(new HashMap<>() {
931-
{
932-
put("user_10", Collections.singletonMap("common_businesses", new HashMap<>() {
933-
{
934-
put("business_12", 6);
935-
put("business_9", 4);
936-
}
937-
}));
938-
put("user_0", Collections.singletonMap("common_businesses", new HashMap<>() {
939-
{
940-
put("business_0", 35);
941-
}
942-
}));
943-
}
944-
}));
945954
assertThat(rareUsers, is(not(nullValue())));
946-
assertThat(rareUsers, equalTo(new HashMap<>() {
947-
{
948-
put("user_5", 1);
949-
put("user_12", 1);
950-
}
951-
}));
955+
assertThat(rareUsers, is(equalTo(Map.of("user_5", 1, "user_12", 1))));
952956
}
953957

954958
private void assertDateHistogramPivot(String indexName) throws Exception {
@@ -1184,8 +1188,8 @@ private void testPreviewTransform(String queryJson) throws Exception {
11841188
List<Map<String, Object>> preview = (List<Map<String, Object>>) previewTransformResponse.get("preview");
11851189
// preview is limited to 100
11861190
assertThat(preview.size(), equalTo(100));
1187-
Set<String> expectedTopLevelFields = new HashSet<>(Arrays.asList("user", "by_day"));
1188-
Set<String> expectedNestedFields = new HashSet<>(Arrays.asList("id", "avg_rating"));
1191+
Set<String> expectedTopLevelFields = Set.of("user", "by_day");
1192+
Set<String> expectedNestedFields = Set.of("id", "avg_rating");
11891193
preview.forEach(p -> {
11901194
Set<String> keys = p.keySet();
11911195
assertThat(keys, equalTo(expectedTopLevelFields));
@@ -1255,8 +1259,8 @@ public void testPreviewTransformWithPipeline() throws Exception {
12551259
List<Map<String, Object>> preview = (List<Map<String, Object>>) previewTransformResponse.get("preview");
12561260
// preview is limited to 100
12571261
assertThat(preview.size(), equalTo(100));
1258-
Set<String> expectedTopLevelFields = new HashSet<>(Arrays.asList("user", "by_day", "pipeline_field"));
1259-
Set<String> expectedNestedFields = new HashSet<>(Arrays.asList("id", "avg_rating"));
1262+
Set<String> expectedTopLevelFields = Set.of("user", "by_day", "pipeline_field");
1263+
Set<String> expectedNestedFields = Set.of("id", "avg_rating");
12601264
preview.forEach(p -> {
12611265
Set<String> keys = p.keySet();
12621266
assertThat(keys, equalTo(expectedTopLevelFields));

0 commit comments

Comments
 (0)