Skip to content

Commit

Permalink
Merge pull request #1 from mrlegowatch/wf-merges
Browse files Browse the repository at this point in the history
Handle nil referenced objects as a normal error, instead of as a fatal error.
  • Loading branch information
mrlegowatch authored Jan 23, 2020
2 parents 3e5b48e + 3fb945d commit 9c89781
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/GarageStorage/Garage+MappableObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ extension Garage {
let type = dictionary[CoreDataObject.Attribute.type] as! String
let identifier = dictionary[CoreDataObject.Attribute.identifier] as! String

let referencedObject = fetchObject(for: type, identifier: identifier)!
guard let referencedObject = fetchObject(for: type, identifier: identifier) else {
throw Garage.makeError("Failed to fetch referenced object for type: \(type) identifier: \(identifier)")
}
return try makeMappableObject(from: referencedObject)
}

Expand Down
24 changes: 24 additions & 0 deletions Tests/GarageStorageTests/MappableObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ class MappableObjectTests: XCTestCase {
}
}

func testMissingReferencedObject() {
let garage = Garage()

// Create a "Sam" person and park it.
do {
let sam = objCPerson()
XCTAssertNoThrow(try garage.parkObject(sam), "parkObject")
}

// Retrieve Nick and remove him
do {
let nick = try? garage.retrieveObject(ObjCPerson.self, identifier: "Nick")
XCTAssertNotNil(nick, "Failed to retrieve 'Nick' from garage store")

XCTAssertNoThrow(try garage.deleteObject(nick!))
}

// Now try to retrieve Sam. This should fail because Sam references Nick, and Nick has been removed from storage.
do {
let sam = try? garage.retrieveObject(ObjCPerson.self, identifier: "Sam")
XCTAssertNil(sam, "Should not have been able to retrieve 'Sam' from garage store")
}
}

func testNilObject() {
// This tests the Objective-C interface to parkObject, which throws in Swift.
let garage = Garage()
Expand Down

0 comments on commit 9c89781

Please # to comment.