Skip to content
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

Move sum&avg composite index tests to composite_index_query.test.ts #7713

Merged
merged 5 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/firestore/firestore_collection_group_index_config.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 packages/firestore/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,82 @@ provider "google" {
project = var.projectId
}

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 = 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 = 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
}
}
}
152 changes: 0 additions & 152 deletions packages/firestore/test/integration/api/aggregation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1385,156 +1385,4 @@ apiDescribe('Aggregation queries - sum / average', persistence => {
expect(snapshot.data().countOfDocs).to.equal(4);
});
});

// Only run tests that require indexes against the emulator, because we don't
// have a way to dynamically create the indexes when running the tests.
(USE_EMULATOR ? apiDescribe : apiDescribe.skip)(
'queries requiring indexes',
() => {
it('aggregate query supports collection groups - multi-aggregate', () => {
return withTestDb(persistence, async db => {
const collectionGroupId = doc(
collection(db, 'aggregateQueryTest')
).id;
const docPaths = [
`${collectionGroupId}/cg-doc1`,
`abc/123/${collectionGroupId}/cg-doc2`,
`zzz${collectionGroupId}/cg-doc3`,
`abc/123/zzz${collectionGroupId}/cg-doc4`,
`abc/123/zzz/${collectionGroupId}`
];
const batch = writeBatch(db);
for (const docPath of docPaths) {
batch.set(doc(db, docPath), { x: 2 });
}
await batch.commit();
const snapshot = await getAggregateFromServer(
collectionGroup(db, collectionGroupId),
{
count: count(),
sum: sum('x'),
avg: average('x')
}
);
expect(snapshot.data().count).to.equal(2);
expect(snapshot.data().sum).to.equal(4);
expect(snapshot.data().avg).to.equal(2);
});
});

it('performs aggregations on documents with all aggregated fields using getAggregationFromServer', () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA', pages: 100, year: 1980 },
b: { author: 'authorB', title: 'titleB', pages: 50, year: 2020 },
c: { author: 'authorC', title: 'titleC', pages: 150, year: 2021 },
d: { author: 'authorD', title: 'titleD', pages: 50 }
};
return withTestCollection(persistence, testDocs, async coll => {
const snapshot = await getAggregateFromServer(coll, {
totalPages: sum('pages'),
averagePages: average('pages'),
averageYear: average('year'),
count: count()
});
expect(snapshot.data().totalPages).to.equal(300);
expect(snapshot.data().averagePages).to.equal(100);
expect(snapshot.data().averageYear).to.equal(2007);
expect(snapshot.data().count).to.equal(3);
});
});

it('performs aggregates on multiple fields where one aggregate could cause short-circuit due to NaN using getAggregationFromServer', () => {
const testDocs = {
a: {
author: 'authorA',
title: 'titleA',
pages: 100,
year: 1980,
rating: 5
},
b: {
author: 'authorB',
title: 'titleB',
pages: 50,
year: 2020,
rating: 4
},
c: {
author: 'authorC',
title: 'titleC',
pages: 100,
year: 1980,
rating: Number.NaN
},
d: {
author: 'authorD',
title: 'titleD',
pages: 50,
year: 2020,
rating: 0
}
};
return withTestCollection(persistence, testDocs, async coll => {
const snapshot = await getAggregateFromServer(coll, {
totalRating: sum('rating'),
totalPages: sum('pages'),
averageYear: average('year')
});
expect(snapshot.data().totalRating).to.be.NaN;
expect(snapshot.data().totalPages).to.equal(300);
expect(snapshot.data().averageYear).to.equal(2000);
});
});

it('performs aggregates when using `array-contains-any` operator getAggregationFromServer', () => {
const testDocs = {
a: {
author: 'authorA',
title: 'titleA',
pages: 100,
year: 1980,
rating: [5, 1000]
},
b: {
author: 'authorB',
title: 'titleB',
pages: 50,
year: 2020,
rating: [4]
},
c: {
author: 'authorC',
title: 'titleC',
pages: 100,
year: 1980,
rating: [2222, 3]
},
d: {
author: 'authorD',
title: 'titleD',
pages: 50,
year: 2020,
rating: [0]
}
};
return withTestCollection(persistence, testDocs, async coll => {
const snapshot = await getAggregateFromServer(
query(coll, where('rating', 'array-contains-any', [5, 3])),
{
totalRating: sum('rating'),
averageRating: average('rating'),
totalPages: sum('pages'),
averagePages: average('pages'),
countOfDocs: count()
}
);
expect(snapshot.data().totalRating).to.equal(0);
expect(snapshot.data().averageRating).to.be.null;
expect(snapshot.data().totalPages).to.equal(200);
expect(snapshot.data().averagePages).to.equal(100);
expect(snapshot.data().countOfDocs).to.equal(2);
});
});
}
);
});
Loading
Loading