Skip to content

Commit

Permalink
compare bson data with a json, to catch quickly bugs like : jeromeleb…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Lebel committed Nov 14, 2013
1 parent 935d810 commit 7535fd0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/MODCursor.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ - (MODSortedMutableDictionary *)nextDocumentWithBsonData:(NSData **)bsonData err

result = [[_mongoCollection.mongoServer class] objectFromBson:&(mongoCursor->current)];
if (bsonData) {
*bsonData = [[[NSData alloc] initWithBytes:mongoCursor->current.data length:mongoCursor->current.dataSize] autorelease];
*bsonData = [[[NSData alloc] initWithBytes:mongoCursor->current.data length:bson_size(&mongoCursor->current)] autorelease];
}
} else if (((mongo_cursor *)_cursor)->err != MONGO_CURSOR_EXHAUSTED) {
NSString *details = nil;
Expand Down
1 change: 1 addition & 0 deletions Sources/MODServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ typedef struct mongo *mongo_ptr;
+ (NSString *)escapeQuotesForString:(NSString *)string;
+ (NSString *)escapeSlashesForString:(NSString *)string;
+ (NSString *)convertObjectToJson:(MODSortedMutableDictionary *)object pretty:(BOOL)pretty strictJson:(BOOL)strictJson;
+ (void)compareJson:(NSString *)json bsonData:(NSData *)document;
+ (void)compareJson:(NSString *)json document:(id)document;
+ (NSArray *)findAllDifferencesInObject1:(id)object1 object2:(id)object2;
@end
20 changes: 20 additions & 0 deletions Sources/MODServer_utils.m
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,26 @@ + (NSString *)escapeSlashesForString:(NSString *)string
return [result autorelease];
}

+ (void)compareJson:(NSString *)json bsonData:(NSData *)document
{
bson bsonDocumentFromJson;
NSError *error;

bson_init(&bsonDocumentFromJson);
[MODJsonToBsonParser bsonFromJson:&bsonDocumentFromJson json:json error:&error];
bson_finish(&bsonDocumentFromJson);
NSAssert(error == nil, @"error with document %@", document);
if ([document length] != bson_size(&bsonDocumentFromJson) || memcmp([document bytes], bsonDocumentFromJson.data, [document length]) != 0) {
NSLog(@"%@", [NSData dataWithBytes:bsonDocumentFromJson.data length:bson_size(&bsonDocumentFromJson)]);
NSLog(@"error with this document %@", document);
NSLog(@"json %@", [MODServer convertObjectToJson:[MODServer objectFromBson:&bsonDocumentFromJson] pretty:YES strictJson:NO]);
NSAssert([document length] == bson_size(&bsonDocumentFromJson), @"not the same size with %@", document);
NSAssert(memcmp([document bytes], bsonDocumentFromJson.data, [document length]) == 0, @"not the same content %@", document);
}

bson_destroy(&bsonDocumentFromJson);
}

+ (void)compareJson:(NSString *)json document:(id)document
{
bson bsonDocument;
Expand Down

0 comments on commit 7535fd0

Please sign in to comment.