From 4e9900a1943525c068fad019341228a47f26b251 Mon Sep 17 00:00:00 2001 From: Ryan Carver Date: Tue, 2 Sep 2025 09:43:54 -0700 Subject: [PATCH 1/2] change assertQuery to use DatabaseWriter/db.write to support updates --- .../SharingGRDBTestSupport/AssertQuery.swift | 6 ++-- Tests/SharingGRDBTests/AssertQueryTests.swift | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Sources/SharingGRDBTestSupport/AssertQuery.swift b/Sources/SharingGRDBTestSupport/AssertQuery.swift index 9ec1b2b8..c3903ec5 100644 --- a/Sources/SharingGRDBTestSupport/AssertQuery.swift +++ b/Sources/SharingGRDBTestSupport/AssertQuery.swift @@ -52,7 +52,7 @@ import StructuredQueriesTestSupport public func assertQuery>( includeSQL: Bool = false, _ query: S, - database: (any DatabaseReader)? = nil, + database: (any DatabaseWriter)? = nil, sql: (() -> String)? = nil, results: (() -> String)? = nil, fileID: StaticString = #fileID, @@ -80,7 +80,7 @@ public func assertQuery( includeSQL: Bool = false, _ query: S, - database: (any DatabaseReader)? = nil, + database: (any DatabaseWriter)? = nil, sql: (() -> String)? = nil, results: (() -> String)? = nil, fileID: StaticString = #fileID, diff --git a/Tests/SharingGRDBTests/AssertQueryTests.swift b/Tests/SharingGRDBTests/AssertQueryTests.swift index a913e27e..bebbb987 100644 --- a/Tests/SharingGRDBTests/AssertQueryTests.swift +++ b/Tests/SharingGRDBTests/AssertQueryTests.swift @@ -41,6 +41,38 @@ struct AssertQueryTests { """ } } + @Test func assertQueryBasicUpdate() throws { + assertQuery( + Record.all + .update { $0.date = Date(timeIntervalSince1970: 45) } + .returning { ($0.id, $0.date) } + ) { + """ + ┌───┬────────────────────────────────┐ + │ 1 │ Date(1970-01-01T00:00:45.000Z) │ + │ 2 │ Date(1970-01-01T00:00:45.000Z) │ + │ 3 │ Date(1970-01-01T00:00:45.000Z) │ + └───┴────────────────────────────────┘ + """ + } + } + @Test func assertQueryRecordUpdate() throws { + assertQuery( + Record + .where { $0.id == 1 } + .update { $0.date = Date(timeIntervalSince1970: 45) } + .returning(\.self) + ) { + """ + ┌────────────────────────────────────────┐ + │ Record( │ + │ id: 1, │ + │ date: Date(1970-01-01T00:00:45.000Z) │ + │ ) │ + └────────────────────────────────────────┘ + """ + } + } #if DEBUG @Test func assertQueryBasicIncludeSQL() throws { assertQuery( From c944cbd8bedc5952d4413e6efa1310c6438544e0 Mon Sep 17 00:00:00 2001 From: Ryan Carver Date: Tue, 2 Sep 2025 09:47:03 -0700 Subject: [PATCH 2/2] fix docs --- Sources/SharingGRDBTestSupport/AssertQuery.swift | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Sources/SharingGRDBTestSupport/AssertQuery.swift b/Sources/SharingGRDBTestSupport/AssertQuery.swift index c3903ec5..86ca41f7 100644 --- a/Sources/SharingGRDBTestSupport/AssertQuery.swift +++ b/Sources/SharingGRDBTestSupport/AssertQuery.swift @@ -36,12 +36,10 @@ import StructuredQueriesTestSupport /// - Parameters: /// - includeSQL: Whether to snapshot the SQL fragment in addition to the results. /// - query: A statement. -/// - database: The database to read from. A value of `nil` will use +/// - database: The database to use. A value of `nil` will use /// `@Dependency(\.defaultDatabase)`. /// - sql: A snapshot of the SQL produced by the statement. /// - results: A snapshot of the results. -/// to `1` for invoking this helper directly, but if you write a wrapper function that automates -/// the `execute` trailing closure, you should pass `0` instead. /// - fileID: The source `#fileID` associated with the assertion. /// - filePath: The source `#filePath` associated with the assertion. /// - function: The source `#function` associated with the assertion @@ -165,11 +163,9 @@ public func assertQuery