Skip to content

Commit d68b18a

Browse files
committed
wip
1 parent 2c5d172 commit d68b18a

2 files changed

Lines changed: 83 additions & 83 deletions

File tree

Sources/SharingGRDBCore/FetchKey.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Dependencies
22
import Dispatch
3+
import Foundation
34
import GRDB
45
import Sharing
56
import StructuredQueriesGRDBCore
@@ -435,18 +436,17 @@ public struct NotFound: Error {
435436
public init() {}
436437
}
437438

438-
439-
440439
package struct TestImmediateScheduler: ValueObservationScheduler, Hashable {
441440
package func immediateInitialValue() -> Bool { true }
442441
package func schedule(_ action: @escaping @Sendable () -> Void) {
443442
if Thread.isMainThread {
444-
DispatchQueue.main.sync { action() }
443+
DispatchQueue.main.sync {
444+
action()
445+
}
445446
} else {
446447
DispatchQueue.main.async {
447448
action()
448449
}
449450
}
450451
}
451452
}
452-
import Foundation

Tests/SharingGRDBTests/SharingGRDBTests.swift

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,85 +7,85 @@ import StructuredQueries
77
import SwiftUI
88
import Testing
99

10-
//@Suite struct GRDBSharingTests {
11-
// @Test
12-
// func fetchOne() async throws {
13-
// try await withDependencies {
14-
// $0.defaultDatabase = try DatabaseQueue()
15-
// } operation: {
16-
// @FetchOne(#sql("SELECT 1")) var bool = false
17-
// try await Task.sleep(nanoseconds: 100_000_000)
18-
// #expect(bool)
19-
// #expect($bool.loadError == nil)
20-
// }
21-
// }
22-
//
23-
// @Test
24-
// func fetchOneOptional() async throws {
25-
// try withDependencies {
26-
// $0.defaultDatabase = try DatabaseQueue()
27-
// } operation: {
28-
// @SharedReader(.fetchOne(sql: "SELECT NULL")) var bool: Bool?
29-
// #expect(bool == nil)
30-
// }
31-
// }
32-
//
33-
// @Test func fetchSyntaxError() async throws {
34-
// try await withDependencies {
35-
// $0.defaultDatabase = try DatabaseQueue()
36-
// } operation: {
37-
// @FetchOne(#sql("SELEC 1")) var bool = false
38-
// #expect(bool == false)
39-
// try await Task.sleep(nanoseconds: 100_000_000)
40-
// #expect($bool.loadError is DatabaseError?)
41-
// let error = try #require($bool.loadError as? DatabaseError)
42-
// #expect(error.message == #"near "SELEC": syntax error"#)
43-
// }
44-
// }
45-
//
46-
// @Test func fetchWithTwoDatabaseConnections() async throws {
47-
// let name = #function
48-
// try await withDependencies {
49-
// $0.defaultDatabase = try .database(named: name)
50-
// } operation: {
51-
// @SharedReader(.fetchAll(sql: "SELECT * FROM records")) var records1: [Record] = []
52-
// try await Task.sleep(nanoseconds: 100_000_000)
53-
// #expect(records1.map(\.id) == [1, 2, 3])
54-
//
55-
// try await withDependencies {
56-
// $0.defaultDatabase = try .database(named: name)
57-
// } operation: {
58-
// @Dependency(\.defaultDatabase) var database2
59-
// @SharedReader(.fetchAll(sql: "SELECT * FROM records")) var records2: [Record] = []
60-
// try await Task.sleep(nanoseconds: 100_000_000)
61-
// #expect(records2.map(\.id) == [1, 2, 3])
62-
// try await database2.write { db in
63-
// _ = try Record.deleteOne(db, key: 1)
64-
// }
65-
// try await Task.sleep(nanoseconds: 100_000_000)
66-
// #expect(records1.map(\.id) == [1, 2, 3])
67-
// #expect(records2.map(\.id) == [2, 3])
68-
// }
69-
//
70-
// try await $records1.load()
71-
// #expect(records1.map(\.id) == [2, 3])
72-
// }
73-
// }
74-
//
75-
// @Test(.dependency(\.defaultDatabase, try .database()))
76-
// func fetchIDHashValue() async throws {
77-
// let fetchKey1: some SharedReaderKey<Void> = .fetch(Fetch1())
78-
// let fetchKey2: some SharedReaderKey<Void> = .fetch(Fetch2())
79-
// #expect(fetchKey1.id.hashValue != fetchKey2.id.hashValue)
80-
// }
81-
//
82-
// @Test(.dependency(\.defaultDatabase, try .database()))
83-
// func fetchAnimationHashValue() async throws {
84-
// let fetchKey1: some SharedReaderKey<Void> = .fetch(Fetch1())
85-
// let fetchKey2: some SharedReaderKey<Void> = .fetch(Fetch2(), animation: .default)
86-
// #expect(fetchKey1.id.hashValue != fetchKey2.id.hashValue)
87-
// }
88-
//}
10+
@Suite struct GRDBSharingTests {
11+
@Test
12+
func fetchOne() async throws {
13+
try await withDependencies {
14+
$0.defaultDatabase = try DatabaseQueue()
15+
} operation: {
16+
@FetchOne(#sql("SELECT 1")) var bool = false
17+
try await Task.sleep(nanoseconds: 100_000_000)
18+
#expect(bool)
19+
#expect($bool.loadError == nil)
20+
}
21+
}
22+
23+
@Test
24+
func fetchOneOptional() async throws {
25+
try withDependencies {
26+
$0.defaultDatabase = try DatabaseQueue()
27+
} operation: {
28+
@SharedReader(.fetchOne(sql: "SELECT NULL")) var bool: Bool?
29+
#expect(bool == nil)
30+
}
31+
}
32+
33+
@Test func fetchSyntaxError() async throws {
34+
try await withDependencies {
35+
$0.defaultDatabase = try DatabaseQueue()
36+
} operation: {
37+
@FetchOne(#sql("SELEC 1")) var bool = false
38+
#expect(bool == false)
39+
try await Task.sleep(nanoseconds: 100_000_000)
40+
#expect($bool.loadError is DatabaseError?)
41+
let error = try #require($bool.loadError as? DatabaseError)
42+
#expect(error.message == #"near "SELEC": syntax error"#)
43+
}
44+
}
45+
46+
@Test func fetchWithTwoDatabaseConnections() async throws {
47+
let name = #function
48+
try await withDependencies {
49+
$0.defaultDatabase = try .database(named: name)
50+
} operation: {
51+
@SharedReader(.fetchAll(sql: "SELECT * FROM records")) var records1: [Record] = []
52+
try await Task.sleep(nanoseconds: 100_000_000)
53+
#expect(records1.map(\.id) == [1, 2, 3])
54+
55+
try await withDependencies {
56+
$0.defaultDatabase = try .database(named: name)
57+
} operation: {
58+
@Dependency(\.defaultDatabase) var database2
59+
@SharedReader(.fetchAll(sql: "SELECT * FROM records")) var records2: [Record] = []
60+
try await Task.sleep(nanoseconds: 100_000_000)
61+
#expect(records2.map(\.id) == [1, 2, 3])
62+
try await database2.write { db in
63+
_ = try Record.deleteOne(db, key: 1)
64+
}
65+
try await Task.sleep(nanoseconds: 100_000_000)
66+
#expect(records1.map(\.id) == [1, 2, 3])
67+
#expect(records2.map(\.id) == [2, 3])
68+
}
69+
70+
try await $records1.load()
71+
#expect(records1.map(\.id) == [2, 3])
72+
}
73+
}
74+
75+
@Test(.dependency(\.defaultDatabase, try .database()))
76+
func fetchIDHashValue() async throws {
77+
let fetchKey1: some SharedReaderKey<Void> = .fetch(Fetch1())
78+
let fetchKey2: some SharedReaderKey<Void> = .fetch(Fetch2())
79+
#expect(fetchKey1.id.hashValue != fetchKey2.id.hashValue)
80+
}
81+
82+
@Test(.dependency(\.defaultDatabase, try .database()))
83+
func fetchAnimationHashValue() async throws {
84+
let fetchKey1: some SharedReaderKey<Void> = .fetch(Fetch1())
85+
let fetchKey2: some SharedReaderKey<Void> = .fetch(Fetch2(), animation: .default)
86+
#expect(fetchKey1.id.hashValue != fetchKey2.id.hashValue)
87+
}
88+
}
8989

9090
private struct Fetch1: FetchKeyRequest {
9191
func fetch(_ db: Database) throws {

0 commit comments

Comments
 (0)