21
21
#import " FEventTester.h"
22
22
#import " FIRDatabaseConfig_Private.h"
23
23
#import " FIRDatabaseQuery_Private.h"
24
+ #import " FIRServerValue_unreleased.h"
24
25
#import " FRepo_Private.h"
25
26
#import " FTestHelpers.h"
26
27
#import " FTupleEventTypeString.h"
@@ -2571,7 +2572,7 @@ - (void)testParentDeleteShadowsChildListenersWithNonDefaultQuery {
2571
2572
WAIT_FOR (done);
2572
2573
}
2573
2574
2574
- - (void )testLocalServerValuesEventuallyButNotImmediatelyMatchServer {
2575
+ - (void )testLocalServerTimestampEventuallyButNotImmediatelyMatchServer {
2575
2576
FTupleFirebase *refs = [FTestHelpers getRandomNodePair ];
2576
2577
FIRDatabaseReference *writer = refs.one ;
2577
2578
FIRDatabaseReference *reader = refs.two ;
@@ -2635,7 +2636,7 @@ - (void)testLocalServerValuesEventuallyButNotImmediatelyMatchServer {
2635
2636
@" Eventual reader and writer priorities should be equal" );
2636
2637
}
2637
2638
2638
- - (void )testServerValuesSetWithPriorityRemoteEvents {
2639
+ - (void )testServerTimestampSetWithPriorityRemoteEvents {
2639
2640
FTupleFirebase *refs = [FTestHelpers getRandomNodePair ];
2640
2641
FIRDatabaseReference *writer = refs.one ;
2641
2642
FIRDatabaseReference *reader = refs.two ;
@@ -2676,7 +2677,7 @@ - (void)testServerValuesSetWithPriorityRemoteEvents {
2676
2677
}];
2677
2678
}
2678
2679
2679
- - (void )testServerValuesSetPriorityRemoteEvents {
2680
+ - (void )testServerTimestampSetPriorityRemoteEvents {
2680
2681
FTupleFirebase *refs = [FTestHelpers getRandomNodePair ];
2681
2682
FIRDatabaseReference *writer = refs.one ;
2682
2683
FIRDatabaseReference *reader = refs.two ;
@@ -2708,7 +2709,7 @@ - (void)testServerValuesSetPriorityRemoteEvents {
2708
2709
@" Number should be no more than 2 seconds ago" );
2709
2710
}
2710
2711
2711
- - (void )testServerValuesUpdateRemoteEvents {
2712
+ - (void )testServerTimestampUpdateRemoteEvents {
2712
2713
FTupleFirebase *refs = [FTestHelpers getRandomNodePair ];
2713
2714
FIRDatabaseReference *writer = refs.one ;
2714
2715
FIRDatabaseReference *reader = refs.two ;
@@ -2738,7 +2739,7 @@ - (void)testServerValuesUpdateRemoteEvents {
2738
2739
@" Number should be no more than 2 seconds ago" );
2739
2740
}
2740
2741
2741
- - (void )testServerValuesSetWithPriorityLocalEvents {
2742
+ - (void )testServerTimestampSetWithPriorityLocalEvents {
2742
2743
FIRDatabaseReference *node = [FTestHelpers getRandomNode ];
2743
2744
2744
2745
NSDictionary *data = @{
@@ -2783,7 +2784,7 @@ - (void)testServerValuesSetWithPriorityLocalEvents {
2783
2784
}];
2784
2785
}
2785
2786
2786
- - (void )testServerValuesSetPriorityLocalEvents {
2787
+ - (void )testServerTimestampSetPriorityLocalEvents {
2787
2788
FIRDatabaseReference *node = [FTestHelpers getRandomNode ];
2788
2789
2789
2790
__block FIRDataSnapshot *snap = nil ;
@@ -2812,7 +2813,7 @@ - (void)testServerValuesSetPriorityLocalEvents {
2812
2813
@" Number should be no more than 2 seconds ago" );
2813
2814
}
2814
2815
2815
- - (void )testServerValuesUpdateLocalEvents {
2816
+ - (void )testServerTimestampUpdateLocalEvents {
2816
2817
FIRDatabaseReference *node1 = [FTestHelpers getRandomNode ];
2817
2818
2818
2819
__block FIRDataSnapshot *snap1 = nil ;
@@ -2849,7 +2850,7 @@ - (void)testServerValuesUpdateLocalEvents {
2849
2850
@" Number should be no more than 2 seconds ago" );
2850
2851
}
2851
2852
2852
- - (void )testServerValuesTransactionLocalEvents {
2853
+ - (void )testServerTimestampTransactionLocalEvents {
2853
2854
FIRDatabaseReference *node = [FTestHelpers getRandomNode ];
2854
2855
2855
2856
__block FIRDataSnapshot *snap = nil ;
@@ -2873,6 +2874,137 @@ - (void)testServerValuesTransactionLocalEvents {
2873
2874
@" Number should be no more than 2 seconds ago" );
2874
2875
}
2875
2876
2877
+ - (void )testServerIncrementOverwritesExistingData {
2878
+ FIRDatabaseReference *ref = [FTestHelpers getRandomNode ];
2879
+ __block NSMutableArray *found = [NSMutableArray new ];
2880
+ NSMutableArray *expected = [NSMutableArray new ];
2881
+ [ref observeEventType: FIRDataEventTypeValue
2882
+ withBlock: ^(FIRDataSnapshot *snap) {
2883
+ [found addObject: snap.value];
2884
+ }];
2885
+
2886
+ // Going offline ensures that local events get queued up before server events
2887
+ [ref.repo interrupt ];
2888
+
2889
+ // null + incr
2890
+ [ref setValue: [FIRServerValue increment: @1 ]];
2891
+ [expected addObject: @1 ];
2892
+
2893
+ // number + incr
2894
+ [ref setValue: @5 ];
2895
+ [ref setValue: [FIRServerValue increment: @1 ]];
2896
+ [expected addObject: @5 ];
2897
+ [expected addObject: @6 ];
2898
+
2899
+ // string + incr
2900
+ [ref setValue: @" hello" ];
2901
+ [ref setValue: [FIRServerValue increment: @1 ]];
2902
+ [expected addObject: @" hello" ];
2903
+ [expected addObject: @1 ];
2904
+
2905
+ // object + incr
2906
+ [ref setValue: @{@" hello" : @" world" }];
2907
+ [ref setValue: [FIRServerValue increment: @1 ]];
2908
+ [expected addObject: @{@" hello" : @" world" }];
2909
+ [expected addObject: @1 ];
2910
+
2911
+ [self waitUntil: ^BOOL {
2912
+ return found.count == expected.count ;
2913
+ }];
2914
+ XCTAssertEqualObjects (expected, found);
2915
+ [ref.repo resume ];
2916
+ }
2917
+
2918
+ - (void )testServerIncrementPriority {
2919
+ FIRDatabaseReference *ref = [FTestHelpers getRandomNode ];
2920
+ __block NSMutableArray *found = [NSMutableArray new ];
2921
+ NSMutableArray *expected = [NSMutableArray new ];
2922
+ [ref observeEventType: FIRDataEventTypeValue
2923
+ withBlock: ^(FIRDataSnapshot *snap) {
2924
+ [found addObject: snap.priority];
2925
+ }];
2926
+
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
+
2931
+ // null + incr
2932
+ [ref setValue: @0 andPriority: [FIRServerValue increment: @1 ]];
2933
+ [expected addObject: @1 ];
2934
+ [ref setValue: @0 andPriority: [FIRServerValue increment: @1.5 ]];
2935
+ [expected addObject: @2.5 ];
2936
+
2937
+ [self waitUntil: ^BOOL {
2938
+ return found.count == expected.count ;
2939
+ }];
2940
+ XCTAssertEqualObjects (expected, found);
2941
+ [ref.repo resume ];
2942
+ }
2943
+
2944
+ - (void )testServerIncrementOverflowAndTypeCoercion {
2945
+ FIRDatabaseReference *ref = [FTestHelpers getRandomNode ];
2946
+ __block NSMutableArray *found = [NSMutableArray new ];
2947
+ __block NSMutableArray *foundTypes = [NSMutableArray new ];
2948
+ NSMutableArray *expected = [NSMutableArray new ];
2949
+ NSMutableArray *expectedTypes = [NSMutableArray new ];
2950
+ [ref observeEventType: FIRDataEventTypeValue
2951
+ withBlock: ^(FIRDataSnapshot *snap) {
2952
+ [found addObject: snap.value];
2953
+ [foundTypes addObject: @([(NSNumber *)snap.value objCType ])];
2954
+ }];
2955
+
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
+
2960
+ // long + double = double
2961
+ [ref setValue: @1 ];
2962
+ [ref setValue: [FIRServerValue increment: @1.0 ]];
2963
+ [expected addObject: @1 ];
2964
+ [expected addObject: @2.0 ];
2965
+ [expectedTypes addObject: @(@encode (int ))];
2966
+ [expectedTypes addObject: @(@encode (double ))];
2967
+
2968
+ // double + long = double
2969
+ [ref setValue: @1.5 ];
2970
+ [ref setValue: [FIRServerValue increment: @1 ]];
2971
+ [expected addObject: @1.5 ];
2972
+ [expected addObject: @2.5 ];
2973
+ [expectedTypes addObject: @(@encode (double ))];
2974
+ [expectedTypes addObject: @(@encode (double ))];
2975
+
2976
+ // long overflow = double
2977
+ [ref setValue: @(1 )];
2978
+ [ref setValue: [FIRServerValue increment: @(LONG_MAX)]];
2979
+ [expected addObject: @(1 )];
2980
+ [expected addObject: @(LONG_MAX + 1.0 )];
2981
+ [expectedTypes addObject: @(@encode (int ))];
2982
+ [expectedTypes addObject: @(@encode (double ))];
2983
+
2984
+ // unsigned long long overflow = double
2985
+ [ref setValue: @1 ];
2986
+ [ref setValue: [FIRServerValue increment: @((unsigned long long )ULLONG_MAX)]];
2987
+ [expected addObject: @1 ];
2988
+ [expected addObject: @((double )ULLONG_MAX + 1 )];
2989
+ [expectedTypes addObject: @(@encode (int ))];
2990
+ [expectedTypes addObject: @(@encode (double ))];
2991
+
2992
+ // long underflow = double
2993
+ [ref setValue: @(-1 )];
2994
+ [ref setValue: [FIRServerValue increment: @(LONG_MIN)]];
2995
+ [expected addObject: @(-1 )];
2996
+ [expected addObject: @(LONG_MIN - 1.0 )];
2997
+ [expectedTypes addObject: @(@encode (int ))];
2998
+ [expectedTypes addObject: @(@encode (double ))];
2999
+
3000
+ [self waitUntil: ^BOOL {
3001
+ return found.count == expected.count && foundTypes.count == expectedTypes.count ;
3002
+ }];
3003
+ XCTAssertEqualObjects (expectedTypes, foundTypes);
3004
+ XCTAssertEqualObjects (expected, found);
3005
+ [ref.repo resume ];
3006
+ }
3007
+
2876
3008
- (void )testUpdateAfterChildSet {
2877
3009
FIRDatabaseReference *node = [FTestHelpers getRandomNode ];
2878
3010
@@ -2949,7 +3081,7 @@ - (void)testDeltaSyncNoDataUpdatesAfterReconnect {
2949
3081
[FRepoManager disposeRepos: cfg];
2950
3082
}
2951
3083
2952
- - (void )testServerValuesEventualConsistencyBetweenLocalAndRemote {
3084
+ - (void )testServerTimestampEventualConsistencyBetweenLocalAndRemote {
2953
3085
FTupleFirebase *refs = [FTestHelpers getRandomNodePair ];
2954
3086
FIRDatabaseReference *writer = refs.one ;
2955
3087
FIRDatabaseReference *reader = refs.two ;
0 commit comments