25
25
#import " Firestore/Source/Local/FSTQueryCache.h"
26
26
#import " Firestore/Source/Local/FSTQueryData.h"
27
27
#import " Firestore/Source/Model/FSTDocument.h"
28
- #import " Firestore/Source/Model/FSTDocumentKey.h"
29
28
#import " Firestore/Source/Model/FSTDocumentSet.h"
30
29
#import " Firestore/Source/Model/FSTMutation.h"
31
30
#import " Firestore/Source/Model/FSTMutationBatch.h"
40
39
#import " Firestore/third_party/Immutable/Tests/FSTImmutableSortedSet+Testing.h"
41
40
42
41
#include " Firestore/core/src/firebase/firestore/auth/user.h"
42
+ #include " Firestore/core/src/firebase/firestore/model/document_map.h"
43
43
#include " Firestore/core/test/firebase/firestore/testutil/testutil.h"
44
44
45
45
namespace testutil = firebase::firestore::testutil;
46
46
using firebase::firestore::auth::User;
47
+ using firebase::firestore::model::DocumentKey;
47
48
using firebase::firestore::model::DocumentKeySet;
48
49
using firebase::firestore::model::ListenSequenceNumber;
50
+ using firebase::firestore::model::DocumentMap;
51
+ using firebase::firestore::model::MaybeDocumentMap;
49
52
using firebase::firestore::model::SnapshotVersion;
50
53
using firebase::firestore::model::TargetId;
51
54
55
+ static NSArray <FSTDocument *> *docMapToArray (const DocumentMap &docs) {
56
+ NSMutableArray <FSTDocument *> *result = [NSMutableArray array ];
57
+ for (const auto &kv : docs.underlying_map ()) {
58
+ [result addObject: static_cast <FSTDocument *>(kv.second)];
59
+ }
60
+ return result;
61
+ }
62
+
52
63
NS_ASSUME_NONNULL_BEGIN
53
64
54
65
@interface FSTLocalStoreTests ()
@@ -57,12 +68,13 @@ @interface FSTLocalStoreTests ()
57
68
@property (nonatomic , strong , readwrite ) FSTLocalStore *localStore;
58
69
59
70
@property (nonatomic , strong , readonly ) NSMutableArray <FSTMutationBatch *> *batches;
60
- @property (nonatomic , strong , readwrite , nullable ) FSTMaybeDocumentDictionary *lastChanges;
61
71
@property (nonatomic , assign , readwrite ) TargetId lastTargetID;
62
72
63
73
@end
64
74
65
- @implementation FSTLocalStoreTests
75
+ @implementation FSTLocalStoreTests {
76
+ MaybeDocumentMap _lastChanges;
77
+ }
66
78
67
79
- (void )setUp {
68
80
[super setUp ];
@@ -78,7 +90,6 @@ - (void)setUp {
78
90
[self .localStore start ];
79
91
80
92
_batches = [NSMutableArray array ];
81
- _lastChanges = nil ;
82
93
_lastTargetID = 0 ;
83
94
}
84
95
@@ -115,11 +126,11 @@ - (void)writeMutations:(NSArray<FSTMutation *> *)mutations {
115
126
[self .batches addObject: [[FSTMutationBatch alloc ] initWithBatchID: result.batchID
116
127
localWriteTime: [FIRTimestamp timestamp ]
117
128
mutations: mutations]];
118
- self. lastChanges = result.changes ;
129
+ _lastChanges = result.changes ;
119
130
}
120
131
121
132
- (void )applyRemoteEvent : (FSTRemoteEvent *)event {
122
- self. lastChanges = [self .localStore applyRemoteEvent: event];
133
+ _lastChanges = [self .localStore applyRemoteEvent: event];
123
134
}
124
135
125
136
- (void )notifyLocalViewChanges : (FSTLocalViewChanges *)changes {
@@ -137,13 +148,13 @@ - (void)acknowledgeMutationWithVersion:(FSTTestSnapshotVersion)documentVersion {
137
148
commitVersion: version
138
149
mutationResults: @[ mutationResult ]
139
150
streamToken: nil ];
140
- self. lastChanges = [self .localStore acknowledgeBatchWithResult: result];
151
+ _lastChanges = [self .localStore acknowledgeBatchWithResult: result];
141
152
}
142
153
143
154
- (void )rejectMutation {
144
155
FSTMutationBatch *batch = [self .batches firstObject ];
145
156
[self .batches removeObjectAtIndex: 0 ];
146
- self. lastChanges = [self .localStore rejectBatchID: batch.batchID];
157
+ _lastChanges = [self .localStore rejectBatchID: batch.batchID];
147
158
}
148
159
149
160
- (TargetId)allocateQuery : (FSTQuery *)query {
@@ -159,34 +170,31 @@ - (TargetId)allocateQuery:(FSTQuery *)query {
159
170
} while (0 )
160
171
161
172
/* * Asserts that a the lastChanges contain the docs in the given array. */
162
- #define FSTAssertChanged (documents ) \
163
- XCTAssertNotNil (self.lastChanges); \
164
- do { \
165
- FSTMaybeDocumentDictionary *actual = self.lastChanges ; \
166
- NSArray <FSTMaybeDocument *> *expected = (documents); \
167
- XCTAssertEqual (actual.count , expected.count ); \
168
- NSEnumerator <FSTMaybeDocument *> *enumerator = expected.objectEnumerator ; \
169
- [actual enumerateKeysAndObjectsUsingBlock: ^(FSTDocumentKey * key, FSTMaybeDocument * value, \
170
- BOOL * stop) { \
171
- XCTAssertEqualObjects (value, [enumerator nextObject ]); \
172
- }]; \
173
- self.lastChanges = nil ; \
173
+ #define FSTAssertChanged (documents ) \
174
+ do { \
175
+ NSArray <FSTMaybeDocument *> *expected = (documents); \
176
+ XCTAssertEqual (_lastChanges.size (), expected.count ); \
177
+ NSEnumerator <FSTMaybeDocument *> *enumerator = expected.objectEnumerator ; \
178
+ for (const auto &kv : _lastChanges) { \
179
+ FSTMaybeDocument *value = kv.second ; \
180
+ XCTAssertEqualObjects (value, [enumerator nextObject ]); \
181
+ } \
182
+ _lastChanges = MaybeDocumentMap{}; \
174
183
} while (0 )
175
184
176
185
/* * Asserts that the given keys were removed. */
177
- #define FSTAssertRemoved (keyPaths ) \
178
- XCTAssertNotNil (self.lastChanges); \
179
- do { \
180
- FSTMaybeDocumentDictionary *actual = self.lastChanges ; \
181
- XCTAssertEqual (actual.count , keyPaths.count ); \
182
- NSEnumerator <NSString *> *keyPathEnumerator = keyPaths.objectEnumerator ; \
183
- [actual enumerateKeysAndObjectsUsingBlock: ^(FSTDocumentKey * actualKey, \
184
- FSTMaybeDocument * value, BOOL * stop) { \
185
- FSTDocumentKey *expectedKey = FSTTestDocKey ([keyPathEnumerator nextObject ]); \
186
- XCTAssertEqualObjects (actualKey, expectedKey); \
187
- XCTAssertTrue ([value isKindOfClass: [FSTDeletedDocument class ]]); \
188
- }]; \
189
- self.lastChanges = nil ; \
186
+ #define FSTAssertRemoved (keyPaths ) \
187
+ do { \
188
+ XCTAssertEqual (_lastChanges.size (), keyPaths.count ); \
189
+ NSEnumerator <NSString *> *keyPathEnumerator = keyPaths.objectEnumerator ; \
190
+ for (const auto &kv : _lastChanges) { \
191
+ const DocumentKey &actualKey = kv.first ; \
192
+ FSTMaybeDocument *value = kv.second ; \
193
+ DocumentKey expectedKey = FSTTestDocKey ([keyPathEnumerator nextObject ]); \
194
+ XCTAssertEqual (actualKey, expectedKey); \
195
+ XCTAssertTrue ([value isKindOfClass: [FSTDeletedDocument class ]]); \
196
+ } \
197
+ _lastChanges = MaybeDocumentMap{}; \
190
198
} while (0 )
191
199
192
200
/* * Asserts that the given local store contains the given document. */
@@ -200,7 +208,7 @@ - (TargetId)allocateQuery:(FSTQuery *)query {
200
208
/* * Asserts that the given local store does not contain the given document. */
201
209
#define FSTAssertNotContains (keyPathString ) \
202
210
do { \
203
- FSTDocumentKey * key = FSTTestDocKey (keyPathString); \
211
+ DocumentKey key = FSTTestDocKey (keyPathString); \
204
212
FSTMaybeDocument *actual = [self .localStore readDocument: key]; \
205
213
XCTAssertNil (actual); \
206
214
} while (0 )
@@ -834,9 +842,9 @@ - (void)testCanExecuteDocumentQueries {
834
842
FSTTestSetMutation (@" foo/bar/Foo/Bar" , @{@" Foo" : @" Bar" })
835
843
]];
836
844
FSTQuery *query = FSTTestQuery (" foo/bar" );
837
- FSTDocumentDictionary * docs = [self .localStore executeQuery: query];
838
- XCTAssertEqualObjects ([ docs values ] , @[ FSTTestDoc (" foo/bar" , 0 , @{@" foo" : @" bar" },
839
- FSTDocumentStateLocalMutations) ]);
845
+ DocumentMap docs = [self .localStore executeQuery: query];
846
+ XCTAssertEqualObjects (docMapToArray ( docs) , @[ FSTTestDoc (" foo/bar" , 0 , @{@" foo" : @" bar" },
847
+ FSTDocumentStateLocalMutations) ]);
840
848
}
841
849
842
850
- (void )testCanExecuteCollectionQueries {
@@ -850,9 +858,9 @@ - (void)testCanExecuteCollectionQueries {
850
858
FSTTestSetMutation (@" fooo/blah" , @{@" fooo" : @" blah" })
851
859
]];
852
860
FSTQuery *query = FSTTestQuery (" foo" );
853
- FSTDocumentDictionary * docs = [self .localStore executeQuery: query];
861
+ DocumentMap docs = [self .localStore executeQuery: query];
854
862
XCTAssertEqualObjects (
855
- [ docs values ] , (@[
863
+ docMapToArray ( docs) , (@[
856
864
FSTTestDoc (" foo/bar" , 0 , @{@" foo" : @" bar" }, FSTDocumentStateLocalMutations),
857
865
FSTTestDoc (" foo/baz" , 0 , @{@" foo" : @" baz" }, FSTDocumentStateLocalMutations)
858
866
]));
@@ -874,8 +882,8 @@ - (void)testCanExecuteMixedCollectionQueries {
874
882
875
883
[self .localStore locallyWriteMutations: @[ FSTTestSetMutation (@" foo/bonk" , @{@" a" : @" b" }) ]];
876
884
877
- FSTDocumentDictionary * docs = [self .localStore executeQuery: query];
878
- XCTAssertEqualObjects ([ docs values ] , (@[
885
+ DocumentMap docs = [self .localStore executeQuery: query];
886
+ XCTAssertEqualObjects (docMapToArray ( docs) , (@[
879
887
FSTTestDoc (" foo/bar" , 20 , @{@" a" : @" b" }, FSTDocumentStateSynced),
880
888
FSTTestDoc (" foo/baz" , 10 , @{@" a" : @" b" }, FSTDocumentStateSynced),
881
889
FSTTestDoc (" foo/bonk" , 0 , @{@" a" : @" b" }, FSTDocumentStateLocalMutations)
0 commit comments