Skip to content

Instantly share code, notes, and snippets.

@mschoch
Created June 16, 2011 20:15
Show Gist options
  • Save mschoch/1030154 to your computer and use it in GitHub Desktop.
Save mschoch/1030154 to your computer and use it in GitHub Desktop.
trundle use document after create operation test case
-(void)trundleTestCase {
CCouchDBServer *local = [[CCouchDBServer alloc] initWithSession:nil URL:self.localCouchURL];
CCouchDBDatabase *demo = [[CCouchDBDatabase alloc] initWithServer:local name:@"demo"];
NSMutableDictionary *sampleDocument = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Marty", @"firstName", nil];
CouchDBSuccessHandler successHandler = ^(id inParameter) {
CCouchDBDocument *document = (CCouchDBDocument*)inParameter;
NSLog(@"Success creating document: %@", document);
NSMutableDictionary *updatedSampleDocument = [[document asJSON] mutableCopy];
[updatedSampleDocument setValue:@"Jonathan" forKey:@"firstName"];
[document populateWithJSON:updatedSampleDocument];
CouchDBSuccessHandler updateSuccessHandler = ^(id inParameter) {
CCouchDBDocument *updatedDocument = (CCouchDBDocument*)inParameter;
NSLog(@"Success updating document: %@", updatedDocument);
};
CouchDBFailureHandler updateFailureHandler = ^(NSError *error) {
NSLog(@"Failed to create document! %@", error);
NSLog(@"manually re-set the id and revision to force them into dictionary");
[updatedSampleDocument setValue:[document identifier] forKey:@"_id"];
[updatedSampleDocument setValue:[document revision] forKey:@"_rev"];
[document populateWithJSON:updatedSampleDocument];
CouchDBSuccessHandler finalUpdateSuccessHandler = ^(id inParameter) {
CCouchDBDocument *finalUpdatedDocument = (CCouchDBDocument*)inParameter;
NSLog(@"Success updating document: %@", finalUpdatedDocument);
};
CouchDBFailureHandler finalUpdateFailureHandler = ^(NSError *error) {
NSLog(@"Failed to update document! %@", error);
};
NSLog(@"Update document, will succeed this time");
CURLOperation *finalUpdateDocumentOperation = [demo operationToUpdateDocument:document successHandler:finalUpdateSuccessHandler failureHandler:finalUpdateFailureHandler];
[finalUpdateDocumentOperation start];
};
NSLog(@"Update document, should succeed, but will fail");
CURLOperation *updateDocumentOperation = [demo operationToUpdateDocument:document successHandler:updateSuccessHandler failureHandler:updateFailureHandler];
[updateDocumentOperation start];
};
CouchDBFailureHandler failureHandler = ^(NSError *error) {
NSLog(@"Failed to create document! %@", error);
};
NSLog(@"Create a document, should succeed");
CURLOperation *createDocumentOperation = [demo operationToCreateDocument:sampleDocument successHandler:successHandler failureHandler:failureHandler];
[createDocumentOperation start];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment