@@ -7,85 +7,85 @@ import StructuredQueries
77import SwiftUI
88import 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
9090private struct Fetch1 : FetchKeyRequest {
9191 func fetch( _ db: Database ) throws {
0 commit comments