Skip to content

Mila/count non lite api tests #6581

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 6 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 25 additions & 2 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { toByteStreamReader } from '../platform/byte_stream_reader';
import { newSerializer, newTextEncoder } from '../platform/serializer';
import { Datastore } from '../remote/datastore';
import {
canUseNetwork,
RemoteStore,
remoteStoreDisableNetwork,
remoteStoreEnableNetwork,
Expand Down Expand Up @@ -507,9 +508,31 @@ export function firestoreClientRunAggregationQuery(
client: FirestoreClient,
query: AggregateQuery
): Promise<AggregateQuerySnapshot> {
return client.asyncQueue.enqueue(() => {
return getAggregate(query);
const deferred = new Deferred<AggregateQuerySnapshot>();
client.asyncQueue.enqueueAndForget(async () => {
const remoteStore = await getRemoteStore(client);
if (!canUseNetwork(remoteStore)) {
logDebug(
LOG_TAG,
'The network is disabled. The task returned by ' +
"'getAggregateFromServerDirect()' will not complete until the network is enabled."
);
deferred.reject(
new FirestoreError(
Code.UNAVAILABLE,
'Failed to get aggregate result because the client is offline.'
)
);
} else {
try {
const result = await getAggregate(query);
deferred.resolve(result);
} catch (e) {
deferred.reject(e as Error);
}
}
});
return deferred.promise;
}

async function readDocumentFromCache(
Expand Down
117 changes: 109 additions & 8 deletions packages/firestore/test/integration/api/aggregation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,125 @@
import { expect } from 'chai';

import {
collection,
collectionGroup,
countQuery,
doc,
getAggregateFromServerDirect,
query
query,
terminate,
where,
writeBatch
} from '../util/firebase_export';
import { apiDescribe, withTestCollection } from '../util/helpers';
import {
apiDescribe,
postConverter,
withTestCollection,
withTestDb
} from '../util/helpers';

apiDescribe('Aggregation query', (persistence: boolean) => {
it('can run count query getAggregateFromServerDirect', () => {
const testDocs = {
a: { k: 'a', sort: 1 },
b: { k: 'b', sort: 2 },
c: { k: 'c', sort: 2 }
a: { author: 'authorA', title: 'titleA' },
b: { author: 'authorB', title: 'titleB' }
};
return withTestCollection(persistence, testDocs, async collection => {
const countQuery_ = countQuery(collection);
const snapshot = await getAggregateFromServerDirect(countQuery_);
expect(snapshot.getCount()).to.equal(2);
});
});

it('aggregateQuery.query equals to original query', () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA' }
};
return withTestCollection(persistence, testDocs, async coll => {
const query_ = query(coll);
return withTestCollection(persistence, testDocs, async collection => {
const query_ = query(collection, where('author', '==', 'authorA'));
const aggregateQuery_ = countQuery(query_);
expect(aggregateQuery_.query).to.be.equal(query_);
});
});

it('aggregateQuerySnapshot.query equals to aggregateQuery', () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA' }
};
return withTestCollection(persistence, testDocs, async collection => {
const query_ = query(collection, where('author', '==', 'authorA'));
const aggregateQuery_ = countQuery(query_);
const snapshot = await getAggregateFromServerDirect(aggregateQuery_);
expect(snapshot.query).to.be.equal(aggregateQuery_);
});
});

it('aggregate query supports withConverter', () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA' },
b: { author: 'authorB', title: 'titleB' }
};
return withTestCollection(persistence, testDocs, async collection => {
const query_ = query(
collection,
where('author', '==', 'authorA')
).withConverter(postConverter);
const countQuery_ = countQuery(query_);
const snapshot = await getAggregateFromServerDirect(countQuery_);
expect(snapshot.getCount()).to.equal(3);
expect(snapshot.getCount()).to.equal(1);
});
});

it('aggregate 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 countQuery_ = countQuery(collectionGroup(db, collectionGroupId));
const snapshot = await getAggregateFromServerDirect(countQuery_);
expect(snapshot.getCount()).to.equal(2);
});
});

it('aggregate query fails if firestore is terminated', () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA' }
};
return withTestCollection(
persistence,
testDocs,
async (collection, firestore) => {
await terminate(firestore);
const countQuery_ = countQuery(collection);
expect(() => getAggregateFromServerDirect(countQuery_)).to.throw(
'The client has already been terminated.'
);
}
);
});

it("terminate doesn't crash when there is flying aggregate query", () => {
const testDocs = {
a: { author: 'authorA', title: 'titleA' }
};
return withTestCollection(
persistence,
testDocs,
async (collection, firestore) => {
const countQuery_ = countQuery(collection);
getAggregateFromServerDirect(countQuery_).then();
await terminate(firestore);
}
);
});
});
24 changes: 23 additions & 1 deletion packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import {
setDoc,
PrivateSettings,
SnapshotListenOptions,
newTestFirestore
newTestFirestore,
QueryDocumentSnapshot
} from './firebase_export';
import {
ALT_PROJECT_ID,
Expand Down Expand Up @@ -280,3 +281,24 @@ export function withTestCollectionSettings(
}
);
}

export class Post {
constructor(
readonly title: string,
readonly author: string,
readonly ref: DocumentReference | null = null
) {}
byline(): string {
return this.title + ', by ' + this.author;
}
}

export const postConverter = {
toFirestore(post: Post): DocumentData {
return { title: post.title, author: post.author };
},
fromFirestore(snapshot: QueryDocumentSnapshot): Post {
const data = snapshot.data();
return new Post(data.title, data.author, snapshot.ref);
}
};
14 changes: 7 additions & 7 deletions packages/firestore/test/lite/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2047,8 +2047,8 @@ describe('withConverter() support', () => {

describe('countQuery()', () => {
it('AggregateQuery and AggregateQuerySnapshot inherits the original query', () => {
return withTestCollection(async coll => {
const query_ = query(coll);
return withTestCollection(async collection => {
const query_ = query(collection);
const countQuery_ = countQuery(query_);
const snapshot = await getAggregate(countQuery_);
expect(countQuery_.query).to.equal(query_);
Expand All @@ -2058,8 +2058,8 @@ describe('countQuery()', () => {
});

it('empty test collection count', () => {
return withTestCollection(async coll => {
const countQuery_ = countQuery(query(coll));
return withTestCollection(async collection => {
const countQuery_ = countQuery(collection);
const snapshot = await getAggregate(countQuery_);
expect(snapshot.getCount()).to.equal(0);
});
Expand All @@ -2072,7 +2072,7 @@ describe('countQuery()', () => {
{ author: 'authorB', title: 'titleC' }
];
return withTestCollectionAndInitialData(testDocs, async collection => {
const countQuery_ = countQuery(query(collection));
const countQuery_ = countQuery(collection);
const snapshot = await getAggregate(countQuery_);
expect(snapshot.getCount()).to.equal(3);
});
Expand Down Expand Up @@ -2311,7 +2311,7 @@ describe('countQuery()', () => {
it('count query on a terminated Firestore', () => {
return withTestCollection(async collection => {
await terminate(collection.firestore);
const countQuery_ = countQuery(query(collection));
const countQuery_ = countQuery(collection);
expect(() => getAggregate(countQuery_)).to.throw(
'The client has already been terminated.'
);
Expand All @@ -2325,7 +2325,7 @@ describe('countQuery()', () => {
{ author: 'authorB', title: 'titleC' }
];
return withTestCollectionAndInitialData(testDocs, async collection => {
const countQuery_ = countQuery(query(collection));
const countQuery_ = countQuery(collection);
const promise = getAggregate(countQuery_);
await terminate(collection.firestore);
const snapshot = await promise;
Expand Down