Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit eea9799

Browse files
committed
Update pipeline search test setup to only run once for the class. This required a refactor of BaseTest. Also added logic to test setup to ensure unexpected docs are cleaned up
1 parent 5ae808d commit eea9799

2 files changed

Lines changed: 40 additions & 15 deletions

File tree

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITBaseTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ static FirestoreEdition getFirestoreEdition() {
7979
return FirestoreEdition.valueOf(firestoreEdition.toUpperCase());
8080
}
8181

82-
@Before
83-
public void before() throws Exception {
82+
public static FirestoreOptions.Builder getOptionsBuilder() {
8483
FirestoreOptions.Builder optionsBuilder = FirestoreOptions.newBuilder();
8584

8685
String dbPropertyName = "FIRESTORE_NAMED_DATABASE";
@@ -110,8 +109,12 @@ public void before() throws Exception {
110109
optionsBuilder.setEmulatorHost("localhost:8080");
111110
}
112111
}
112+
return optionsBuilder;
113+
}
113114

114-
firestoreOptions = optionsBuilder.build();
115+
@Before
116+
public void before() throws Exception {
117+
firestoreOptions = getOptionsBuilder().build();
115118
logger.log(
116119
Level.INFO,
117120
"Integration test against " + firestoreOptions.getTransportChannelProvider().getEndpoint());

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineSearchTest.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import static org.junit.Assume.assumeTrue;
2323

2424
import com.google.cloud.firestore.CollectionReference;
25+
import com.google.cloud.firestore.DocumentSnapshot;
26+
import com.google.cloud.firestore.Firestore;
2527
import com.google.cloud.firestore.GeoPoint;
2628
import com.google.cloud.firestore.Pipeline;
2729
import com.google.cloud.firestore.PipelineResult;
30+
import com.google.cloud.firestore.QuerySnapshot;
2831
import com.google.cloud.firestore.WriteBatch;
2932
import com.google.cloud.firestore.pipeline.stages.Search;
3033
import java.util.Arrays;
@@ -34,17 +37,15 @@
3437
import java.util.Objects;
3538
import java.util.concurrent.TimeUnit;
3639
import java.util.stream.Collectors;
37-
import org.junit.Before;
40+
import org.junit.BeforeClass;
3841
import org.junit.Test;
3942
import org.junit.runner.RunWith;
4043
import org.junit.runners.JUnit4;
4144

4245
@RunWith(JUnit4.class)
4346
public class ITPipelineSearchTest extends ITBaseTest {
4447

45-
private final String COLLECTION_NAME = "TextSearchIntegrationTests";
46-
47-
private CollectionReference restaurantsCollection;
48+
private static final String COLLECTION_NAME = "TextSearchIntegrationTests";
4849

4950
private static final Map<String, Map<String, Object>> restaurantDocs = new HashMap<>();
5051

@@ -184,20 +185,41 @@ public void primeBackend() throws Exception {
184185
// Disable priming as it uses Watch/Listen
185186
}
186187

187-
@Before
188-
public void setupRestaurantDocs() throws Exception {
188+
@BeforeClass
189+
public static void setupRestaurantDocs() throws Exception {
189190
assumeTrue(
190191
"This test suite only runs against the Enterprise edition in Nightly.",
191192
getFirestoreEdition().equals(FirestoreEdition.ENTERPRISE)
192193
&& "NIGHTLY".equalsIgnoreCase(getTargetBackend()));
193194

194-
restaurantsCollection = firestore.collection(COLLECTION_NAME);
195-
196-
WriteBatch batch = firestore.batch();
197-
for (Map.Entry<String, Map<String, Object>> entry : restaurantDocs.entrySet()) {
198-
batch.set(restaurantsCollection.document(entry.getKey()), entry.getValue());
195+
// Initialize a temporary Firestore instance for class-level setup.
196+
Firestore db = getOptionsBuilder().build().getService();
197+
198+
// Setup restaurant docs
199+
try {
200+
// Get the existing contents of the test collection
201+
CollectionReference collection = db.collection(COLLECTION_NAME);
202+
QuerySnapshot snapshot = collection.get().get();
203+
204+
// A batch will be used to update the test collection to the desired state
205+
WriteBatch batch = db.batch();
206+
207+
// Delete unexpected documents
208+
for (DocumentSnapshot doc : snapshot.getDocuments()) {
209+
if (!restaurantDocs.containsKey(doc.getId())) {
210+
batch.delete(doc.getReference());
211+
}
212+
}
213+
214+
// Add/overwrite expected documents
215+
for (Map.Entry<String, Map<String, Object>> entry : restaurantDocs.entrySet()) {
216+
batch.set(collection.document(entry.getKey()), entry.getValue());
217+
}
218+
219+
batch.commit().get(10, TimeUnit.SECONDS);
220+
} finally {
221+
db.close();
199222
}
200-
batch.commit().get(10, TimeUnit.SECONDS);
201223
}
202224

203225
private void assertResultIds(Pipeline.Snapshot snapshot, String... ids) {

0 commit comments

Comments
 (0)