Skip to content

Commit

Permalink
[vividus] Add ability to keep all columns in DISTINCTING transformer (
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst authored Apr 5, 2024
1 parent 09adb57 commit a1173de
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 22 deletions.
38 changes: 34 additions & 4 deletions docs/modules/commons/pages/table-transformers.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,24 @@ in the exampe above we select only those rows that do not contain `bbb` string i

`DISTINCTING` transformer generates xref:ROOT:glossary.adoc#_examplestable[ExamplesTable] from the specified columns, deleting the rows with repeating values.

[cols="1,3", options="header"]
[cols="1,1,4", options="header"]
|===
|Parameter
|Default value
|Description

|`byColumnNames`
|the names of the columns to keep separated by semicolon
|[subs=+quotes]`*byColumnNames*`
|
|The names of the columns to keep (separated by semicolon).

|`keepAllColumns`
|`false`
|If `false`, only the columns specified in `byColumnNames` will appear in the resulting table. Otherwise, all columns
from the initial table will be included in the resulting table. If the initial table has rows with repeated values,
the value from the first row will be retained in the resulting table.
|===

The following `DISTINCTING` transformer:
This `DISTINCTING` transformer:

[source,gherkin]
----
Expand All @@ -391,6 +399,28 @@ will result in the following xref:ROOT:glossary.adoc#_examplestable[ExamplesTabl
|B |D |
----

This `DISTINCTING` transformer keeping all columns:

[source,gherkin]
----
{transformer=DISTINCTING, byColumnNames=column1;column2, keepAllColumns=true}
|column1|column2|column3|
|A |C |0 |
|B |D |1 |
|A |C |1 |
|A |C |0 |
|B |D |0 |
----

will result in the following xref:ROOT:glossary.adoc#_examplestable[ExamplesTable]:

[source,gherkin]
----
|column1|column2|column3|
|A |C |0 |
|B |D |1 |
----

== MERGING

`MERGING` transformer merges several tables into one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Examples:
}
{transformer=FILTERING, column.col1=.+}

Scenario: Verify DISTINCTING transformer
Scenario: Validate DISTINCTING transformer
Meta:
@requirementId 992
Then `<column1>` is equal to `a a b b`
Expand All @@ -219,6 +219,25 @@ Examples:
|b |x |b |
|b |y |b |

Scenario: Validate DISTINCTING transformer keeping all columns
Meta:
@requirementId 992
Then `<column1>` is equal to `a a b b`
Then `<column2>` is equal to `x x x x`
Then `<column3>` is equal to `a b a b`
Examples:
{transformer=DISTINCTING, byColumnNames=column1;column3, keepAllColumns=true}
{transformer=JOINING, joinMode=rows}
|column1|column2|column3|
|a |x |a |
|a |y |a |
|a |x |b |
|a |y |b |
|b |x |a |
|b |y |a |
|b |x |b |
|b |y |b |

Scenario: Verify possibility to use the range of ALL cells
Meta:
@requirementId 919
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,9 +16,13 @@

package org.vividus.transformer;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.jbehave.core.model.ExamplesTable.TableProperties;
import org.jbehave.core.model.ExamplesTable.TableRows;
Expand All @@ -31,14 +35,36 @@ public class DistinctingTableTransformer extends AbstractFilteringTableTransform
public String transform(String tableAsString, TableParsers tableParsers, TableProperties tableProperties)
{
String byColumnNames = tableProperties.getMandatoryNonBlankProperty(BY_COLUMNS_NAMES_PROPERTY, String.class);
String keepAllColumns = tableProperties.getProperties().getProperty("keepAllColumns");

TableRows tableRows = tableParsers.parseRows(tableAsString, tableProperties);
List<String> allColumnNames = tableRows.getHeaders();
List<String> filteredColumnNames = filterColumnNames(allColumnNames, byColumnNames);
List<List<String>> rows = tableRows.getRows();
filterRowsByColumnNames(allColumnNames, rows, filteredColumnNames);

Set<List<String>> distinctRows = new LinkedHashSet<>(rows);
Collection<String> headers;
Collection<List<String>> distinctRows;
if (Boolean.parseBoolean(keepAllColumns))
{
int[] indexesToDistinct = IntStream.range(0, allColumnNames.size())
.filter(i -> filteredColumnNames.contains(allColumnNames.get(i)))
.sorted()
.toArray();

return ExamplesTableProcessor.buildExamplesTable(filteredColumnNames, distinctRows, tableProperties);
distinctRows = rows.stream().collect(Collectors.toMap(
r -> IntStream.of(indexesToDistinct).mapToObj(r::get).toList(),
Function.identity(),
(r1, r2) -> r1,
LinkedHashMap::new
)).values();
headers = allColumnNames;
}
else
{
filterRowsByColumnNames(allColumnNames, rows, filteredColumnNames);
distinctRows = new LinkedHashSet<>(rows);
headers = filteredColumnNames;
}
return ExamplesTableProcessor.buildExamplesTable(headers, distinctRows, tableProperties);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,23 +26,24 @@

class DistinctingTableTransformerTests
{
private static final String INPUT_TABLE = """
|key1|key2|key3|
|a |x |a |
|a |y |a |
|b |x |a |
|b |y |a |
|b |x |b |
|b |y |b |
|a |x |b |
|a |y |b |""";

@Test
void testDistinct()
{
var parameterConverters = new ParameterConverters();
var tableParsers = new TableParsers(parameterConverters);
var tableProperties = new TableProperties("byColumnNames=key1;key3", new Keywords(), parameterConverters);
var inputTable = """
|key1|key2|key3|
|a |x |a |
|a |y |a |
|b |x |a |
|b |y |a |
|b |x |b |
|b |y |b |
|a |x |b |
|a |y |b |""";
String result = new DistinctingTableTransformer().transform(inputTable, tableParsers, tableProperties);
String result = new DistinctingTableTransformer().transform(INPUT_TABLE, tableParsers, tableProperties);
var distinctTable = """
|key1|key3|
|a|a|
Expand All @@ -51,4 +52,21 @@ void testDistinct()
|a|b|""";
assertEquals(distinctTable, result);
}

@Test
void shouldReturnDistinctTableKeepingAllColumns()
{
var parameterConverters = new ParameterConverters();
var tableParsers = new TableParsers(parameterConverters);
var tableProperties = new TableProperties("byColumnNames=key1;key3, keepAllColumns=true", new Keywords(),
parameterConverters);
String result = new DistinctingTableTransformer().transform(INPUT_TABLE, tableParsers, tableProperties);
var distinctTable = """
|key1|key2|key3|
|a|x|a|
|b|x|a|
|b|x|b|
|a|x|b|""";
assertEquals(distinctTable, result);
}
}

0 comments on commit a1173de

Please # to comment.