Skip to content

Commit 2ec2bd4

Browse files
tabledata: add .addRowsExistingColumnsOnly(other) to merge into (#1519)
1 parent b9e516d commit 2ec2bd4

8 files changed

Lines changed: 59 additions & 22 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@
293293
<dependency>
294294
<groupId>io.github.bonigarcia</groupId>
295295
<artifactId>webdrivermanager</artifactId>
296-
<version>5.4.0</version>
296+
<version>5.4.1</version>
297297
</dependency>
298298

299299
<dependency>

webtau-core-groovy/src/test/groovy/org/testingisdocumenting/webtau/data/table/TableDataGroovyTest.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,24 @@ class TableDataGroovyTest {
8181
"id31" | "id32" | "description three" }
8282
}
8383

84+
@Test
85+
void "should merge in another table using only matching column names"() {
86+
def table = ["hello" | "world"] {
87+
12 | 46
88+
54 | 88 }
89+
90+
def incoming = ["extra" | "hello"] {
91+
700 | 24
92+
800 | 48 }
93+
94+
table.addRowsExistingColumnsOnly(incoming)
95+
table.should == ["hello" | "world"] {
96+
12 | 46
97+
54 | 88
98+
24 | null
99+
48 | null }
100+
}
101+
84102
@Test
85103
void "should generate multiple rows from multi-values"() {
86104
def tableData = createTableDataWithPermute()

webtau-core/src/main/java/org/testingisdocumenting/webtau/data/table/Record.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,33 @@
2626

2727
import java.util.*;
2828
import java.util.function.Function;
29+
import java.util.stream.Collectors;
2930
import java.util.stream.Stream;
3031

3132
public class Record implements PrettyPrintable {
3233
private final TableDataHeader header;
33-
private final List<Object> values;
34+
private final List<?> values;
3435
private final CompositeKey key;
3536

3637
private final boolean hasMultiValues;
3738
private final boolean hasValueGenerators;
3839

3940
public Record(TableDataHeader header, Stream<?> values) {
41+
this(header, values.collect(Collectors.toList()));
42+
}
43+
44+
public Record(TableDataHeader header, List<?> values) {
4045
this.header = header;
41-
RecordFromStream recordFromStream = new RecordFromStream(values);
46+
RecordFromList recordFromList = new RecordFromList(values);
4247

43-
if (recordFromStream.values.size() != header.size()) {
48+
if (recordFromList.values.size() != header.size()) {
4449
throw new IllegalArgumentException("header size is " + header.size() +
45-
", but received " + recordFromStream.values.size() + " value(s)");
50+
", but received " + recordFromList.values.size() + " value(s)");
4651
}
4752

48-
hasMultiValues = recordFromStream.hasMultiValues;
49-
hasValueGenerators = recordFromStream.hasValueGenerators;
50-
this.values = recordFromStream.values;
53+
hasMultiValues = recordFromList.hasMultiValues;
54+
hasValueGenerators = recordFromList.hasValueGenerators;
55+
this.values = recordFromList.values;
5156

5257
this.key = header.hasKeyColumns() ?
5358
new CompositeKey(header.getKeyIdxStream().map(this::get)) : null;
@@ -87,11 +92,11 @@ public <E> E get(int idx, E defaultValue) {
8792
return (E) values.get(idx);
8893
}
8994

90-
public Stream<Object> valuesStream() {
95+
public Stream<?> valuesStream() {
9196
return values.stream();
9297
}
9398

94-
public List<Object> getValues() {
99+
public List<?> getValues() {
95100
return values;
96101
}
97102

@@ -182,25 +187,22 @@ void add(Record record) {
182187
}
183188
}
184189

185-
private static class RecordFromStream {
190+
private static class RecordFromList {
186191
private boolean hasMultiValues;
187192
private boolean hasValueGenerators;
188-
private final List<Object> values;
189-
190-
public RecordFromStream(Stream<?> valuesStream) {
191-
values = new ArrayList<>();
193+
private final List<?> values;
192194

193-
valuesStream.forEach(v -> {
195+
public RecordFromList(List<?> valuesList) {
196+
values = valuesList;
197+
for (Object v : valuesList) {
194198
if (v instanceof MultiValue) {
195199
hasMultiValues = true;
196200
}
197201

198202
if (v instanceof TableDataCellValueGenerator) {
199203
hasValueGenerators = true;
200204
}
201-
202-
values.add(v);
203-
});
205+
}
204206
}
205207
}
206208
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/data/table/TableData.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,22 @@ public Record findByKey(Object... keyParts) {
191191
return find(key);
192192
}
193193

194+
public void addRowsExistingColumnsOnly(TableData tableData) {
195+
List<String> existingNames = getColumnNames();
196+
197+
for (Record targetRow : tableData.rows) {
198+
List<Object> newRowData = new ArrayList<>();
199+
200+
for (String existingColumnName : existingNames) {
201+
int idx = tableData.header.findColumnIdxByName(existingColumnName);
202+
203+
newRowData.add(idx == -1 ? null : targetRow.get(idx));
204+
}
205+
206+
addRow(new Record(header, newRowData));
207+
}
208+
}
209+
194210
public void addRow(List<?> values) {
195211
addRow(values.stream());
196212
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/data/table/header/TableDataHeader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public TableDataHeader(Stream<String> names, Stream<String> keyNames) {
4242
names.forEach(name -> add(name, keyNamesAsSet.contains(name)));
4343
}
4444

45-
public Record createRecord(Stream<Object> values) {
45+
public Record createRecord(Stream<?> values) {
4646
return new Record(this, values);
4747
}
4848

webtau-db/src/main/java/org/testingisdocumenting/webtau/db/DatabaseTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void insertTableStep(List<Map<String, Object>> rows) {
123123
private void insertMultipleRowsStep(Supplier<Boolean> isEmpty,
124124
Supplier<Integer> size,
125125
Supplier<Stream<String>> header,
126-
Function<Integer, Stream<Object>> valuesByRowIdx) {
126+
Function<Integer, Stream<?>> valuesByRowIdx) {
127127
if (isEmpty.get()) {
128128
return;
129129
}

webtau-db/src/main/java/org/testingisdocumenting/webtau/db/gen/SqlQueriesGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static String insert(String tableName, Map<String, Object> row) {
3434
return insert(tableName, row.keySet().stream(), row.values().stream());
3535
}
3636

37-
public static String insert(String tableName, Stream<String> columnNamesStream, Stream<Object> valuesStream) {
37+
public static String insert(String tableName, Stream<String> columnNamesStream, Stream<?> valuesStream) {
3838
String enumeratedColumnNames = columnNamesStream.collect(Collectors.joining(", "));
3939
String questionMarks = valuesStream.map(v -> "?").collect(Collectors.joining(", "));
4040

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: [TableData](reference/table-data) `.addRowsExistingColumnsOnly(otherTable)` to merge other tables into

0 commit comments

Comments
 (0)