Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.4.0</version>
<version>5.4.1</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ class TableDataGroovyTest {
"id31" | "id32" | "description three" }
}

@Test
void "should merge in another table using only matching column names"() {
def table = ["hello" | "world"] {
12 | 46
54 | 88 }

def incoming = ["extra" | "hello"] {
700 | 24
800 | 48 }

table.addRowsExistingColumnsOnly(incoming)
table.should == ["hello" | "world"] {
12 | 46
54 | 88
24 | null
48 | null }
}

@Test
void "should generate multiple rows from multi-values"() {
def tableData = createTableDataWithPermute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,33 @@

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Record implements PrettyPrintable {
private final TableDataHeader header;
private final List<Object> values;
private final List<?> values;
private final CompositeKey key;

private final boolean hasMultiValues;
private final boolean hasValueGenerators;

public Record(TableDataHeader header, Stream<?> values) {
this(header, values.collect(Collectors.toList()));
}

public Record(TableDataHeader header, List<?> values) {
this.header = header;
RecordFromStream recordFromStream = new RecordFromStream(values);
RecordFromList recordFromList = new RecordFromList(values);

if (recordFromStream.values.size() != header.size()) {
if (recordFromList.values.size() != header.size()) {
throw new IllegalArgumentException("header size is " + header.size() +
", but received " + recordFromStream.values.size() + " value(s)");
", but received " + recordFromList.values.size() + " value(s)");
}

hasMultiValues = recordFromStream.hasMultiValues;
hasValueGenerators = recordFromStream.hasValueGenerators;
this.values = recordFromStream.values;
hasMultiValues = recordFromList.hasMultiValues;
hasValueGenerators = recordFromList.hasValueGenerators;
this.values = recordFromList.values;

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

public Stream<Object> valuesStream() {
public Stream<?> valuesStream() {
return values.stream();
}

public List<Object> getValues() {
public List<?> getValues() {
return values;
}

Expand Down Expand Up @@ -182,25 +187,22 @@ void add(Record record) {
}
}

private static class RecordFromStream {
private static class RecordFromList {
private boolean hasMultiValues;
private boolean hasValueGenerators;
private final List<Object> values;

public RecordFromStream(Stream<?> valuesStream) {
values = new ArrayList<>();
private final List<?> values;

valuesStream.forEach(v -> {
public RecordFromList(List<?> valuesList) {
values = valuesList;
for (Object v : valuesList) {
if (v instanceof MultiValue) {
hasMultiValues = true;
}

if (v instanceof TableDataCellValueGenerator) {
hasValueGenerators = true;
}

values.add(v);
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,22 @@ public Record findByKey(Object... keyParts) {
return find(key);
}

public void addRowsExistingColumnsOnly(TableData tableData) {
List<String> existingNames = getColumnNames();

for (Record targetRow : tableData.rows) {
List<Object> newRowData = new ArrayList<>();

for (String existingColumnName : existingNames) {
int idx = tableData.header.findColumnIdxByName(existingColumnName);

newRowData.add(idx == -1 ? null : targetRow.get(idx));
}

addRow(new Record(header, newRowData));
}
}

public void addRow(List<?> values) {
addRow(values.stream());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public TableDataHeader(Stream<String> names, Stream<String> keyNames) {
names.forEach(name -> add(name, keyNamesAsSet.contains(name)));
}

public Record createRecord(Stream<Object> values) {
public Record createRecord(Stream<?> values) {
return new Record(this, values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void insertTableStep(List<Map<String, Object>> rows) {
private void insertMultipleRowsStep(Supplier<Boolean> isEmpty,
Supplier<Integer> size,
Supplier<Stream<String>> header,
Function<Integer, Stream<Object>> valuesByRowIdx) {
Function<Integer, Stream<?>> valuesByRowIdx) {
if (isEmpty.get()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static String insert(String tableName, Map<String, Object> row) {
return insert(tableName, row.keySet().stream(), row.values().stream());
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add: [TableData](reference/table-data) `.addRowsExistingColumnsOnly(otherTable)` to merge other tables into