16
16
17
17
import static com .google .firebase .firestore .testutil .IntegrationTestUtil .checkOnlineAndOfflineResultsMatch ;
18
18
import static com .google .firebase .firestore .testutil .IntegrationTestUtil .testFirestore ;
19
+ import static com .google .firebase .firestore .testutil .IntegrationTestUtil .waitFor ;
19
20
import static com .google .firebase .firestore .testutil .IntegrationTestUtil .writeAllDocs ;
20
21
import static com .google .firebase .firestore .util .Util .autoId ;
21
22
24
25
import com .google .firebase .Timestamp ;
25
26
import com .google .firebase .firestore .CollectionReference ;
26
27
import com .google .firebase .firestore .DocumentReference ;
28
+ import com .google .firebase .firestore .DocumentSnapshot ;
27
29
import com .google .firebase .firestore .Query ;
30
+ import com .google .firebase .firestore .QuerySnapshot ;
31
+ import java .util .ArrayList ;
28
32
import java .util .HashMap ;
33
+ import java .util .List ;
29
34
import java .util .Map ;
30
35
31
36
/**
32
37
* This helper class is designed to facilitate integration testing of Firestore queries that require
33
38
* composite indexes within a controlled testing environment.
34
39
*
35
- * <p>Key Features: - Runs tests against the dedicated test collection with predefined composite
36
- * indexes. - Automatically associates a test ID with documents for data isolation. - Utilizes TTL
37
- * policy for automatic test data cleanup. - Constructs Firestore queries with test ID filters.
40
+ * <p>Key Features:
41
+ *
42
+ * <ul>
43
+ * <li>Runs tests against the dedicated test collection with predefined composite indexes.
44
+ * <li>Automatically associates a test ID with documents for data isolation.
45
+ * <li>Utilizes TTL policy for automatic test data cleanup.
46
+ * <li>Constructs Firestore queries with test ID filters.
47
+ * </ul>
38
48
*/
39
49
public class CompositeIndexTestHelper {
40
50
private final String testId ;
41
51
private static final String TEST_ID_FIELD = "testId" ;
52
+ private static final String TTL_FIELD = "expireAt" ;
42
53
private static final String COMPOSITE_INDEX_TEST_COLLECTION = "composite-index-test-collection" ;
43
54
44
55
// Creates a new instance of the CompositeIndexTestHelper class, with a unique test
@@ -56,12 +67,6 @@ public CollectionReference withTestDocs(@NonNull Map<String, Map<String, Object>
56
67
return reader ;
57
68
}
58
69
59
- // Adds a filter on test id for a query.
60
- @ NonNull
61
- public Query query (@ NonNull Query query_ ) {
62
- return query_ .whereEqualTo (TEST_ID_FIELD , testId );
63
- }
64
-
65
70
// Hash the document key with testId.
66
71
private String toHashedId (String docId ) {
67
72
return docId + '-' + testId ;
@@ -80,12 +85,19 @@ private Map<String, Object> addTestSpecificFieldsToDoc(Map<String, Object> doc)
80
85
Map <String , Object > updatedDoc = new HashMap <>(doc );
81
86
updatedDoc .put (TEST_ID_FIELD , testId );
82
87
updatedDoc .put (
83
- "expireAt" ,
88
+ TTL_FIELD ,
84
89
new Timestamp ( // Expire test data after 24 hours
85
90
Timestamp .now ().getSeconds () + 24 * 60 * 60 , Timestamp .now ().getNanoseconds ()));
86
91
return updatedDoc ;
87
92
}
88
93
94
+ // Remove test-specific fields from a document.
95
+ private Map <String , Object > removeTestSpecificFieldsFromDoc (Map <String , Object > doc ) {
96
+ doc .remove (TTL_FIELD );
97
+ doc .remove (TEST_ID_FIELD );
98
+ return doc ;
99
+ }
100
+
89
101
// Helper method to hash document keys and add test-specific fields for the provided documents.
90
102
private Map <String , Map <String , Object >> prepareTestDocuments (
91
103
Map <String , Map <String , Object >> docs ) {
@@ -106,6 +118,22 @@ public void assertOnlineAndOfflineResultsMatch(
106
118
checkOnlineAndOfflineResultsMatch (query , toHashedIds (expectedDocs ));
107
119
}
108
120
121
+ // Adds a filter on test id for a query.
122
+ @ NonNull
123
+ public Query query (@ NonNull Query query_ ) {
124
+ return query_ .whereEqualTo (TEST_ID_FIELD , testId );
125
+ }
126
+
127
+ // Get document reference from a document key.
128
+ @ NonNull
129
+ public DocumentReference getDocRef (
130
+ @ NonNull CollectionReference collection , @ NonNull String docId ) {
131
+ if (!docId .contains ("test-id-" )) {
132
+ docId = toHashedId (docId );
133
+ }
134
+ return collection .document (docId );
135
+ }
136
+
109
137
// Adds a document to a Firestore collection with test-specific fields.
110
138
@ NonNull
111
139
public Task <DocumentReference > addDoc (
@@ -118,4 +146,33 @@ public Task<DocumentReference> addDoc(
118
146
public Task <Void > setDoc (@ NonNull DocumentReference document , @ NonNull Map <String , Object > data ) {
119
147
return document .set (addTestSpecificFieldsToDoc (data ));
120
148
}
149
+
150
+ @ NonNull
151
+ public Task <Void > updateDoc (
152
+ @ NonNull DocumentReference document , @ NonNull Map <String , Object > data ) {
153
+ return document .update (data );
154
+ }
155
+
156
+ @ NonNull
157
+ public Task <Void > deleteDoc (@ NonNull DocumentReference document ) {
158
+ return document .delete ();
159
+ }
160
+
161
+ // Retrieves a single document from Firestore with test-specific fields removed.
162
+ @ NonNull
163
+ public Map <String , Object > getDoc (@ NonNull DocumentReference document ) {
164
+ DocumentSnapshot docSnapshot = waitFor (document .get ());
165
+ return removeTestSpecificFieldsFromDoc (docSnapshot .getData ());
166
+ }
167
+
168
+ // Retrieves multiple documents from Firestore with test-specific fields removed.
169
+ @ NonNull
170
+ public List <Map <String , Object >> getDocs (@ NonNull Query query_ ) {
171
+ QuerySnapshot querySnapshot = waitFor (query (query_ ).get ());
172
+ List <Map <String , Object >> res = new ArrayList <>();
173
+ for (DocumentSnapshot doc : querySnapshot ) {
174
+ res .add (removeTestSpecificFieldsFromDoc (doc .getData ()));
175
+ }
176
+ return res ;
177
+ }
121
178
}
0 commit comments