Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions Sources/SharingGRDBTestSupport/AssertQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,7 +50,7 @@ import StructuredQueriesTestSupport
public func assertQuery<each V: QueryRepresentable, S: StructuredQueriesCore.Statement<(repeat each V)>>(
includeSQL: Bool = false,
_ query: S,
database: (any DatabaseReader)? = nil,
database: (any DatabaseWriter)? = nil,
sql: (() -> String)? = nil,
results: (() -> String)? = nil,
fileID: StaticString = #fileID,
Expand Down Expand Up @@ -80,7 +78,7 @@ public func assertQuery<each V: QueryRepresentable, S: StructuredQueriesCore.Sta
}
do {
@Dependency(\.defaultDatabase) var defaultDatabase
let rows = try (database ?? defaultDatabase).read { try query.fetchAll($0) }
let rows = try (database ?? defaultDatabase).write { try query.fetchAll($0) }
var table = ""
printTable(rows, to: &table)
if !table.isEmpty {
Expand Down Expand Up @@ -165,11 +163,9 @@ public func assertQuery<each V: QueryRepresentable, S: StructuredQueriesCore.Sta
/// - includeSQL: Whether to snapshot the SQL fragment in addition to the results.
/// - query: A statement.
/// - sql: A snapshot of the SQL produced by the 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)`.
/// - 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
Expand All @@ -179,7 +175,7 @@ public func assertQuery<each V: QueryRepresentable, S: StructuredQueriesCore.Sta
public func assertQuery<S: SelectStatement, each J: StructuredQueriesCore.Table>(
includeSQL: Bool = false,
_ query: S,
database: (any DatabaseReader)? = nil,
database: (any DatabaseWriter)? = nil,
sql: (() -> String)? = nil,
results: (() -> String)? = nil,
fileID: StaticString = #fileID,
Expand Down
32 changes: 32 additions & 0 deletions Tests/SharingGRDBTests/AssertQueryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading