Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Commit

Permalink
improve api
Browse files Browse the repository at this point in the history
  • Loading branch information
geor-kasapidi committed Mar 1, 2021
1 parent f3cccd5 commit f1ca3ac
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 38 deletions.
34 changes: 26 additions & 8 deletions Sources/Sworm/Wrappers/ManagedObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,33 @@ public extension ManagedObject {
@discardableResult
func set<Destination: ManagedObjectConvertible>(
_ keyPath: KeyPath<PlainObject.Relations, ToOneRelation<Destination>>,
value: Destination,
in context: ManagedObjectContext
) -> ManagedObject<Destination> {
if let currentObject = self[dynamicMember: keyPath] {
return currentObject.encode(value)
value: Destination?,
context: ManagedObjectContext
) -> Self {
guard let value = value else {
return self.delete(keyPath, context: context)
}

if let object = self[dynamicMember: keyPath] {
object.encode(value)
} else {
self[dynamicMember: keyPath] = context.insert(value)
}

let newObject = context.insert(value)
self[dynamicMember: keyPath] = newObject
return newObject
return self
}

@discardableResult
func delete<Destination: ManagedObjectConvertible>(
_ keyPath: KeyPath<PlainObject.Relations, ToOneRelation<Destination>>,
context: ManagedObjectContext
) -> Self {
if let object = self[dynamicMember: keyPath] {
self[dynamicMember: keyPath] = nil

context.delete(object)
}

return self
}
}
32 changes: 24 additions & 8 deletions Sources/Sworm/Wrappers/ManagedObjectSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public final class ManagedObjectSet<PlainObject: ManagedObjectConvertible>: Sequ
.init(iterator: self.set.makeIterator())
}

public func add(_ value: ManagedObject<PlainObject>) {
self.set.add(value.instance)
public func add(_ object: ManagedObject<PlainObject>) {
self.set.add(object.instance)
}

public func remove(_ value: ManagedObject<PlainObject>) {
self.set.remove(value.instance)
public func remove(_ object: ManagedObject<PlainObject>) {
self.set.remove(object.instance)
}
}

Expand All @@ -59,11 +59,27 @@ public final class ManagedObjectOrderedSet<PlainObject: ManagedObjectConvertible
.init(iterator: self.set.makeIterator())
}

public func add(_ value: ManagedObject<PlainObject>) {
self.set.add(value.instance)
public func add(_ object: ManagedObject<PlainObject>) {
self.set.add(object.instance)
}

public func remove(_ value: ManagedObject<PlainObject>) {
self.set.remove(value.instance)
public func remove(_ object: ManagedObject<PlainObject>) {
self.set.remove(object.instance)
}
}

public extension ManagedObjectSet {
func delete(_ object: ManagedObject<PlainObject>, context: ManagedObjectContext) {
self.remove(object)

context.delete(object)
}
}

public extension ManagedObjectOrderedSet {
func delete(_ object: ManagedObject<PlainObject>, context: ManagedObjectContext) {
self.remove(object)

context.delete(object)
}
}
12 changes: 6 additions & 6 deletions Tests/SwormTests/AttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import XCTest
@available(OSX 10.15, *)
final class AttributeTests: XCTestCase {
func testPrimitiveAttributeFullSetReadWrite() {
TestDB.inMemoryContainer(store: DataModels.attributes) { pc in
TestDB.temporaryContainer(store: DataModels.attributes) { pc in
let sourceInstance = PrimitiveAttributeFullSet(
x1: .random(),
x2: .random(in: .min ... .max),
Expand Down Expand Up @@ -36,7 +36,7 @@ final class AttributeTests: XCTestCase {
}

func testCustomAttributeSetReadWrite() {
TestDB.inMemoryContainer(store: DataModels.attributes) { pc in
TestDB.temporaryContainer(store: DataModels.attributes) { pc in
let sourceInstances = [
CustomAttributeSet(
x1: .init(.init(x: 1, y: 2)),
Expand Down Expand Up @@ -72,7 +72,7 @@ final class AttributeTests: XCTestCase {
}

func testDemoAttributeSetRefReadWrite() {
TestDB.inMemoryContainer(store: DataModels.attributes) { pc in
TestDB.temporaryContainer(store: DataModels.attributes) { pc in
let sourceInstance1 = DemoAttributeSetRef()
sourceInstance1.x1 = 10

Expand All @@ -96,7 +96,7 @@ final class AttributeTests: XCTestCase {
func _testPrimitiveAttributeFullSetReadWriteMeasure() {
let N = 10000

TestDB.inMemoryContainer(store: DataModels.attributes) { pc in
TestDB.temporaryContainer(store: DataModels.attributes) { pc in
self.measure {
do {
try self.writeRandomPrimitiveAttributeFullSets(n: N, pc: pc)
Expand All @@ -112,7 +112,7 @@ final class AttributeTests: XCTestCase {
func _testPrimitiveAttributeFullSetWriteMeasure() {
let N = 10000

TestDB.inMemoryContainer(store: DataModels.attributes) { pc in
TestDB.temporaryContainer(store: DataModels.attributes) { pc in
self.measure {
do {
try self.writeRandomPrimitiveAttributeFullSets(n: N, pc: pc)
Expand All @@ -124,7 +124,7 @@ final class AttributeTests: XCTestCase {
func _testPrimitiveAttributeFullSetReadMeasure() {
let N = 10000

TestDB.inMemoryContainer(store: DataModels.attributes) { pc in
TestDB.temporaryContainer(store: DataModels.attributes) { pc in
try self.writeRandomPrimitiveAttributeFullSets(n: N, pc: pc)

self.measure {
Expand Down
14 changes: 7 additions & 7 deletions Tests/SwormTests/PredicateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class PredicateTests: XCTestCase {
}

func testEqualityAndComparability() {
TestDB.inMemoryContainer(store: DataModels.predicates) { pc in
TestDB.temporaryContainer(store: DataModels.predicates) { pc in
let sourceEntries = [
PredicateABCD(a: 10, b: 20),
PredicateABCD(a: 30, b: 40),
Expand Down Expand Up @@ -60,7 +60,7 @@ final class PredicateTests: XCTestCase {
}

func testNilComparison() {
TestDB.inMemoryContainer(store: DataModels.predicates) { pc in
TestDB.temporaryContainer(store: DataModels.predicates) { pc in
let sourceEntries = [
PredicateABCD(a: 10, b: 20, c: nil),
PredicateABCD(a: 30, b: 40, c: "foo"),
Expand Down Expand Up @@ -101,7 +101,7 @@ final class PredicateTests: XCTestCase {
}

func testNonPrimitiveAttributeQuery() {
TestDB.inMemoryContainer(store: DataModels.predicates) { pc in
TestDB.temporaryContainer(store: DataModels.predicates) { pc in
let sourceEntries = [
PredicateABCD(a: 1, d: .foo),
PredicateABCD(a: 2, d: .bar),
Expand Down Expand Up @@ -144,7 +144,7 @@ final class PredicateTests: XCTestCase {
}

func testUUIDAndURLQuery() {
TestDB.inMemoryContainer(store: DataModels.predicates) { pc in
TestDB.temporaryContainer(store: DataModels.predicates) { pc in
let ids: [UUID] = [.init(), .init(), .init()]
let urls: [URL?] = [
URL(string: "https://xyz.com"),
Expand Down Expand Up @@ -183,7 +183,7 @@ final class PredicateTests: XCTestCase {
}

func testInLittle() {
TestDB.inMemoryContainer(store: DataModels.predicates) { pc in
TestDB.temporaryContainer(store: DataModels.predicates) { pc in
let sourceEntries = [
PredicateABCD(a: 1, c: "a"),
PredicateABCD(a: 2, c: "b"),
Expand Down Expand Up @@ -229,7 +229,7 @@ final class PredicateTests: XCTestCase {
func testInBig() {
let N = 200

TestDB.inMemoryContainer(store: DataModels.predicates) { pc in
TestDB.temporaryContainer(store: DataModels.predicates) { pc in
let sourceEntries = (0 ..< N).map { _ in
PredicateIDURL(id: .init())
}
Expand All @@ -255,7 +255,7 @@ final class PredicateTests: XCTestCase {
}

func testStringComparability() {
TestDB.inMemoryContainer(store: DataModels.predicates) { pc in
TestDB.temporaryContainer(store: DataModels.predicates) { pc in
let sourceEntries = [
PredicateABCD(c: "fOo1baR"),
PredicateABCD(c: "foO2bAr"),
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwormTests/ReadWriteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import XCTest
@available(OSX 10.15, *)
final class ReadWriteTests: XCTestCase {
func testCRUD() {
TestDB.inMemoryContainer(store: DataModels.bookLibrary) { pc in
TestDB.temporaryContainer(store: DataModels.bookLibrary) { pc in
let Author = BookLibrary.Author(
name: "cool author",
age: 50
Expand Down Expand Up @@ -145,7 +145,7 @@ final class ReadWriteTests: XCTestCase {
}

func testRequestSortLimit() {
TestDB.inMemoryContainer(store: DataModels.bookLibrary) { pc in
TestDB.temporaryContainer(store: DataModels.bookLibrary) { pc in
try pc.perform(action: { context in
(1 ... 10).reversed().forEach {
context.insert(
Expand Down Expand Up @@ -173,7 +173,7 @@ final class ReadWriteTests: XCTestCase {
}

func testNotUniqueInsertFail() {
TestDB.inMemoryContainer(store: DataModels.bookLibrary) { pc in
TestDB.temporaryContainer(store: DataModels.bookLibrary) { pc in
let id = UUID()

var err: Error?
Expand All @@ -192,7 +192,7 @@ final class ReadWriteTests: XCTestCase {
}

func testMultiThreadReadWrite() {
TestDB.inMemoryContainer(store: DataModels.bookLibrary) { pc in
TestDB.temporaryContainer(store: DataModels.bookLibrary) { pc in
let group = DispatchGroup()

let range = 1 ... 100
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwormTests/UnsafeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import XCTest
// Remove low dash to test
final class UnsafeTests: XCTestCase {
func _testDeallocatedReferenceAccess1() {
TestDB.inMemoryContainer(store: DataModels.bookLibrary) { pc in
TestDB.temporaryContainer(store: DataModels.bookLibrary) { pc in
let book = BookLibrary.Book(name: "some book")

try pc.perform(action: { ctx in
Expand All @@ -29,7 +29,7 @@ final class UnsafeTests: XCTestCase {
}

func _testDeallocatedReferenceAccess2() {
TestDB.inMemoryContainer(store: DataModels.bookLibrary) { pc in
TestDB.temporaryContainer(store: DataModels.bookLibrary) { pc in
try pc.perform(action: { ctx in
let authorObject = ctx.insert(BookLibrary.Author())
authorObject.books.add(ctx.insert(BookLibrary.Book()))
Expand All @@ -46,7 +46,7 @@ final class UnsafeTests: XCTestCase {
}

func _testDeallocatedReferenceAccess3() {
TestDB.inMemoryContainer(store: DataModels.bookLibrary) { pc in
TestDB.temporaryContainer(store: DataModels.bookLibrary) { pc in
var managedObjects: ManagedObjectSet<BookLibrary.Book>?

try pc.perform(action: { ctx in
Expand All @@ -62,7 +62,7 @@ final class UnsafeTests: XCTestCase {
}

func _testIterator() {
TestDB.inMemoryContainer(store: DataModels.bookLibrary) { pc in
TestDB.temporaryContainer(store: DataModels.bookLibrary) { pc in
try pc.perform(action: { ctx in
let authorObject = ctx.insert(BookLibrary.Author())
authorObject.books.add(ctx.insert(BookLibrary.Book()))
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwormTests/Utils/TestDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SwormTools
import XCTest

enum TestDB {
static func inMemoryContainer(
static func temporaryContainer(
store: SQLiteStoreDescription,
action: (PersistentContainer) throws -> Void
) {
Expand Down

0 comments on commit f1ca3ac

Please sign in to comment.