Skip to content

Commit

Permalink
fixing swift testing
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Sep 6, 2024
1 parent d23d984 commit b9bfae8
Show file tree
Hide file tree
Showing 26 changed files with 1,380 additions and 923 deletions.
20 changes: 19 additions & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "e5c5459f572d6a25f98fc4225248f15148418bc33ead188712287f082ea9172b",
"originHash" : "5e5356dbef441dac47edcb2318eb7c1f4d7f2c21f5b86beee9a5ac5c31dc84cd",
"pins" : [
{
"identity" : "felinepine",
Expand All @@ -18,6 +18,24 @@
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
"version" : "1.6.1"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-syntax.git",
"state" : {
"revision" : "515f79b522918f83483068d99c68daeb5116342d",
"version" : "600.0.0-prerelease-2024-08-20"
}
},
{
"identity" : "swift-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-testing.git",
"state" : {
"revision" : "c55848b2aa4b29a4df542b235dfdd792a6fbe341",
"version" : "0.12.0"
}
}
],
"version" : 3
Expand Down
8 changes: 6 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/brightdigit/FelinePine.git", from: "1.0.0-beta.2")
.package(url: "https://github.com/brightdigit/FelinePine.git", from: "1.0.0-beta.2"),
.package(url: "https://github.com/swiftlang/swift-testing.git", from: "0.12.0"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand All @@ -25,7 +26,10 @@ let package = Package(
),
.testTarget(
name: "DataThespianTests",
dependencies: ["DataThespian"]
dependencies: [
"DataThespian",
.product(name: "Testing", package: "swift-testing"),
]
)
]
)
45 changes: 32 additions & 13 deletions Sources/DataThespian/AgentRegister.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
//
// AgentRegister.swift
// Copyright (c) 2024 BrightDigit.
// AgentRegister.swift
// DataThespian
//
// Created by Leo Dion.
// Copyright © 2024 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

#if canImport(SwiftData)
public protocol AgentRegister: Sendable {
associatedtype AgentType: DataAgent
var id: String { get }
@Sendable
func agent() async -> AgentType
}
public protocol AgentRegister: Sendable {
associatedtype AgentType: DataAgent
var id: String { get }
@Sendable func agent() async -> AgentType
}

public extension AgentRegister {
var id: String {
"\(AgentType.self)"
}
}
extension AgentRegister { public var id: String { "\(AgentType.self)" } }
#endif
155 changes: 84 additions & 71 deletions Sources/DataThespian/BackgroundDatabase.swift
Original file line number Diff line number Diff line change
@@ -1,97 +1,111 @@
//
// BackgroundDatabase.swift
// Copyright (c) 2024 BrightDigit.
// BackgroundDatabase.swift
// DataThespian
//
// Created by Leo Dion.
// Copyright © 2024 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

#if canImport(SwiftData)

public import Foundation
public import Foundation

public import SwiftData
import SwiftUI
public import SwiftData
import SwiftUI

public final class BackgroundDatabase: Database {
public func delete(_ modelType: (some PersistentModel).Type, withID id: PersistentIdentifier) async -> Bool {
await self.database.delete(modelType, withID: id)
}
public final class BackgroundDatabase: Database {
public func delete(_ modelType: (some PersistentModel).Type, withID id: PersistentIdentifier)
async -> Bool
{ await self.database.delete(modelType, withID: id) }

public func delete(where predicate: Predicate<some PersistentModel>?) async throws {
try await self.database.delete(where: predicate)
}
public func delete(where predicate: Predicate<some PersistentModel>?) async throws {
try await self.database.delete(where: predicate)
}

public func insert(_ closuer: @escaping @Sendable () -> some PersistentModel) async -> PersistentIdentifier {
await self.database.insert(closuer)
}
public func insert(_ closuer: @escaping @Sendable () -> some PersistentModel) async
-> PersistentIdentifier
{ await self.database.insert(closuer) }

public func fetch<T, U>(
_ selectDescriptor: @escaping @Sendable () -> FetchDescriptor<T>,
with closure: @escaping @Sendable ([T]) throws -> U
) async throws -> U where T: PersistentModel, U: Sendable {
try await self.database.fetch(selectDescriptor, with: closure)
}
public func fetch<T, U>(
_ selectDescriptor: @escaping @Sendable () -> FetchDescriptor<T>,
with closure: @escaping @Sendable ([T]) throws -> U
) async throws -> U where T: PersistentModel, U: Sendable {
try await self.database.fetch(selectDescriptor, with: closure)
}

public func get<T, U>(for objectID: PersistentIdentifier, with closure: @escaping @Sendable (T?) throws -> U) async throws -> U where T: PersistentModel, U: Sendable {
try await self.database.get(for: objectID, with: closure)
}
public func get<T, U>(
for objectID: PersistentIdentifier,
with closure: @escaping @Sendable (T?) throws -> U
) async throws -> U where T: PersistentModel, U: Sendable {
try await self.database.get(for: objectID, with: closure)
}

private actor DatabaseContainer {
private let factory: @Sendable () -> any Database
private var wrappedTask: Task<any Database, Never>?
private actor DatabaseContainer {
private let factory: @Sendable () -> any Database
private var wrappedTask: Task<any Database, Never>?

// swiftlint:disable:next strict_fileprivate
fileprivate init(factory: @escaping @Sendable () -> any Database) {
self.factory = factory
}
// swiftlint:disable:next strict_fileprivate
fileprivate init(factory: @escaping @Sendable () -> any Database) { self.factory = factory }

// swiftlint:disable:next strict_fileprivate
fileprivate var database: any Database {
get async {
if let wrappedTask {
return await wrappedTask.value
// swiftlint:disable:next strict_fileprivate
fileprivate var database: any Database {
get async {
if let wrappedTask { return await wrappedTask.value }
let task = Task { factory() }
self.wrappedTask = task
return await task.value
}
let task = Task {
factory()
}
self.wrappedTask = task
return await task.value
}
}
}

private let container: DatabaseContainer
private let container: DatabaseContainer

private var database: any Database {
get async {
await container.database
}
}
private var database: any Database { get async { await container.database } }

public convenience init(modelContainer: ModelContainer) {
self.init {
assert(isMainThread: false)
return ModelActorDatabase(modelContainer: modelContainer)
public convenience init(modelContainer: ModelContainer) {
self.init {
assert(isMainThread: false)
return ModelActorDatabase(modelContainer: modelContainer)
}
}
}

internal init(_ factory: @Sendable @escaping () -> any Database) {
self.container = .init(factory: factory)
}
internal init(_ factory: @Sendable @escaping () -> any Database) {
self.container = .init(factory: factory)
}

public func transaction(_ block: @escaping @Sendable (ModelContext) throws -> Void) async throws {
assert(isMainThread: false)
try await self.database.transaction(block)
public func transaction(_ block: @escaping @Sendable (ModelContext) throws -> Void) async throws
{
assert(isMainThread: false)
try await self.database.transaction(block)
}
}
}

extension BackgroundDatabase {
// public func obsoleteDelete(where predicate: Predicate<some PersistentModel & Sendable>?) async throws {
// assert(isMainThread: false)
// return try await self.database.obsoleteDelete(predicate)
// }

public func obsoleteFetch<T>(
_ selectDescriptor: @escaping @Sendable () -> FetchDescriptor<T>
) async throws -> [T] where T: Sendable, T: PersistentModel {

public func obsoleteFetch<T>(_ selectDescriptor: @escaping @Sendable () -> FetchDescriptor<T>)
async throws -> [T] where T: Sendable, T: PersistentModel
{
assert(isMainThread: false)
return try await self.database.obsoleteFetch(selectDescriptor)
}
Expand All @@ -111,9 +125,8 @@ public final class BackgroundDatabase: Database {
return try await self.database.obsoleteSave()
}

public func obsoleteExistingModel<T>(
for objectID: PersistentIdentifier
) async throws -> T? where T: Sendable, T: PersistentModel {
public func obsoleteExistingModel<T>(for objectID: PersistentIdentifier) async throws -> T?
where T: Sendable, T: PersistentModel {
assert(isMainThread: false)
return try await self.database.obsoleteExistingModel(for: objectID)
}
Expand Down
48 changes: 34 additions & 14 deletions Sources/DataThespian/DataAgent.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
//
// DataAgent.swift
// Copyright (c) 2024 BrightDigit.
// DataAgent.swift
// DataThespian
//
// Created by Leo Dion.
// Copyright © 2024 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//



#if canImport(SwiftData)
public import Foundation
public protocol DataAgent: Sendable {
var agentID: UUID { get }
func onUpdate(_ update: any DatabaseChangeSet)
func onCompleted(_ closure: @Sendable @escaping () -> Void)
func finish() async
}
public import Foundation
public protocol DataAgent: Sendable {
var agentID: UUID { get }
func onUpdate(_ update: any DatabaseChangeSet)
func onCompleted(_ closure: @Sendable @escaping () -> Void)
func finish() async
}

public extension DataAgent {
func onCompleted(_: @Sendable @escaping () -> Void) {}
}
extension DataAgent { public func onCompleted(_: @Sendable @escaping () -> Void) {} }
#endif
Loading

0 comments on commit b9bfae8

Please sign in to comment.