Skip to content

Commit a034e97

Browse files
db: createCountQuery table shortcut
1 parent 958b56e commit a034e97

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ public TableData query() {
4949
return createQuery().queryTableData();
5050
}
5151

52+
public DbQuery createCountQuery() {
53+
return QueryRunnerUtils.createQuery(dataSource, SqlQueriesGenerator.count(name));
54+
}
55+
5256
public DbQuery createQuery() {
53-
return QueryRunnerUtils.createQuery(dataSource, SqlQueriesGenerator.query(name));
57+
return QueryRunnerUtils.createQuery(dataSource, SqlQueriesGenerator.fullTable(name));
5458
}
5559

5660
private void insertStep(TableData tableData) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ public static String insert(String tableName, Record record) {
3434
return "INSERT INTO " + tableName + " (" + columnNames + ") VALUES (" + questionMarks + ")";
3535
}
3636

37-
public static String query(String tableName) {
37+
public static String fullTable(String tableName) {
3838
return "SELECT * FROM " + tableName;
3939
}
40+
41+
public static String count(String tableName) {
42+
return "SELECT count(*) FROM " + tableName;
43+
}
4044
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,26 @@ class DatabaseFacadeTest extends DatabaseBaseTest {
130130
setupPrices()
131131
// query with where clause start
132132
def prices = db.createQuery("select * from PRICES")
133+
134+
prices.shouldNot == []
133135
db.update("delete from PRICES")
134136
prices.should == []
135137
// query with where clause end
136138
}
137139

140+
@Test
141+
void "lazy count query"() {
142+
setupPrices()
143+
// query with where clause start
144+
def PRICES = db.table("PRICES")
145+
def numberOfItems = PRICES.createCountQuery()
146+
147+
numberOfItems.shouldNot == 0
148+
db.update("delete from PRICES")
149+
numberOfItems.should == 0
150+
// query with where clause end
151+
}
152+
138153
@Test
139154
void "query table to match one row and assert against map"() {
140155
setupPrices()

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ If your query uses a single unique placeholder name, you can pass a regular valu
6666
excludeStartEnd: true
6767
}
6868

69+
:include-groovy: org/testingisdocumenting/webtau/db/DatabaseFacadeTest.groovy {
70+
entry: "lazy count query",
71+
title: "lazy count query",
72+
bodyOnly: true,
73+
startLine: "query with where clause start",
74+
endLine: "query with where clause end",
75+
excludeStartEnd: true
76+
}
77+
6978
# Single Value
7079

7180
:include-groovy: org/testingisdocumenting/webtau/db/DatabaseFacadeTest.groovy {

0 commit comments

Comments
 (0)