Skip to content

Commit

Permalink
Added tests to identify issue #11
Browse files Browse the repository at this point in the history
  • Loading branch information
terhechte committed Mar 9, 2016
1 parent 556d19a commit 669bd62
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
50 changes: 50 additions & 0 deletions CoreValueMacTests/CoreValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@ struct Employee : CVManagedStruct {
}
}

struct StoredShopEmployee : CVManagedPersistentStruct {

static let EntityName = "Employee"

var objectID: NSManagedObjectID?
let name: String
let age: Int16
let position: String?
let department: String
let job: String
let shop: StoredShop?

static func fromObject(o: NSManagedObject) throws -> StoredShopEmployee {
return try curry(self.init)
<^> o <|? "objectID"
<^> o <| "name"
<^> o <| "age"
<^> o <|? "position"
<^> o <| "department"
<^> o <| "job"
<^> o <|? "shop"
}
}

struct Shop: CVManagedStruct {
static let EntityName = "Shop"

Expand Down Expand Up @@ -91,6 +115,21 @@ struct StoredShop: CVManagedPersistentStruct {
}
}

struct StoredEmployeeShop: CVManagedPersistentStruct {
static let EntityName = "Shop"

var objectID: NSManagedObjectID?
var name: String
var employees: [StoredShopEmployee]

static func fromObject(o: NSManagedObject) throws -> StoredEmployeeShop {
return try curry(self.init)
<^> o <|? "objectID"
<^> o <| "name"
<^> o <|| "employees"
}
}

enum CarType:String{
case Pickup = "pickup"
case Sedan = "sedan"
Expand Down Expand Up @@ -538,6 +577,17 @@ class CoreValueQueryTests: XCTestCase {
XCTAssert(results.count == 1, "Failed to delete object \(s2) from context")
}
}

func testInfiniteLoop() {
let employee = StoredShopEmployee(objectID: nil, name: "John Doe", age: 18, position: "Clerk", department: "Carpet", job: "Cleaner", shop:nil)
var shop = StoredEmployeeShop(objectID: nil, name: "Carpet shop", employees: [employee])

try! shop.save(context)
//Will crash in infinite loop
let shops:[StoredEmployeeShop] = try! StoredEmployeeShop.query(context, predicate: nil)
print(shops)
XCTAssertNotNil(shops)
}
}


Expand Down
8 changes: 5 additions & 3 deletions CoreValueMacTests/CoreValueTests.xcdatamodel/contents
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<attribute name="position" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="company" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Company" inverseName="employees" inverseEntity="Company" syncable="YES"/>
<relationship name="shop" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Shop" inverseName="owner" inverseEntity="Shop" syncable="YES"/>
<relationship name="work" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Shop" inverseName="employees" inverseEntity="Shop" syncable="YES"/>
</entity>
<entity name="Other" syncable="YES">
<attribute name="boolean" optional="YES" attributeType="Boolean" syncable="YES"/>
Expand All @@ -28,13 +29,14 @@
</entity>
<entity name="Shop" syncable="YES">
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="employees" optional="YES" toMany="YES" deletionRule="Nullify" ordered="YES" destinationEntity="Employee" inverseName="work" inverseEntity="Employee" syncable="YES"/>
<relationship name="owner" optional="YES" maxCount="1" deletionRule="Nullify" ordered="YES" destinationEntity="Employee" inverseName="shop" inverseEntity="Employee" syncable="YES"/>
</entity>
<elements>
<element name="Car" positionX="-45" positionY="54" width="128" height="75"/>
<element name="Company" positionX="-54" positionY="36" width="128" height="75"/>
<element name="Employee" positionX="-63" positionY="-18" width="128" height="150"/>
<element name="Employee" positionX="-63" positionY="-18" width="128" height="165"/>
<element name="Other" positionX="-54" positionY="36" width="128" height="30"/>
<element name="Shop" positionX="-54" positionY="27" width="128" height="75"/>
<element name="Car" positionX="-45" positionY="54" width="128" height="75"/>
<element name="Shop" positionX="-54" positionY="27" width="128" height="90"/>
</elements>
</model>

0 comments on commit 669bd62

Please sign in to comment.