Skip to content

Commit adbd038

Browse files
authored
Add additional tests for -[FIRServerValues increment:]
Add tests translated from the JS SDK
1 parent 38e1246 commit adbd038

File tree

1 file changed

+74
-14
lines changed
  • Example/Database/Tests/Integration

1 file changed

+74
-14
lines changed

Example/Database/Tests/Integration/FData.m

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,7 +2874,15 @@ - (void)testServerTimestampTransactionLocalEvents {
28742874
@"Number should be no more than 2 seconds ago");
28752875
}
28762876

2877-
- (void)testServerIncrementOverwritesExistingData {
2877+
- (void)testServerIncrementOverwritesExistingDataOffline {
2878+
[self checkServerIncrementOverridesExistingDataWhileOffline:true];
2879+
}
2880+
2881+
- (void)testServerIncrementOverwritesExistingDataOnline {
2882+
[self checkServerIncrementOverridesExistingDataWhileOffline:false];
2883+
}
2884+
2885+
- (void)checkServerIncrementOverridesExistingDataWhileOffline:(BOOL)offline {
28782886
FIRDatabaseReference *ref = [FTestHelpers getRandomNode];
28792887
__block NSMutableArray *found = [NSMutableArray new];
28802888
NSMutableArray *expected = [NSMutableArray new];
@@ -2883,8 +2891,9 @@ - (void)testServerIncrementOverwritesExistingData {
28832891
[found addObject:snap.value];
28842892
}];
28852893

2886-
// Going offline ensures that local events get queued up before server events
2887-
[ref.repo interrupt];
2894+
if (offline) {
2895+
[ref.repo interrupt];
2896+
}
28882897

28892898
// null + incr
28902899
[ref setValue:[FIRServerValue increment:@1]];
@@ -2912,7 +2921,10 @@ - (void)testServerIncrementOverwritesExistingData {
29122921
return found.count == expected.count;
29132922
}];
29142923
XCTAssertEqualObjects(expected, found);
2915-
[ref.repo resume];
2924+
2925+
if (offline) {
2926+
[ref.repo resume];
2927+
}
29162928
}
29172929

29182930
- (void)testServerIncrementPriority {
@@ -2924,10 +2936,6 @@ - (void)testServerIncrementPriority {
29242936
[found addObject:snap.priority];
29252937
}];
29262938

2927-
// Going offline ensures that local events get queued up before server events
2928-
// Also necessary because increment may not be live yet in the server.
2929-
[ref.repo interrupt];
2930-
29312939
// null + incr
29322940
[ref setValue:@0 andPriority:[FIRServerValue increment:@1]];
29332941
[expected addObject:@1];
@@ -2938,7 +2946,6 @@ - (void)testServerIncrementPriority {
29382946
return found.count == expected.count;
29392947
}];
29402948
XCTAssertEqualObjects(expected, found);
2941-
[ref.repo resume];
29422949
}
29432950

29442951
- (void)testServerIncrementOverflowAndTypeCoercion {
@@ -2953,10 +2960,6 @@ - (void)testServerIncrementOverflowAndTypeCoercion {
29532960
[foundTypes addObject:@([(NSNumber *)snap.value objCType])];
29542961
}];
29552962

2956-
// Going offline ensures that local events get queued up before server events
2957-
// Also necessary because increment may not be live yet in the server.
2958-
[ref.repo interrupt];
2959-
29602963
// long + double = double
29612964
[ref setValue:@1];
29622965
[ref setValue:[FIRServerValue increment:@1.0]];
@@ -3002,7 +3005,64 @@ - (void)testServerIncrementOverflowAndTypeCoercion {
30023005
}];
30033006
XCTAssertEqualObjects(expectedTypes, foundTypes);
30043007
XCTAssertEqualObjects(expected, found);
3005-
[ref.repo resume];
3008+
}
3009+
3010+
- (void)testIncrementSparseUpdates {
3011+
FIRDatabaseReference *node = [FTestHelpers getRandomNode];
3012+
3013+
__block BOOL done = NO;
3014+
__block NSDictionary *found = nil;
3015+
__weak FIRDatabaseReference *weakRef = node;
3016+
void (^captureValue)(NSError *, FIRDatabaseReference *) =
3017+
^(NSError *error, FIRDatabaseReference *ref) {
3018+
[weakRef observeEventType:FIRDataEventTypeValue
3019+
withBlock:^(FIRDataSnapshot *snapshot) {
3020+
found = snapshot.value;
3021+
done = YES;
3022+
}];
3023+
};
3024+
3025+
[node updateChildValues:@{@"literal" : @5, @"child/increment" : [FIRServerValue increment:@1]}
3026+
withCompletionBlock:captureValue];
3027+
NSDictionary *expected = @{@"literal" : @5, @"child" : @{@"increment" : @1}};
3028+
[self waitUntil:^BOOL {
3029+
return done;
3030+
}];
3031+
XCTAssertEqualObjects(expected, found);
3032+
3033+
done = NO;
3034+
found = nil;
3035+
3036+
[node updateChildValues:@{@"child/increment" : [FIRServerValue increment:@41]}
3037+
withCompletionBlock:captureValue];
3038+
3039+
expected = @{@"literal" : @5, @"child" : @{@"increment" : @42}};
3040+
[self waitUntil:^BOOL {
3041+
return done;
3042+
}];
3043+
XCTAssertEqualObjects(expected, found);
3044+
}
3045+
3046+
- (void)testIncrementRaces {
3047+
FIRDatabaseReference *node = [FTestHelpers getRandomNode];
3048+
__block int runs;
3049+
__block id value;
3050+
const int REPETITIONS = 20;
3051+
3052+
[node observeEventType:FIRDataEventTypeValue
3053+
withBlock:^(FIRDataSnapshot *snap) {
3054+
runs++;
3055+
value = snap.value;
3056+
}];
3057+
for (int i = 1; i < REPETITIONS; i++) {
3058+
[node setValue:[FIRServerValue increment:@1]];
3059+
}
3060+
3061+
[self waitUntil:^BOOL {
3062+
// Include 1 server event
3063+
return runs == REPETITIONS + 1;
3064+
}];
3065+
XCTAssertEqualObjects(@(REPETITIONS), value);
30063066
}
30073067

30083068
- (void)testUpdateAfterChildSet {

0 commit comments

Comments
 (0)