Skip to content

Commit 8b86ffc

Browse files
committed
upload tests
1 parent fe871ce commit 8b86ffc

File tree

4 files changed

+164
-18
lines changed

4 files changed

+164
-18
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { toByteStreamReader } from '../platform/byte_stream_reader';
4040
import { newSerializer, newTextEncoder } from '../platform/serializer';
4141
import { Datastore } from '../remote/datastore';
4242
import {
43+
canUseNetwork,
4344
RemoteStore,
4445
remoteStoreDisableNetwork,
4546
remoteStoreEnableNetwork,
@@ -507,9 +508,31 @@ export function firestoreClientRunAggregationQuery(
507508
client: FirestoreClient,
508509
query: AggregateQuery
509510
): Promise<AggregateQuerySnapshot> {
510-
return client.asyncQueue.enqueue(() => {
511-
return getAggregate(query);
511+
const deferred = new Deferred<AggregateQuerySnapshot>();
512+
client.asyncQueue.enqueueAndForget(async () => {
513+
const remoteStore = await getRemoteStore(client);
514+
if (!canUseNetwork(remoteStore)) {
515+
logDebug(
516+
LOG_TAG,
517+
'The network is disabled. The task returned by ' +
518+
"'getAggregateFromServerDirect()' will not complete until the network is enabled."
519+
);
520+
deferred.reject(
521+
new FirestoreError(
522+
Code.UNAVAILABLE,
523+
'Failed to get aggregate result because the client is offline.'
524+
)
525+
);
526+
} else {
527+
try {
528+
const result = await getAggregate(query);
529+
deferred.resolve(result);
530+
} catch (e) {
531+
deferred.reject(e as Error);
532+
}
533+
}
512534
});
535+
return deferred.promise;
513536
}
514537

515538
async function readDocumentFromCache(

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

Lines changed: 109 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,125 @@
1818
import { expect } from 'chai';
1919

2020
import {
21+
collection,
22+
collectionGroup,
2123
countQuery,
24+
doc,
2225
getAggregateFromServerDirect,
23-
query
26+
query,
27+
terminate,
28+
where,
29+
writeBatch
2430
} from '../util/firebase_export';
25-
import { apiDescribe, withTestCollection } from '../util/helpers';
31+
import {
32+
apiDescribe,
33+
postConverter,
34+
withTestCollection,
35+
withTestDb
36+
} from '../util/helpers';
2637

2738
apiDescribe('Aggregation query', (persistence: boolean) => {
2839
it('can run count query getAggregateFromServerDirect', () => {
2940
const testDocs = {
30-
a: { k: 'a', sort: 1 },
31-
b: { k: 'b', sort: 2 },
32-
c: { k: 'c', sort: 2 }
41+
a: { author: 'authorA', title: 'titleA' },
42+
b: { author: 'authorB', title: 'titleB' }
43+
};
44+
return withTestCollection(persistence, testDocs, async collection => {
45+
const countQuery_ = countQuery(collection);
46+
const snapshot = await getAggregateFromServerDirect(countQuery_);
47+
expect(snapshot.getCount()).to.equal(2);
48+
});
49+
});
50+
51+
it('aggregateQuery.query equals to original query', () => {
52+
const testDocs = {
53+
a: { author: 'authorA', title: 'titleA' }
3354
};
34-
return withTestCollection(persistence, testDocs, async coll => {
35-
const query_ = query(coll);
55+
return withTestCollection(persistence, testDocs, async collection => {
56+
const query_ = query(collection, where('author', '==', 'authorA'));
57+
const aggregateQuery_ = countQuery(query_);
58+
expect(aggregateQuery_.query).to.be.equal(query_);
59+
});
60+
});
61+
62+
it('aggregateQuerySnapshot.query equals to aggregateQuery', () => {
63+
const testDocs = {
64+
a: { author: 'authorA', title: 'titleA' }
65+
};
66+
return withTestCollection(persistence, testDocs, async collection => {
67+
const query_ = query(collection, where('author', '==', 'authorA'));
68+
const aggregateQuery_ = countQuery(query_);
69+
const snapshot = await getAggregateFromServerDirect(aggregateQuery_);
70+
expect(snapshot.query).to.be.equal(aggregateQuery_);
71+
});
72+
});
73+
74+
it('aggregate query supports withConverter on query', () => {
75+
const testDocs = {
76+
a: { author: 'authorA', title: 'titleA' },
77+
b: { author: 'authorB', title: 'titleB' }
78+
};
79+
return withTestCollection(persistence, testDocs, async collection => {
80+
const query_ = query(
81+
collection,
82+
where('author', '==', 'authorA')
83+
).withConverter(postConverter);
3684
const countQuery_ = countQuery(query_);
3785
const snapshot = await getAggregateFromServerDirect(countQuery_);
38-
expect(snapshot.getCount()).to.equal(3);
86+
expect(snapshot.getCount()).to.equal(1);
3987
});
4088
});
89+
90+
it('aggregate query supports collection groups', () => {
91+
return withTestDb(persistence, async db => {
92+
const collectionGroupId = doc(collection(db, 'aggregateQueryTest')).id;
93+
const docPaths = [
94+
`${collectionGroupId}/cg-doc1`,
95+
`abc/123/${collectionGroupId}/cg-doc2`,
96+
`zzz${collectionGroupId}/cg-doc3`,
97+
`abc/123/zzz${collectionGroupId}/cg-doc4`,
98+
`abc/123/zzz/${collectionGroupId}`
99+
];
100+
const batch = writeBatch(db);
101+
for (const docPath of docPaths) {
102+
batch.set(doc(db, docPath), { x: 1 });
103+
}
104+
await batch.commit();
105+
const countQuery_ = countQuery(collectionGroup(db, collectionGroupId));
106+
const snapshot = await getAggregateFromServerDirect(countQuery_);
107+
expect(snapshot.getCount()).to.equal(2);
108+
});
109+
});
110+
111+
it('getAggregateFromServerDirect fails if firestore is terminated', () => {
112+
const testDocs = {
113+
a: { author: 'authorA', title: 'titleA' }
114+
};
115+
return withTestCollection(
116+
persistence,
117+
testDocs,
118+
async (collection, firestore) => {
119+
await terminate(firestore);
120+
const countQuery_ = countQuery(collection);
121+
expect(() => getAggregateFromServerDirect(countQuery_)).to.throw(
122+
'The client has already been terminated.'
123+
);
124+
}
125+
);
126+
});
127+
128+
it("terminate doesn't crash when there is flying aggregate query", () => {
129+
const testDocs = {
130+
a: { author: 'authorA', title: 'titleA' }
131+
};
132+
return withTestCollection(
133+
persistence,
134+
testDocs,
135+
async (collection, firestore) => {
136+
const countQuery_ = countQuery(collection);
137+
getAggregateFromServerDirect(countQuery_).then();
138+
await terminate(firestore);
139+
}
140+
);
141+
});
41142
});

packages/firestore/test/integration/util/helpers.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import {
3131
setDoc,
3232
PrivateSettings,
3333
SnapshotListenOptions,
34-
newTestFirestore
34+
newTestFirestore,
35+
QueryDocumentSnapshot
3536
} from './firebase_export';
3637
import {
3738
ALT_PROJECT_ID,
@@ -280,3 +281,24 @@ export function withTestCollectionSettings(
280281
}
281282
);
282283
}
284+
285+
export class Post {
286+
constructor(
287+
readonly title: string,
288+
readonly author: string,
289+
readonly ref: DocumentReference | null = null
290+
) {}
291+
byline(): string {
292+
return this.title + ', by ' + this.author;
293+
}
294+
}
295+
296+
export const postConverter = {
297+
toFirestore(post: Post): DocumentData {
298+
return { title: post.title, author: post.author };
299+
},
300+
fromFirestore(snapshot: QueryDocumentSnapshot): Post {
301+
const data = snapshot.data();
302+
return new Post(data.title, data.author, snapshot.ref);
303+
}
304+
};

packages/firestore/test/lite/integration.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,8 +2047,8 @@ describe('withConverter() support', () => {
20472047

20482048
describe('countQuery()', () => {
20492049
it('AggregateQuery and AggregateQuerySnapshot inherits the original query', () => {
2050-
return withTestCollection(async coll => {
2051-
const query_ = query(coll);
2050+
return withTestCollection(async collection => {
2051+
const query_ = query(collection);
20522052
const countQuery_ = countQuery(query_);
20532053
const snapshot = await getAggregate(countQuery_);
20542054
expect(countQuery_.query).to.equal(query_);
@@ -2058,8 +2058,8 @@ describe('countQuery()', () => {
20582058
});
20592059

20602060
it('empty test collection count', () => {
2061-
return withTestCollection(async coll => {
2062-
const countQuery_ = countQuery(query(coll));
2061+
return withTestCollection(async collection => {
2062+
const countQuery_ = countQuery(collection);
20632063
const snapshot = await getAggregate(countQuery_);
20642064
expect(snapshot.getCount()).to.equal(0);
20652065
});
@@ -2072,7 +2072,7 @@ describe('countQuery()', () => {
20722072
{ author: 'authorB', title: 'titleC' }
20732073
];
20742074
return withTestCollectionAndInitialData(testDocs, async collection => {
2075-
const countQuery_ = countQuery(query(collection));
2075+
const countQuery_ = countQuery(collection);
20762076
const snapshot = await getAggregate(countQuery_);
20772077
expect(snapshot.getCount()).to.equal(3);
20782078
});
@@ -2311,7 +2311,7 @@ describe('countQuery()', () => {
23112311
it('count query on a terminated Firestore', () => {
23122312
return withTestCollection(async collection => {
23132313
await terminate(collection.firestore);
2314-
const countQuery_ = countQuery(query(collection));
2314+
const countQuery_ = countQuery(collection);
23152315
expect(() => getAggregate(countQuery_)).to.throw(
23162316
'The client has already been terminated.'
23172317
);
@@ -2325,7 +2325,7 @@ describe('countQuery()', () => {
23252325
{ author: 'authorB', title: 'titleC' }
23262326
];
23272327
return withTestCollectionAndInitialData(testDocs, async collection => {
2328-
const countQuery_ = countQuery(query(collection));
2328+
const countQuery_ = countQuery(collection);
23292329
const promise = getAggregate(countQuery_);
23302330
await terminate(collection.firestore);
23312331
const snapshot = await promise;

0 commit comments

Comments
 (0)