mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-20 09:20:16 +00:00
Remove QueryUtils.queryAll and fix test (#885)
This commit is contained in:
@@ -91,97 +91,11 @@ describe("Query Utils", () => {
|
||||
expect(queryStub.getCall(1).args[0]).toBe(0);
|
||||
});
|
||||
|
||||
it("should not perform multiple queries if the first page of results has items", (done) => {
|
||||
it("should not perform multiple queries if the first page of results has items", async () => {
|
||||
const queryStub = sinon.stub().returns(Q.resolve(queryResultWithItemsInPage));
|
||||
QueryUtils.queryPagesUntilContentPresent(0, queryStub).finally(() => {
|
||||
expect(queryStub.callCount).toBe(1);
|
||||
expect(queryStub.getCall(0).args[0]).toBe(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it("should not proceed with subsequent queries if the first one errors out", (done) => {
|
||||
const queryStub = sinon.stub().returns(Q.reject("Error injected for testing purposes"));
|
||||
QueryUtils.queryPagesUntilContentPresent(0, queryStub).finally(() => {
|
||||
expect(queryStub.callCount).toBe(1);
|
||||
expect(queryStub.getCall(0).args[0]).toBe(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("queryAllPages()", () => {
|
||||
const queryResultWithNoContinuation: ViewModels.QueryResults = {
|
||||
documents: [{ a: "123" }],
|
||||
activityId: "123",
|
||||
requestCharge: 1,
|
||||
hasMoreResults: false,
|
||||
firstItemIndex: 1,
|
||||
lastItemIndex: 1,
|
||||
itemCount: 1,
|
||||
};
|
||||
const queryResultWithContinuation: ViewModels.QueryResults = {
|
||||
documents: [{ b: "123" }],
|
||||
activityId: "123",
|
||||
requestCharge: 1,
|
||||
hasMoreResults: true,
|
||||
firstItemIndex: 0,
|
||||
lastItemIndex: 0,
|
||||
itemCount: 1,
|
||||
};
|
||||
|
||||
it("should follow continuation token to fetch all pages", (done) => {
|
||||
const queryStub = sinon
|
||||
.stub()
|
||||
.onFirstCall()
|
||||
.returns(Q.resolve(queryResultWithContinuation))
|
||||
.returns(Q.resolve(queryResultWithNoContinuation));
|
||||
QueryUtils.queryAllPages(queryStub).then(
|
||||
(results: ViewModels.QueryResults) => {
|
||||
expect(queryStub.callCount).toBe(2);
|
||||
expect(queryStub.getCall(0).args[0]).toBe(0);
|
||||
expect(queryStub.getCall(1).args[0]).toBe(1);
|
||||
expect(results.itemCount).toBe(
|
||||
queryResultWithContinuation.documents.length + queryResultWithNoContinuation.documents.length
|
||||
);
|
||||
expect(results.requestCharge).toBe(
|
||||
queryResultWithContinuation.requestCharge + queryResultWithNoContinuation.requestCharge
|
||||
);
|
||||
expect(results.documents).toEqual(
|
||||
queryResultWithContinuation.documents.concat(queryResultWithNoContinuation.documents)
|
||||
);
|
||||
done();
|
||||
},
|
||||
(error: any) => {
|
||||
fail(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it("should not perform subsequent fetches when result has no continuation", (done) => {
|
||||
const queryStub = sinon.stub().returns(Q.resolve(queryResultWithNoContinuation));
|
||||
QueryUtils.queryAllPages(queryStub).then(
|
||||
(results: ViewModels.QueryResults) => {
|
||||
expect(queryStub.callCount).toBe(1);
|
||||
expect(queryStub.getCall(0).args[0]).toBe(0);
|
||||
expect(results.itemCount).toBe(queryResultWithNoContinuation.documents.length);
|
||||
expect(results.requestCharge).toBe(queryResultWithNoContinuation.requestCharge);
|
||||
expect(results.documents).toEqual(queryResultWithNoContinuation.documents);
|
||||
done();
|
||||
},
|
||||
(error: any) => {
|
||||
fail(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it("should not proceed with subsequent fetches if the first one errors out", (done) => {
|
||||
const queryStub = sinon.stub().returns(Q.reject("Error injected for testing purposes"));
|
||||
QueryUtils.queryAllPages(queryStub).finally(() => {
|
||||
expect(queryStub.callCount).toBe(1);
|
||||
expect(queryStub.getCall(0).args[0]).toBe(0);
|
||||
done();
|
||||
});
|
||||
await QueryUtils.queryPagesUntilContentPresent(0, queryStub);
|
||||
expect(queryStub.callCount).toBe(1);
|
||||
expect(queryStub.getCall(0).args[0]).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -81,34 +81,3 @@ export const queryPagesUntilContentPresent = async (
|
||||
|
||||
return await doRequest(firstItemIndex);
|
||||
};
|
||||
|
||||
export const queryAllPages = async (
|
||||
queryItems: (itemIndex: number) => Promise<ViewModels.QueryResults>
|
||||
): Promise<ViewModels.QueryResults> => {
|
||||
const queryResults: ViewModels.QueryResults = {
|
||||
documents: [],
|
||||
activityId: undefined,
|
||||
hasMoreResults: false,
|
||||
itemCount: 0,
|
||||
firstItemIndex: 0,
|
||||
lastItemIndex: 0,
|
||||
requestCharge: 0,
|
||||
roundTrips: 0,
|
||||
};
|
||||
const doRequest = async (itemIndex: number): Promise<ViewModels.QueryResults> => {
|
||||
const results = await queryItems(itemIndex);
|
||||
const { requestCharge, hasMoreResults, itemCount, lastItemIndex, documents } = results;
|
||||
queryResults.roundTrips = queryResults.roundTrips + 1;
|
||||
queryResults.requestCharge = Number(queryResults.requestCharge) + Number(requestCharge);
|
||||
queryResults.hasMoreResults = hasMoreResults;
|
||||
queryResults.itemCount = queryResults.itemCount + itemCount;
|
||||
queryResults.lastItemIndex = lastItemIndex;
|
||||
queryResults.documents = queryResults.documents.concat(documents);
|
||||
if (queryResults.hasMoreResults) {
|
||||
return doRequest(queryResults.lastItemIndex + 1);
|
||||
}
|
||||
return queryResults;
|
||||
};
|
||||
|
||||
return doRequest(0);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user