Skip to content

Commit

Permalink
move sum& avg tests, add new indexes required
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL committed Oct 23, 2023
1 parent 250c69f commit cfa4592
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 164 deletions.
14 changes: 14 additions & 0 deletions firebase-firestore/firestore_collection_group_index_config.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
locals {
collection_group_indexes = {
index1 = [
{
field_path = "testId"
order = "ASCENDING"
},
{
field_path = "a"
order = "ASCENDING"
},
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,55 @@ locals {
order = "DESCENDING"
},
]
index9 = [
{
field_path = "testId"
order = "ASCENDING"
},
{
field_path = "pages"
order = "ASCENDING"
},
{
field_path = "year"
order = "ASCENDING"
},
]
index10 = [
{
field_path = "testId"
order = "ASCENDING"
},
{
field_path = "pages"
order = "ASCENDING"
},
{
field_path = "rating"
order = "ASCENDING"
},
{
field_path = "year"
order = "ASCENDING"
},
]
index11 = [
{
field_path = "rating"
array_config = "CONTAINS"
},
{
field_path = "testId"
order = "ASCENDING"
},
{
field_path = "pages"
order = "ASCENDING"
},
{
field_path = "rating"
order = "ASCENDING"
},
]
}
}
64 changes: 54 additions & 10 deletions firebase-firestore/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,82 @@ provider "google" {
project = var.project_info.project_id
}

resource "google_firestore_index" "default-db-index" {
resource "google_firestore_index" "default_db_index" {
collection = "composite-index-test-collection"

for_each = local.indexes
dynamic "fields" {
for_each = distinct(flatten([for k, v in local.indexes : [
for i in each.value : {
field_path = i.field_path
order = i.order
field_path = i.field_path
order = can(i.order) ? i.order : null
array_config = can(i.array_config) ? i.array_config : null
}]]))
content {
field_path = lookup(fields.value, "field_path", null)
order = lookup(fields.value, "order", null)
field_path = fields.value.field_path
order = fields.value.order
array_config = fields.value.array_config
}
}
}

resource "google_firestore_index" "default_db_collection_group_index" {
collection = "composite-index-test-collection"
query_scope = "COLLECTION_GROUP"

for_each = local.collection_group_indexes
dynamic "fields" {
for_each = distinct(flatten([for k, v in local.indexes : [
for i in each.value : {
field_path = i.field_path
order = can(i.order) ? i.order : null
array_config = can(i.array_config) ? i.array_config : null
}]]))
content {
field_path = fields.value.field_path
order = fields.value.order
array_config = fields.value.array_config
}
}
}

resource "google_firestore_index" "named-db-index" {
resource "google_firestore_index" "named_db_index" {
collection = "composite-index-test-collection"
database = "test-db"

for_each = local.indexes
dynamic "fields" {
for_each = distinct(flatten([for k, v in local.indexes : [
for i in each.value : {
field_path = i.field_path
order = i.order
field_path = i.field_path
order = can(i.order) ? i.order : null
array_config = can(i.array_config) ? i.array_config : null
}]]))
content {
field_path = lookup(fields.value, "field_path", null)
order = lookup(fields.value, "order", null)
field_path = fields.value.field_path
order = fields.value.order
array_config = fields.value.array_config
}
}
}

resource "google_firestore_index" "named_db_collection_group_index" {
collection = "composite-index-test-collection"
database = "test-db"
query_scope = "COLLECTION_GROUP"

for_each = local.collection_group_indexes
dynamic "fields" {
for_each = distinct(flatten([for k, v in local.indexes : [
for i in each.value : {
field_path = i.field_path
order = can(i.order) ? i.order : null
array_config = can(i.array_config) ? i.array_config : null
}]]))
content {
field_path = fields.value.field_path
order = fields.value.order
array_config = fields.value.array_config
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.isRunningAgainstEmulator;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollection;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollectionWithDocs;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor;
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitForException;
import static com.google.firebase.firestore.testutil.TestUtil.map;
Expand All @@ -34,7 +33,6 @@
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.gms.tasks.Task;
Expand Down Expand Up @@ -343,30 +341,6 @@ public void testTerminateDoesNotCrashWithFlyingAggregateQuery() {
waitFor(collection.firestore.terminate());
}

@Test
public void testCanGetCorrectTypeForSum() {
assumeTrue(
"This query requires a composite index, which is not support when testing"
+ " against production. So it only runs against emulator which doesn't require "
+ "an index. ",
isRunningAgainstEmulator());

CollectionReference collection = testCollectionWithDocs(testDocs1);

AggregateQuerySnapshot snapshot =
waitFor(
collection
.aggregate(sum("pages"), sum("height"), sum("weight"))
.get(AggregateSource.SERVER));

Object sumPages = snapshot.get(sum("pages"));
Object sumHeight = snapshot.get(sum("height"));
Object sumWeight = snapshot.get(sum("weight"));
assertTrue(sumPages instanceof Long);
assertTrue(sumHeight instanceof Double);
assertTrue(sumWeight instanceof Double);
}

@Test
public void testCanGetCorrectDoubleTypeForSum() {
CollectionReference collection = testCollectionWithDocs(testDocs1);
Expand Down Expand Up @@ -411,31 +385,6 @@ public void testCanGetCorrectTypeForAvg() {
assertTrue(avg instanceof Double);
}

@Test
public void testCanPerformMaxAggregations() {
assumeTrue(
"This query requires a composite index, which is not support when testing"
+ " against production. So it only runs against emulator which doesn't require "
+ "an index. ",
isRunningAgainstEmulator());

CollectionReference collection = testCollectionWithDocs(testDocs1);
AggregateField f1 = sum("pages");
AggregateField f2 = average("pages");
AggregateField f3 = AggregateField.count();
AggregateField f4 = sum("foo");
AggregateField f5 = sum("bar");

AggregateQuerySnapshot snapshot =
waitFor(collection.aggregate(f1, f2, f3, f4, f5).get(AggregateSource.SERVER));

assertEquals(snapshot.get(f1), 150L);
assertEquals(snapshot.get(f2), 75.0);
assertEquals(snapshot.get(f3), 2L);
assertEquals(snapshot.get(f4), 2L);
assertEquals(snapshot.get(f5), 4L);
}

@Test
public void testCannotPerformMoreThanMaxAggregations() {
CollectionReference collection = testCollectionWithDocs(testDocs1);
Expand All @@ -455,51 +404,6 @@ public void testCannotPerformMoreThanMaxAggregations() {
assertTrue(firestoreException.getMessage().contains("maximum number of aggregations"));
}

@Test
public void testCanRunAggregateCollectionGroupQuery() {
assumeTrue(
"This query requires a composite index, which is not support when testing"
+ " against production. So it only runs against emulator which doesn't require "
+ "an index. ",
isRunningAgainstEmulator());

FirebaseFirestore db = testFirestore();
// Use .document() to get a random collection group name to use but ensure it starts with 'b'
// for predictable ordering.
String collectionGroup = "b" + db.collection("foo").document().getId();

String[] docPaths =
new String[] {
"abc/123/${collectionGroup}/cg-doc1",
"abc/123/${collectionGroup}/cg-doc2",
"${collectionGroup}/cg-doc3",
"${collectionGroup}/cg-doc4",
"def/456/${collectionGroup}/cg-doc5",
"${collectionGroup}/virtual-doc/nested-coll/not-cg-doc",
"x${collectionGroup}/not-cg-doc",
"${collectionGroup}x/not-cg-doc",
"abc/123/${collectionGroup}x/not-cg-doc",
"abc/123/x${collectionGroup}/not-cg-doc",
"abc/${collectionGroup}"
};
WriteBatch batch = db.batch();
for (String path : docPaths) {
batch.set(db.document(path.replace("${collectionGroup}", collectionGroup)), map("x", 2));
}
waitFor(batch.commit());

AggregateQuerySnapshot snapshot =
waitFor(
db.collectionGroup(collectionGroup)
.aggregate(AggregateField.count(), sum("x"), average("x"))
.get(AggregateSource.SERVER));
assertEquals(
5L, // "cg-doc1", "cg-doc2", "cg-doc3", "cg-doc4", "cg-doc5",
snapshot.get(AggregateField.count()));
assertEquals(10L, snapshot.get(sum("x")));
assertEquals((Double) 2.0, snapshot.get(average("x")));
}

@Test
public void testPerformsAggregationsWhenNaNExistsForSomeFieldValues() {
Map<String, Map<String, Object>> testDocs =
Expand Down Expand Up @@ -588,62 +492,6 @@ public void testPerformsAggregationWhenUsingInOperator() {
assertEquals(snapshot.get(AggregateField.count()), 2L);
}

@Test
public void testPerformsAggregationWhenUsingArrayContainsAnyOperator() {
assumeTrue(
"This query requires a composite index, which is not support when testing"
+ " against production. So it only runs against emulator which doesn't require "
+ "an index. ",
isRunningAgainstEmulator());

Map<String, Map<String, Object>> testDocs =
map(
"a",
map(
"author",
"authorA",
"title",
"titleA",
"pages",
100,
"year",
1980,
"rating",
asList(5, 1000)),
"b",
map(
"author", "authorB", "title", "titleB", "pages", 50, "year", 2020, "rating",
asList(4)),
"c",
map(
"author",
"authorC",
"title",
"titleC",
"pages",
100,
"year",
1980,
"rating",
asList(2222, 3)),
"d",
map(
"author", "authorD", "title", "titleD", "pages", 50, "year", 2020, "rating",
asList(0)));
CollectionReference collection = testCollectionWithDocs(testDocs);

AggregateQuerySnapshot snapshot =
waitFor(
collection
.whereArrayContainsAny("rating", asList(5, 3))
.aggregate(sum("pages"), average("pages"), AggregateField.count())
.get(AggregateSource.SERVER));

assertEquals(snapshot.get(sum("pages")), 200L);
assertEquals(snapshot.get(average("pages")), (Double) 100.0);
assertEquals(snapshot.get(AggregateField.count()), 2L);
}

@Test
public void testPerformsAggregationsOnNestedMapValues() {
Map<String, Map<String, Object>> testDocs =
Expand Down
Loading

0 comments on commit cfa4592

Please sign in to comment.