aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Example/Tests/Integration/API/FIRArrayTransformTests.mm
blob: 0a5b4135fdcd9599b5438665522252e3e64078e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright 2018 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 <FirebaseFirestore/FirebaseFirestore.h>

#import <XCTest/XCTest.h>

#import "Firestore/Source/API/FIRFieldValue+Internal.h"

#import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"

/**
 * Note: Transforms are tested pretty thoroughly in FIRServerTimestampTests (via set, update,
 * transactions, nested in documents, multiple transforms together, etc.) and so these tests
 * mostly focus on the array transform semantics.
 */
@interface FIRArrayTransformTests : FSTIntegrationTestCase
@end

@implementation FIRArrayTransformTests {
  // A document reference to read and write to.
  FIRDocumentReference *_docRef;

  // Accumulator used to capture events during the test.
  FSTEventAccumulator<FIRDocumentSnapshot *> *_accumulator;

  // Listener registration for a listener maintained during the course of the test.
  id<FIRListenerRegistration> _listenerRegistration;
}

- (void)setUp {
  [super setUp];

  _docRef = [self documentRef];
  _accumulator = [FSTEventAccumulator accumulatorForTest:self];
  _listenerRegistration =
      [_docRef addSnapshotListenerWithIncludeMetadataChanges:YES
                                                    listener:_accumulator.valueEventHandler];

  // Wait for initial nil snapshot to avoid potential races.
  FIRDocumentSnapshot *initialSnapshot = [_accumulator awaitEventWithName:@"initial event"];
  XCTAssertFalse(initialSnapshot.exists);
}

- (void)tearDown {
  [_listenerRegistration remove];

  [super tearDown];
}

#pragma mark - Test Helpers

/** Writes some initial data and consumes the events generated. */
- (void)writeInitialData:(NSDictionary<NSString *, id> *)data {
  [self writeDocumentRef:_docRef data:data];
  XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, data);
  XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, data);
}

#pragma mark - Test Cases

- (void)testCreateDocumentWithArrayUnion {
  [self writeDocumentRef:_docRef
                    data:@{
                      @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
                    }];
  id expected = @{ @"array" : @[ @1, @2 ] };
  XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
}

- (void)testAppendToArrayViaUpdate {
  [self writeInitialData:@{ @"array" : @[ @1, @3 ] }];

  [self updateDocumentRef:_docRef
                     data:@{
                       @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @1, @4 ]]
                     }];

  id expected = @{ @"array" : @[ @1, @3, @2, @4 ] };
  XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
}

- (void)testAppendToArrayViaMergeSet {
  [self writeInitialData:@{ @"array" : @[ @1, @3 ] }];

  [self mergeDocumentRef:_docRef
                    data:@{
                      @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @2, @1, @4 ]]
                    }];

  id expected = @{ @"array" : @[ @1, @3, @2, @4 ] };
  XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
}

- (void)testAppendObjectToArrayViaUpdate {
  [self writeInitialData:@{ @"array" : @[ @{@"a" : @"hi"} ] }];

  [self updateDocumentRef:_docRef
                     data:@{
                       @"array" : [FIRFieldValue
                           fieldValueForArrayUnion:@[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ]]
                     }];

  id expected = @{ @"array" : @[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ] };
  XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
}

- (void)testRemoveFromArrayViaUpdate {
  [self writeInitialData:@{ @"array" : @[ @1, @3, @1, @3 ] }];

  [self updateDocumentRef:_docRef
                     data:@{
                       @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @4 ]]
                     }];

  id expected = @{ @"array" : @[ @3, @3 ] };
  XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
}

- (void)testRemoveFromArrayViaMergeSet {
  [self writeInitialData:@{ @"array" : @[ @1, @3, @1, @3 ] }];

  [self mergeDocumentRef:_docRef
                    data:@{
                      @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @4 ]]
                    }];

  id expected = @{ @"array" : @[ @3, @3 ] };
  XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
}

- (void)testRemoveObjectFromArrayViaUpdate {
  [self writeInitialData:@{ @"array" : @[ @{@"a" : @"hi"}, @{@"a" : @"bye"} ] }];

  [self updateDocumentRef:_docRef
                     data:@{
                       @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @{@"a" : @"hi"} ]]
                     }];

  id expected = @{ @"array" : @[ @{@"a" : @"bye"} ] };
  XCTAssertEqualObjects([_accumulator awaitLocalEvent].data, expected);
  XCTAssertEqualObjects([_accumulator awaitRemoteEvent].data, expected);
}

@end

/**
 * Unlike the FIRArrayTransformTests above, these tests intentionally avoid having any ongoing
 * listeners so that we can test what gets stored in the offline cache based purely on the write
 * acknowledgement (without receiving an updated document via watch). As such they also rely on
 * persistence being enabled so documents remain in the cache after the write.
 */
@interface FIRArrayTransformServerApplicationTests : FSTIntegrationTestCase
@end

@implementation FIRArrayTransformServerApplicationTests {
  // A document reference to read and write to.
  FIRDocumentReference *_docRef;
}

- (void)setUp {
  [super setUp];

  _docRef = [self documentRef];
}

/**
 * Helper that uses a temporary listener to read from cache (returning nil if no document seems
 * to be in cache). Can probably be replaced with get(source=cache) in the future.
 */
- (FIRDocumentSnapshot *_Nullable)getFromCache {
  FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  id<FIRListenerRegistration> listenerRegistration =
      [_docRef addSnapshotListener:accumulator.valueEventHandler];
  FIRDocumentSnapshot *snapshot = [accumulator awaitEventWithName:@"listenForOneEvent"];
  [listenerRegistration remove];
  if (snapshot.metadata.fromCache) {
    return snapshot;
  } else {
    return nil;
  }
}

- (void)testServerApplicationOfSetWithNoCachedBaseDoc {
  [self writeDocumentRef:_docRef
                    data:@{
                      @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
                    }];
  id expected = @{ @"array" : @[ @1, @2 ] };
  XCTAssertEqualObjects([self getFromCache].data, expected);
}

- (void)testServerApplicationOfUpdateWithNoCachedBaseDoc {
  // Write an initial document out-of-band so it's not in our cache
  [self writeDocumentRef:[[self firestore] documentWithPath:_docRef.path]
                    data:@{
                      @"array" : @[ @42 ]
                    }];

  [self updateDocumentRef:_docRef
                     data:@{
                       @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
                     }];

  // Nothing should be cached since it was an update and we had no base doc.
  XCTAssertNil([self getFromCache]);
}

- (void)testServerApplicationOfMergeSetWithNoCachedBaseDoc {
  // Write an initial document out-of-band so it's not in our cache
  [self writeDocumentRef:[[self firestore] documentWithPath:_docRef.path]
                    data:@{
                      @"array" : @[ @42 ]
                    }];

  [self mergeDocumentRef:_docRef
                    data:@{
                      @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
                    }];

  // Document will be cached but we'll be missing 42.
  id expected = @{ @"array" : @[ @1, @2 ] };
  XCTAssertEqualObjects([self getFromCache].data, expected);
}

- (void)testServerApplicationOfArrayUnionUpdateWithCachedBaseDoc {
  // Cache a document with an array.
  [self writeDocumentRef:_docRef data:@{ @"array" : @[ @42 ] }];

  [self updateDocumentRef:_docRef
                     data:@{
                       @"array" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @2 ]]
                     }];

  // Should have merged the update with the cached doc.
  id expected = @{ @"array" : @[ @42, @1, @2 ] };
  XCTAssertEqualObjects([self getFromCache].data, expected);
}

- (void)testServerApplicationOfArrayRemoveUpdateWithCachedBaseDoc {
  // Cache a document with an array.
  [self writeDocumentRef:_docRef data:@{ @"array" : @[ @42, @1, @2 ] }];

  [self updateDocumentRef:_docRef
                     data:@{
                       @"array" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @2 ]]
                     }];

  // Should have merged the update with the cached doc.
  id expected = @{ @"array" : @[ @42 ] };
  XCTAssertEqualObjects([self getFromCache].data, expected);
}
@end