Skip to content

Commit cac9165

Browse files
authored
Move sum&avg composite index tests to composite_index_query.test.ts (#7713)
1 parent f002ef3 commit cac9165

6 files changed

+314
-166
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
locals {
2+
collection_group_indexes = {
3+
index1 = [
4+
{
5+
field_path = "testId"
6+
order = "ASCENDING"
7+
},
8+
{
9+
field_path = "a"
10+
order = "ASCENDING"
11+
},
12+
]
13+
14+
}
15+
}

packages/firestore/firestore_index_config.tf renamed to packages/firestore/firestore_composite_index_config.tf

+50
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,55 @@ locals {
100100
order = "DESCENDING"
101101
},
102102
]
103+
index9 = [
104+
{
105+
field_path = "testId"
106+
order = "ASCENDING"
107+
},
108+
{
109+
field_path = "pages"
110+
order = "ASCENDING"
111+
},
112+
{
113+
field_path = "year"
114+
order = "ASCENDING"
115+
},
116+
]
117+
index10 = [
118+
{
119+
field_path = "testId"
120+
order = "ASCENDING"
121+
},
122+
{
123+
field_path = "pages"
124+
order = "ASCENDING"
125+
},
126+
{
127+
field_path = "rating"
128+
order = "ASCENDING"
129+
},
130+
{
131+
field_path = "year"
132+
order = "ASCENDING"
133+
},
134+
]
135+
index11 = [
136+
{
137+
field_path = "rating"
138+
array_config = "CONTAINS"
139+
},
140+
{
141+
field_path = "testId"
142+
order = "ASCENDING"
143+
},
144+
{
145+
field_path = "pages"
146+
order = "ASCENDING"
147+
},
148+
{
149+
field_path = "rating"
150+
order = "ASCENDING"
151+
},
152+
]
103153
}
104154
}

packages/firestore/main.tf

+54-10
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,82 @@ provider "google" {
44
project = var.projectId
55
}
66

7-
resource "google_firestore_index" "default-db-index" {
7+
resource "google_firestore_index" "default_db_index" {
88
collection = "composite-index-test-collection"
99

1010
for_each = local.indexes
1111
dynamic "fields" {
1212
for_each = distinct(flatten([for k, v in local.indexes : [
1313
for i in each.value : {
14-
field_path = i.field_path
15-
order = i.order
14+
field_path = i.field_path
15+
order = can(i.order) ? i.order : null
16+
array_config = can(i.array_config) ? i.array_config : null
1617
}]]))
1718
content {
18-
field_path = lookup(fields.value, "field_path", null)
19-
order = lookup(fields.value, "order", null)
19+
field_path = fields.value.field_path
20+
order = fields.value.order
21+
array_config = fields.value.array_config
2022
}
2123
}
24+
}
25+
26+
resource "google_firestore_index" "default_db_collection_group_index" {
27+
collection = "composite-index-test-collection"
28+
query_scope = "COLLECTION_GROUP"
2229

30+
for_each = local.collection_group_indexes
31+
dynamic "fields" {
32+
for_each = distinct(flatten([for k, v in local.indexes : [
33+
for i in each.value : {
34+
field_path = i.field_path
35+
order = can(i.order) ? i.order : null
36+
array_config = can(i.array_config) ? i.array_config : null
37+
}]]))
38+
content {
39+
field_path = fields.value.field_path
40+
order = fields.value.order
41+
array_config = fields.value.array_config
42+
}
43+
}
2344
}
2445

25-
resource "google_firestore_index" "named-db-index" {
46+
resource "google_firestore_index" "named_db_index" {
2647
collection = "composite-index-test-collection"
2748
database = "test-db"
2849

2950
for_each = local.indexes
3051
dynamic "fields" {
3152
for_each = distinct(flatten([for k, v in local.indexes : [
3253
for i in each.value : {
33-
field_path = i.field_path
34-
order = i.order
54+
field_path = i.field_path
55+
order = can(i.order) ? i.order : null
56+
array_config = can(i.array_config) ? i.array_config : null
57+
}]]))
58+
content {
59+
field_path = fields.value.field_path
60+
order = fields.value.order
61+
array_config = fields.value.array_config
62+
}
63+
}
64+
}
65+
66+
resource "google_firestore_index" "named_db_collection_group_index" {
67+
collection = "composite-index-test-collection"
68+
database = "test-db"
69+
query_scope = "COLLECTION_GROUP"
70+
71+
for_each = local.collection_group_indexes
72+
dynamic "fields" {
73+
for_each = distinct(flatten([for k, v in local.indexes : [
74+
for i in each.value : {
75+
field_path = i.field_path
76+
order = can(i.order) ? i.order : null
77+
array_config = can(i.array_config) ? i.array_config : null
3578
}]]))
3679
content {
37-
field_path = lookup(fields.value, "field_path", null)
38-
order = lookup(fields.value, "order", null)
80+
field_path = fields.value.field_path
81+
order = fields.value.order
82+
array_config = fields.value.array_config
3983
}
4084
}
4185
}

packages/firestore/test/integration/api/aggregation.test.ts

-152
Original file line numberDiff line numberDiff line change
@@ -1385,156 +1385,4 @@ apiDescribe('Aggregation queries - sum / average', persistence => {
13851385
expect(snapshot.data().countOfDocs).to.equal(4);
13861386
});
13871387
});
1388-
1389-
// Only run tests that require indexes against the emulator, because we don't
1390-
// have a way to dynamically create the indexes when running the tests.
1391-
(USE_EMULATOR ? apiDescribe : apiDescribe.skip)(
1392-
'queries requiring indexes',
1393-
() => {
1394-
it('aggregate query supports collection groups - multi-aggregate', () => {
1395-
return withTestDb(persistence, async db => {
1396-
const collectionGroupId = doc(
1397-
collection(db, 'aggregateQueryTest')
1398-
).id;
1399-
const docPaths = [
1400-
`${collectionGroupId}/cg-doc1`,
1401-
`abc/123/${collectionGroupId}/cg-doc2`,
1402-
`zzz${collectionGroupId}/cg-doc3`,
1403-
`abc/123/zzz${collectionGroupId}/cg-doc4`,
1404-
`abc/123/zzz/${collectionGroupId}`
1405-
];
1406-
const batch = writeBatch(db);
1407-
for (const docPath of docPaths) {
1408-
batch.set(doc(db, docPath), { x: 2 });
1409-
}
1410-
await batch.commit();
1411-
const snapshot = await getAggregateFromServer(
1412-
collectionGroup(db, collectionGroupId),
1413-
{
1414-
count: count(),
1415-
sum: sum('x'),
1416-
avg: average('x')
1417-
}
1418-
);
1419-
expect(snapshot.data().count).to.equal(2);
1420-
expect(snapshot.data().sum).to.equal(4);
1421-
expect(snapshot.data().avg).to.equal(2);
1422-
});
1423-
});
1424-
1425-
it('performs aggregations on documents with all aggregated fields using getAggregationFromServer', () => {
1426-
const testDocs = {
1427-
a: { author: 'authorA', title: 'titleA', pages: 100, year: 1980 },
1428-
b: { author: 'authorB', title: 'titleB', pages: 50, year: 2020 },
1429-
c: { author: 'authorC', title: 'titleC', pages: 150, year: 2021 },
1430-
d: { author: 'authorD', title: 'titleD', pages: 50 }
1431-
};
1432-
return withTestCollection(persistence, testDocs, async coll => {
1433-
const snapshot = await getAggregateFromServer(coll, {
1434-
totalPages: sum('pages'),
1435-
averagePages: average('pages'),
1436-
averageYear: average('year'),
1437-
count: count()
1438-
});
1439-
expect(snapshot.data().totalPages).to.equal(300);
1440-
expect(snapshot.data().averagePages).to.equal(100);
1441-
expect(snapshot.data().averageYear).to.equal(2007);
1442-
expect(snapshot.data().count).to.equal(3);
1443-
});
1444-
});
1445-
1446-
it('performs aggregates on multiple fields where one aggregate could cause short-circuit due to NaN using getAggregationFromServer', () => {
1447-
const testDocs = {
1448-
a: {
1449-
author: 'authorA',
1450-
title: 'titleA',
1451-
pages: 100,
1452-
year: 1980,
1453-
rating: 5
1454-
},
1455-
b: {
1456-
author: 'authorB',
1457-
title: 'titleB',
1458-
pages: 50,
1459-
year: 2020,
1460-
rating: 4
1461-
},
1462-
c: {
1463-
author: 'authorC',
1464-
title: 'titleC',
1465-
pages: 100,
1466-
year: 1980,
1467-
rating: Number.NaN
1468-
},
1469-
d: {
1470-
author: 'authorD',
1471-
title: 'titleD',
1472-
pages: 50,
1473-
year: 2020,
1474-
rating: 0
1475-
}
1476-
};
1477-
return withTestCollection(persistence, testDocs, async coll => {
1478-
const snapshot = await getAggregateFromServer(coll, {
1479-
totalRating: sum('rating'),
1480-
totalPages: sum('pages'),
1481-
averageYear: average('year')
1482-
});
1483-
expect(snapshot.data().totalRating).to.be.NaN;
1484-
expect(snapshot.data().totalPages).to.equal(300);
1485-
expect(snapshot.data().averageYear).to.equal(2000);
1486-
});
1487-
});
1488-
1489-
it('performs aggregates when using `array-contains-any` operator getAggregationFromServer', () => {
1490-
const testDocs = {
1491-
a: {
1492-
author: 'authorA',
1493-
title: 'titleA',
1494-
pages: 100,
1495-
year: 1980,
1496-
rating: [5, 1000]
1497-
},
1498-
b: {
1499-
author: 'authorB',
1500-
title: 'titleB',
1501-
pages: 50,
1502-
year: 2020,
1503-
rating: [4]
1504-
},
1505-
c: {
1506-
author: 'authorC',
1507-
title: 'titleC',
1508-
pages: 100,
1509-
year: 1980,
1510-
rating: [2222, 3]
1511-
},
1512-
d: {
1513-
author: 'authorD',
1514-
title: 'titleD',
1515-
pages: 50,
1516-
year: 2020,
1517-
rating: [0]
1518-
}
1519-
};
1520-
return withTestCollection(persistence, testDocs, async coll => {
1521-
const snapshot = await getAggregateFromServer(
1522-
query(coll, where('rating', 'array-contains-any', [5, 3])),
1523-
{
1524-
totalRating: sum('rating'),
1525-
averageRating: average('rating'),
1526-
totalPages: sum('pages'),
1527-
averagePages: average('pages'),
1528-
countOfDocs: count()
1529-
}
1530-
);
1531-
expect(snapshot.data().totalRating).to.equal(0);
1532-
expect(snapshot.data().averageRating).to.be.null;
1533-
expect(snapshot.data().totalPages).to.equal(200);
1534-
expect(snapshot.data().averagePages).to.equal(100);
1535-
expect(snapshot.data().countOfDocs).to.equal(2);
1536-
});
1537-
});
1538-
}
1539-
);
15401388
});

0 commit comments

Comments
 (0)