-
Notifications
You must be signed in to change notification settings - Fork 615
Testing composite index queries against production #5352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ed2cdb5
add composite index test helper
milaGGL be22ebb
add terrafrom to CI
milaGGL e596678
format
milaGGL 2d6a1d7
Update ci_tests.yml
milaGGL 908ad73
run terrafrom only for firestore tests
milaGGL 307d634
Update ci_tests.yml
milaGGL b6ce791
Update CompositeIndexTestHelper.java
milaGGL 1d5ff95
Merge branch 'master' into mila/composite-index-testing
milaGGL 168c450
Merge branch 'master' into mila/composite-index-testing
milaGGL 2562401
keep consistent with Web
milaGGL 9f71b5c
Merge branch 'master' into mila/composite-index-testing
milaGGL 4f39e2d
Merge branch 'master' into mila/composite-index-testing
milaGGL b2e8188
add test class comments
milaGGL a8b167b
Merge branch 'master' into mila/composite-index-testing
milaGGL 504ffd7
Merge branch 'master' into mila/composite-index-testing
milaGGL 57c0ab4
add extra test helper, move terraform into firestore folder
milaGGL 539f548
remove pwd
milaGGL 61c69b3
rename getDoc and getDocs function
milaGGL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...firestore/src/androidTest/java/com/google/firebase/firestore/CompositeIndexQueryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package com.google.firebase.firestore; | ||
|
||
import static com.google.firebase.firestore.Filter.equalTo; | ||
import static com.google.firebase.firestore.Filter.greaterThan; | ||
import static com.google.firebase.firestore.Filter.or; | ||
import static com.google.firebase.firestore.testutil.TestUtil.map; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import com.google.firebase.firestore.testutil.CompositeIndexTestHelper; | ||
import com.google.firebase.firestore.testutil.IntegrationTestUtil; | ||
import java.util.Map; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
/* | ||
* Guidance for Creating Tests: | ||
* ---------------------------- | ||
* When creating tests that require composite indexes, it is recommended to utilize the | ||
* "CompositeIndexTestHelper" class. This utility class provides methods for creating | ||
* and setting test documents and running queries with ease, ensuring proper data | ||
* isolation and query construction. | ||
* | ||
* Please remember to update the main index configuration file (firestore_index_config.tf) | ||
* with any new composite indexes needed for the tests. This ensures synchronization with | ||
* other testing environments, including CI. You can generate the required index link by | ||
* clicking on the Firebase console link in the error message while running tests locally. | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class CompositeIndexQueryTest { | ||
|
||
@After | ||
public void tearDown() { | ||
IntegrationTestUtil.tearDown(); | ||
} | ||
|
||
@Test | ||
public void testOrQueriesWithCompositeIndexes() { | ||
CompositeIndexTestHelper testHelper = new CompositeIndexTestHelper(); | ||
Map<String, Map<String, Object>> testDocs = | ||
map( | ||
"doc1", map("a", 1, "b", 0), | ||
"doc2", map("a", 2, "b", 1), | ||
"doc3", map("a", 3, "b", 2), | ||
"doc4", map("a", 1, "b", 3), | ||
"doc5", map("a", 1, "b", 1)); | ||
CollectionReference collection = testHelper.withTestDocs(testDocs); | ||
|
||
// with one inequality: a>2 || b==1. | ||
testHelper.assertOnlineAndOfflineResultsMatch( | ||
testHelper.query(collection.where(or(greaterThan("a", 2), equalTo("b", 1)))), | ||
"doc5", | ||
"doc2", | ||
"doc3"); | ||
|
||
// Test with limits (implicit order by ASC): (a==1) || (b > 0) LIMIT 2 | ||
testHelper.assertOnlineAndOfflineResultsMatch( | ||
testHelper.query(collection.where(or(equalTo("a", 1), greaterThan("b", 0))).limit(2)), | ||
"doc1", | ||
"doc2"); | ||
|
||
// Test with limits (explicit order by): (a==1) || (b > 0) LIMIT_TO_LAST 2 | ||
// Note: The public query API does not allow implicit ordering when limitToLast is used. | ||
testHelper.assertOnlineAndOfflineResultsMatch( | ||
testHelper.query( | ||
collection.where(or(equalTo("a", 1), greaterThan("b", 0))).limitToLast(2).orderBy("b")), | ||
"doc3", | ||
"doc4"); | ||
|
||
// Test with limits (explicit order by ASC): (a==2) || (b == 1) ORDER BY a LIMIT 1 | ||
testHelper.assertOnlineAndOfflineResultsMatch( | ||
testHelper.query( | ||
collection.where(or(equalTo("a", 2), equalTo("b", 1))).limit(1).orderBy("a")), | ||
"doc5"); | ||
|
||
// Test with limits (explicit order by DESC): (a==2) || (b == 1) ORDER BY a LIMIT_TO_LAST 1 | ||
testHelper.assertOnlineAndOfflineResultsMatch( | ||
testHelper.query( | ||
collection.where(or(equalTo("a", 2), equalTo("b", 1))).limitToLast(1).orderBy("a")), | ||
"doc2"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...src/androidTest/java/com/google/firebase/firestore/testutil/CompositeIndexTestHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.firebase.firestore.testutil; | ||
|
||
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.checkOnlineAndOfflineResultsMatch; | ||
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore; | ||
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.writeAllDocs; | ||
import static com.google.firebase.firestore.util.Util.autoId; | ||
|
||
import androidx.annotation.NonNull; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.firebase.Timestamp; | ||
import com.google.firebase.firestore.CollectionReference; | ||
import com.google.firebase.firestore.DocumentReference; | ||
import com.google.firebase.firestore.Query; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* This helper class is designed to facilitate integration testing of Firestore queries that require | ||
* composite indexes within a controlled testing environment. | ||
* | ||
* <p>Key Features: - Runs tests against the dedicated test collection with predefined composite | ||
* indexes. - Automatically associates a test ID with documents for data isolation. - Utilizes TTL | ||
* policy for automatic test data cleanup. - Constructs Firestore queries with test ID filters. | ||
*/ | ||
ehsannas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class CompositeIndexTestHelper { | ||
private final String testId; | ||
private static final String TEST_ID_FIELD = "testId"; | ||
private static final String COMPOSITE_INDEX_TEST_COLLECTION = "composite-index-test-collection"; | ||
|
||
// Creates a new instance of the CompositeIndexTestHelper class, with a unique test | ||
// identifier for data isolation. | ||
public CompositeIndexTestHelper() { | ||
this.testId = "test-id-" + autoId(); | ||
} | ||
|
||
// Runs a test with specified documents in the COMPOSITE_INDEX_TEST_COLLECTION. | ||
@NonNull | ||
public CollectionReference withTestDocs(@NonNull Map<String, Map<String, Object>> docs) { | ||
CollectionReference writer = testFirestore().collection(COMPOSITE_INDEX_TEST_COLLECTION); | ||
writeAllDocs(writer, prepareTestDocuments(docs)); | ||
CollectionReference reader = testFirestore().collection(writer.getPath()); | ||
return reader; | ||
} | ||
|
||
// Adds a filter on test id for a query. | ||
@NonNull | ||
public Query query(@NonNull Query query_) { | ||
return query_.whereEqualTo(TEST_ID_FIELD, testId); | ||
} | ||
|
||
// Hash the document key with testId. | ||
private String toHashedId(String docId) { | ||
return docId + '-' + testId; | ||
} | ||
|
||
private String[] toHashedIds(String[] docs) { | ||
String[] hashedIds = new String[docs.length]; | ||
for (int i = 0; i < docs.length; i++) { | ||
hashedIds[i] = toHashedId(docs[i]); | ||
} | ||
return hashedIds; | ||
} | ||
|
||
// Adds test-specific fields to a document, including the testId and expiration date. | ||
private Map<String, Object> addTestSpecificFieldsToDoc(Map<String, Object> doc) { | ||
Map<String, Object> updatedDoc = new HashMap<>(doc); | ||
updatedDoc.put(TEST_ID_FIELD, testId); | ||
updatedDoc.put( | ||
"expireAt", | ||
new Timestamp( // Expire test data after 24 hours | ||
Timestamp.now().getSeconds() + 24 * 60 * 60, Timestamp.now().getNanoseconds())); | ||
return updatedDoc; | ||
} | ||
|
||
// Helper method to hash document keys and add test-specific fields for the provided documents. | ||
private Map<String, Map<String, Object>> prepareTestDocuments( | ||
Map<String, Map<String, Object>> docs) { | ||
Map<String, Map<String, Object>> result = new HashMap<>(); | ||
for (String key : docs.keySet()) { | ||
Map<String, Object> doc = addTestSpecificFieldsToDoc(docs.get(key)); | ||
result.put(toHashedId(key), doc); | ||
} | ||
return result; | ||
} | ||
|
||
// Asserts that the result of running the query while online (against the backend/emulator) is | ||
// the same as running it while offline. The expected document Ids are hashed to match the | ||
// actual document IDs created by the test helper. | ||
@NonNull | ||
public void assertOnlineAndOfflineResultsMatch( | ||
@NonNull Query query, @NonNull String... expectedDocs) { | ||
checkOnlineAndOfflineResultsMatch(query, toHashedIds(expectedDocs)); | ||
} | ||
|
||
// Adds a document to a Firestore collection with test-specific fields. | ||
@NonNull | ||
public Task<DocumentReference> addDoc( | ||
@NonNull CollectionReference collection, @NonNull Map<String, Object> data) { | ||
return collection.add(addTestSpecificFieldsToDoc(data)); | ||
} | ||
|
||
// Sets a document in Firestore with test-specific fields. | ||
@NonNull | ||
public Task<Void> setDoc(@NonNull DocumentReference document, @NonNull Map<String, Object> data) { | ||
return document.set(addTestSpecificFieldsToDoc(data)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.