-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring the ModelChange API to use a real model instead of option…
…als (#48)
- Loading branch information
Showing
8 changed files
with
131 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// © 2016 LinkedIn Corp. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
|
||
import Foundation | ||
|
||
/** | ||
This class is used to show which child models have changed. This object is usually in a dictionary: `[String: ModelChange]`. | ||
The strings represent IDs and it shows what's changed in this ID. Either the ID has been deleted or updated. | ||
If it's been updated, there are potentially several models that have updated if you are using projections. | ||
*/ | ||
public enum ModelChange: Equatable { | ||
/** | ||
This indicates a model has been updated and lists the new models. | ||
If you are using projections, there may be multiple models that represent this change. | ||
Otherwise, there will just be one model here. | ||
*/ | ||
case updated([ConsistencyManagerModel]) | ||
/** | ||
This indicates a model has been deleted. | ||
*/ | ||
case deleted | ||
|
||
public static func ==(lhs: ModelChange, rhs: ModelChange) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.updated(let l), .updated(let r)): | ||
guard l.count == r.count else { | ||
return false | ||
} | ||
return zip(l, r).reduce(true) { isEqual, tuple in | ||
return isEqual && tuple.0.isEqualToModel(tuple.1) | ||
} | ||
case (.deleted, .deleted): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
ConsistencyManagerTests/DataStructureTests/ModelChangeTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// © 2016 LinkedIn Corp. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
|
||
import XCTest | ||
import ConsistencyManager | ||
|
||
class ModelChangeTests: ConsistencyManagerTestCase { | ||
|
||
func testDeleteEquality() { | ||
XCTAssertEqual(ModelChange.deleted, ModelChange.deleted) | ||
XCTAssertNotEqual(ModelChange.deleted, ModelChange.updated([])) | ||
} | ||
|
||
func testUpdatedEquality() { | ||
let model = TestModel(id: "0", data: nil, children: [], requiredModel: TestRequiredModel(id: nil, data: 0)) | ||
let otherModel = TestModel(id: "1", data: nil, children: [], requiredModel: TestRequiredModel(id: nil, data: 0)) | ||
|
||
XCTAssertEqual(ModelChange.updated([model]), ModelChange.updated([model])) | ||
XCTAssertNotEqual(ModelChange.updated([model]), ModelChange.updated([otherModel])) | ||
XCTAssertNotEqual(ModelChange.updated([model]), ModelChange.updated([model, model])) | ||
XCTAssertNotEqual(ModelChange.updated([model]), ModelChange.deleted) | ||
} | ||
} |
Oops, something went wrong.