Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed a bogus warning when querying a view with group_level but not g…

…roup.

Also added a new unit case for grouping when the keys are strings not arrays.
  • Loading branch information...
commit cd8b8ddbf42326a923a45e720433b3ace649d5f5 1 parent e23509c
@snej snej authored
Showing with 36 additions and 11 deletions.
  1. +8 −11 Source/TDView.m
  2. +28 −0 Source/TDView_Tests.m
View
19 Source/TDView.m
@@ -265,17 +265,7 @@ - (FMResultSet*) resultSetWithOptions: (const TDQueryOptions*)options
{
if (!options)
options = &kDefaultTDQueryOptions;
-
- if (!options->group) {
- if (options->reduce && !_reduceBlock) {
- Warn(@"Cannot use reduce option in view %@ which has no reduce block defined", _name);
- *outStatus = 400;
- return nil;
- }
- if (options->groupLevel > 0)
- Warn(@"Setting groupLevel without group makes no sense");
- }
-
+
*outStatus = [self updateIndex];
if (*outStatus >= 300)
return nil;
@@ -358,6 +348,13 @@ - (NSArray*) queryWithOptions: (const TDQueryOptions*)options
unsigned groupLevel = options->groupLevel;
bool group = options->group || groupLevel > 0;
bool reduce = options->reduce || group;
+
+ if (reduce && !_reduceBlock && !group) {
+ Warn(@"Cannot use reduce option in view %@ which has no reduce block defined", _name);
+ *outStatus = 400;
+ return nil;
+ }
+
NSMutableArray* rows = $marray();
NSMutableArray* keysToReduce=nil, *valuesToReduce=nil;
id lastKey = nil;
View
28 Source/TDView_Tests.m
@@ -337,5 +337,33 @@
}
+TestCase(TDView_GroupedStrings) {
+ RequireTestCase(TDView_Grouped);
+ TDDatabase *db = [TDDatabase createEmptyDBAtPath: @"/tmp/TouchDB_ViewTest.touchdb"];
+ putDoc(db, $dict({@"name", @"Alice"}));
+ putDoc(db, $dict({@"name", @"Albert"}));
+ putDoc(db, $dict({@"name", @"Naomi"}));
+ putDoc(db, $dict({@"name", @"Jens"}));
+ putDoc(db, $dict({@"name", @"Jed"}));
+
+ TDView* view = [db viewNamed: @"default/names"];
+ [view setMapBlock: ^(NSDictionary* doc, TDMapEmitBlock emit) {
+ NSString *name = [doc objectForKey: @"name"];
+ if (name)
+ emit([name substringToIndex:1], [NSNumber numberWithInt:1]);
+ } reduceBlock:^id(NSArray *keys, NSArray *values, BOOL rereduce) {
+ return [NSNumber numberWithInt:[values count]];
+ } version:@"1.0"];
+
+ TDQueryOptions options = kDefaultTDQueryOptions;
+ options.groupLevel = 1;
+ TDStatus status;
+ NSArray* rows = [view queryWithOptions: &options status: &status];
+ CAssertEq(status, 200);
+ CAssertEqual(rows, $array($dict({@"key", @"A"}, {@"value", $object(2)}),
+ $dict({@"key", @"J"}, {@"value", $object(2)}),
+ $dict({@"key", @"N"}, {@"value", $object(1)})));
+}
+
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.