17
17
18
18
import { and } from '../../../src/lite-api/query' ;
19
19
import { AutoId } from '../../../src/util/misc' ;
20
+ import { field } from '../../util/helpers' ;
20
21
21
22
import {
22
23
query as internalQuery ,
@@ -32,7 +33,13 @@ import {
32
33
setDoc as setDocument ,
33
34
QueryCompositeFilterConstraint ,
34
35
QueryNonFilterConstraint ,
35
- Timestamp
36
+ Timestamp ,
37
+ DocumentSnapshot ,
38
+ getDoc as getDocument ,
39
+ updateDoc as updateDocument ,
40
+ UpdateData ,
41
+ getDocs as getDocuments ,
42
+ QuerySnapshot
36
43
} from './firebase_export' ;
37
44
import {
38
45
batchCommitDocsToCollection ,
@@ -55,6 +62,7 @@ import { COMPOSITE_INDEX_TEST_COLLECTION, DEFAULT_SETTINGS } from './settings';
55
62
export class CompositeIndexTestHelper {
56
63
private readonly testId : string ;
57
64
private readonly TEST_ID_FIELD : string = 'testId' ;
65
+ private readonly TTL_FIELD : string = 'expireAt' ;
58
66
59
67
// Creates a new instance of the CompositeIndexTestHelper class, with a unique test
60
68
// identifier for data isolation.
@@ -91,13 +99,20 @@ export class CompositeIndexTestHelper {
91
99
return {
92
100
...doc ,
93
101
[ this . TEST_ID_FIELD ] : this . testId ,
94
- expireAt : new Timestamp ( // Expire test data after 24 hours
102
+ [ this . TTL_FIELD ] : new Timestamp ( // Expire test data after 24 hours
95
103
Timestamp . now ( ) . seconds + 24 * 60 * 60 ,
96
104
Timestamp . now ( ) . nanoseconds
97
105
)
98
106
} ;
99
107
}
100
108
109
+ // Remove test-specific fields from a document, including the testId and expiration date.
110
+ removeTestSpecificFieldsFromDoc ( doc : DocumentData ) : DocumentData {
111
+ doc . _document ?. data ?. delete ( field ( this . TTL_FIELD ) ) ;
112
+ doc . _document ?. data ?. delete ( field ( this . TEST_ID_FIELD ) ) ;
113
+ return doc ;
114
+ }
115
+
101
116
// Helper method to hash document keys and add test-specific fields for the provided documents.
102
117
private prepareTestDocuments ( docs : { [ key : string ] : DocumentData } ) : {
103
118
[ key : string ] : DocumentData ;
@@ -172,4 +187,34 @@ export class CompositeIndexTestHelper {
172
187
) as WithFieldValue < T > ;
173
188
return setDocument ( reference , processedData ) ;
174
189
}
190
+
191
+ // This is is the same as making the update on the doc directly with merge=true.
192
+ updateDoc < T , DbModelType extends DocumentData > (
193
+ reference : DocumentReference < T , DbModelType > ,
194
+ data : UpdateData < DbModelType >
195
+ ) : Promise < void > {
196
+ const processedData = this . addTestSpecificFieldsToDoc (
197
+ data
198
+ ) as UpdateData < DbModelType > ;
199
+ return updateDocument ( reference , processedData ) ;
200
+ }
201
+
202
+
203
+ async getDoc < T , DbModelType extends DocumentData > (
204
+ reference : DocumentReference < T , DbModelType >
205
+ ) : Promise < DocumentSnapshot < T , DbModelType > > {
206
+ const docSnapshot = await getDocument < T , DbModelType > ( reference ) ;
207
+ this . removeTestSpecificFieldsFromDoc ( docSnapshot ) ;
208
+ return docSnapshot ;
209
+ }
210
+
211
+ async getDocs < T , DbModelType extends DocumentData > (
212
+ query_ : Query < T , DbModelType >
213
+ ) : Promise < QuerySnapshot < T , DbModelType > > {
214
+ const querySnapshot = await getDocuments ( this . query ( query_ ) ) ;
215
+ querySnapshot . forEach ( doc => {
216
+ this . removeTestSpecificFieldsFromDoc ( doc ) ;
217
+ } ) ;
218
+ return querySnapshot ;
219
+ }
175
220
}
0 commit comments