Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #207 from sdrew/allDocsQuery-not_found-support
Browse files Browse the repository at this point in the history
Avoid assertion in allDocsQuery:options: when a docId isn't found.
  • Loading branch information
rhyshort committed Nov 2, 2015
2 parents 2461851 + 2e0a78e commit e23eb9f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Classes/common/CDTDatastore.m
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ - (NSArray *)allDocsQuery:(NSArray *)docIds options:(TDQueryOptions *)queryOptio
NSString *docId = row[@"id"];

NSString *revId = row[@"value"][@"rev"];

// If a document isn't found, docId and revId will be null, and row will
// contain an @"error" key.
if (docId == nil && revId == nil && [row[@"error"] isEqualToString:@"not_found"]) {
continue;
}

// deleted field only present in deleted documents, but to be safe we use
// the fact that (BOOL)[nil -boolValue] is false
Expand Down
7 changes: 7 additions & 0 deletions Tests/Tests/CDTDatastoreQueryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@
expect(results).toNot.beNil();
expect([results.documentIds count]).to.equal(3);
});

it(@"can find documents and ignore non-existent documents", ^{
NSDictionary *query = @{ @"_id" : @{ @"$in" : @[@"mike12", @"mike34", @"mike72", @"mike-not-found"] } };
CDTQResultSet *results = [ds find:query];
expect(results).toNot.beNil();
expect([results.documentIds count]).to.equal(3);
});

it(@" can delete an index", ^{
[ds ensureIndexed:@[ @"name", @"address" ] withName:@"basic"];
Expand Down
38 changes: 38 additions & 0 deletions Tests/Tests/DatastoreCRUD.m
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,44 @@ -(void)testGetDocumentsWithIds
}
}

-(void)testGetDocumentsWithIds_NonExistentDocument
{
NSError *error;
NSMutableArray *docIds = [NSMutableArray arrayWithCapacity:20];

for (int i = 0; i < 200; i++) {
error = nil;
CDTDocumentRevision *rev = [CDTDocumentRevision revision];
rev.body = [@{@"hello":@"world",@"index":[NSNumber numberWithInt:i]} mutableCopy];
CDTDocumentRevision *ob = [self.datastore createDocumentFromRevision:rev error:&error];
XCTAssertNil(error, @"Error creating document");

NSString *docId = ob.docId;
[docIds addObject:docId];
}

NSArray *retrivedDocIds = @[docIds[5], docIds[7], docIds[12], docIds[170], @"i_do_not_exist"];
NSArray *obs = [self.datastore getDocumentsWithIds:retrivedDocIds];
XCTAssertNotNil(obs, @"Error getting documents");

XCTAssertEqual([obs count], 4, @"Unexpected number of documents");

int ob_index = 0;
for (NSNumber *index in @[@5, @7, @12, @170]) {
NSString *docId = [docIds objectAtIndex:[index intValue]];
CDTDocumentRevision *retrieved = [obs objectAtIndex:ob_index];

XCTAssertNotNil(retrieved, @"retrieved object was nil");
XCTAssertEqualObjects(retrieved.docId, docId, @"Object retrieved from database has wrong docid");
const NSUInteger expected_count = 2;
XCTAssertEqual(retrieved.body.count, expected_count, @"Object from database has != 2 keys");
XCTAssertEqualObjects(retrieved.body[@"hello"], @"world", @"Object from database has wrong data");
XCTAssertEqualObjects(retrieved.body[@"index"], index, @"Object from database has wrong data");

ob_index++;
}
}

-(void)testGetNonExistingDocument
{
NSError *error;
Expand Down

0 comments on commit e23eb9f

Please sign in to comment.