18
18
19
19
#import < XCTest/XCTest.h>
20
20
21
+ #import " Firestore/Source/API/FIRFirestore+Internal.h"
22
+ #import " Firestore/Source/API/FIRPersistentCacheIndexManager+Internal.h"
23
+
24
+ #import " Firestore/Example/Tests/Util/FSTHelpers.h"
21
25
#import " Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
22
26
23
27
@interface FIRIndexingTests : FSTIntegrationTestCase
@@ -29,15 +33,15 @@ @implementation FIRIndexingTests
29
33
- (void )setUp {
30
34
[super setUp ];
31
35
self.db = [self firestore ];
32
- XCTestExpectation* exp = [self expectationWithDescription: @" clear persistence" ];
33
- [self .db clearPersistenceWithCompletion: ^(NSError *) {
36
+ XCTestExpectation * exp = [self expectationWithDescription: @" clear persistence" ];
37
+ [self .db clearPersistenceWithCompletion: ^(NSError *) {
34
38
[exp fulfill ];
35
39
}];
36
40
[self awaitExpectation: exp];
37
41
}
38
42
39
43
- (void )testCanConfigureIndexes {
40
- NSString * json = @" {\n "
44
+ NSString * json = @" {\n "
41
45
" \t\" indexes\" : [{\n "
42
46
" \t\t\t\" collectionGroup\" : \" restaurants\" ,\n "
43
47
" \t\t\t\" queryScope\" : \" COLLECTION\" ,\n "
@@ -64,22 +68,22 @@ - (void)testCanConfigureIndexes {
64
68
" }" ;
65
69
66
70
[self .db setIndexConfigurationFromJSON: json
67
- completion: ^(NSError * error) {
71
+ completion: ^(NSError * error) {
68
72
XCTAssertNil (error);
69
73
}];
70
74
}
71
75
72
76
- (void )testBadJsonDoesNotCrashClient {
73
77
[self .db setIndexConfigurationFromJSON: @" {,"
74
- completion: ^(NSError * error) {
78
+ completion: ^(NSError * error) {
75
79
XCTAssertNotNil (error);
76
80
XCTAssertEqualObjects (error.domain , FIRFirestoreErrorDomain);
77
81
XCTAssertEqual (error.code , FIRFirestoreErrorCodeInvalidArgument);
78
82
}];
79
83
}
80
84
81
85
- (void )testBadIndexDoesNotCrashClient {
82
- NSString * json = @" {\n "
86
+ NSString * json = @" {\n "
83
87
" \t\" indexes\" : [{\n "
84
88
" \t\t\" collectionGroup\" : \" restaurants\" ,\n "
85
89
" \t\t\" queryScope\" : \" COLLECTION\" ,\n "
@@ -92,11 +96,114 @@ - (void)testBadIndexDoesNotCrashClient {
92
96
" }" ;
93
97
94
98
[self .db setIndexConfigurationFromJSON: json
95
- completion: ^(NSError * error) {
99
+ completion: ^(NSError * error) {
96
100
XCTAssertNotNil (error);
97
101
XCTAssertEqualObjects (error.domain , FIRFirestoreErrorDomain);
98
102
XCTAssertEqual (error.code , FIRFirestoreErrorCodeInvalidArgument);
99
103
}];
100
104
}
101
105
106
+ /* *
107
+ * After Auto Index Creation is enabled, through public API there is no way to see the indexes
108
+ * sitting inside SDK. So this test only checks the API of auto index creation.
109
+ */
110
+ - (void )testAutoIndexCreationSetSuccessfully {
111
+ // Use persistent disk cache (explict)
112
+ FIRFirestoreSettings *settings = [self .db settings ];
113
+ [settings setCacheSettings: [[FIRPersistentCacheSettings alloc ] init ]];
114
+ [self .db setSettings: settings];
115
+
116
+ FIRCollectionReference *coll = [self collectionRef ];
117
+ NSDictionary *testDocs = @{
118
+ @" a" : @{@" match" : @YES },
119
+ @" b" : @{@" match" : @NO },
120
+ @" c" : @{@" match" : @NO },
121
+ };
122
+ [self writeAllDocuments: testDocs toCollection: coll];
123
+
124
+ FIRQuery *query = [coll queryWhereField: @" match" isEqualTo: @YES ];
125
+
126
+ [query getDocumentsWithSource: FIRFirestoreSourceCache
127
+ completion: ^(FIRQuerySnapshot *results, NSError *error) {
128
+ XCTAssertNil (error);
129
+ XCTAssertEqual (results.count , 1 );
130
+ }];
131
+
132
+ XCTAssertNoThrow ([self .db.persistentCacheIndexManager enableIndexAutoCreation ]);
133
+ [query getDocumentsWithSource: FIRFirestoreSourceCache
134
+ completion: ^(FIRQuerySnapshot *results, NSError *error) {
135
+ XCTAssertNil (error);
136
+ XCTAssertEqual (results.count , 1 );
137
+ }];
138
+
139
+ XCTAssertNoThrow ([self .db.persistentCacheIndexManager disableIndexAutoCreation ]);
140
+ [query getDocumentsWithSource: FIRFirestoreSourceCache
141
+ completion: ^(FIRQuerySnapshot *results, NSError *error) {
142
+ XCTAssertNil (error);
143
+ XCTAssertEqual (results.count , 1 );
144
+ }];
145
+
146
+ XCTAssertNoThrow ([self .db.persistentCacheIndexManager deleteAllIndexes ]);
147
+ [query getDocumentsWithSource: FIRFirestoreSourceCache
148
+ completion: ^(FIRQuerySnapshot *results, NSError *error) {
149
+ XCTAssertNil (error);
150
+ XCTAssertEqual (results.count , 1 );
151
+ }];
152
+ }
153
+
154
+ - (void )testAutoIndexCreationSetSuccessfullyUsingDefault {
155
+ // Use persistent disk cache (default)
156
+ FIRCollectionReference *coll = [self collectionRef ];
157
+ NSDictionary *testDocs = @{
158
+ @" a" : @{@" match" : @YES },
159
+ @" b" : @{@" match" : @NO },
160
+ @" c" : @{@" match" : @NO },
161
+ };
162
+ [self writeAllDocuments: testDocs toCollection: coll];
163
+
164
+ FIRQuery *query = [coll queryWhereField: @" match" isEqualTo: @YES ];
165
+
166
+ [query getDocumentsWithSource: FIRFirestoreSourceCache
167
+ completion: ^(FIRQuerySnapshot *results, NSError *error) {
168
+ XCTAssertNil (error);
169
+ XCTAssertEqual (results.count , 1 );
170
+ }];
171
+
172
+ XCTAssertNoThrow ([self .db.persistentCacheIndexManager enableIndexAutoCreation ]);
173
+ [query getDocumentsWithSource: FIRFirestoreSourceCache
174
+ completion: ^(FIRQuerySnapshot *results, NSError *error) {
175
+ XCTAssertNil (error);
176
+ XCTAssertEqual (results.count , 1 );
177
+ }];
178
+
179
+ XCTAssertNoThrow ([self .db.persistentCacheIndexManager disableIndexAutoCreation ]);
180
+ [query getDocumentsWithSource: FIRFirestoreSourceCache
181
+ completion: ^(FIRQuerySnapshot *results, NSError *error) {
182
+ XCTAssertNil (error);
183
+ XCTAssertEqual (results.count , 1 );
184
+ }];
185
+
186
+ XCTAssertNoThrow ([self .db.persistentCacheIndexManager deleteAllIndexes ]);
187
+ [query getDocumentsWithSource: FIRFirestoreSourceCache
188
+ completion: ^(FIRQuerySnapshot *results, NSError *error) {
189
+ XCTAssertNil (error);
190
+ XCTAssertEqual (results.count , 1 );
191
+ }];
192
+ }
193
+
194
+ - (void )testAutoIndexCreationAfterFailsTermination {
195
+ [self terminateFirestore: self .db];
196
+
197
+ FSTAssertThrows ([self .db.persistentCacheIndexManager enableIndexAutoCreation ],
198
+ @" The client has already been terminated." );
199
+
200
+ FSTAssertThrows ([self .db.persistentCacheIndexManager disableIndexAutoCreation ],
201
+ @" The client has already been terminated." );
202
+
203
+ FSTAssertThrows ([self .db.persistentCacheIndexManager deleteAllIndexes ],
204
+ @" The client has already been terminated." );
205
+ }
206
+
207
+ // TODO(b/296100693) Add testing hooks to verify indexes are created as expected.
208
+
102
209
@end
0 commit comments