Skip to content

Commit 57c0ab4

Browse files
committed
add extra test helper, move terraform into firestore folder
1 parent 504ffd7 commit 57c0ab4

File tree

5 files changed

+85
-34
lines changed

5 files changed

+85
-34
lines changed

.github/workflows/ci_tests.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,17 @@ jobs:
127127
uses: hashicorp/setup-terraform@v2
128128
- name: Terraform Init
129129
if: contains(matrix.module, ':firebase-firestore')
130-
run: terraform init
130+
run: |
131+
pwd
132+
cd firebase-firestore
133+
terraform init
131134
continue-on-error: true
132135
- name: Terraform Apply
133136
if: github.event_name == 'pull_request' && contains(matrix.module, ':firebase-firestore')
134-
run: terraform apply -var-file=google-services.json -auto-approve
137+
run: |
138+
pwd
139+
cd firebase-firestore
140+
terraform apply -var-file=../google-services.json -auto-approve
135141
continue-on-error: true
136142
- name: ${{ matrix.module }} Integ Tests
137143
env:
File renamed without changes.

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/CompositeIndexQueryTest.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,25 @@ public void testOrQueriesWithCompositeIndexes() {
5959
"doc5", map("a", 1, "b", 1));
6060
CollectionReference collection = testHelper.withTestDocs(testDocs);
6161

62+
Query query = collection.where(or(greaterThan("a", 2), equalTo("b", 1)));
6263
// with one inequality: a>2 || b==1.
63-
testHelper.assertOnlineAndOfflineResultsMatch(
64-
testHelper.query(collection.where(or(greaterThan("a", 2), equalTo("b", 1)))),
65-
"doc5",
66-
"doc2",
67-
"doc3");
64+
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc5", "doc2", "doc3");
6865

6966
// Test with limits (implicit order by ASC): (a==1) || (b > 0) LIMIT 2
70-
testHelper.assertOnlineAndOfflineResultsMatch(
71-
testHelper.query(collection.where(or(equalTo("a", 1), greaterThan("b", 0))).limit(2)),
72-
"doc1",
73-
"doc2");
67+
query = collection.where(or(equalTo("a", 1), greaterThan("b", 0))).limit(2);
68+
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc1", "doc2");
7469

7570
// Test with limits (explicit order by): (a==1) || (b > 0) LIMIT_TO_LAST 2
7671
// Note: The public query API does not allow implicit ordering when limitToLast is used.
77-
testHelper.assertOnlineAndOfflineResultsMatch(
78-
testHelper.query(
79-
collection.where(or(equalTo("a", 1), greaterThan("b", 0))).limitToLast(2).orderBy("b")),
80-
"doc3",
81-
"doc4");
72+
query = collection.where(or(equalTo("a", 1), greaterThan("b", 0))).limitToLast(2).orderBy("b");
73+
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc3", "doc4");
8274

8375
// Test with limits (explicit order by ASC): (a==2) || (b == 1) ORDER BY a LIMIT 1
84-
testHelper.assertOnlineAndOfflineResultsMatch(
85-
testHelper.query(
86-
collection.where(or(equalTo("a", 2), equalTo("b", 1))).limit(1).orderBy("a")),
87-
"doc5");
76+
query = collection.where(or(equalTo("a", 2), equalTo("b", 1))).limit(1).orderBy("a");
77+
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc5");
8878

8979
// Test with limits (explicit order by DESC): (a==2) || (b == 1) ORDER BY a LIMIT_TO_LAST 1
90-
testHelper.assertOnlineAndOfflineResultsMatch(
91-
testHelper.query(
92-
collection.where(or(equalTo("a", 2), equalTo("b", 1))).limitToLast(1).orderBy("a")),
93-
"doc2");
80+
query = collection.where(or(equalTo("a", 2), equalTo("b", 1))).limitToLast(1).orderBy("a");
81+
testHelper.assertOnlineAndOfflineResultsMatch(testHelper.query(query), "doc2");
9482
}
9583
}

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/testutil/CompositeIndexTestHelper.java

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

1717
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.checkOnlineAndOfflineResultsMatch;
1818
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
19+
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor;
1920
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.writeAllDocs;
2021
import static com.google.firebase.firestore.util.Util.autoId;
2122

@@ -24,21 +25,31 @@
2425
import com.google.firebase.Timestamp;
2526
import com.google.firebase.firestore.CollectionReference;
2627
import com.google.firebase.firestore.DocumentReference;
28+
import com.google.firebase.firestore.DocumentSnapshot;
2729
import com.google.firebase.firestore.Query;
30+
import com.google.firebase.firestore.QuerySnapshot;
31+
import java.util.ArrayList;
2832
import java.util.HashMap;
33+
import java.util.List;
2934
import java.util.Map;
3035

3136
/**
3237
* This helper class is designed to facilitate integration testing of Firestore queries that require
3338
* composite indexes within a controlled testing environment.
3439
*
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>
3848
*/
3949
public class CompositeIndexTestHelper {
4050
private final String testId;
4151
private static final String TEST_ID_FIELD = "testId";
52+
private static final String TTL_FIELD = "expireAt";
4253
private static final String COMPOSITE_INDEX_TEST_COLLECTION = "composite-index-test-collection";
4354

4455
// 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>
5667
return reader;
5768
}
5869

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-
6570
// Hash the document key with testId.
6671
private String toHashedId(String docId) {
6772
return docId + '-' + testId;
@@ -80,12 +85,19 @@ private Map<String, Object> addTestSpecificFieldsToDoc(Map<String, Object> doc)
8085
Map<String, Object> updatedDoc = new HashMap<>(doc);
8186
updatedDoc.put(TEST_ID_FIELD, testId);
8287
updatedDoc.put(
83-
"expireAt",
88+
TTL_FIELD,
8489
new Timestamp( // Expire test data after 24 hours
8590
Timestamp.now().getSeconds() + 24 * 60 * 60, Timestamp.now().getNanoseconds()));
8691
return updatedDoc;
8792
}
8893

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+
89101
// Helper method to hash document keys and add test-specific fields for the provided documents.
90102
private Map<String, Map<String, Object>> prepareTestDocuments(
91103
Map<String, Map<String, Object>> docs) {
@@ -106,6 +118,22 @@ public void assertOnlineAndOfflineResultsMatch(
106118
checkOnlineAndOfflineResultsMatch(query, toHashedIds(expectedDocs));
107119
}
108120

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+
109137
// Adds a document to a Firestore collection with test-specific fields.
110138
@NonNull
111139
public Task<DocumentReference> addDoc(
@@ -118,4 +146,33 @@ public Task<DocumentReference> addDoc(
118146
public Task<Void> setDoc(@NonNull DocumentReference document, @NonNull Map<String, Object> data) {
119147
return document.set(addTestSpecificFieldsToDoc(data));
120148
}
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+
}
121178
}

0 commit comments

Comments
 (0)