Skip to content

Commit 0aab0b4

Browse files
committed
more tests
1 parent f9d8c4c commit 0aab0b4

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Sources/SharingGRDBCore/FetchAll.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public struct FetchAll<Element: Sendable>: Sendable {
7979
}
8080

8181
/// Initializes this property with a default value.
82+
@_disfavoredOverload
8283
public init(
8384
wrappedValue: [Element] = []
8485
) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import Dependencies
2+
import DependenciesTestSupport
3+
import GRDB
4+
import Sharing
5+
import SharingGRDB
6+
import StructuredQueries
7+
import SwiftUI
8+
import Testing
9+
10+
@Suite(.dependency(\.defaultDatabase, try .database()))
11+
struct FetchTests {
12+
@Test func bareFetchAll() async throws {
13+
@FetchAll var records: [Record]
14+
try await Task.sleep(nanoseconds: 100_000_000)
15+
#expect(records == [Record(id: 1), Record(id: 2), Record(id: 3)])
16+
}
17+
18+
@Test func fetchAllWithQuery() async throws {
19+
@FetchAll(Record.where { $0.id > 1 }) var records: [Record]
20+
try await Task.sleep(nanoseconds: 100_000_000)
21+
#expect(records == [Record(id: 2), Record(id: 3)])
22+
}
23+
24+
@Test func bareFetchCount() async throws {
25+
@FetchOne var recordsCount = 0
26+
try await Task.sleep(nanoseconds: 100_000_000)
27+
#expect(recordsCount == 3)
28+
}
29+
30+
@Test func fetchCountWithQuery() async throws {
31+
@FetchOne(Record.where { $0.id > 1 }.count()) var recordsCount = 0
32+
try await Task.sleep(nanoseconds: 100_000_000)
33+
#expect(recordsCount == 2)
34+
}
35+
}
36+
37+
@Table
38+
private struct Record: Equatable {
39+
let id: Int
40+
}
41+
extension DatabaseWriter where Self == DatabaseQueue {
42+
fileprivate static func database() throws -> DatabaseQueue {
43+
let database = try DatabaseQueue()
44+
var migrator = DatabaseMigrator()
45+
migrator.registerMigration("Up") { db in
46+
try #sql(
47+
"""
48+
CREATE TABLE "records" ("id" INTEGER PRIMARY KEY AUTOINCREMENT)
49+
"""
50+
)
51+
.execute(db)
52+
for _ in 1...3 {
53+
_ = try Record.insert(Record.Draft()).execute(db)
54+
}
55+
}
56+
try migrator.migrate(database)
57+
return database
58+
}
59+
}

0 commit comments

Comments
 (0)