Skip to content

Commit 2d1f7b1

Browse files
db: single row insert shortcut
1 parent e02b7ab commit 2d1f7b1

5 files changed

Lines changed: 60 additions & 11 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public String toString() {
142142
}
143143

144144
private static class MultiValuesUnwrapper {
145-
private List<Record> result;
145+
private final List<Record> result;
146146

147147
MultiValuesUnwrapper() {
148148
this.result = new ArrayList<>();
@@ -173,7 +173,7 @@ void add(Record record) {
173173
private static class RecordFromStream {
174174
private boolean hasMultiValues;
175175
private boolean hasValueGenerators;
176-
private List<Object> values;
176+
private final List<Object> values;
177177

178178
public RecordFromStream(Stream<Object> valuesStream) {
179179
values = new ArrayList<>();

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.testingisdocumenting.webtau.reporter.MessageToken;
2323

2424
import java.sql.SQLException;
25+
import java.util.Map;
2526

2627
import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.*;
2728
import static org.testingisdocumenting.webtau.reporter.TokenizedMessage.tokenizedMessage;
@@ -42,7 +43,16 @@ public void insert(TableData tableData) {
4243
INTO, createMessageId()),
4344
() -> tokenizedMessage(action("inserted"), numberValue(tableData.numberOfRows()), action("row(s)"),
4445
INTO, createMessageId()),
45-
() -> insertStep(tableData));
46+
() -> insertTableStep(tableData));
47+
}
48+
49+
public void insert(Map<String, Object> row) {
50+
createAndExecuteStep(
51+
tokenizedMessage(action("inserting"), numberValue(1), action("row"),
52+
INTO, createMessageId()),
53+
() -> tokenizedMessage(action("inserted"), numberValue(1), action("row"),
54+
INTO, createMessageId()),
55+
() -> insertRowStep(row));
4656
}
4757

4858
public TableData query() {
@@ -57,7 +67,7 @@ public DbQuery createQuery() {
5767
return QueryRunnerUtils.createQuery(dataSource, SqlQueriesGenerator.fullTable(name));
5868
}
5969

60-
private void insertStep(TableData tableData) {
70+
private void insertTableStep(TableData tableData) {
6171
if (tableData.isEmpty()) {
6272
return;
6373
}
@@ -75,11 +85,25 @@ private void insertStep(TableData tableData) {
7585
}
7686
}
7787

88+
private void insertRowStep(Map<String, Object> row) {
89+
QueryRunner run = new QueryRunner(dataSource.getDataSource());
90+
try {
91+
run.update(SqlQueriesGenerator.insert(name, row.keySet().stream(), row.values().stream()),
92+
row.values().toArray());
93+
} catch (SQLException e) {
94+
throw new RuntimeException(e);
95+
}
96+
}
97+
7898
private MessageToken createMessageId() {
7999
return id(dataSource.getLabel() + "." + name);
80100
}
81101

82102
public void leftShift(TableData tableData) {
83103
insert(tableData);
84104
}
105+
106+
public void leftShift(Map<String, Object> row) {
107+
insert(row);
108+
}
85109
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@
1818

1919
import org.testingisdocumenting.webtau.data.table.Record;
2020

21+
import java.util.Collection;
2122
import java.util.List;
2223
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
2325

2426
public class SqlQueriesGenerator {
2527
private SqlQueriesGenerator() {
2628
}
2729

2830
public static String insert(String tableName, Record record) {
29-
String columnNames = record.getHeader().getNamesStream().collect(Collectors.joining(", "));
31+
return insert(tableName, record.getHeader().getNamesStream(), record.valuesStream());
32+
}
3033

31-
List<Object> values = record.getValues();
32-
String questionMarks = values.stream().map(v -> "?").collect(Collectors.joining(", "));
34+
public static String insert(String tableName, Stream<String> columnNamesStream, Stream<Object> valuesStream) {
35+
String enumeratedColumnNames = columnNamesStream.collect(Collectors.joining(", "));
36+
String questionMarks = valuesStream.map(v -> "?").collect(Collectors.joining(", "));
3337

34-
return "INSERT INTO " + tableName + " (" + columnNames + ") VALUES (" + questionMarks + ")";
38+
return "INSERT INTO " + tableName + " (" + enumeratedColumnNames + ") VALUES (" + questionMarks + ")";
3539
}
3640

3741
public static String fullTable(String tableName) {

webtau-db/src/test/groovy/org/testingisdocumenting/webtau/db/DatabaseFacadeTest.groovy

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
3636
"id2" | "another set" | 2000 }
3737

3838
PRICES.createQuery().should == ["ID" | "DESCRIPTION" | "PRICE"] {
39-
___________________________________
40-
"id1" | "nice set" | 1000
41-
"id2" | "another set" | 2000 }
39+
___________________________________
40+
"id1" | "nice set" | 1000
41+
"id2" | "another set" | 2000 }
4242

4343
db.createQuery("select * from PRICES where id='id2'").should ==
4444
["ID" | "DESCRIPTION" | "PRICE"] {
@@ -59,6 +59,18 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
5959
PRICES.query().numberOfRows().should == 2
6060
}
6161

62+
@Test
63+
void "should insert single row into table"() {
64+
db.update("delete from PRICES")
65+
66+
def PRICES = db.table("PRICES")
67+
PRICES << [id: "id1", description: "nice set", price: 1000]
68+
69+
PRICES.should == ["ID" | "DESCRIPTION" | "PRICE"] {
70+
___________________________________
71+
"id1" | "nice set" | 1000 }
72+
}
73+
6274
@Test
6375
void "use table data permute, above and guid to generate rows"() {
6476
db.update("delete from PRICES")

webtau-docs/znai/database/data-setup.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ The simplest way to set up a DB state is to use handcrafted [TableData](referenc
1111
commentsType: "inline"
1212
}
1313

14+
:include-groovy: org/testingisdocumenting/webtau/db/DatabaseFacadeTest.groovy {
15+
entry: "should insert single row into table",
16+
title: "inserting single row",
17+
bodyOnly: true,
18+
startLine: "def PRICES",
19+
endLine: "1000]",
20+
commentsType: "inline"
21+
}
22+
1423
# Semi-Auto Generated TableData
1524

1625
[TableData](reference/table-data) has features like [permute](reference/table-data#permutations) and

0 commit comments

Comments
 (0)