2017-05-15 12:27:07 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2017 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import "FOrderByTests.h"
|
|
|
|
|
|
|
|
|
|
@interface FOrderByTests ()
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation FOrderByTests
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testCanDefineAndUseAnIndex {
|
|
|
|
|
__block FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *users = @[
|
|
|
|
|
@{@"name" : @"Andrew", @"nuggets" : @35}, @{@"name" : @"Rob", @"nuggets" : @40},
|
|
|
|
|
@{@"name" : @"Greg", @"nuggets" : @38}
|
|
|
|
|
];
|
2017-06-30 10:47:37 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block int setCount = 0;
|
|
|
|
|
[users enumerateObjectsUsingBlock:^(NSDictionary *user, NSUInteger idx, BOOL *stop) {
|
|
|
|
|
[[ref childByAutoId] setValue:user
|
|
|
|
|
withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
|
|
|
|
|
setCount++;
|
|
|
|
|
}];
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitUntil:^BOOL {
|
|
|
|
|
return setCount == users.count;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block NSMutableArray *byNuggets = [[NSMutableArray alloc] init];
|
|
|
|
|
[[ref queryOrderedByChild:@"nuggets"] observeEventType:FIRDataEventTypeChildAdded
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
NSDictionary *user = snapshot.value;
|
|
|
|
|
[byNuggets addObject:user[@"name"]];
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitUntil:^BOOL {
|
|
|
|
|
return byNuggets.count == users.count;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *expected = @[ @"Andrew", @"Greg", @"Rob" ];
|
|
|
|
|
XCTAssertEqualObjects(byNuggets, expected, @"Correct by-nugget ordering.");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testCanDefineAndUseDeepIndex {
|
|
|
|
|
__block FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *users = @[
|
|
|
|
|
@{@"name" : @"Andrew", @"deep" : @{@"nuggets" : @35}},
|
|
|
|
|
@{@"name" : @"Rob", @"deep" : @{@"nuggets" : @40}},
|
|
|
|
|
@{@"name" : @"Greg", @"deep" : @{@"nuggets" : @38}}
|
|
|
|
|
];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block int setCount = 0;
|
|
|
|
|
[users enumerateObjectsUsingBlock:^(NSDictionary *user, NSUInteger idx, BOOL *stop) {
|
|
|
|
|
[[ref childByAutoId] setValue:user
|
|
|
|
|
withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
|
|
|
|
|
setCount++;
|
|
|
|
|
}];
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitUntil:^BOOL {
|
|
|
|
|
return setCount == users.count;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block NSMutableArray *byNuggets = [[NSMutableArray alloc] init];
|
|
|
|
|
[[ref queryOrderedByChild:@"deep/nuggets"] observeEventType:FIRDataEventTypeChildAdded
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
NSDictionary *user = snapshot.value;
|
|
|
|
|
[byNuggets addObject:user[@"name"]];
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitUntil:^BOOL {
|
|
|
|
|
return byNuggets.count == users.count;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *expected = @[ @"Andrew", @"Greg", @"Rob" ];
|
|
|
|
|
XCTAssertEqualObjects(byNuggets, expected, @"Correct by-nugget ordering.");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testCanUsaAFallbackThenDefineTheSpecifiedIndex {
|
|
|
|
|
FTupleFirebase *tuple = [FTestHelpers getRandomNodePair];
|
|
|
|
|
FIRDatabaseReference *reader = tuple.one, *writer = tuple.two;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *foo1 = @{
|
|
|
|
|
@"a" : @{@"order" : @2, @"foo" : @1},
|
|
|
|
|
@"b" : @{@"order" : @0},
|
|
|
|
|
@"c" : @{@"order" : @1, @"foo" : @NO},
|
|
|
|
|
@"d" : @{@"order" : @3, @"foo" : @"hello"}
|
|
|
|
|
};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *foo_e = @{@"order" : @1.5, @"foo" : @YES};
|
|
|
|
|
NSDictionary *foo_f = @{@"order" : @4, @"foo" : @{@"bar" : @"baz"}};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:writer setValue:foo1];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *snaps = [[NSMutableArray alloc] init];
|
|
|
|
|
[[[reader queryOrderedByChild:@"order"] queryLimitedToLast:2]
|
|
|
|
|
observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
[snaps addObject:snapshot.value];
|
|
|
|
|
}];
|
|
|
|
|
WAIT_FOR(snaps.count == 1);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *expected =
|
|
|
|
|
@{@"d" : @{@"order" : @3, @"foo" : @"hello"}, @"a" : @{@"order" : @2, @"foo" : @1}};
|
|
|
|
|
XCTAssertEqualObjects(snaps[0], expected, @"Got correct result");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:[writer child:@"e"] setValue:foo_e];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForRoundTrip:reader];
|
|
|
|
|
NSLog(@"snaps: %@", snaps);
|
|
|
|
|
NSLog(@"snaps.count: %ld", (unsigned long)snaps.count);
|
|
|
|
|
XCTAssertEqual(snaps.count, (NSUInteger)1, @"Should still have one event.");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:[writer child:@"f"] setValue:foo_f];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForRoundTrip:reader];
|
|
|
|
|
XCTAssertEqual(snaps.count, (NSUInteger)2, @"Should have gotten another event.");
|
|
|
|
|
expected = @{@"f" : foo_f, @"d" : @{@"order" : @3, @"foo" : @"hello"}};
|
|
|
|
|
XCTAssertEqualObjects(snaps[1], expected, @"Correct event.");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testSnapshotsAreIteratedInOrder {
|
|
|
|
|
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initial = @{
|
|
|
|
|
@"alex" : @{@"nuggets" : @60},
|
|
|
|
|
@"rob" : @{@"nuggets" : @56},
|
|
|
|
|
@"vassili" : @{@"nuggets" : @55.5},
|
|
|
|
|
@"tony" : @{@"nuggets" : @52},
|
|
|
|
|
@"greg" : @{@"nuggets" : @52}
|
|
|
|
|
};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *expectedOrder = @[ @"greg", @"tony", @"vassili", @"rob", @"alex" ];
|
|
|
|
|
NSArray *expectedPrevNames = @[ [NSNull null], @"greg", @"tony", @"vassili", @"rob" ];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *valueOrder = [[NSMutableArray alloc] init];
|
|
|
|
|
NSMutableArray *addedOrder = [[NSMutableArray alloc] init];
|
|
|
|
|
NSMutableArray *addedPrevNames = [[NSMutableArray alloc] init];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
FIRDatabaseQuery *orderedRef = [ref queryOrderedByChild:@"nuggets"];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[orderedRef observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
for (FIRDataSnapshot *child in snapshot.children) {
|
|
|
|
|
[valueOrder addObject:child.key];
|
|
|
|
|
}
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[orderedRef observeEventType:FIRDataEventTypeChildAdded
|
|
|
|
|
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *prevName) {
|
2017-05-15 12:27:07 -07:00
|
|
|
[addedOrder addObject:snapshot.key];
|
|
|
|
|
[addedPrevNames addObject:prevName ? prevName : [NSNull null]];
|
2019-08-05 13:27:47 -07:00
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[ref setValue:initial];
|
|
|
|
|
WAIT_FOR(addedOrder.count == expectedOrder.count && valueOrder.count == expectedOrder.count);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
XCTAssertEqualObjects(addedOrder, expectedOrder, @"child_added events in correct order.");
|
|
|
|
|
XCTAssertEqualObjects(addedPrevNames, expectedPrevNames,
|
|
|
|
|
@"Got correct prevnames for child_added events.");
|
|
|
|
|
XCTAssertEqualObjects(valueOrder, expectedOrder,
|
|
|
|
|
@"enumerated snapshot children in correct order.");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testSnapshotsAreIteratedInOrderForValueIndex {
|
|
|
|
|
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initial =
|
|
|
|
|
@{@"alex" : @60, @"rob" : @56, @"vassili" : @55.5, @"tony" : @52, @"greg" : @52};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *expectedOrder = @[ @"greg", @"tony", @"vassili", @"rob", @"alex" ];
|
|
|
|
|
NSArray *expectedPrevNames = @[ [NSNull null], @"greg", @"tony", @"vassili", @"rob" ];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *valueOrder = [[NSMutableArray alloc] init];
|
|
|
|
|
NSMutableArray *addedOrder = [[NSMutableArray alloc] init];
|
|
|
|
|
NSMutableArray *addedPrevNames = [[NSMutableArray alloc] init];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
FIRDatabaseQuery *orderedRef = [ref queryOrderedByValue];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[orderedRef observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
for (FIRDataSnapshot *child in snapshot.children) {
|
|
|
|
|
[valueOrder addObject:child.key];
|
|
|
|
|
}
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[orderedRef observeEventType:FIRDataEventTypeChildAdded
|
|
|
|
|
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *prevName) {
|
2017-05-15 12:27:07 -07:00
|
|
|
[addedOrder addObject:snapshot.key];
|
|
|
|
|
[addedPrevNames addObject:prevName ? prevName : [NSNull null]];
|
2019-08-05 13:27:47 -07:00
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[ref setValue:initial];
|
|
|
|
|
WAIT_FOR(addedOrder.count == expectedOrder.count && valueOrder.count == expectedOrder.count);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
XCTAssertEqualObjects(addedOrder, expectedOrder, @"child_added events in correct order.");
|
|
|
|
|
XCTAssertEqualObjects(addedPrevNames, expectedPrevNames,
|
|
|
|
|
@"Got correct prevnames for child_added events.");
|
|
|
|
|
XCTAssertEqualObjects(valueOrder, expectedOrder,
|
|
|
|
|
@"enumerated snapshot children in correct order.");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testFiresChildMovedEvents {
|
|
|
|
|
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initial = @{
|
|
|
|
|
@"alex" : @{@"nuggets" : @60},
|
|
|
|
|
@"rob" : @{@"nuggets" : @56},
|
|
|
|
|
@"vassili" : @{@"nuggets" : @55.5},
|
|
|
|
|
@"tony" : @{@"nuggets" : @52},
|
|
|
|
|
@"greg" : @{@"nuggets" : @52}
|
|
|
|
|
};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
FIRDatabaseQuery *orderedRef = [ref queryOrderedByChild:@"nuggets"];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block BOOL moved = NO;
|
|
|
|
|
[orderedRef observeEventType:FIRDataEventTypeChildMoved
|
|
|
|
|
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *prevName) {
|
2017-05-15 12:27:07 -07:00
|
|
|
moved = YES;
|
|
|
|
|
XCTAssertEqualObjects(snapshot.key, @"greg", @"");
|
|
|
|
|
XCTAssertEqualObjects(prevName, @"rob", @"");
|
2019-09-03 08:46:21 -07:00
|
|
|
XCTAssertEqualObjects(
|
|
|
|
|
snapshot.value,
|
|
|
|
|
@{@"nuggets" : @57}, @"");
|
2019-08-05 13:27:47 -07:00
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[ref setValue:initial];
|
|
|
|
|
[[ref child:@"greg/nuggets"] setValue:@57];
|
|
|
|
|
WAIT_FOR(moved);
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testDefineMultipleIndexesAtALocation {
|
|
|
|
|
FTupleFirebase *tuple = [FTestHelpers getRandomNodePair];
|
|
|
|
|
FIRDatabaseReference *reader = tuple.one, *writer = tuple.two;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *foo1 = @{
|
|
|
|
|
@"a" : @{@"order" : @2, @"foo" : @2},
|
|
|
|
|
@"b" : @{@"order" : @0},
|
|
|
|
|
@"c" : @{@"order" : @1, @"foo" : @NO},
|
|
|
|
|
@"d" : @{@"order" : @3, @"foo" : @"hello"}
|
|
|
|
|
};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:writer setValue:foo1];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
FIRDatabaseQuery *fooOrder = [reader queryOrderedByChild:@"foo"];
|
|
|
|
|
FIRDatabaseQuery *orderOrder = [reader queryOrderedByChild:@"order"];
|
|
|
|
|
NSMutableArray *fooSnaps = [[NSMutableArray alloc] init];
|
|
|
|
|
NSMutableArray *orderSnaps = [[NSMutableArray alloc] init];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[[[fooOrder queryStartingAtValue:nil] queryEndingAtValue:@1]
|
|
|
|
|
observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
[fooSnaps addObject:snapshot.value];
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[[orderOrder queryLimitedToLast:2] observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
[orderSnaps addObject:snapshot.value];
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(fooSnaps.count == 1 && orderSnaps.count == 1);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *expected = @{@"b" : @{@"order" : @0}, @"c" : @{@"order" : @1, @"foo" : @NO}};
|
|
|
|
|
XCTAssertEqualObjects(fooSnaps[0], expected, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
expected = @{
|
|
|
|
|
@"d" : @{@"order" : @3, @"foo" : @"hello"},
|
|
|
|
|
@"a" : @{@"order" : @2, @"foo" : @2},
|
|
|
|
|
};
|
|
|
|
|
XCTAssertEqualObjects(orderSnaps[0], expected, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[[writer child:@"a"] setValue:@{@"order" : @-1, @"foo" : @1}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(fooSnaps.count == 2 && orderSnaps.count == 2);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
expected = @{
|
|
|
|
|
@"a" : @{@"order" : @-1, @"foo" : @1},
|
|
|
|
|
@"b" : @{@"order" : @0},
|
|
|
|
|
@"c" : @{@"order" : @1, @"foo" : @NO}
|
|
|
|
|
};
|
|
|
|
|
XCTAssertEqualObjects(fooSnaps[1], expected, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
expected = @{@"d" : @{@"order" : @3, @"foo" : @"hello"}, @"c" : @{@"order" : @1, @"foo" : @NO}};
|
|
|
|
|
XCTAssertEqualObjects(orderSnaps[1], expected, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testCallbackRemovalWorks {
|
|
|
|
|
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block int reads = 0;
|
|
|
|
|
FIRDatabaseHandle fooHandle, bazHandle;
|
|
|
|
|
fooHandle = [[ref queryOrderedByChild:@"foo"] observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
reads++;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[[ref queryOrderedByChild:@"bar"] observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
reads++;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
bazHandle = [[ref queryOrderedByChild:@"baz"] observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
reads++;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[ref observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
reads++;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:ref setValue:@1];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
XCTAssertEqual(reads, 4, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[ref removeObserverWithHandle:fooHandle];
|
|
|
|
|
[self waitForCompletionOf:ref setValue:@2];
|
|
|
|
|
XCTAssertEqual(reads, 7, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
// should be a no-op, resulting in 3 more reads.
|
|
|
|
|
[[ref queryOrderedByChild:@"foo"] removeObserverWithHandle:bazHandle];
|
|
|
|
|
[self waitForCompletionOf:ref setValue:@3];
|
|
|
|
|
XCTAssertEqual(reads, 10, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[[ref queryOrderedByChild:@"bar"] removeAllObservers];
|
|
|
|
|
[self waitForCompletionOf:ref setValue:@4];
|
|
|
|
|
XCTAssertEqual(reads, 12, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
// Now, remove everything.
|
|
|
|
|
[ref removeAllObservers];
|
|
|
|
|
[self waitForCompletionOf:ref setValue:@5];
|
|
|
|
|
XCTAssertEqual(reads, 12, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testChildAddedEventsAreInTheCorrectOrder {
|
|
|
|
|
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initial = @{@"a" : @{@"value" : @5}, @"c" : @{@"value" : @3}};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *added = [[NSMutableArray alloc] init];
|
|
|
|
|
[[ref queryOrderedByChild:@"value"] observeEventType:FIRDataEventTypeChildAdded
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
[added addObject:snapshot.key];
|
|
|
|
|
}];
|
|
|
|
|
[ref setValue:initial];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(added.count == 2);
|
|
|
|
|
NSArray *expected = @[ @"c", @"a" ];
|
|
|
|
|
XCTAssertEqualObjects(added, expected, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[ref updateChildValues:@{@"b" : @{@"value" : @4}, @"d" : @{@"value" : @2}}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(added.count == 4);
|
|
|
|
|
expected = @[ @"c", @"a", @"d", @"b" ];
|
|
|
|
|
XCTAssertEqualObjects(added, expected, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testCanUseKeyIndex {
|
|
|
|
|
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *data = @{
|
|
|
|
|
@"a" : @{@".priority" : @10, @".value" : @"a"},
|
|
|
|
|
@"b" : @{@".priority" : @5, @".value" : @"b"},
|
|
|
|
|
@"c" : @{@".priority" : @20, @".value" : @"c"},
|
|
|
|
|
@"d" : @{@".priority" : @7, @".value" : @"d"},
|
|
|
|
|
@"e" : @{@".priority" : @30, @".value" : @"e"},
|
|
|
|
|
@"f" : @{@".priority" : @8, @".value" : @"f"}
|
|
|
|
|
};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:ref setValue:data];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block BOOL valueDone = NO;
|
|
|
|
|
[[[ref queryOrderedByKey] queryStartingAtValue:@"c"]
|
|
|
|
|
observeSingleEventOfType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
NSMutableArray *keys = [[NSMutableArray alloc] init];
|
|
|
|
|
for (FIRDataSnapshot *child in snapshot.children) {
|
|
|
|
|
[keys addObject:child.key];
|
|
|
|
|
}
|
|
|
|
|
NSArray *expected = @[ @"c", @"d", @"e", @"f" ];
|
|
|
|
|
XCTAssertEqualObjects(keys, expected, @"");
|
|
|
|
|
valueDone = YES;
|
|
|
|
|
}];
|
|
|
|
|
WAIT_FOR(valueDone);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *keys = [[NSMutableArray alloc] init];
|
|
|
|
|
[[[ref queryOrderedByKey] queryLimitedToLast:5]
|
|
|
|
|
observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
for (FIRDataSnapshot *child in snapshot.children) {
|
|
|
|
|
[keys addObject:child.key];
|
|
|
|
|
}
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(keys.count == 5);
|
|
|
|
|
NSArray *expected = @[ @"b", @"c", @"d", @"e", @"f" ];
|
|
|
|
|
XCTAssertEqualObjects(keys, expected, @"");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testQueriesWorkOnLeafNodes {
|
|
|
|
|
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:ref setValue:@"leaf-node"];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block BOOL valueDone = NO;
|
|
|
|
|
[[[ref queryOrderedByChild:@"foo"] queryLimitedToLast:1]
|
|
|
|
|
observeSingleEventOfType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
XCTAssertEqual(snapshot.value, [NSNull null]);
|
|
|
|
|
valueDone = YES;
|
|
|
|
|
}];
|
|
|
|
|
WAIT_FOR(valueDone);
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testUpdatesForUnindexedQuery {
|
|
|
|
|
FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
|
|
|
|
|
FIRDatabaseReference *reader = refs.one;
|
|
|
|
|
FIRDatabaseReference *writer = refs.two;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block BOOL done = NO;
|
|
|
|
|
NSDictionary *value = @{
|
|
|
|
|
@"one" : @{@"index" : @1, @"value" : @"one"},
|
|
|
|
|
@"two" : @{@"index" : @2, @"value" : @"two"},
|
|
|
|
|
@"three" : @{@"index" : @3, @"value" : @"three"}
|
|
|
|
|
};
|
|
|
|
|
[writer setValue:value
|
|
|
|
|
withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref) {
|
2017-05-15 12:27:07 -07:00
|
|
|
done = YES;
|
2019-08-05 13:27:47 -07:00
|
|
|
}];
|
|
|
|
|
WAIT_FOR(done);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
done = NO;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *snapshots = [NSMutableArray array];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[[[reader queryOrderedByChild:@"index"] queryLimitedToLast:2]
|
|
|
|
|
observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
[snapshots addObject:snapshot.value];
|
|
|
|
|
done = YES;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(done);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *expected = @{
|
|
|
|
|
@"two" : @{@"index" : @2, @"value" : @"two"},
|
|
|
|
|
@"three" : @{@"index" : @3, @"value" : @"three"}
|
|
|
|
|
};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
XCTAssertEqual(snapshots.count, (NSUInteger)1);
|
|
|
|
|
XCTAssertEqualObjects(snapshots[0], expected);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
done = NO;
|
|
|
|
|
[[writer child:@"one/index"] setValue:@4];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(done);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
expected = @{
|
|
|
|
|
@"one" : @{@"index" : @4, @"value" : @"one"},
|
|
|
|
|
@"three" : @{@"index" : @3, @"value" : @"three"}
|
|
|
|
|
};
|
|
|
|
|
XCTAssertEqual(snapshots.count, (NSUInteger)2);
|
|
|
|
|
XCTAssertEqualObjects(snapshots[1], expected);
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testServerRespectsKeyIndex {
|
|
|
|
|
FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
|
|
|
|
|
FIRDatabaseReference *writer = refs.one;
|
|
|
|
|
FIRDatabaseReference *reader = refs.two;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initial = @{@"a" : @1, @"b" : @2, @"c" : @3};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
// If the server doesn't respect the index, it will send down limited data, but with no offset, so
|
|
|
|
|
// the expected and actual data don't match
|
|
|
|
|
FIRDatabaseQuery *query =
|
|
|
|
|
[[[reader queryOrderedByKey] queryStartingAtValue:@"b"] queryLimitedToFirst:2];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *expectedChildren = @[ @"b", @"c" ];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:writer setValue:initial];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *children = [[NSMutableArray alloc] init];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block BOOL done = NO;
|
|
|
|
|
[query observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
for (FIRDataSnapshot *child in snapshot.children) {
|
|
|
|
|
[children addObject:child.key];
|
|
|
|
|
}
|
|
|
|
|
done = YES;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(done);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
XCTAssertEqualObjects(expectedChildren, children, @"Got correct children");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testServerRespectsValueIndex {
|
|
|
|
|
FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
|
|
|
|
|
FIRDatabaseReference *writer = refs.one;
|
|
|
|
|
FIRDatabaseReference *reader = refs.two;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initial = @{@"a" : @1, @"c" : @2, @"b" : @3};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
// If the server doesn't respect the index, it will send down limited data, but with no offset, so
|
|
|
|
|
// the expected and actual data don't match
|
|
|
|
|
FIRDatabaseQuery *query =
|
|
|
|
|
[[[reader queryOrderedByValue] queryStartingAtValue:@2] queryLimitedToFirst:2];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *expectedChildren = @[ @"c", @"b" ];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:writer setValue:initial];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *children = [[NSMutableArray alloc] init];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block BOOL done = NO;
|
|
|
|
|
[query observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
for (FIRDataSnapshot *child in snapshot.children) {
|
|
|
|
|
[children addObject:child.key];
|
|
|
|
|
}
|
|
|
|
|
done = YES;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(done);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
XCTAssertEqualObjects(expectedChildren, children, @"Got correct children");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testDeepUpdatesWorkWithQueries {
|
|
|
|
|
FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
|
|
|
|
|
FIRDatabaseReference *writer = refs.one;
|
|
|
|
|
FIRDatabaseReference *reader = refs.two;
|
2017-06-30 10:47:37 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initial = @{
|
|
|
|
|
@"a" : @{@"data" : @"foo", @"idx" : @YES},
|
|
|
|
|
@"b" : @{@"data" : @"bar", @"idx" : @YES},
|
|
|
|
|
@"c" : @{@"data" : @"baz", @"idx" : @NO}
|
|
|
|
|
};
|
|
|
|
|
[self waitForCompletionOf:writer setValue:initial];
|
2017-06-30 10:47:37 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
FIRDatabaseQuery *query = [[reader queryOrderedByChild:@"idx"] queryEqualToValue:@YES];
|
2017-06-30 10:47:37 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *expected =
|
|
|
|
|
@{@"a" : @{@"data" : @"foo", @"idx" : @YES}, @"b" : @{@"data" : @"bar", @"idx" : @YES}};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForExportValueOf:query toBe:expected];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *update = @{@"a/idx" : @NO, @"b/data" : @"blah", @"c/idx" : @YES};
|
|
|
|
|
[self waitForCompletionOf:writer updateChildValues:update];
|
2017-06-30 10:47:37 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
expected =
|
|
|
|
|
@{@"b" : @{@"data" : @"blah", @"idx" : @YES}, @"c" : @{@"data" : @"baz", @"idx" : @YES}};
|
|
|
|
|
[self waitForExportValueOf:query toBe:expected];
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testServerRespectsDeepIndex {
|
|
|
|
|
FTupleFirebase *refs = [FTestHelpers getRandomNodePair];
|
|
|
|
|
FIRDatabaseReference *writer = refs.one;
|
|
|
|
|
FIRDatabaseReference *reader = refs.two;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initial = @{
|
|
|
|
|
@"a" : @{@"deep" : @{@"index" : @1}},
|
|
|
|
|
@"c" : @{@"deep" : @{@"index" : @2}},
|
|
|
|
|
@"b" : @{@"deep" : @{@"index" : @3}}
|
|
|
|
|
};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
// If the server doesn't respect the index, it will send down limited data, but with no offset, so
|
|
|
|
|
// the expected and actual data don't match
|
|
|
|
|
FIRDatabaseQuery *query =
|
|
|
|
|
[[[reader queryOrderedByChild:@"deep/index"] queryStartingAtValue:@2] queryLimitedToFirst:2];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *expectedChildren = @[ @"c", @"b" ];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[self waitForCompletionOf:writer setValue:initial];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *children = [[NSMutableArray alloc] init];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block BOOL done = NO;
|
|
|
|
|
[query observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
for (FIRDataSnapshot *child in snapshot.children) {
|
|
|
|
|
[children addObject:child.key];
|
|
|
|
|
}
|
|
|
|
|
done = YES;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(done);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
XCTAssertEqualObjects(expectedChildren, children, @"Got correct children");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testStartAtEndAtWorksWithValueIndex {
|
|
|
|
|
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initial =
|
|
|
|
|
@{@"alex" : @60, @"rob" : @56, @"vassili" : @55.5, @"tony" : @52, @"greg" : @52};
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSArray *expectedOrder = @[ @"tony", @"vassili", @"rob" ];
|
|
|
|
|
NSArray *expectedPrevNames = @[ [NSNull null], @"tony", @"vassili" ];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSMutableArray *valueOrder = [[NSMutableArray alloc] init];
|
|
|
|
|
NSMutableArray *addedOrder = [[NSMutableArray alloc] init];
|
|
|
|
|
NSMutableArray *addedPrevNames = [[NSMutableArray alloc] init];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
FIRDatabaseQuery *orderedRef =
|
|
|
|
|
[[[ref queryOrderedByValue] queryStartingAtValue:@52
|
|
|
|
|
childKey:@"tony"] queryEndingAtValue:@59];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[orderedRef observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
for (FIRDataSnapshot *child in snapshot.children) {
|
|
|
|
|
[valueOrder addObject:child.key];
|
|
|
|
|
}
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[orderedRef observeEventType:FIRDataEventTypeChildAdded
|
|
|
|
|
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *prevName) {
|
2017-05-15 12:27:07 -07:00
|
|
|
[addedOrder addObject:snapshot.key];
|
|
|
|
|
[addedPrevNames addObject:prevName ? prevName : [NSNull null]];
|
2019-08-05 13:27:47 -07:00
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[ref setValue:initial];
|
|
|
|
|
WAIT_FOR(addedOrder.count == expectedOrder.count && valueOrder.count == expectedOrder.count);
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
XCTAssertEqualObjects(addedOrder, expectedOrder, @"child_added events in correct order.");
|
|
|
|
|
XCTAssertEqualObjects(addedPrevNames, expectedPrevNames,
|
|
|
|
|
@"Got correct prevnames for child_added events.");
|
|
|
|
|
XCTAssertEqualObjects(valueOrder, expectedOrder,
|
|
|
|
|
@"enumerated snapshot children in correct order.");
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
- (void)testRemovingDefaultListenerRemovesNonDefaultListenWithLoadsAllData {
|
|
|
|
|
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
NSDictionary *initialData = @{@"key" : @"value"};
|
|
|
|
|
[self waitForCompletionOf:ref setValue:initialData];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
[[ref queryOrderedByKey] observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot){
|
|
|
|
|
}];
|
|
|
|
|
[ref observeEventType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot){
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
// Should remove both listener and should remove the listen sent to the server
|
|
|
|
|
[ref removeAllObservers];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
__block id result = nil;
|
|
|
|
|
// This used to crash because a listener for [ref queryOrderedByKey] existed already
|
|
|
|
|
[[ref queryOrderedByKey] observeSingleEventOfType:FIRDataEventTypeValue
|
|
|
|
|
withBlock:^(FIRDataSnapshot *snapshot) {
|
|
|
|
|
result = snapshot.value;
|
|
|
|
|
}];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-08-05 13:27:47 -07:00
|
|
|
WAIT_FOR(result);
|
|
|
|
|
XCTAssertEqualObjects(result, initialData);
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|