Skip to content

Commit c3fcb10

Browse files
committed
iter
1 parent 7266958 commit c3fcb10

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

test/framework/src/main/java/org/elasticsearch/logsdb/datageneration/matchers/source/FieldSpecificMatcher.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121
import java.util.Objects;
2222
import java.util.Set;
23+
import java.util.TreeMap;
2324
import java.util.stream.Collectors;
2425

2526
import static org.elasticsearch.logsdb.datageneration.matchers.Messages.formatErrorMessage;
@@ -46,12 +47,17 @@ class CountedKeywordMatcher implements FieldSpecificMatcher {
4647
this.expectedSettings = expectedSettings;
4748
}
4849

49-
private static List<String> normalize(List<Object> values) {
50-
return values.stream().filter(Objects::nonNull).map(it -> (String) it).toList();
51-
}
50+
@Override
51+
public MatchResult match(
52+
List<Object> actual,
53+
List<Object> expected,
54+
Map<String, Object> actualMapping,
55+
Map<String, Object> expectedMapping
56+
) {
57+
var actualNormalized = normalize(actual);
58+
var expectedNormalized = normalize(expected);
5259

53-
private static boolean matchCountsEqualExact(List<String> actualNormalized, List<String> expectedNormalized) {
54-
HashMap<String, Integer> counts = new HashMap<>();
60+
Map<String, Integer> counts = new TreeMap<>();
5561
for (String value : actualNormalized) {
5662
counts.put(value, counts.getOrDefault(value, 0) + 1);
5763
}
@@ -64,31 +70,29 @@ private static boolean matchCountsEqualExact(List<String> actualNormalized, List
6470
}
6571
}
6672

67-
return counts.isEmpty();
68-
}
69-
70-
@Override
71-
public MatchResult match(
72-
List<Object> actual,
73-
List<Object> expected,
74-
Map<String, Object> actualMapping,
75-
Map<String, Object> expectedMapping
76-
) {
77-
var actualNormalized = normalize(actual);
78-
var expectedNormalized = normalize(expected);
73+
if (counts.isEmpty() == false) {
74+
var extraValuesMessage = new StringBuilder("extra values: ");
75+
for (var entry : counts.entrySet()) {
76+
extraValuesMessage.append('\n').append(entry.getKey()).append(": ").append(entry.getValue());
77+
}
7978

80-
return matchCountsEqualExact(actualNormalized, expectedNormalized)
81-
? MatchResult.match()
82-
: MatchResult.noMatch(
79+
return MatchResult.noMatch(
8380
formatErrorMessage(
8481
actualMappings,
8582
actualSettings,
8683
expectedMappings,
8784
expectedSettings,
88-
"Values of type [counted_keyword] don't match after normalization, normalized"
85+
"Values of type [counted_keyword] don't match, " + extraValuesMessage + ".\n"
8986
+ prettyPrintCollections(actualNormalized, expectedNormalized)
9087
)
9188
);
89+
}
90+
91+
return MatchResult.match();
92+
}
93+
94+
private static List<String> normalize(List<Object> values) {
95+
return values.stream().filter(Objects::nonNull).map(it -> (String) it).toList();
9296
}
9397
}
9498

test/framework/src/test/java/org/elasticsearch/logsdb/datageneration/SourceMatcherTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ public void testDynamicMismatch() throws IOException {
6262

6363
public void testMappedMatch() throws IOException {
6464
List<Map<String, Object>> values = List.of(
65-
Map.of("aaa", 124, "bbb", false, "ccc", 12.34),
66-
Map.of("aaa", 124, "bbb", false, "ccc", 12.34)
65+
Map.of("aaa", 124, "bbb", "hey", "ccc", 12.34),
66+
Map.of("aaa", 124, "bbb", "yeh", "ccc", 12.34)
6767
);
6868

6969
var mapping = XContentBuilder.builder(XContentType.JSON.xContent());
7070
mapping.startObject();
7171
mapping.startObject("_doc");
7272
{
7373
mapping.startObject("aaa").field("type", "long").endObject();
74-
mapping.startObject("bbb").field("type", "boolean").endObject();
74+
mapping.startObject("bbb").field("type", "keyword").endObject();
7575
mapping.startObject("ccc").field("type", "half_float").endObject();
7676
}
7777
mapping.endObject();
@@ -83,20 +83,20 @@ public void testMappedMatch() throws IOException {
8383

8484
public void testMappedMismatch() throws IOException {
8585
List<Map<String, Object>> actual = List.of(
86-
Map.of("aaa", 124, "bbb", false, "ccc", 12.34),
87-
Map.of("aaa", 124, "bbb", false, "ccc", 12.34)
86+
Map.of("aaa", 124, "bbb", "hey", "ccc", 12.34),
87+
Map.of("aaa", 124, "bbb", "yeh", "ccc", 12.34)
8888
);
8989
List<Map<String, Object>> expected = List.of(
90-
Map.of("aaa", 124, "bbb", false, "ccc", 12.34),
91-
Map.of("aaa", 124, "bbb", false, "ccc", 12.35)
90+
Map.of("aaa", 124, "bbb", "hey", "ccc", 12.34),
91+
Map.of("aaa", 124, "bbb", "yeh", "ccc", 12.35)
9292
);
9393

9494
var mapping = XContentBuilder.builder(XContentType.JSON.xContent());
9595
mapping.startObject();
9696
mapping.startObject("_doc");
9797
{
9898
mapping.startObject("aaa").field("type", "long").endObject();
99-
mapping.startObject("bbb").field("type", "boolean").endObject();
99+
mapping.startObject("bbb").field("type", "keyword").endObject();
100100
mapping.startObject("ccc").field("type", "half_float").endObject();
101101
}
102102
mapping.endObject();

0 commit comments

Comments
 (0)