Skip to content

Commit

Permalink
Merge pull request #527 from 3lvis/rollback-extended-operations
Browse files Browse the repository at this point in the history
Rollback extended operations
  • Loading branch information
3lvis authored Aug 23, 2018
2 parents ee77614 + 25c6624 commit 3317492
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 151 deletions.
4 changes: 1 addition & 3 deletions Source/Sync/NSManagedObject+Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ extension NSManagedObject {
/// - objectJSONBlock: A block that gives the oportunity to the called to act on the current processed JSON.
/// - Throws: An exception that will throw if any of the underlaying operations fail.
func sync_toManyRelationship(_ relationship: NSRelationshipDescription, dictionary: [String: Any], parent: NSManagedObject?, parentRelationship: NSRelationshipDescription?, context: NSManagedObjectContext, operations: Sync.OperationOptions, shouldContinueBlock: (() -> Bool)?, objectJSONBlock: ((_ objectJSON: [String: Any]) -> [String: Any])?) throws {
let updatedOperations = operations.relationshipOperations()

var children: [[String: Any]]?
let childrenIsNull = (relationship.customKey as Any?) is NSNull || dictionary[relationship.name.hyp_snakeCase()] is NSNull || dictionary[relationship.name] is NSNull
if childrenIsNull {
Expand Down Expand Up @@ -306,7 +304,7 @@ extension NSManagedObject {

var childPredicate: NSPredicate?
let manyToMany = inverseIsToMany && relationship.isToMany
var childOperations = updatedOperations
var childOperations = operations
let childrenIDs = (((childIDs as Any) as AnyObject) as? NSArray) ?? NSArray()

if manyToMany {
Expand Down
23 changes: 1 addition & 22 deletions Source/Sync/Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,7 @@ public protocol SyncDelegate: class {
public static let insert = OperationOptions(rawValue: 1 << 0)
public static let update = OperationOptions(rawValue: 1 << 1)
public static let delete = OperationOptions(rawValue: 1 << 2)
public static let insertRelationships = OperationOptions(rawValue: 1 << 3)
public static let updateRelationships = OperationOptions(rawValue: 1 << 4)
public static let deleteRelationships = OperationOptions(rawValue: 1 << 5)
public static let all: OperationOptions = [.insert, .update, .delete, .insertRelationships, .updateRelationships, .deleteRelationships]

func relationshipOperations() -> OperationOptions {
var options = OperationOptions.all

if !self.contains(.insertRelationships) {
options.remove(.insert)
}

if !self.contains(.updateRelationships) {
options.remove(.update)
}

if !self.contains(.deleteRelationships) {
options.remove(.delete)
}

return options
}
public static let all: OperationOptions = [.insert, .update, .delete]
}

var downloadFinished = false
Expand Down
126 changes: 0 additions & 126 deletions Tests/Sync/SyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1583,132 +1583,6 @@ class SyncTests: XCTestCase {

dataStack.drop()
}

// https://github.com/3lvis/Sync/issues/422

// -----------
// One to many
// -----------

func test422OneToManyOperationOptionsInsert() {
let dataStack = Helper.dataStackWithModelName("422OneToMany")
let initial = Helper.objectsFromJSON("422-one-to-many-insert-option-initial.json") as! [[String: Any]]

dataStack.sync(initial, inEntityNamed: "User", completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1)

let updated = Helper.objectsFromJSON("422-one-to-many-insert-option-update.json") as! [[String: Any]]
dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .insertRelationships], completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 2)

let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 101"), inContext: dataStack.mainContext).first
XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "101")

dataStack.drop()
}

func test422OneToManyOperationOptionsUpdateRelationships() {
let dataStack = Helper.dataStackWithModelName("422OneToMany")

let initial = Helper.objectsFromJSON("422-one-to-many-update-option-initial.json") as! [[String: Any]]
dataStack.sync(initial, inEntityNamed: "User", completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1)

let updated = Helper.objectsFromJSON("422-one-to-many-update-option-update.json") as! [[String: Any]]
dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .updateRelationships], completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1)

let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 101"), inContext: dataStack.mainContext).first
XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "updated-101")

dataStack.drop()
}

func test422OneToManyOperationOptionsDeleteRelationships() {
let dataStack = Helper.dataStackWithModelName("422OneToMany")

let initial = Helper.objectsFromJSON("422-one-to-many-delete-option-initial.json") as! [[String: Any]]
dataStack.sync(initial, inEntityNamed: "User", completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 2)

let updated = Helper.objectsFromJSON("422-one-to-many-delete-option-update.json") as! [[String: Any]]
dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .deleteRelationships], completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1)

let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 102"), inContext: dataStack.mainContext).first
XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "102")

dataStack.drop()
}

// -----------
// Many to many
// -----------

func test422ManyToManyOperationOptionsInsert() {
let dataStack = Helper.dataStackWithModelName("422ManyToMany")
let initial = Helper.objectsFromJSON("422-many-to-many-insert-option-initial.json") as! [[String: Any]]

dataStack.sync(initial, inEntityNamed: "User", completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1)

let updated = Helper.objectsFromJSON("422-many-to-many-insert-option-update.json") as! [[String: Any]]
dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .insertRelationships], completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 2)

let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 101"), inContext: dataStack.mainContext).first
XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "101")

dataStack.drop()
}

func test422ManyToManyOperationOptionsUpdateRelationships() {
let dataStack = Helper.dataStackWithModelName("422ManyToMany")

let initial = Helper.objectsFromJSON("422-many-to-many-update-option-initial.json") as! [[String: Any]]
dataStack.sync(initial, inEntityNamed: "User", completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1)

let updated = Helper.objectsFromJSON("422-many-to-many-update-option-update.json") as! [[String: Any]]
dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .updateRelationships], completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 1)

let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 101"), inContext: dataStack.mainContext).first
XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "updated-101")

dataStack.drop()
}

func test422ManyToManyOperationOptionsDeleteRelationships() {
let dataStack = Helper.dataStackWithModelName("422ManyToMany")

let initial = Helper.objectsFromJSON("422-many-to-many-delete-option-initial.json") as! [[String: Any]]
dataStack.sync(initial, inEntityNamed: "User", completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Message", inContext: dataStack.mainContext), 2)

let updated = Helper.objectsFromJSON("422-many-to-many-delete-option-update.json") as! [[String: Any]]
dataStack.sync(updated, inEntityNamed: "User", operations: [.insert, .update, .delete, .deleteRelationships], completion: nil)
XCTAssertEqual(Helper.countForEntity("User", inContext: dataStack.mainContext), 1)
// count the messages that user 1 has instead of counting all messages
let userMessageCount = Helper.countForEntity("Message", predicate: NSPredicate(format: "ANY users.id = 1"), inContext: dataStack.mainContext)
XCTAssertEqual(userMessageCount, 1)

let updatedMessage = Helper.fetchEntity("Message", predicate: NSPredicate(format: "id = 102"), inContext: dataStack.mainContext).first
XCTAssertEqual(updatedMessage?.value(forKey: "text") as? String, "102")

dataStack.drop()
}

// https://github.com/3lvis/Sync/issues/476
func test476() {
Expand Down

0 comments on commit 3317492

Please sign in to comment.