Skip to content
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
146 changes: 71 additions & 75 deletions packages/firestore/test/integration/api/aggregation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,90 +36,86 @@ import {
withTestCollection,
withTestDb
} from '../util/helpers';
import { USE_EMULATOR } from '../util/settings';

(USE_EMULATOR ? apiDescribe : apiDescribe.skip)(
'Count quries',
(persistence: boolean) => {
it('can run count query getCountFromServer', () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA' },
b: { author: 'authorB', title: 'titleB' }
};
return withTestCollection(persistence, testDocs, async coll => {
const snapshot = await getCountFromServer(coll);
expect(snapshot.data().count).to.equal(2);
});
apiDescribe('Count quries', (persistence: boolean) => {
it('can run count query getCountFromServer', () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA' },
b: { author: 'authorB', title: 'titleB' }
};
return withTestCollection(persistence, testDocs, async coll => {
const snapshot = await getCountFromServer(coll);
expect(snapshot.data().count).to.equal(2);
});
});

it("count query doesn't use converter", () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA' },
b: { author: 'authorB', title: 'titleB' }
};
const throwingConverter = {
toFirestore(obj: never): DocumentData {
throw new Error('should never be called');
},
fromFirestore(snapshot: QueryDocumentSnapshot): never {
throw new Error('should never be called');
}
};
return withTestCollection(persistence, testDocs, async coll => {
const query_ = query(
coll,
where('author', '==', 'authorA')
).withConverter(throwingConverter);
const snapshot = await getCountFromServer(query_);
expect(snapshot.data().count).to.equal(1);
});
it("count query doesn't use converter", () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA' },
b: { author: 'authorB', title: 'titleB' }
};
const throwingConverter = {
toFirestore(obj: never): DocumentData {
throw new Error('should never be called');
},
fromFirestore(snapshot: QueryDocumentSnapshot): never {
throw new Error('should never be called');
}
};
return withTestCollection(persistence, testDocs, async coll => {
const query_ = query(
coll,
where('author', '==', 'authorA')
).withConverter(throwingConverter);
const snapshot = await getCountFromServer(query_);
expect(snapshot.data().count).to.equal(1);
});
});

it('count query supports collection groups', () => {
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: 1 });
}
await batch.commit();
const snapshot = await getCountFromServer(
collectionGroup(db, collectionGroupId)
);
expect(snapshot.data().count).to.equal(2);
});
it('count query supports collection groups', () => {
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: 1 });
}
await batch.commit();
const snapshot = await getCountFromServer(
collectionGroup(db, collectionGroupId)
);
expect(snapshot.data().count).to.equal(2);
});
});

it('getCountFromServer fails if firestore is terminated', () => {
return withEmptyTestCollection(persistence, async (coll, firestore) => {
await terminate(firestore);
expect(() => getCountFromServer(coll)).to.throw(
'The client has already been terminated.'
);
});
it('getCountFromServer fails if firestore is terminated', () => {
return withEmptyTestCollection(persistence, async (coll, firestore) => {
await terminate(firestore);
expect(() => getCountFromServer(coll)).to.throw(
'The client has already been terminated.'
);
});
});

it("terminate doesn't crash when there is count query in flight", () => {
return withEmptyTestCollection(persistence, async (coll, firestore) => {
void getCountFromServer(coll);
await terminate(firestore);
});
it("terminate doesn't crash when there is count query in flight", () => {
return withEmptyTestCollection(persistence, async (coll, firestore) => {
void getCountFromServer(coll);
await terminate(firestore);
});
});

it('getCountFromServer fails if user is offline', () => {
return withEmptyTestCollection(persistence, async (coll, firestore) => {
await disableNetwork(firestore);
await expect(getCountFromServer(coll)).to.be.eventually.rejectedWith(
'Failed to get count result because the client is offline'
);
});
it('getCountFromServer fails if user is offline', () => {
return withEmptyTestCollection(persistence, async (coll, firestore) => {
await disableNetwork(firestore);
await expect(getCountFromServer(coll)).to.be.eventually.rejectedWith(
'Failed to get count result because the client is offline'
);
});
}
);
});
});
122 changes: 0 additions & 122 deletions packages/firestore/test/integration/api_internal/aggregation.test.ts

This file was deleted.