Skip to content

Commit a76c17a

Browse files
data: csv as a separate namespace. add tabledata.replace
1 parent 059175e commit a76c17a

12 files changed

Lines changed: 215 additions & 45 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2020 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,6 +26,7 @@ import static org.testingisdocumenting.webtau.data.table.TableDataJavaTestValida
2526
import static org.testingisdocumenting.webtau.data.table.TableDataJavaTestValidations.validateAboveValueWithMath
2627
import static org.testingisdocumenting.webtau.data.table.TableDataJavaTestValidations.validatePermute
2728
import static org.testingisdocumenting.webtau.data.table.TableDataJavaTestValidations.validateSimpleTableData
29+
import static org.testingisdocumenting.webtau.data.table.TableDataJavaTestValidations.validateSimpleTableDataAfterReplace
2830

2931
class TableDataGroovyTest {
3032
@Test
@@ -75,6 +77,19 @@ class TableDataGroovyTest {
7577
validateAboveValueWithMath(tableData)
7678
}
7779

80+
@Test
81+
void "should replace single specified value"() {
82+
def tableData = createTableWithUnderscore()
83+
TableData newTableData = replaceValue(tableData)
84+
85+
validateSimpleTableData(tableData)
86+
validateSimpleTableDataAfterReplace(newTableData)
87+
}
88+
89+
private static TableData replaceValue(TableData tableData) {
90+
tableData.replace("v1b", "v1b_")
91+
}
92+
7893
@Test
7994
void "should ignore underscore under header"() {
8095
def table = ["hello" | "world"] {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ public <T, R> TableData map(TableDataCellMapFunction<T, R> mapper) {
149149
return mapped;
150150
}
151151

152+
public TableData replace(Object before, Object after) {
153+
return map(((rowIdx, colIdx, columnName, v) -> v.equals(before) ? after : v));
154+
}
155+
152156
public <T, R> Stream<R> mapColumn(String columnName, Function<T, R> mapper) {
153157
int idx = header.columnIdxByName(columnName);
154158
return rows.stream().map(r -> mapper.apply(r.get(idx)));

webtau-core/src/test/java/org/testingisdocumenting/webtau/data/table/TableDataJavaTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2020 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,7 +28,7 @@
2728
import static org.testingisdocumenting.webtau.data.table.TableDataJavaTestValidations.*;
2829

2930
public class TableDataJavaTest {
30-
private static TableDataCellValueGenerator<?> increment = cell.above.plus(1);
31+
private static final TableDataCellValueGenerator<?> increment = cell.above.plus(1);
3132

3233
@Test
3334
public void shouldCreateTableUsingConvenienceMethodsForTableAndValues() {
@@ -61,7 +62,6 @@ public void cellAboveShouldBeSubstitutedWithValueFromPreviousRow() {
6162
TableData tableData = createTableDataWithAboveRef();
6263
validateAboveValue(tableData);
6364

64-
6565
saveTableWithDate(tableData, "table-with-cell-above");
6666
}
6767

@@ -70,7 +70,6 @@ public void cellAboveShouldSupportPlusOperation() {
7070
TableData tableData = createTableDataWithAboveRefAndMath();
7171
validateAboveValueWithMath(tableData);
7272

73-
7473
saveTableWithDate(tableData, "table-with-cell-above-math");
7574
}
7675

@@ -80,6 +79,21 @@ public void cellAboveShouldSupportPlusOperationWithExtraction() {
8079
validateAboveValueWithMath(tableData);
8180
}
8281

82+
@Test
83+
public void shouldReplaceSingleSpecifiedValue() {
84+
TableData tableData = createTableDataInOneGo();
85+
TableData newTableData = replaceValue(tableData);
86+
87+
validateSimpleTableData(tableData);
88+
validateSimpleTableDataAfterReplace(newTableData);
89+
90+
saveTableWithDate(newTableData, "table-after-replace");
91+
}
92+
93+
private static TableData replaceValue(TableData tableData) {
94+
return tableData.replace("v1b", "v1b_");
95+
}
96+
8397
private static TableData createTableDataSeparateValues() {
8498
return table("Col A", "Col B", "Col C").values(
8599
"v1a", "v1b", "v1c",

webtau-core/src/test/java/org/testingisdocumenting/webtau/data/table/TableDataJavaTestValidations.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ public static void validateSimpleTableData(TableData tableData) {
2929
aMapOf("Col A", "v2a", "Col B", "v2b", "Col C", "v2c")));
3030
}
3131

32+
public static void validateSimpleTableDataAfterReplace(TableData tableData) {
33+
actual(tableData.numberOfRows()).should(equal(2));
34+
actual(tableData.row(0).toMap()).should(equal(
35+
aMapOf("Col A", "v1a", "Col B", "v1b_", "Col C", "v1c")));
36+
actual(tableData.row(1).toMap()).should(equal(
37+
aMapOf("Col A", "v2a", "Col B", "v2b", "Col C", "v2c")));
38+
}
39+
3240
public static void validatePermute(TableData tableData) {
3341
actual(tableData.numberOfRows()).should(equal(6));
3442
actual(tableData.row(0).toMap()).should(equal(

webtau-data/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3+
~ Copyright 2020 webtau maintainers
34
~ Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
45
~
56
~ Licensed under the Apache License, Version 2.0 (the "License");
@@ -33,6 +34,12 @@
3334
<version>${project.version}</version>
3435
</dependency>
3536

37+
<dependency>
38+
<groupId>org.testingisdocumenting.webtau</groupId>
39+
<artifactId>webtau-core</artifactId>
40+
<version>${project.version}</version>
41+
</dependency>
42+
3643
<dependency>
3744
<groupId>org.testingisdocumenting.webtau</groupId>
3845
<artifactId>webtau-config</artifactId>
Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2020 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,51 +17,13 @@
1617

1718
package org.testingisdocumenting.webtau.data;
1819

19-
import org.testingisdocumenting.webtau.cfg.WebTauConfig;
20-
import org.testingisdocumenting.webtau.utils.CsvUtils;
21-
import org.testingisdocumenting.webtau.utils.FileUtils;
22-
import org.testingisdocumenting.webtau.utils.ResourceUtils;
23-
24-
import java.nio.file.Files;
25-
import java.nio.file.Path;
26-
import java.util.List;
27-
import java.util.Map;
20+
import org.testingisdocumenting.webtau.data.csv.DataCsv;
2821

2922
public class Data {
3023
public static final Data data = new Data();
3124

32-
private Data() {
33-
}
34-
35-
public List<Map<String, String>> csv(String fileOrResourcePath) {
36-
return CsvUtils.parse(textContent(fileOrResourcePath));
37-
}
38-
39-
public List<Map<String, Object>> csvAutoConverted(String fileOrResourcePath) {
40-
return CsvUtils.parseWithAutoConversion(textContent(fileOrResourcePath));
41-
}
25+
public final DataCsv csv = new DataCsv();
4226

43-
public List<Map<String, String>> csv(List<String> header, String fileOrResourcePath) {
44-
return CsvUtils.parse(header, textContent(fileOrResourcePath));
45-
}
46-
47-
public List<Map<String, Object>> csvAutoConverted(List<String> header, String fileOrResourcePath) {
48-
return CsvUtils.parseWithAutoConversion(header, textContent(fileOrResourcePath));
49-
}
50-
51-
private String textContent(String fileOrResourcePath) {
52-
Path filePath = WebTauConfig.getCfg().getWorkingDir().resolve(fileOrResourcePath);
53-
54-
boolean hasResource = ResourceUtils.hasResource(fileOrResourcePath);
55-
boolean hasFile = Files.exists(filePath);
56-
57-
if (!hasResource && ! hasFile) {
58-
throw new IllegalArgumentException("Can't find resource \"" + fileOrResourcePath + "\" or " +
59-
"file \"" + filePath.toAbsolutePath() + "\"");
60-
}
61-
62-
return hasResource ?
63-
ResourceUtils.textContent(fileOrResourcePath) :
64-
FileUtils.fileTextContent(filePath);
27+
private Data() {
6528
}
6629
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2020 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.data.csv;
18+
19+
import org.testingisdocumenting.webtau.cfg.WebTauConfig;
20+
import org.testingisdocumenting.webtau.data.table.TableData;
21+
import org.testingisdocumenting.webtau.utils.CsvUtils;
22+
import org.testingisdocumenting.webtau.utils.FileUtils;
23+
import org.testingisdocumenting.webtau.utils.ResourceUtils;
24+
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.util.Collections;
28+
import java.util.List;
29+
import java.util.Map;
30+
31+
public class DataCsv {
32+
public TableData table(String fileOrResourcePath) {
33+
return tableFromListOfMaps(CsvUtils.parse(textContent(fileOrResourcePath)));
34+
}
35+
36+
public TableData tableAutoConverted(String fileOrResourcePath) {
37+
return tableFromListOfMaps(CsvUtils.parseWithAutoConversion(textContent(fileOrResourcePath)));
38+
}
39+
40+
public List<Map<String, String>> listOfMaps(String fileOrResourcePath) {
41+
return CsvUtils.parse(textContent(fileOrResourcePath));
42+
}
43+
44+
public List<Map<String, Object>> listOfMapsAutoConverted(String fileOrResourcePath) {
45+
return CsvUtils.parseWithAutoConversion(textContent(fileOrResourcePath));
46+
}
47+
48+
public List<Map<String, String>> listOfMaps(List<String> header, String fileOrResourcePath) {
49+
return CsvUtils.parse(header, textContent(fileOrResourcePath));
50+
}
51+
52+
public List<Map<String, Object>> listOfMapsAutoConverted(List<String> header, String fileOrResourcePath) {
53+
return CsvUtils.parseWithAutoConversion(header, textContent(fileOrResourcePath));
54+
}
55+
56+
@SuppressWarnings("unchecked")
57+
private TableData tableFromListOfMaps(List<?> listOfMaps) {
58+
if (listOfMaps.isEmpty()) {
59+
return new TableData(Collections.emptyList());
60+
}
61+
62+
Map<String, Object> firstRow = (Map<String, Object>) listOfMaps.get(0);
63+
64+
TableData result = new TableData(firstRow.keySet().stream());
65+
listOfMaps.forEach((row) -> {
66+
Map<String, Object> asMap = (Map<String, Object>) row;
67+
result.addRow(asMap.values().stream());
68+
});
69+
70+
return result;
71+
}
72+
73+
private String textContent(String fileOrResourcePath) {
74+
Path filePath = WebTauConfig.getCfg().getWorkingDir().resolve(fileOrResourcePath);
75+
76+
boolean hasResource = ResourceUtils.hasResource(fileOrResourcePath);
77+
boolean hasFile = Files.exists(filePath);
78+
79+
if (!hasResource && !hasFile) {
80+
throw new IllegalArgumentException("Can't find resource \"" + fileOrResourcePath + "\" or " +
81+
"file \"" + filePath.toAbsolutePath() + "\"");
82+
}
83+
84+
return hasResource ?
85+
ResourceUtils.textContent(fileOrResourcePath) :
86+
FileUtils.fileTextContent(filePath);
87+
}
88+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2020 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.data.csv
18+
19+
import org.junit.Test
20+
21+
class DataCsvTest {
22+
@Test
23+
void "parse csv as table data"() {
24+
def table = new DataCsv().table('src/test/resources/test.csv')
25+
table.should == ['id' | 'absolute' | 'number' | 'comment'] {
26+
_______________________________________________
27+
'id1' | 'yes' | '12' | 'what can you say'
28+
'id2' | 'no' | '24' | 'fourth'
29+
}
30+
}
31+
32+
@Test
33+
void "parse csv as table data with auto conversion of types"() {
34+
def table = new DataCsv().tableAutoConverted('src/test/resources/test.csv')
35+
table.should == ['id' | 'absolute' | 'number' | 'comment'] {
36+
_______________________________________________
37+
'id1' | 'yes' | 12 | 'what can you say'
38+
'id2' | 'no' | 24 | 'fourth' }
39+
40+
(table.row(0).get('number') instanceof Number).should == true
41+
(table.row(1).get('number') instanceof Number).should == true
42+
}
43+
44+
@Test
45+
void "parse empty csv as table data"() {
46+
def table = new DataCsv().table('src/test/resources/empty.csv')
47+
table.size().should == 0
48+
table.header.size().should == 0
49+
}
50+
}

webtau-data/src/test/resources/empty.csv

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
id, absolute, number, comment
2+
id1, yes, 12, what can you say
3+
id2, no, 24, fourth

0 commit comments

Comments
 (0)