forked from bignerdranch/CoreDataStack
/
EntityMonitorTests.swift
187 lines (152 loc) · 6.66 KB
/
EntityMonitorTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// EntityMonitorTests.swift
// CoreDataStack
//
// Created by Robert Edwards on 11/18/15.
// Copyright © 2015-2016 Big Nerd Ranch. All rights reserved.
//
// swiftlint:disable force_try
import XCTest
import CoreData
import CoreDataStack
var insertExpectation: XCTestExpectation!
var deleteExpectation: XCTestExpectation!
var updateExpectation: XCTestExpectation!
class AuthorMonitorDelegate: EntityMonitorDelegate {
func entityMonitorObservedDeletions(_ monitor: EntityMonitor<Author>, entities: Set<Author>) {
XCTAssertGreaterThan(entities.count, 0)
deleteExpectation.fulfill()
}
func entityMonitorObservedInserts(_ monitor: EntityMonitor<Author>, entities: Set<Author>) {
XCTAssertGreaterThan(entities.count, 0)
insertExpectation.fulfill()
}
func entityMonitorObservedModifications(_ monitor: EntityMonitor<Author>, entities: Set<Author>) {
XCTAssertGreaterThan(entities.count, 0)
XCTAssertNotNil(entities.first?.firstName)
updateExpectation.fulfill()
}
}
class BookMonitorDelegate: EntityMonitorDelegate {
func entityMonitorObservedDeletions(_ monitor: EntityMonitor<Book>, entities: Set<Book>) {
deleteExpectation.fulfill()
XCTAssertEqual(entities.count, 1)
}
func entityMonitorObservedInserts(_ monitor: EntityMonitor<Book>, entities: Set<Book>) {
XCTFail("Book inserts will never have a matching title so we shouldn't get this callback")
}
func entityMonitorObservedModifications(_ monitor: EntityMonitor<Book>, entities: Set<Book>) {
updateExpectation.fulfill()
XCTAssertEqual(entities.count, 1)
}
}
class EntityMonitorTests: XCTestCase {
lazy var model: NSManagedObjectModel = {
return self.unitTestBundle.managedObjectModel(name: "Container_Example")
}()
lazy var container: NSPersistentContainer = {
return NSPersistentContainer(name: "Container_Example", managedObjectModel: self.model)
}()
lazy var authorEntityDescription: NSEntityDescription = {
return NSEntityDescription.entity(forEntityName: "Author", in: self.container.viewContext)!
}()
lazy var bookEntityDescription: NSEntityDescription = {
return Book.entity()
}()
override func setUp() {
super.setUp()
weak var setupEx = expectation(description: "Setup")
continueAfterFailure = false
let configuration = NSPersistentStoreDescription()
configuration.type = NSInMemoryStoreType
container.persistentStoreDescriptions = [configuration]
container.loadPersistentStores() { _, error in
if let error = error as? NSError {
XCTFail("Unresolved error \(error), \(error.userInfo)")
}
setupEx?.fulfill()
}
waitForExpectations(timeout: 5, handler: nil)
// Insert and save a new item so we can test updates
let moc = self.container.viewContext
let fr = NSFetchRequest<Author>()
fr.entity = authorEntityDescription
let results = try! moc.fetch(fr)
if results.count < 1 {
let _ = Author(context: moc)
try! moc.saveContextAndWait()
}
}
// MARK: - Tests
func testOnSaveNotifications() {
// Setup monitor
let moc = container.viewContext
let authorMonitor = EntityMonitor<Author>(context: moc, entity: authorEntityDescription, frequency: .onSave)
let authorMonitorDelegate = AuthorMonitorDelegate()
authorMonitor.setDelegate(authorMonitorDelegate)
insertExpectation = expectation(description: "EntityMonitor Insert Callback")
updateExpectation = expectation(description: "EntityMonitor Update Callback")
deleteExpectation = expectation(description: "EntityMonitor Delete Callback")
// Insert an Item
let entity = Author(context: moc)
try! moc.saveContextAndWait()
// New book (since we aren't observing this shouldn't show up in our delegate callback)
let _ = Book(context: moc)
try! moc.saveContextAndWait()
// Modify an existing
let existing = try! Author.findFirstInContext(moc)!
existing.setValue("Robert", forKey: "firstName")
moc.saveContext()
// Delete an item
moc.delete(entity)
try! moc.saveContextAndWait()
waitForExpectations(timeout: 10, handler: nil)
}
func testOnChangeNotifications() {
// Setup monitor
let moc = container.viewContext
let authorMonitor = EntityMonitor<Author>(context: moc, entity: authorEntityDescription, frequency: .onChange)
let authorMonitorDelegate = AuthorMonitorDelegate()
authorMonitor.setDelegate(authorMonitorDelegate)
// Test Insert
insertExpectation = expectation(description: "EntityMonitor Insert Callback")
let _ = Author(context: moc)
waitForExpectations(timeout: 10, handler: nil)
// New Book (since we aren't observing this shouldn't show up in our delegate callback)
let _ = Book(context: moc)
// Test Update
updateExpectation = expectation(description: "EntityMonitor Update Callback")
let existing = try! Author.findFirstInContext(moc)!
existing.firstName = "Robert"
moc.processPendingChanges()
waitForExpectations(timeout: 10, handler: nil)
// Test Delete
deleteExpectation = expectation(description: "EntityMonitor Delete Callback")
let deleteMe = Author(context: moc)
container.viewContext.delete(deleteMe)
waitForExpectations(timeout: 10, handler: nil)
}
func testFilterPredicate() {
// Create filter predicate
let matchingTitle = "nerd"
let predicate = NSPredicate(format: "title CONTAINS[cd] %@", matchingTitle)
// Setup monitor
let moc = container.viewContext
let filteredMonitor = EntityMonitor<Book>(context: moc, entity: bookEntityDescription, filterPredicate: predicate)
let bookMonitorDelegate = BookMonitorDelegate()
filteredMonitor.setDelegate(bookMonitorDelegate)
// Create an initial book
let newBook = Book(context: moc)
try! moc.saveContextAndWait()
// Look for an update
updateExpectation = expectation(description: "EntityMonitor Update Callback")
newBook.title = "Swift Programming: The Big Nerd Ranch Guide"
try! moc.saveContextAndWait()
waitForExpectations(timeout: 10, handler: nil)
// Look for deletion
deleteExpectation = expectation(description: "EntityMonitor Delete Callback")
moc.delete(newBook)
try! moc.saveContextAndWait()
waitForExpectations(timeout: 10, handler: nil)
}
}