Skip to content

Mila/count add tests #6566

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 10 commits into from
Aug 30, 2022
86 changes: 79 additions & 7 deletions packages/firestore/test/lite/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2047,11 +2047,12 @@ describe('withConverter() support', () => {

describe('countQuery()', () => {
const testDocs = [
{ author: 'authorA', title: 'titleA' },
{ author: 'authorA', title: 'titleB' },
{ author: 'authorB', title: 'titleC' },
{ author: 'authorB', title: 'titleD' },
{ author: 'authorB', title: 'titleE' }
{ id: 3, author: 'authorA', title: 'titleA' },
{ id: 4, author: 'authorA', title: 'titleB' },
{ id: 2, author: 'authorB', title: 'titleC' },
{ id: null, author: 'authorB', title: 'titleD' },
{ id: 1, author: 'authorB' },
{ id: 5, author: null, title: 'titleE' }
];

it('AggregateQuery and AggregateQuerySnapshot inherits the original query', () => {
Expand All @@ -2073,11 +2074,11 @@ describe('countQuery()', () => {
});
});

it('test collection count with 5 docs', () => {
it('test collection count with 6 docs', () => {
return withTestCollectionAndInitialData(testDocs, async collection => {
const countQuery_ = countQuery(query(collection));
const snapshot = await getAggregateFromServerDirect(countQuery_);
expect(snapshot.getCount()).to.equal(5);
expect(snapshot.getCount()).to.equal(6);
});
});

Expand Down Expand Up @@ -2116,6 +2117,45 @@ describe('countQuery()', () => {
});
});

it('count with filter and order by', () => {
return withTestCollectionAndInitialData(testDocs, async collection => {
const query1 = query(
collection,
where('author', '==', 'authorB'),
orderBy('title')
);
const countQuery_ = countQuery(query1);
const snapshot = await getAggregateFromServerDirect(countQuery_);
expect(snapshot.getCount()).to.equal(2);
});
});

it('count with order by and startAt, startAfter', () => {
return withTestCollectionAndInitialData(testDocs, async collection => {
const query1 = query(collection, orderBy('id'), startAt(2));
const query2 = query(collection, orderBy('id'), startAfter(2));
const countQuery1 = countQuery(query1);
const countQuery2 = countQuery(query2);
const snapshot1 = await getAggregateFromServerDirect(countQuery1);
const snapshot2 = await getAggregateFromServerDirect(countQuery2);
expect(snapshot1.getCount()).to.equal(4);
expect(snapshot2.getCount()).to.equal(3);
});
});

it('count with order by and endAt, endBefore', () => {
return withTestCollectionAndInitialData(testDocs, async collection => {
const query1 = query(collection, orderBy('id'), startAt(1), endAt(2));
const query2 = query(collection, orderBy('id'), startAt(1), endBefore(2));
const countQuery1 = countQuery(query1);
const countQuery2 = countQuery(query2);
const snapshot1 = await getAggregateFromServerDirect(countQuery1);
const snapshot2 = await getAggregateFromServerDirect(countQuery2);
expect(snapshot1.getCount()).to.equal(2);
expect(snapshot2.getCount()).to.equal(1);
});
});

it('test collection count with converter on query', () => {
return withTestCollectionAndInitialData(testDocs, async collection => {
const query_ = query(
Expand All @@ -2128,6 +2168,28 @@ describe('countQuery()', () => {
});
});

it('count query with collection groups', () => {
return withTestDb(async db => {
const collectionGroupId = doc(collection(db, 'countTest')).id;
const docPaths = [
`${collectionGroupId}/cg-doc1`,
`abc/123/${collectionGroupId}/cg-doc2`,
`invalid${collectionGroupId}/cg-doc3`,
`abc/123/invalid${collectionGroupId}/cg-doc4`,
`abc/123/invalid/${collectionGroupId}`
];
const batch = writeBatch(db);
for (const docPath of docPaths) {
batch.set(doc(db, docPath), { x: 1 });
}
await batch.commit();

const countQuery_ = countQuery(collectionGroup(db, collectionGroupId));
const snapshot = await getAggregateFromServerDirect(countQuery_);
expect(snapshot.getCount()).to.equal(2);
});
});

it('aggregateQueryEqual on same queries', () => {
return withTestCollectionAndInitialData(testDocs, async collection => {
const query1 = query(collection, where('author', '==', 'authorA'));
Expand Down Expand Up @@ -2174,4 +2236,14 @@ describe('countQuery()', () => {
expect(aggregateQuerySnapshotEqual(snapshot1, snapshot2)).to.be.false;
});
});

it('count query on a terminated Firestore', () => {
return withTestCollection(async collection => {
await terminate(collection.firestore);
const countQuery_ = countQuery(query(collection));
expect(() => getAggregateFromServerDirect(countQuery_)).to.throw(
'The client has already been terminated'
);
});
});
});