39 return [[
self context] persistentStoreCoordinator];
42 - (NSManagedObjectModel *)
model
44 return [[
self coordinator] managedObjectModel];
58 NSBundle *bundle = [NSBundle bundleForClass:[
self class]];
59 NSURL *modelURL = [bundle URLForResource:@"ActiveResourceKitTests" withExtension:@"momd"];
60 NSManagedObjectModel *
model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
61 NSPersistentStoreCoordinator *
coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
63 NSError *__autoreleasing error = nil;
66 URL:ActiveResourceKitTestsBaseURL()
69 STAssertNotNil(store, nil);
70 STAssertNil(error, nil);
72 NSManagedObjectContext *
context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
73 [
context setPersistentStoreCoordinator:coordinator];
74 [
self setContext:context];
83 NSError *__autoreleasing error = nil;
84 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
85 NSArray *people = [[
self context] executeFetchRequest:request error:&error];
86 for (NSManagedObject *person in people)
88 NSString *name = [person valueForKey:@"name"];
89 NSLog(
@"person named %@", name);
95 NSError *__autoreleasing error = nil;
96 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Post"];
97 NSArray *posts = [[
self context] executeFetchRequest:request error:&error];
98 for (NSManagedObject *post in posts)
100 NSString *title = [post valueForKey:@"title"];
101 NSManagedObject *person = [post valueForKey:@"poster"];
102 NSString *name = [person valueForKey:@"name"];
103 NSLog(
@"post entitled %@ by %@", title, name);
109 NSError *__autoreleasing error = nil;
110 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Post"];
111 NSArray *posts = [[
self context] executeFetchRequest:request error:&error];
112 for (NSManagedObject *post in posts)
114 NSString *title = [post valueForKey:@"title"];
115 NSLog(
@"post entitled %@", title);
116 STAssertNotNil(title, nil);
117 for (NSManagedObject *comment in [post valueForKey:
@"comments"])
119 NSString *text = [comment valueForKey:@"text"];
121 STAssertNotNil(text, nil);
128 NSError *__autoreleasing error = nil;
129 NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[
self context]];
130 [person setValue:@"Roy Ratcliffe" forKey:@"name"];
131 BOOL yes = [[
self context] save:&error];
132 STAssertNotNil(person, nil);
133 STAssertTrue(yes, nil);
134 STAssertNil(error, nil);
136 [[
self context] deleteObject:person];
137 yes = [[
self context] save:&error];
138 STAssertTrue(yes, nil);
139 STAssertNil(error, nil);
144 NSError *__autoreleasing error = nil;
146 NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[
self context]];
147 BOOL yes = [[
self context] save:&error];
148 STAssertNotNil(person, nil);
149 STAssertTrue(yes, nil);
150 STAssertNil(error, nil);
152 [person setValue:@"Roy Ratcliffe" forKey:@"name"];
153 yes = [[
self context] save:&error];
154 STAssertTrue(yes, nil);
155 STAssertNil(error, nil);
170 NSError *__autoreleasing error = nil;
172 NSManagedObject *post = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:[
self context]];
173 NSManagedObject *comment = [NSEntityDescription insertNewObjectForEntityForName:@"Comment" inManagedObjectContext:[
self context]];
174 STAssertNotNil(post, nil);
175 STAssertNotNil(comment, nil);
178 [post setValue:@"De finibus bonorum et malorum" forKey:@"title"];
179 [post setValue:@"Non eram nescius…" forKey:@"body"];
180 [comment setValue:@"Quae cum dixisset…" forKey:@"text"];
183 [comment setValue:post forKey:@"post"];
186 STAssertTrue([[
self context] save:&error], nil);
187 STAssertNil(error, nil);
189 [[[[
self coordinator] persistentStores] objectAtIndex:0] evictAllResources];
190 [[post managedObjectContext] refreshObject:post mergeChanges:NO];
191 NSMutableArray *comments = [NSMutableArray array];
192 for (NSManagedObject *comment in [post valueForKey:
@"comments"])
194 [comments addObject:[comment valueForKey:@"text"]];
196 STAssertFalse([comments count] == 0, nil);
197 STAssertTrue([[comments objectAtIndex:0] rangeOfString:
@"Quae cum dixisset…"].location != NSNotFound, nil);
202 NSError *__autoreleasing error = nil;
205 NSManagedObject *post = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:[
self context]];
206 [post setValue:@"title" forKey:@"title"];
207 [post setValue:@"body" forKey:@"body"];
208 STAssertNotNil(post, nil);
209 STAssertTrue([[
self context] save:&error], nil);
210 STAssertNil(error, nil);
213 NSManagedObject *comment = [NSEntityDescription insertNewObjectForEntityForName:@"Comment" inManagedObjectContext:[
self context]];
214 [comment setValue:@"text" forKey:@"text"];
215 STAssertNotNil(comment, nil);
216 STAssertTrue([[
self context] save:&error], nil);
217 STAssertNil(error, nil);
229 [comment setValue:post forKey:@"post"];
230 STAssertTrue([[
self context] save:&error], nil);
231 STAssertNil(error, nil);
233 [[[[
self coordinator] persistentStores] objectAtIndex:0] evictAllResources];
234 [[post managedObjectContext] refreshObject:post mergeChanges:NO];
235 NSMutableArray *comments = [NSMutableArray array];
236 for (NSManagedObject *comment in [post valueForKey:
@"comments"])
245 NSManagedObject *commentPost = [comment valueForKey:@"post"];
246 STAssertEqualObjects(post, commentPost, nil);
248 [comments addObject:[comment valueForKey:@"text"]];
250 STAssertFalse([comments count] == 0, nil);
251 STAssertTrue([[comments objectAtIndex:0] rangeOfString:
@"text"].location != NSNotFound, nil);
262 NSError *__autoreleasing error = nil;
264 NSManagedObject *post = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:[
self context]];
265 STAssertNotNil(post, nil);
266 STAssertTrue([[
self context] save:&error], nil);
267 STAssertNil(error, nil);
269 NSManagedObject *comment = [NSEntityDescription insertNewObjectForEntityForName:@"Comment" inManagedObjectContext:[
self context]];
270 STAssertNotNil(comment, nil);
271 STAssertTrue([[
self context] save:&error], nil);
272 STAssertNil(error, nil);
274 [comment setValue:post forKey:@"post"];
275 STAssertTrue([[
self context] save:&error], nil);
276 STAssertNil(error, nil);
278 [[[[
self coordinator] persistentStores] objectAtIndex:0] evictAllResources];
279 [[post managedObjectContext] refreshObject:post mergeChanges:NO];
280 NSMutableArray *comments = [NSMutableArray array];
281 for (NSManagedObject *comment in [post valueForKey:
@"comments"])
283 NSString *text = [comment valueForKey:@"text"];
286 [comments addObject:text];
288 STAssertNil(text, nil);
291 NSManagedObject *commentPost = [comment valueForKey:@"post"];
292 STAssertEqualObjects(post, commentPost, nil);
294 STAssertTrue([comments count] == 0, nil);