Skip to content

Commit ec6eed5

Browse files
authored
Let assertQuery support update queries (#149)
* change assertQuery to use DatabaseWriter/db.write to support updates * fix docs
1 parent 09e4c8a commit ec6eed5

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

Sources/SharingGRDBTestSupport/AssertQuery.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ import StructuredQueriesTestSupport
3636
/// - Parameters:
3737
/// - includeSQL: Whether to snapshot the SQL fragment in addition to the results.
3838
/// - query: A statement.
39-
/// - database: The database to read from. A value of `nil` will use
39+
/// - database: The database to use. A value of `nil` will use
4040
/// `@Dependency(\.defaultDatabase)`.
4141
/// - sql: A snapshot of the SQL produced by the statement.
4242
/// - results: A snapshot of the results.
43-
/// to `1` for invoking this helper directly, but if you write a wrapper function that automates
44-
/// the `execute` trailing closure, you should pass `0` instead.
4543
/// - fileID: The source `#fileID` associated with the assertion.
4644
/// - filePath: The source `#filePath` associated with the assertion.
4745
/// - function: The source `#function` associated with the assertion
@@ -52,7 +50,7 @@ import StructuredQueriesTestSupport
5250
public func assertQuery<each V: QueryRepresentable, S: StructuredQueriesCore.Statement<(repeat each V)>>(
5351
includeSQL: Bool = false,
5452
_ query: S,
55-
database: (any DatabaseReader)? = nil,
53+
database: (any DatabaseWriter)? = nil,
5654
sql: (() -> String)? = nil,
5755
results: (() -> String)? = nil,
5856
fileID: StaticString = #fileID,
@@ -80,7 +78,7 @@ public func assertQuery<each V: QueryRepresentable, S: StructuredQueriesCore.Sta
8078
}
8179
do {
8280
@Dependency(\.defaultDatabase) var defaultDatabase
83-
let rows = try (database ?? defaultDatabase).read { try query.fetchAll($0) }
81+
let rows = try (database ?? defaultDatabase).write { try query.fetchAll($0) }
8482
var table = ""
8583
printTable(rows, to: &table)
8684
if !table.isEmpty {
@@ -165,11 +163,9 @@ public func assertQuery<each V: QueryRepresentable, S: StructuredQueriesCore.Sta
165163
/// - includeSQL: Whether to snapshot the SQL fragment in addition to the results.
166164
/// - query: A statement.
167165
/// - sql: A snapshot of the SQL produced by the statement.
168-
/// - database: The database to read from. A value of `nil` will use
166+
/// - database: The database to use. A value of `nil` will use
169167
/// `@Dependency(\.defaultDatabase)`.
170168
/// - results: A snapshot of the results.
171-
/// to `1` for invoking this helper directly, but if you write a wrapper function that automates
172-
/// the `execute` trailing closure, you should pass `0` instead.
173169
/// - fileID: The source `#fileID` associated with the assertion.
174170
/// - filePath: The source `#filePath` associated with the assertion.
175171
/// - function: The source `#function` associated with the assertion
@@ -179,7 +175,7 @@ public func assertQuery<each V: QueryRepresentable, S: StructuredQueriesCore.Sta
179175
public func assertQuery<S: SelectStatement, each J: StructuredQueriesCore.Table>(
180176
includeSQL: Bool = false,
181177
_ query: S,
182-
database: (any DatabaseReader)? = nil,
178+
database: (any DatabaseWriter)? = nil,
183179
sql: (() -> String)? = nil,
184180
results: (() -> String)? = nil,
185181
fileID: StaticString = #fileID,

Tests/SharingGRDBTests/AssertQueryTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,38 @@ struct AssertQueryTests {
4141
"""
4242
}
4343
}
44+
@Test func assertQueryBasicUpdate() throws {
45+
assertQuery(
46+
Record.all
47+
.update { $0.date = Date(timeIntervalSince1970: 45) }
48+
.returning { ($0.id, $0.date) }
49+
) {
50+
"""
51+
┌───┬────────────────────────────────┐
52+
│ 1 │ Date(1970-01-01T00:00:45.000Z) │
53+
│ 2 │ Date(1970-01-01T00:00:45.000Z) │
54+
│ 3 │ Date(1970-01-01T00:00:45.000Z) │
55+
└───┴────────────────────────────────┘
56+
"""
57+
}
58+
}
59+
@Test func assertQueryRecordUpdate() throws {
60+
assertQuery(
61+
Record
62+
.where { $0.id == 1 }
63+
.update { $0.date = Date(timeIntervalSince1970: 45) }
64+
.returning(\.self)
65+
) {
66+
"""
67+
┌────────────────────────────────────────┐
68+
│ Record( │
69+
│ id: 1, │
70+
│ date: Date(1970-01-01T00:00:45.000Z) │
71+
│ ) │
72+
└────────────────────────────────────────┘
73+
"""
74+
}
75+
}
4476
#if DEBUG
4577
@Test func assertQueryBasicIncludeSQL() throws {
4678
assertQuery(

0 commit comments

Comments
 (0)