Skip to content

Commit

Permalink
Added a JOIN unit test based on the situation in LiteCore#379
Browse files Browse the repository at this point in the history
Also updated LiteCore to pick up the fix

See couchbase/couchbase-lite-core#379
  • Loading branch information
snej committed Feb 8, 2018
1 parent 2f68948 commit da87ce4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Objective-C/Tests/CBLTestCase.h
Expand Up @@ -78,9 +78,13 @@ NS_ASSUME_NONNULL_BEGIN
/** Reads a bundle resource file into an NSString. */
- (NSString*) stringFromResource: (NSString*)resourceName ofType: (NSString*)type;

/** Loads the database with documents read from a JSON resource file in the test bundle.
Each line of the file should be a complete JSON object, which will become a document.
/** Loads the database with documents read from a multiline JSON string.
Each line of the string should be a complete JSON object, which will become a document.
The document IDs will be of the form "doc-#" where "#" is the line number, starting at 1. */
- (void) loadJSONString: (NSString*)contents named: (NSString*)resourceName;

/** Loads the database with documents read from a JSON resource file in the test bundle,
using -loadJSONString:named:.*/
- (void) loadJSONResource: (NSString*)resourceName;

/** Utility to check a failure case. This method asserts that the block returns NO, and that
Expand Down
8 changes: 7 additions & 1 deletion Objective-C/Tests/CBLTestCase.m
Expand Up @@ -169,6 +169,12 @@ - (NSString*) stringFromResource: (NSString*)resourceName ofType: (NSString*)typ
- (void) loadJSONResource: (NSString*)resourceName {
@autoreleasepool {
NSString* contents = [self stringFromResource: resourceName ofType: @"json"];
return [self loadJSONString: contents named: resourceName];
}
}

- (void) loadJSONString: (NSString*)contents named: (NSString*)resourceName {
@autoreleasepool {
__block uint64_t n = 0;
NSError *batchError;
BOOL ok = [self.db inBatch: &batchError usingBlock: ^{
Expand All @@ -185,7 +191,7 @@ - (void) loadJSONResource: (NSString*)resourceName {
Assert(saved, @"Couldn't save document: %@", error);
}];
}];
Assert(ok, @"loadJSONResource failed: %@", batchError);
Assert(ok, @"loadJSONString failed: %@", batchError);
}
}

Expand Down
49 changes: 49 additions & 0 deletions Objective-C/Tests/QueryTest.m
Expand Up @@ -1607,4 +1607,53 @@ - (void) testMissingValue {
AssertEqualObjects([r toDictionary], (@{@"name": @"Scott", @"address": [NSNull null]}));
}


- (void) testForumJoin {
[self loadJSONString:
@"{\"id\":\"ecc:102\",\"type\":\"category\",\"items\":[\"eci:742\",\"eci:743\",\"eci:744\"],\"name\":\"Skills\"}\n"
"{\"id\":\"eci:742\",\"type\":\"item\",\"chinese\":\"技术\",\"english\":\"technique\",\"pinyin\":\"jìshù\"}\n"
"{\"id\":\"eci:743\",\"type\":\"item\",\"chinese\":\"技术\",\"english\":\"skill\",\"pinyin\":\"jìqiǎo\"}"
named: @"forum.json"];


CBLQuerySelectResult* ITEM_DOC_ID =
[CBLQuerySelectResult expression: [CBLQueryMeta idFrom: @"itemDS"] as:@"ITEMID"];


CBLQuerySelectResult* CATEGORY_DOC_ID =
[CBLQuerySelectResult expression: [CBLQueryMeta idFrom: @"categoryDS"] as:@"CATEGORYID"];

CBLQueryExpression* ITEMID = [CBLQueryExpression property:@"id" from: @"itemDS"];

CBLQueryExpression* CATEGORYITEMS = [CBLQueryExpression property: @"items" from:@"categoryDS"] ;
CBLQueryVariableExpression* CATEGORYITEMVAR = [CBLQueryArrayExpression variableWithName: @"item"];

CBLQueryExpression* on = [CBLQueryArrayExpression
any: CATEGORYITEMVAR
in: CATEGORYITEMS
satisfies: [CATEGORYITEMVAR equalTo: ITEMID]];


CBLQueryJoin* join = [CBLQueryJoin join: [CBLQueryDataSource database: self.db as: @"itemDS"]
on: on];
CBLQuery* q = [CBLQueryBuilder select: @[ITEM_DOC_ID, CATEGORY_DOC_ID]
from: [CBLQueryDataSource database: self.db as: @"categoryDS"]
join: @[join]];
Assert(q);
NSError* error;

NSLog(@"%@",[q explain:nil]);

CBLQueryResultSet* rs = [q execute: &error];
Assert(rs);

int i = 0;
for (CBLQueryResult* r in rs) {
NSLog(@" %@",[r toDictionary]);
i++;
}
AssertEqual(i, 2);
}


@end

0 comments on commit da87ce4

Please sign in to comment.