Skip to content

Commit 9acddca

Browse files
db: rollback createQuery -> query (#817)
1 parent 339bfe4 commit 9acddca

10 files changed

Lines changed: 44 additions & 49 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public DatabaseTable table(String name) {
3838
return new DatabaseTable(dataSource, name);
3939
}
4040

41-
public DbQuery createQuery(String query) {
41+
public DbQuery query(String query) {
4242
return QueryRunnerUtils.createQuery(dataSource, query);
4343
}
4444

45-
public DbQuery createQuery(String query, Map<String, Object> params) {
45+
public DbQuery query(String query, Map<String, Object> params) {
4646
return QueryRunnerUtils.createQuery(dataSource, query, params);
4747
}
4848

49-
public <E> DbQuery createQuery(String query, E singleParam) {
49+
public <E> DbQuery query(String query, E singleParam) {
5050
return QueryRunnerUtils.createQuery(dataSource, query, DbNamedParamsQuery.singleNoNameParam(singleParam));
5151
}
5252

@@ -55,15 +55,15 @@ public TableData queryTableData(String query) {
5555
}
5656

5757
public TableData queryTableData(String query, Map<String, Object> params) {
58-
return createQuery(query, params).queryTableData();
58+
return query(query, params).tableData();
5959
}
6060

6161
public <E> E querySingleValue(String query) {
6262
return querySingleValue(query, Collections.emptyMap());
6363
}
6464

6565
public <E> E querySingleValue(String query, Map<String, Object> params) {
66-
return createQuery(query, params).querySingleValue();
66+
return query(query, params).singleValue();
6767
}
6868

6969
public void update(String query) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void compareEqualOnly(CompareToComparator comparator, ActualPath actualPa
3737

3838
private Object extractActual(Object expected, Object actual) {
3939
if (actual instanceof DatabaseTable) {
40-
return ((DatabaseTable) actual).createQuery().queryTableDataNoStep();
40+
return ((DatabaseTable) actual).query().queryTableDataNoStep();
4141
}
4242

4343
DbQuery actualResult = (DbQuery) actual;

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.testingisdocumenting.webtau.data.table.TableData;
2020

2121
import javax.sql.DataSource;
22-
import java.util.Collections;
2322
import java.util.Map;
2423

2524
public class DatabaseFacade {
@@ -40,16 +39,16 @@ public DatabaseTable table(String tableName) {
4039
return from(getPrimaryDataSource()).table(tableName);
4140
}
4241

43-
public DbQuery createQuery(String query) {
44-
return from(getPrimaryDataSource()).createQuery(query);
42+
public DbQuery query(String query) {
43+
return from(getPrimaryDataSource()).query(query);
4544
}
4645

47-
public DbQuery createQuery(String query, Map<String, Object> params) {
48-
return from(getPrimaryDataSource()).createQuery(query, params);
46+
public DbQuery query(String query, Map<String, Object> params) {
47+
return from(getPrimaryDataSource()).query(query, params);
4948
}
5049

51-
public <E> DbQuery createQuery(String query, E singleParam) {
52-
return from(getPrimaryDataSource()).createQuery(query, singleParam);
50+
public <E> DbQuery query(String query, E singleParam) {
51+
return from(getPrimaryDataSource()).query(query, singleParam);
5352
}
5453

5554
public TableData queryTableData(String query) {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,11 @@ INTO, createMessageId()),
5555
() -> insertRowStep(row));
5656
}
5757

58-
public TableData query() {
59-
return createQuery().queryTableData();
60-
}
61-
62-
public DbQuery createCountQuery() {
58+
public DbQuery queryCount() {
6359
return QueryRunnerUtils.createQuery(dataSource, SqlQueriesGenerator.count(name));
6460
}
6561

66-
public DbQuery createQuery() {
62+
public DbQuery query() {
6763
return QueryRunnerUtils.createQuery(dataSource, SqlQueriesGenerator.fullTable(name));
6864
}
6965

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ public class DbQuery implements ActualValueExpectations, ActualPathAware {
5757
this.params = params;
5858
}
5959

60-
public int queryNumberOfRows() {
61-
return queryTableDataNoStep().numberOfRows();
60+
public int numberOfRows() {
61+
return tableData().numberOfRows();
6262
}
6363

64-
public TableData queryTableData() {
64+
public TableData tableData() {
6565
return fetchValueAsStep(this::queryTableDataNoStep);
6666
}
6767

68-
public <E> E querySingleValue() {
68+
public <E> E singleValue() {
6969
return fetchValueAsStep(this::querySingleValueNoStep);
7070
}
7171

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
3535
"id1" | "nice set" | 1000
3636
"id2" | "another set" | 2000 }
3737

38-
PRICES.createQuery().should == ["ID" | "DESCRIPTION" | "PRICE"] {
38+
PRICES.query().should == ["ID" | "DESCRIPTION" | "PRICE"] {
3939
___________________________________
4040
"id1" | "nice set" | 1000
4141
"id2" | "another set" | 2000 }
4242

43-
db.createQuery("select * from PRICES where id='id2'").should ==
43+
db.query("select * from PRICES where id='id2'").should ==
4444
["ID" | "DESCRIPTION" | "PRICE"] {
4545
__________________________________
4646
"id2" | "another set" | 2000 }
@@ -82,7 +82,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
8282
cell.guid | "nice set" | true | "card" | cell.above + 10 // cell.above refers values above and can be modified with simple math operations
8383
cell.guid | "another set" | permute(true, false) | permute("rts", "fps") | cell.above + 20 } // permute generates additional rows generating new rows with all the permutations
8484

85-
doc.capture('db-setup-permute-table', PRICES.query())
85+
doc.capture('db-setup-permute-table', PRICES.query().tableData())
8686
PRICES.query().numberOfRows().should == 6
8787
}
8888

@@ -93,7 +93,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
9393
def PRICES = db.table("PRICES")
9494
PRICES << data.csv.table('prices-db.csv')
9595

96-
doc.capture('db-setup-csv-table', PRICES.query())
96+
doc.capture('db-setup-csv-table', PRICES.query().tableData())
9797
PRICES.should == [ "*ID" | "DESCRIPTION" | "AVAILABLE" | "TYPE" | "PRICE"] {
9898
__________________________________________________________
9999
"id1" | "description1" | true | "card" | 200
@@ -118,7 +118,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
118118
setupPrices()
119119

120120
// query without where clause start
121-
def prices = db.createQuery("select * from PRICES")
121+
def prices = db.query("select * from PRICES")
122122
prices.should == ["ID" | "DESCRIPTION" | "PRICE"] {
123123
___________________________________
124124
"id1" | "nice set" | 1000
@@ -130,7 +130,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
130130
void "query table with select statement and param"() {
131131
setupPrices()
132132
// query with where clause start
133-
def prices = db.createQuery("select * from PRICES where id=:id", [id: "id1"])
133+
def prices = db.query("select * from PRICES where id=:id", [id: "id1"])
134134
prices.should == ["ID" | "DESCRIPTION" | "PRICE"] {
135135
___________________________________
136136
"id1" | "nice set" | 1000 }
@@ -141,7 +141,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
141141
void "create query is lazy"() {
142142
setupPrices()
143143
// query with where clause start
144-
def prices = db.createQuery("select * from PRICES")
144+
def prices = db.query("select * from PRICES")
145145

146146
prices.shouldNot == []
147147
db.update("delete from PRICES")
@@ -154,7 +154,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
154154
setupPrices()
155155
// query with where clause start
156156
def PRICES = db.table("PRICES")
157-
def numberOfItems = PRICES.createCountQuery()
157+
def numberOfItems = PRICES.queryCount()
158158

159159
numberOfItems.shouldNot == 0
160160
db.update("delete from PRICES")
@@ -166,7 +166,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
166166
void "query table to match one row and assert against map"() {
167167
setupPrices()
168168
// query with where clause start
169-
def prices = db.createQuery("select * from PRICES where id=:id", [id: "id1"])
169+
def prices = db.query("select * from PRICES where id=:id", [id: "id1"])
170170
prices.should == [ID: "id1", "DESCRIPTION": "nice set", PRICE: 1000]
171171
// query with where clause end
172172
}
@@ -175,15 +175,15 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
175175
void "query table to match one row using single param shortcut and assert against map"() {
176176
setupPrices()
177177
// query with where clause start
178-
def prices = db.createQuery("select * from PRICES where id=:id or external_id=:id", "id1")
178+
def prices = db.query("select * from PRICES where id=:id or external_id=:id", "id1")
179179
prices.should == [ID: "id1", "DESCRIPTION": "nice set", PRICE: 1000]
180180
// query with where clause end
181181
}
182182

183183
@Test
184184
void "query table to match multiple row and assert against map"() {
185185
setupPrices()
186-
def prices = db.createQuery("select * from PRICES")
186+
def prices = db.query("select * from PRICES")
187187

188188
code {
189189
prices.should == [ID: "id1", "DESCRIPTION": "nice set", PRICE: 1000]
@@ -194,7 +194,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
194194
void "query table with select statement and array param"() {
195195
setupPrices()
196196
// query with where clause start
197-
def prices = db.createQuery("select * from PRICES where id in (:ids)", [ids: ["id1", "id2"]])
197+
def prices = db.query("select * from PRICES where id in (:ids)", [ids: ["id1", "id2"]])
198198
prices.should == ["ID" | "DESCRIPTION" | "PRICE"] {
199199
___________________________________
200200
"id1" | "nice set" | 1000
@@ -213,7 +213,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
213213
}
214214

215215
// query with where clause start
216-
def count = db.createQuery("select count(*) from PRICES")
216+
def count = db.query("select count(*) from PRICES")
217217
count.should == 2
218218

219219
// event happen somewhere to increase the number of rows...
@@ -227,7 +227,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
227227
void "should query single value"() {
228228
setupPrices()
229229

230-
def price = db.createQuery("select price from PRICES where id='id1'")
230+
def price = db.query("select price from PRICES where id='id1'")
231231
price.should == 1000
232232
price.shouldNot == 2000
233233
}
@@ -237,7 +237,7 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
237237
setupPrices()
238238

239239
// query single value params start
240-
def price = db.createQuery("select price from PRICES where id=:id", [id: 'id1'])
240+
def price = db.query("select price from PRICES where id=:id", [id: 'id1'])
241241
price.should == 1000
242242
price.shouldNot == 2000
243243
// query single value params end
@@ -247,8 +247,8 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
247247
void "value returned from query is a special wrapper value"() {
248248
setupPrices()
249249
// single value access start
250-
def price = db.createQuery("select price from PRICES where id=:id", [id: 'id1'])
251-
if (price.querySingleValue() > 100) {
250+
def price = db.query("select price from PRICES where id=:id", [id: 'id1'])
251+
if (price.singleValue() > 100) {
252252
println("do something")
253253
}
254254
// single value access end
@@ -265,10 +265,10 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
265265
@Test
266266
void "should run updates with params"() {
267267
def PRICES = setupPrices()
268-
doc.capture('db-before-update', PRICES.query())
268+
doc.capture('db-before-update', PRICES.query().tableData())
269269

270270
db.update("update PRICES set price=:price where id=:id", [id: 'id2', price: 4000])
271-
doc.capture('db-after-update', PRICES.query())
271+
doc.capture('db-after-update', PRICES.query().tableData())
272272

273273
PRICES.should == ["ID" | "DESCRIPTION" | "PRICE"] {
274274
___________________________________

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class DatabaseStepReporterTest extends DatabaseBaseTest implements StepReporter
4444
void "query result comparison step should capture query and params"() {
4545
setupPrices()
4646

47-
def price = db.createQuery("select price from PRICES where id=:id", [id: 'id1'])
47+
def price = db.query("select price from PRICES where id=:id", [id: 'id1'])
4848
price.should == 1000
4949
price.shouldNot == 2000
5050

@@ -57,7 +57,7 @@ class DatabaseStepReporterTest extends DatabaseBaseTest implements StepReporter
5757
void "query result comparison step should capture query and params in case of single param"() {
5858
setupPrices()
5959

60-
def price = db.createQuery("select price from PRICES where id=:id", 'id1')
60+
def price = db.query("select price from PRICES where id=:id", 'id1')
6161
price.should == 1000
6262

6363
def fullMessage = stepMessages.join('\n')
@@ -68,7 +68,7 @@ class DatabaseStepReporterTest extends DatabaseBaseTest implements StepReporter
6868
void "query result comparison step should not capture params when no params ara passed"() {
6969
setupPrices()
7070

71-
def price = db.createQuery("select price from PRICES where id='id1'")
71+
def price = db.query("select price from PRICES where id='id1'")
7272
price.should == 1000
7373

7474
def fullMessage = stepMessages.join('\n')

webtau-db/src/test/groovy/org/testingisdocumenting/webtau/db/cfg/ConfigBasedDbProviderTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ class ConfigBasedDbProviderTest {
3131
void "should use data source provider for primary database"() {
3232
DbConfig.setDbPrimaryUrl("jdbc:h2:mem:dbconfig;DB_CLOSE_DELAY=-1")
3333
DbConfig.setDbPrimaryUserName("sa")
34-
db.createQuery("select 1")
34+
db.query("select 1")
3535
}
3636
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ If your query uses a single unique placeholder name, you can pass a regular valu
5555

5656
# Lazy Declaration
5757

58-
`createQuery` doesn't query database at the call time. It defines a query to be used later.
58+
`query` doesn't query database at the call time. It defines a query to be used later.
5959

6060
:include-groovy: org/testingisdocumenting/webtau/db/DatabaseFacadeTest.groovy {
6161
entry: "create query is lazy",
@@ -102,7 +102,7 @@ Use `waitTo` on query result to continuously query database until condition is m
102102

103103
# Query Result Value
104104

105-
Value returned from `createQuery` methods is an instance of `DbQuery` type.
105+
Value returned from `query` methods is an instance of `DbQuery` type.
106106
No actual query is performed when `DbQuery` instance is created. It holds information about what query is, and what its parameters
107107
and only performs query when validation is triggered.
108108

webtau-groovy/src/main/groovy/org/testingisdocumenting/webtau/cli/repl/ReplResultRenderer.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ReplResultRenderer {
5555
}
5656

5757
private static void renderDbQueryResult(DbQuery queryResult) {
58-
out(ReplTableRenderer.render(queryResult.tableData))
58+
out(ReplTableRenderer.render(queryResult.tableData()))
5959
}
6060

6161
private static void renderPageElementAndHighlight(PageElement pageElement) {

0 commit comments

Comments
 (0)