Skip to content

Commit

Permalink
Add change tag mocking to CKRecord mock (#19)
Browse files Browse the repository at this point in the history
This PR adds support for mocking CKRecord change tag. This can be useful in tests that test code which works with change tags.
  • Loading branch information
jaanus authored Aug 13, 2023
1 parent bb9ffe2 commit 060e975
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Targets/Canopy/Tests/MockObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ final class MockObjectTests: XCTestCase {
let share = CKShare.mock_owned_by_current_user
XCTAssertEqual(share.participants.count, 3)
}

func test_mock_record_change_tag() {
let mockRecord = MockCKRecord(recordType: "MyType")
mockRecord[MockCKRecord.testingRecordChangeTag] = "myTag"
XCTAssertEqual(mockRecord.recordChangeTag, "myTag")
}
}
19 changes: 15 additions & 4 deletions Targets/CanopyTestTools/Sources/MockCKRecord/MockCKRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ public protocol CKRecordMocking {
///
/// This behaves similarly to CKRecord, but lets you override some fields
/// that are set by CloudKit on the server side: most importantly,
/// creator user record name, creation and modification times. This is useful
/// for testing where you need to test with the content of these fields
/// creator user record name, creation and modification times, and change tag.
/// This is useful for testing where you need to test with the content of these fields
/// present, but are constructing the records locally as test fixtures
/// instead of obtaining them from the server.
public class MockCKRecord: CKRecord, CKRecordMocking {
public static let testingCreatorUserRecordNameKey = "testingCreatorUserRecordNameKey"
public static let testingCreatedAtKey = "testingCreatedAtKey"
public static let testingModifiedAtKey = "testingModifiedAtKey"
public static let testingRecordChangeTag = "testingRecordChangeTag"

override public var creatorUserRecordID: CKRecord.ID? {
guard let testing = self[MockCKRecord.testingCreatorUserRecordNameKey] as? String else {
Expand All @@ -39,6 +40,13 @@ public class MockCKRecord: CKRecord, CKRecordMocking {
}
return testing
}

override public var recordChangeTag: String? {
guard let testing = self[MockCKRecord.testingRecordChangeTag] as? String else {
return nil
}
return testing
}

override public class var supportsSecureCoding: Bool {
true
Expand All @@ -49,6 +57,7 @@ public class MockCKRecord: CKRecord, CKRecordMocking {
recordID: CKRecord.ID,
parentRecordID: CKRecord.ID? = nil,
creatorUserRecordName: String = UUID().uuidString,
recordChangeTag: String? = nil,
properties: [String: CKRecordValueProtocol?]
) -> MockCKRecord {
let record = MockCKRecord(recordType: recordType, recordID: recordID)
Expand All @@ -58,7 +67,8 @@ public class MockCKRecord: CKRecord, CKRecordMocking {
}

record[testingCreatorUserRecordNameKey] = creatorUserRecordName

record[testingRecordChangeTag] = recordChangeTag

for (key, value) in properties {
record[key] = value
}
Expand All @@ -74,7 +84,8 @@ public class MockCKRecord: CKRecord, CKRecordMocking {
}

mock[testingCreatorUserRecordNameKey] = record.creatorUserRecordID?.recordName

mock[testingRecordChangeTag] = record.recordChangeTag

for key in mock.allKeys() {
mock[key] = record[key]
}
Expand Down

0 comments on commit 060e975

Please sign in to comment.