|
| 1 | +/* |
| 2 | + * Copyright 2018 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import <FirebaseFirestore/FirebaseFirestore.h> |
| 18 | + |
| 19 | +#import <XCTest/XCTest.h> |
| 20 | + |
| 21 | +#import "Firestore/Example/Tests/Util/FSTEventAccumulator.h" |
| 22 | +#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h" |
| 23 | + |
| 24 | +/** |
| 25 | + * Note: Transforms are tested pretty thoroughly in FIRServerTimestampTests (via set, update, |
| 26 | + * transactions, nested in documents, multiple transforms together, etc.) and so these tests |
| 27 | + * mostly focus on the array transform semantics. |
| 28 | + */ |
| 29 | +@interface FIRArrayTransformTests : FSTIntegrationTestCase |
| 30 | +@end |
| 31 | + |
| 32 | +@implementation FIRArrayTransformTests { |
| 33 | + // A document reference to read and write to. |
| 34 | + FIRDocumentReference *_docRef; |
| 35 | + |
| 36 | + // Accumulator used to capture events during the test. |
| 37 | + FSTEventAccumulator *_accumulator; |
| 38 | + |
| 39 | + // Listener registration for a listener maintained during the course of the test. |
| 40 | + id<FIRListenerRegistration> _listenerRegistration; |
| 41 | +} |
| 42 | + |
| 43 | +- (void)setUp { |
| 44 | + [super setUp]; |
| 45 | + |
| 46 | + _docRef = [self documentRef]; |
| 47 | + _accumulator = [FSTEventAccumulator accumulatorForTest:self]; |
| 48 | + _listenerRegistration = |
| 49 | + [_docRef addSnapshotListenerWithIncludeMetadataChanges:YES |
| 50 | + listener:_accumulator.valueEventHandler]; |
| 51 | + |
| 52 | + // Wait for initial nil snapshot to avoid potential races. |
| 53 | + FIRDocumentSnapshot *initialSnapshot = [_accumulator awaitEventWithName:@"initial event"]; |
| 54 | + XCTAssertFalse(initialSnapshot.exists); |
| 55 | +} |
| 56 | + |
| 57 | +- (void)tearDown { |
| 58 | + [_listenerRegistration remove]; |
| 59 | + |
| 60 | + [super tearDown]; |
| 61 | +} |
| 62 | + |
| 63 | +#pragma mark - Test Helpers |
| 64 | + |
| 65 | +/** Waits for a snapshot with local writes. */ |
| 66 | +- (FIRDocumentSnapshot *)waitForLocalEvent { |
| 67 | + FIRDocumentSnapshot *snapshot; |
| 68 | + do { |
| 69 | + snapshot = [_accumulator awaitEventWithName:@"Local event."]; |
| 70 | + } while (!snapshot.metadata.hasPendingWrites); |
| 71 | + return snapshot; |
| 72 | +} |
| 73 | + |
| 74 | +/** Waits for a snapshot that has no pending writes */ |
| 75 | +- (FIRDocumentSnapshot *)waitForRemoteEvent { |
| 76 | + FIRDocumentSnapshot *snapshot; |
| 77 | + do { |
| 78 | + snapshot = [_accumulator awaitEventWithName:@"Remote event."]; |
| 79 | + } while (snapshot.metadata.hasPendingWrites); |
| 80 | + return snapshot; |
| 81 | +} |
| 82 | + |
| 83 | +/** Writes some initial data and consumes the events generated. */ |
| 84 | +- (void)writeInitialData:(NSDictionary<NSString *, id> *)data { |
| 85 | + [self writeDocumentRef:_docRef data:data]; |
| 86 | + XCTAssertEqualObjects([self waitForLocalEvent].data, data); |
| 87 | + XCTAssertEqualObjects([self waitForRemoteEvent].data, data); |
| 88 | +} |
| 89 | + |
| 90 | +#pragma mark - Test Cases |
| 91 | + |
| 92 | +- (void)testCreateDocumentWithArrayUnion { |
| 93 | + [self writeDocumentRef:_docRef |
| 94 | + data:@{ |
| 95 | + @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]] |
| 96 | + }]; |
| 97 | + id expected = @{ @"array" : @[ @1, @2 ] }; |
| 98 | + XCTAssertEqualObjects([self waitForLocalEvent].data, expected); |
| 99 | + XCTAssertEqualObjects([self waitForRemoteEvent].data, expected); |
| 100 | +} |
| 101 | + |
| 102 | +- (void)testAppendToArrayViaUpdate { |
| 103 | + [self writeInitialData:@{ @"array" : @[ @1, @3 ] }]; |
| 104 | + |
| 105 | + [self updateDocumentRef:_docRef |
| 106 | + data:@{ |
| 107 | + @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @1, @4 ]] |
| 108 | + }]; |
| 109 | + |
| 110 | + id expected = @{ @"array" : @[ @1, @3, @2, @4 ] }; |
| 111 | + XCTAssertEqualObjects([self waitForLocalEvent].data, expected); |
| 112 | + XCTAssertEqualObjects([self waitForRemoteEvent].data, expected); |
| 113 | +} |
| 114 | + |
| 115 | +- (void)testAppendToArrayViaMergeSet { |
| 116 | + [self writeInitialData:@{ @"array" : @[ @1, @3 ] }]; |
| 117 | + |
| 118 | + [self mergeDocumentRef:_docRef |
| 119 | + data:@{ |
| 120 | + @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @1, @4 ]] |
| 121 | + }]; |
| 122 | + |
| 123 | + id expected = @{ @"array" : @[ @1, @3, @2, @4 ] }; |
| 124 | + XCTAssertEqualObjects([self waitForLocalEvent].data, expected); |
| 125 | + XCTAssertEqualObjects([self waitForRemoteEvent].data, expected); |
| 126 | +} |
| 127 | + |
| 128 | +- (void)testAppendObjectToArrayViaUpdate { |
| 129 | + [self writeInitialData:@{ @"array" : @[ @{@"a" : @"hi"} ] }]; |
| 130 | + |
| 131 | + [self updateDocumentRef:_docRef |
| 132 | + data:@{ |
| 133 | + @"array" : [FIRFieldValue |
| 134 | + fieldValueForArrayUnion:@[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ]] |
| 135 | + }]; |
| 136 | + |
| 137 | + id expected = @{ @"array" : @[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ] }; |
| 138 | + XCTAssertEqualObjects([self waitForLocalEvent].data, expected); |
| 139 | + XCTAssertEqualObjects([self waitForRemoteEvent].data, expected); |
| 140 | +} |
| 141 | + |
| 142 | +- (void)testRemoveFromArrayViaUpdate { |
| 143 | + [self writeInitialData:@{ @"array" : @[ @1, @3, @1, @3 ] }]; |
| 144 | + |
| 145 | + [self updateDocumentRef:_docRef |
| 146 | + data:@{ |
| 147 | + @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @4 ]] |
| 148 | + }]; |
| 149 | + |
| 150 | + id expected = @{ @"array" : @[ @3, @3 ] }; |
| 151 | + XCTAssertEqualObjects([self waitForLocalEvent].data, expected); |
| 152 | + XCTAssertEqualObjects([self waitForRemoteEvent].data, expected); |
| 153 | +} |
| 154 | + |
| 155 | +- (void)testRemoveFromArrayViaMergeSet { |
| 156 | + [self writeInitialData:@{ @"array" : @[ @1, @3, @1, @3 ] }]; |
| 157 | + |
| 158 | + [self mergeDocumentRef:_docRef |
| 159 | + data:@{ |
| 160 | + @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @4 ]] |
| 161 | + }]; |
| 162 | + |
| 163 | + id expected = @{ @"array" : @[ @3, @3 ] }; |
| 164 | + XCTAssertEqualObjects([self waitForLocalEvent].data, expected); |
| 165 | + XCTAssertEqualObjects([self waitForRemoteEvent].data, expected); |
| 166 | +} |
| 167 | + |
| 168 | +- (void)testRemoveObjectFromArrayViaUpdate { |
| 169 | + [self writeInitialData:@{ @"array" : @[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ] }]; |
| 170 | + |
| 171 | + [self updateDocumentRef:_docRef |
| 172 | + data:@{ |
| 173 | + @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @{@"a" : @"hi"} ]] |
| 174 | + }]; |
| 175 | + |
| 176 | + id expected = @{ @"array" : @[ @{@"a" : @"bye"} ] }; |
| 177 | + XCTAssertEqualObjects([self waitForLocalEvent].data, expected); |
| 178 | + XCTAssertEqualObjects([self waitForRemoteEvent].data, expected); |
| 179 | +} |
| 180 | + |
| 181 | +@end |
| 182 | + |
| 183 | +/** |
| 184 | + * Unlike the FIRArrayTransformTests above, these tests intentionally avoid having any ongoing |
| 185 | + * listeners so that we can test what gets stored in the offline cache based purely on the write |
| 186 | + * acknowledgement (without receiving an updated document via watch). As such they also rely on |
| 187 | + * persistence being enabled so documents remain in the cache after the write. |
| 188 | + */ |
| 189 | +@interface FIRArrayTransformServerApplicationTests : FSTIntegrationTestCase |
| 190 | +@end |
| 191 | + |
| 192 | +@implementation FIRArrayTransformServerApplicationTests { |
| 193 | + // A document reference to read and write to. |
| 194 | + FIRDocumentReference *_docRef; |
| 195 | +} |
| 196 | + |
| 197 | +- (void)setUp { |
| 198 | + [super setUp]; |
| 199 | + |
| 200 | + _docRef = [self documentRef]; |
| 201 | +} |
| 202 | + |
| 203 | +/** |
| 204 | + * Helper that uses a temporary listener to read from cache (returning nil if no document seems |
| 205 | + * to be in cache). Can probably be replaced with get(source=cache) in the future. |
| 206 | + */ |
| 207 | +- (FIRDocumentSnapshot *_Nullable)getFromCache { |
| 208 | + FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self]; |
| 209 | + id<FIRListenerRegistration> listenerRegistration = |
| 210 | + [_docRef addSnapshotListener:accumulator.valueEventHandler]; |
| 211 | + FIRDocumentSnapshot *snapshot = [accumulator awaitEventWithName:@"listenForOneEvent"]; |
| 212 | + [listenerRegistration remove]; |
| 213 | + if (snapshot.metadata.fromCache) { |
| 214 | + return snapshot; |
| 215 | + } else { |
| 216 | + return nil; |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +- (void)testServerApplicationOfSetWithNoCachedBaseDoc { |
| 221 | + [self writeDocumentRef:_docRef |
| 222 | + data:@{ |
| 223 | + @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]] |
| 224 | + }]; |
| 225 | + id expected = @{ @"array" : @[ @1, @2 ] }; |
| 226 | + XCTAssertEqualObjects([self getFromCache].data, expected); |
| 227 | +} |
| 228 | + |
| 229 | +- (void)testServerApplicationOfUpdateWithNoCachedBaseDoc { |
| 230 | + // Write an initial document out-of-band so it's not in our cache |
| 231 | + [self writeDocumentRef:[[self firestore] documentWithPath:_docRef.path] |
| 232 | + data:@{ |
| 233 | + @"array" : @[ @42 ] |
| 234 | + }]; |
| 235 | + |
| 236 | + [self updateDocumentRef:_docRef |
| 237 | + data:@{ |
| 238 | + @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]] |
| 239 | + }]; |
| 240 | + |
| 241 | + // Nothing should be cached since it was an update and we had no base doc. |
| 242 | + XCTAssertNil([self getFromCache]); |
| 243 | +} |
| 244 | + |
| 245 | +- (void)testServerApplicationOfMergeSetWithNoCachedBaseDoc { |
| 246 | + // Write an initial document out-of-band so it's not in our cache |
| 247 | + [self writeDocumentRef:[[self firestore] documentWithPath:_docRef.path] |
| 248 | + data:@{ |
| 249 | + @"array" : @[ @42 ] |
| 250 | + }]; |
| 251 | + |
| 252 | + [self mergeDocumentRef:_docRef |
| 253 | + data:@{ |
| 254 | + @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]] |
| 255 | + }]; |
| 256 | + |
| 257 | + // Document will be cached but we'll be missing 42. |
| 258 | + id expected = @{ @"array" : @[ @1, @2 ] }; |
| 259 | + XCTAssertEqualObjects([self getFromCache].data, expected); |
| 260 | +} |
| 261 | + |
| 262 | +- (void)testServerApplicationOfArrayUnionUpdateWithCachedBaseDoc { |
| 263 | + // Cache a document with an array. |
| 264 | + [self writeDocumentRef:_docRef data:@{ @"array" : @[ @42 ] }]; |
| 265 | + |
| 266 | + [self updateDocumentRef:_docRef |
| 267 | + data:@{ |
| 268 | + @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]] |
| 269 | + }]; |
| 270 | + |
| 271 | + // Should have merged the update with the cached doc. |
| 272 | + id expected = @{ @"array" : @[ @42, @1, @2 ] }; |
| 273 | + XCTAssertEqualObjects([self getFromCache].data, expected); |
| 274 | +} |
| 275 | + |
| 276 | +- (void)testServerApplicationOfArrayRemoveUpdateWithCachedBaseDoc { |
| 277 | + // Cache a document with an array. |
| 278 | + [self writeDocumentRef:_docRef data:@{ @"array" : @[ @42, @1, @2 ] }]; |
| 279 | + |
| 280 | + [self updateDocumentRef:_docRef |
| 281 | + data:@{ |
| 282 | + @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @2 ]] |
| 283 | + }]; |
| 284 | + |
| 285 | + // Should have merged the update with the cached doc. |
| 286 | + id expected = @{ @"array" : @[ @42 ] }; |
| 287 | + XCTAssertEqualObjects([self getFromCache].data, expected); |
| 288 | +} |
| 289 | +@end |
0 commit comments