Skip to content

Commit f871e17

Browse files
db: update queries single param shortcut (#818)
1 parent 9acddca commit f871e17

6 files changed

Lines changed: 84 additions & 13 deletions

File tree

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.testingisdocumenting.webtau.data.table.TableData;
2020
import org.testingisdocumenting.webtau.reporter.StepReportOptions;
21+
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
2122
import org.testingisdocumenting.webtau.reporter.WebTauStep;
2223

2324
import java.util.Collections;
@@ -67,22 +68,45 @@ public <E> E querySingleValue(String query, Map<String, Object> params) {
6768
}
6869

6970
public void update(String query) {
70-
WebTauStep step = createStep(null,
71-
tokenizedMessage(action("running DB update"), stringValue(query), ON, id(dataSource.getLabel())),
72-
(rows) -> tokenizedMessage(action("ran DB update"), stringValue(query), ON, id(dataSource.getLabel()),
73-
action("affected"), numberValue(rows), classifier("rows")),
74-
() -> QueryRunnerUtils.runUpdate(dataSource.getDataSource(), query));
71+
update(query, Collections.emptyMap());
72+
}
7573

76-
step.execute(StepReportOptions.REPORT_ALL);
74+
public <E> void update(String query, E singleParam) {
75+
update(query, DbNamedParamsQuery.singleNoNameParam(singleParam));
7776
}
7877

7978
public void update(String query, Map<String, Object> params) {
79+
DbNamedParamsQuery namedParamsQuery = new DbNamedParamsQuery(query, params);
80+
8081
WebTauStep step = createStep(null,
81-
tokenizedMessage(action("running DB update"), stringValue(query), ON, id(dataSource.getLabel()), WITH, stringValue(params)),
82-
(rows) -> tokenizedMessage(action("ran DB update"), stringValue(query), ON, id(dataSource.getLabel()),
83-
action("affected"), numberValue(rows), classifier("rows")),
84-
() -> QueryRunnerUtils.runUpdate(dataSource.getDataSource(), query, params));
82+
updateMessage("running DB update", query, namedParamsQuery.effectiveParams(), null),
83+
(rows) -> updateMessage("ran DB update", query, Collections.emptyMap(), (Integer) rows),
84+
() -> QueryRunnerUtils.runUpdate(dataSource.getDataSource(), query, namedParamsQuery));
8585

8686
step.execute(StepReportOptions.REPORT_ALL);
8787
}
88+
89+
private TokenizedMessage updateMessage(String actionLabel,
90+
String query,
91+
Map<String, Object> params,
92+
Integer numberOfRows) {
93+
return appendParamsAndAffectedIfRequired(
94+
tokenizedMessage(action(actionLabel), stringValue(query), ON, id(dataSource.getLabel())),
95+
params,
96+
numberOfRows);
97+
}
98+
99+
private TokenizedMessage appendParamsAndAffectedIfRequired(TokenizedMessage message,
100+
Map<String, Object> params,
101+
Integer numberOfRows) {
102+
if (!params.isEmpty()) {
103+
message.add(WITH, stringValue(params));
104+
}
105+
106+
if (numberOfRows != null) {
107+
message.add(action("affected"), numberValue(numberOfRows), classifier("rows"));
108+
}
109+
110+
return message;
111+
}
88112
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public void update(String query, Map<String, Object> params) {
7575
from(getPrimaryDataSource()).update(query, params);
7676
}
7777

78+
public <E> void update(String query, E singleParam) {
79+
from(getPrimaryDataSource()).update(query, singleParam);
80+
}
81+
7882
private static LabeledDataSource getPrimaryDataSource() {
7983
return new LabeledDataSource(DbDataSourceProviders.provideByName("primary"), "primary-db");
8084
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ static int runUpdate(DataSource dataSource, String query) {
4646
}
4747

4848
static int runUpdate(DataSource dataSource, String query, Map<String, Object> params) {
49+
DbNamedParamsQuery namedParamsQuery = new DbNamedParamsQuery(query, params);
50+
return runUpdate(dataSource, query, namedParamsQuery);
51+
}
52+
53+
static int runUpdate(DataSource dataSource, String query, DbNamedParamsQuery namedParamsQuery) {
4954
QueryRunner run = new QueryRunner(dataSource);
5055

5156
try {
52-
if (params.isEmpty()) {
57+
if (namedParamsQuery.isEmpty()) {
5358
return run.update(query);
5459
} else {
55-
DbNamedParamsQuery namedParamsQuery = new DbNamedParamsQuery(query, params);
5660
return run.update(namedParamsQuery.getQuestionMarksQuery(), namedParamsQuery.getQuestionMarksValues());
5761
}
5862
} catch (SQLException e) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
262262
db.table("PRICES").query().numberOfRows().should == 0
263263
}
264264

265+
@Test
266+
void "delete with single param shortcut"() {
267+
setupPrices()
268+
269+
db.update("delete from PRICES where price > :price", 950)
270+
db.table("PRICES").query().numberOfRows().should == 0
271+
}
272+
265273
@Test
266274
void "should run updates with params"() {
267275
def PRICES = setupPrices()

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class DatabaseStepReporterTest extends DatabaseBaseTest implements StepReporter
6565
}
6666

6767
@Test
68-
void "query result comparison step should not capture params when no params ara passed"() {
68+
void "query result comparison step should not capture params when no params are passed"() {
6969
setupPrices()
7070

7171
def price = db.query("select price from PRICES where id='id1'")
@@ -75,8 +75,31 @@ class DatabaseStepReporterTest extends DatabaseBaseTest implements StepReporter
7575
fullMessage.should contain("select price from PRICES where id='id1' equals 1000")
7676
}
7777

78+
@Test
79+
void "update step should capture query and params in case of maps param"() {
80+
setupPrices()
81+
82+
db.update("delete from PRICES where price>:price1 or price>:price2", [price1: 1000, price2: 2000])
83+
84+
def fullMessage = stepMessages.join('\n')
85+
fullMessage.should contain(
86+
"running DB update delete from PRICES where price>:price1 or price>:price2 on primary-db with {price1=1000, price2=2000}")
87+
}
88+
89+
@Test
90+
void "update step should capture query and params in case of single param"() {
91+
setupPrices()
92+
93+
db.update("delete from PRICES where price>:price", 950)
94+
95+
def fullMessage = stepMessages.join('\n')
96+
fullMessage.should contain(
97+
"running DB update delete from PRICES where price>:price on primary-db with {price=950}")
98+
}
99+
78100
@Override
79101
void onStepStart(WebTauStep step) {
102+
stepMessages << step.inProgressMessage.toString()
80103
}
81104

82105
@Override

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ Note: code above assumes `WebTauCore.*` static import or `WebTauGroovyDsl.*` sta
6565
commentsType: "inline"
6666
}
6767

68+
:include-groovy: org/testingisdocumenting/webtau/db/DatabaseFacadeTest.groovy {
69+
entry: "delete with single param shortcut",
70+
title: "delete data with single param shortcut",
71+
bodyOnly: true,
72+
includeRegexp: "delete",
73+
commentsType: "inline"
74+
}
75+
6876
# Updating Tables
6977

7078
:include-table: doc-artifacts/db-before-update.json { title: "PRICES" }

0 commit comments

Comments
 (0)