Skip to content

Commit 36fc2da

Browse files
committed
Add FSTTestDocumentChange
To help to test FIRDocumentChanges returned via the getDocuments call.
1 parent 893e56a commit 36fc2da

File tree

5 files changed

+141
-41
lines changed

5 files changed

+141
-41
lines changed

Firestore/Example/Tests/Integration/API/FIRGetOptionsTests.m

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#import <XCTest/XCTest.h>
2020

21+
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2122
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
2223
#import "Firestore/Source/API/FIRFirestore+Internal.h"
2324
#import "Firestore/Source/Core/FSTFirestoreClient.h"
@@ -62,11 +63,18 @@ - (void)testGetCollectionWhileOnlineWithDefaultGetOptions {
6263
XCTAssertEqualObjects(
6364
FIRQuerySnapshotGetData(result),
6465
(@[ @{@"key1" : @"value1"}, @{@"key2" : @"value2"}, @{@"key3" : @"value3"} ]));
65-
XCTAssertEqualObjects(FIRQuerySnapshotGetDocChangesData(result), (@[
66-
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ],
67-
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2"} ],
68-
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3" : @"value3"} ]
69-
]));
66+
XCTAssertEqualObjects(
67+
FIRQuerySnapshotGetDocChangesData(result), (@[
68+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
69+
name:@"doc1"
70+
data:@{@"key1" : @"value1"}],
71+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
72+
name:@"doc2"
73+
data:@{@"key2" : @"value2"}],
74+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
75+
name:@"doc3"
76+
data:@{@"key3" : @"value3"}]
77+
]));
7078
}
7179

7280
- (void)testGetDocumentWhileOfflineWithDefaultGetOptions {
@@ -128,10 +136,19 @@ - (void)testGetCollectionWhileOfflineWithDefaultGetOptions {
128136
]));
129137
XCTAssertEqualObjects(
130138
FIRQuerySnapshotGetDocChangesData(result), (@[
131-
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ],
132-
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2", @"key2b" : @"value2b"} ],
133-
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3b" : @"value3b"} ],
134-
@[ @(FIRDocumentChangeTypeAdded), @"doc4", @{@"key4" : @"value4"} ]
139+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
140+
name:@"doc1"
141+
data:@{@"key1" : @"value1"}],
142+
[[FSTTestDocumentChange alloc]
143+
initWithChangeType:FIRDocumentChangeTypeAdded
144+
name:@"doc2"
145+
data:@{@"key2" : @"value2", @"key2b" : @"value2b"}],
146+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
147+
name:@"doc3"
148+
data:@{@"key3b" : @"value3b"}],
149+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
150+
name:@"doc4"
151+
data:@{@"key4" : @"value4"}]
135152
]));
136153
}
137154

@@ -176,11 +193,18 @@ - (void)testGetCollectionWhileOnlineCacheOnly {
176193
@{@"key2" : @"value2"},
177194
@{@"key3" : @"value3"},
178195
]));
179-
XCTAssertEqualObjects(FIRQuerySnapshotGetDocChangesData(result), (@[
180-
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ],
181-
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2"} ],
182-
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3" : @"value3"} ]
183-
]));
196+
XCTAssertEqualObjects(
197+
FIRQuerySnapshotGetDocChangesData(result), (@[
198+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
199+
name:@"doc1"
200+
data:@{@"key1" : @"value1"}],
201+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
202+
name:@"doc2"
203+
data:@{@"key2" : @"value2"}],
204+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
205+
name:@"doc3"
206+
data:@{@"key3" : @"value3"}]
207+
]));
184208
}
185209

186210
- (void)testGetDocumentWhileOfflineCacheOnly {
@@ -247,10 +271,19 @@ - (void)testGetCollectionWhileOfflineCacheOnly {
247271
]));
248272
XCTAssertEqualObjects(
249273
FIRQuerySnapshotGetDocChangesData(result), (@[
250-
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ],
251-
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2", @"key2b" : @"value2b"} ],
252-
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3b" : @"value3b"} ],
253-
@[ @(FIRDocumentChangeTypeAdded), @"doc4", @{@"key4" : @"value4"} ]
274+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
275+
name:@"doc1"
276+
data:@{@"key1" : @"value1"}],
277+
[[FSTTestDocumentChange alloc]
278+
initWithChangeType:FIRDocumentChangeTypeAdded
279+
name:@"doc2"
280+
data:@{@"key2" : @"value2", @"key2b" : @"value2b"}],
281+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
282+
name:@"doc3"
283+
data:@{@"key3b" : @"value3b"}],
284+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
285+
name:@"doc4"
286+
data:@{@"key4" : @"value4"}]
254287
]));
255288
}
256289

@@ -295,11 +328,18 @@ - (void)testGetCollectionWhileOnlineServerOnly {
295328
@{@"key2" : @"value2"},
296329
@{@"key3" : @"value3"},
297330
]));
298-
XCTAssertEqualObjects(FIRQuerySnapshotGetDocChangesData(result), (@[
299-
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ],
300-
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2"} ],
301-
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3" : @"value3"} ]
302-
]));
331+
XCTAssertEqualObjects(
332+
FIRQuerySnapshotGetDocChangesData(result), (@[
333+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
334+
name:@"doc1"
335+
data:@{@"key1" : @"value1"}],
336+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
337+
name:@"doc2"
338+
data:@{@"key2" : @"value2"}],
339+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
340+
name:@"doc3"
341+
data:@{@"key3" : @"value3"}]
342+
]));
303343
}
304344

305345
- (void)testGetDocumentWhileOfflineServerOnly {
@@ -450,10 +490,19 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions {
450490
]));
451491
XCTAssertEqualObjects(
452492
FIRQuerySnapshotGetDocChangesData(result), (@[
453-
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ],
454-
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2", @"key2b" : @"value2b"} ],
455-
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3b" : @"value3b"} ],
456-
@[ @(FIRDocumentChangeTypeAdded), @"doc4", @{@"key4" : @"value4"} ]
493+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
494+
name:@"doc1"
495+
data:@{@"key1" : @"value1"}],
496+
[[FSTTestDocumentChange alloc]
497+
initWithChangeType:FIRDocumentChangeTypeAdded
498+
name:@"doc2"
499+
data:@{@"key2" : @"value2", @"key2b" : @"value2b"}],
500+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
501+
name:@"doc3"
502+
data:@{@"key3b" : @"value3b"}],
503+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
504+
name:@"doc4"
505+
data:@{@"key4" : @"value4"}]
457506
]));
458507

459508
// attempt to get docs (with default get options)
@@ -466,10 +515,19 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions {
466515
]));
467516
XCTAssertEqualObjects(
468517
FIRQuerySnapshotGetDocChangesData(result), (@[
469-
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ],
470-
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2", @"key2b" : @"value2b"} ],
471-
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3b" : @"value3b"} ],
472-
@[ @(FIRDocumentChangeTypeAdded), @"doc4", @{@"key4" : @"value4"} ]
518+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
519+
name:@"doc1"
520+
data:@{@"key1" : @"value1"}],
521+
[[FSTTestDocumentChange alloc]
522+
initWithChangeType:FIRDocumentChangeTypeAdded
523+
name:@"doc2"
524+
data:@{@"key2" : @"value2", @"key2b" : @"value2b"}],
525+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
526+
name:@"doc3"
527+
data:@{@"key3b" : @"value3b"}],
528+
[[FSTTestDocumentChange alloc] initWithChangeType:FIRDocumentChangeTypeAdded
529+
name:@"doc4"
530+
data:@{@"key4" : @"value4"}]
473531
]));
474532

475533
// attempt to get docs (from the server) and ensure they cannot be retreived

Firestore/Example/Tests/Util/FSTHelpers.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19+
#import "FIRDocumentChange.h"
1920
#import "Firestore/Source/Core/FSTTypes.h"
2021
#import "Firestore/Source/Model/FSTDocumentDictionary.h"
2122
#import "Firestore/Source/Model/FSTDocumentKeySet.h"
@@ -132,6 +133,16 @@ extern "C" {
132133
XCTAssertTrue(__didThrow, ##__VA_ARGS__); \
133134
})
134135

136+
/** A convenience object for creating doc changes for tests. */
137+
@interface FSTTestDocumentChange : NSObject
138+
@property(nonatomic, readonly) FIRDocumentChangeType changeType;
139+
@property(nonatomic, strong, readonly) NSString *name;
140+
@property(nonatomic, strong, readonly) NSDictionary<NSString *, id> *data;
141+
- (instancetype)initWithChangeType:(FIRDocumentChangeType)changeType
142+
name:(NSString *)name
143+
data:(NSDictionary<NSString *, id> *)data;
144+
@end
145+
135146
/** Creates a new FSTTimestamp from components. Note that year, month, and day are all one-based. */
136147
FSTTimestamp *FSTTestTimestamp(int year, int month, int day, int hour, int minute, int second);
137148

Firestore/Example/Tests/Util/FSTHelpers.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@
4646
static const int kMicrosPerSec = 1000000;
4747
static const int kMillisPerSec = 1000;
4848

49+
@implementation FSTTestDocumentChange
50+
51+
- (instancetype)initWithChangeType:(FIRDocumentChangeType)changeType
52+
name:(NSString *)name
53+
data:(NSDictionary<NSString *, id> *)data {
54+
self = [super init];
55+
if (self) {
56+
_changeType = changeType;
57+
_name = name;
58+
_data = data;
59+
}
60+
return self;
61+
}
62+
63+
- (BOOL)isEqual:(nullable id)other {
64+
if (other == self) return YES;
65+
if (![[other class] isEqual:[self class]]) return NO;
66+
67+
return [self isEqualToReference:other];
68+
}
69+
70+
- (BOOL)isEqualToReference:(nullable FSTTestDocumentChange *)reference {
71+
if (self == reference) return YES;
72+
if (reference == nil) return NO;
73+
return _changeType == reference.changeType && [_name isEqualToString:reference.name] &&
74+
[_data isEqualToDictionary:reference.data];
75+
}
76+
77+
@end // FSTTestDocumentChange
78+
4979
FSTTimestamp *FSTTestTimestamp(int year, int month, int day, int hour, int minute, int second) {
5080
NSDate *date = FSTTestDate(year, month, day, hour, minute, second);
5181
return [FSTTimestamp timestampWithDate:date];

Firestore/Example/Tests/Util/FSTIntegrationTestCase.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
@class FIRFirestoreSettings;
3030
@class FIRQuery;
3131
@class FSTEventAccumulator;
32+
@class FSTTestDocumentChange;
3233

3334
NS_ASSUME_NONNULL_BEGIN
3435

@@ -111,9 +112,8 @@ NSArray<NSDictionary<NSString *, id> *> *FIRQuerySnapshotGetData(FIRQuerySnapsho
111112
/** Converts the FIRQuerySnapshot to an NSArray containing the document IDs in order. */
112113
NSArray<NSString *> *FIRQuerySnapshotGetIDs(FIRQuerySnapshot *docs);
113114

114-
/** Converts the FIRQuerySnapshot to an NSArray containing an NSArray containing the doc change data
115-
* in order of { type, doc title, doc data }. */
116-
NSArray<NSArray<id> *> *FIRQuerySnapshotGetDocChangesData(FIRQuerySnapshot *docs);
115+
/** Converts the FIRQuerySnapshot to an NSArray containing FSTTestDocumentChange objects. */
116+
NSArray<FSTTestDocumentChange *> *FIRQuerySnapshotGetDocChangesData(FIRQuerySnapshot *docs);
117117

118118
#if __cplusplus
119119
} // extern "C"

Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#import "Firestore/Source/Util/FSTDispatchQueue.h"
3232

3333
#import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
34+
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
3435
#import "Firestore/Example/Tests/Util/FSTTestDispatchQueue.h"
3536

3637
using firebase::firestore::util::CreateAutoId;
@@ -325,14 +326,14 @@ - (void)waitUntil:(BOOL (^)())predicate {
325326
return result;
326327
}
327328

328-
extern "C" NSArray<NSArray<id> *> *FIRQuerySnapshotGetDocChangesData(FIRQuerySnapshot *docs) {
329-
NSMutableArray<NSMutableArray<id> *> *result = [NSMutableArray array];
329+
extern "C" NSArray<FSTTestDocumentChange *> *FIRQuerySnapshotGetDocChangesData(
330+
FIRQuerySnapshot *docs) {
331+
NSMutableArray<FSTTestDocumentChange *> *result = [NSMutableArray array];
330332
for (FIRDocumentChange *docChange in docs.documentChanges) {
331-
NSMutableArray<id> *docChangeData = [NSMutableArray array];
332-
[docChangeData addObject:@(docChange.type)];
333-
[docChangeData addObject:docChange.document.documentID];
334-
[docChangeData addObject:docChange.document.data];
335-
[result addObject:docChangeData];
333+
[result
334+
addObject:[[FSTTestDocumentChange alloc] initWithChangeType:docChange.type
335+
name:docChange.document.documentID
336+
data:docChange.document.data]];
336337
}
337338
return result;
338339
}

0 commit comments

Comments
 (0)