Skip to content

Commit b296e34

Browse files
committed
Refactored to be easier to read and extend the tests
1 parent 9b50f2b commit b296e34

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/action/LookupJoinTypesIT.java

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
import java.util.ArrayList;
1818
import java.util.Collection;
1919
import java.util.Iterator;
20+
import java.util.LinkedHashSet;
2021
import java.util.List;
2122
import java.util.Locale;
22-
import java.util.Map;
23+
import java.util.Set;
2324
import java.util.stream.Collectors;
2425

2526
import static org.elasticsearch.test.ESIntegTestCase.Scope.SUITE;
@@ -29,18 +30,32 @@
2930
@ClusterScope(scope = SUITE, numClientNodes = 1, numDataNodes = 1)
3031
public class LookupJoinTypesIT extends ESIntegTestCase {
3132

32-
private static final Map<DataType, DataType> compatibleJoinTypes = Map.of(
33-
DataType.KEYWORD,
34-
DataType.KEYWORD,
35-
DataType.TEXT,
36-
DataType.KEYWORD,
37-
DataType.INTEGER,
38-
DataType.INTEGER,
39-
DataType.FLOAT,
40-
DataType.FLOAT,
41-
DataType.DOUBLE,
42-
DataType.DOUBLE
43-
);
33+
private static final Set<TestConfig> compatibleJoinTypes = new LinkedHashSet<>();
34+
static {
35+
addConfig(DataType.KEYWORD, DataType.KEYWORD, true);
36+
addConfig(DataType.TEXT, DataType.KEYWORD, true);
37+
addConfig(DataType.INTEGER, DataType.INTEGER, true);
38+
addConfig(DataType.FLOAT, DataType.FLOAT, true);
39+
addConfig(DataType.DOUBLE, DataType.DOUBLE, true);
40+
}
41+
42+
private static void addConfig(DataType mainType, DataType lookupType, boolean passes) {
43+
compatibleJoinTypes.add(new TestConfig(mainType, lookupType, passes));
44+
}
45+
46+
record TestConfig(DataType mainType, DataType lookupType, boolean passes) {
47+
private String indexName() {
48+
return "index_" + mainType.esType() + "_" + lookupType.esType();
49+
}
50+
51+
private String fieldName() {
52+
return "field_" + mainType.esType();
53+
}
54+
55+
private String mainProperty() {
56+
return "\"" + fieldName() + "\": { \"type\" : \"" + mainType.esType() + "\" }";
57+
}
58+
}
4459

4560
protected Collection<Class<? extends Plugin>> nodePlugins() {
4661
return List.of(EsqlPlugin.class);
@@ -49,12 +64,12 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
4964
public void testLookupJoinTypes() {
5065
initIndexes();
5166
initData();
52-
for (Map.Entry<DataType, DataType> entry : compatibleJoinTypes.entrySet()) {
67+
for (TestConfig config : compatibleJoinTypes) {
5368
String query = String.format(
5469
Locale.ROOT,
55-
"FROM index | LOOKUP JOIN %s ON field_%s | KEEP other",
56-
indexName(entry.getKey(), entry.getValue()),
57-
entry.getKey().esType()
70+
"FROM index | LOOKUP JOIN %s ON %s | KEEP other",
71+
config.indexName(),
72+
config.fieldName()
5873
);
5974
try (var response = EsqlQueryRequestBuilder.newRequestBuilder(client()).query(query).get()) {
6075
Iterator<Object> results = response.response().column(0).iterator();
@@ -68,12 +83,7 @@ public void testLookupJoinTypes() {
6883
private void initIndexes() {
6984
// The main index will have many fields, one of each type to use in later type specific joins
7085
StringBuilder mainFields = new StringBuilder("{\n \"properties\" : {\n");
71-
mainFields.append(
72-
compatibleJoinTypes.keySet()
73-
.stream()
74-
.map((l) -> "\"field_" + l.esType() + "\": { \"type\" : \"" + l.esType() + "\" }")
75-
.collect(Collectors.joining(",\n "))
76-
);
86+
mainFields.append(compatibleJoinTypes.stream().map(TestConfig::mainProperty).collect(Collectors.joining(",\n ")));
7787
mainFields.append(" }\n}\n");
7888
assertAcked(prepareCreate("index").setMapping(mainFields.toString()));
7989

@@ -83,39 +93,31 @@ private void initIndexes() {
8393
.put("index.mode", "lookup");
8494
compatibleJoinTypes.forEach(
8595
// Each lookup index will get a document with a field to join on, and a results field to get back
86-
(l, r) -> { assertAcked(prepareCreate(indexName(l, r)).setSettings(settings.build()).setMapping(String.format(Locale.ROOT, """
96+
(c) -> { assertAcked(prepareCreate(c.indexName()).setSettings(settings.build()).setMapping(String.format(Locale.ROOT, """
8797
{
8898
"properties" : {
89-
"field_%s": { "type" : "%s" },
99+
"%s": { "type" : "%s" },
90100
"other": { "type" : "keyword" }
91101
}
92102
}
93-
""", l.esType(), r.esType()))); }
103+
""", c.fieldName(), c.lookupType.esType()))); }
94104
);
95105
}
96106

97-
private String indexName(DataType mainType, DataType lookupType) {
98-
return "index_" + mainType.esType() + "_" + lookupType.esType();
99-
}
100-
101107
private void initData() {
102108
List<String> mainProperties = new ArrayList<>();
103109
int docId = 0;
104-
for (Map.Entry<DataType, DataType> entry : compatibleJoinTypes.entrySet()) {
105-
DataType mainType = entry.getKey();
106-
DataType lookupType = entry.getValue();
107-
String index = indexName(mainType, lookupType);
108-
String field = "field_" + mainType.esType();
109-
String value = sampleDataFor(lookupType);
110+
for (TestConfig config : compatibleJoinTypes) {
111+
String value = sampleDataFor(config.lookupType());
110112
String doc = String.format(Locale.ROOT, """
111113
{
112114
"%s": %s,
113115
"other": "value"
114116
}
115-
""", field, value);
116-
mainProperties.add(String.format(Locale.ROOT, "\"%s\": %s", field, value));
117-
index(index, "" + (++docId), doc);
118-
refresh(index);
117+
""", config.fieldName(), value);
118+
mainProperties.add(String.format(Locale.ROOT, "\"%s\": %s", config.fieldName(), value));
119+
index(config.indexName(), "" + (++docId), doc);
120+
refresh(config.indexName());
119121
}
120122
index("index", "1", String.format(Locale.ROOT, """
121123
{

0 commit comments

Comments
 (0)