Skip to content

Commit 2d4f47d

Browse files
committed
Handle empty cursor result sets
Fixes #683.
1 parent bec456c commit 2d4f47d

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ This driver uses semantic versioning:
1818

1919
### Fixed
2020

21-
- Fixed incorrect HTTP method call in `patch` method ([#687](https://github.com/arangodb/arangojs/issues/687))
21+
- Fixed incorrect HTTP method call in `patch` method ([#687](https://github.com/arangodb/arangojs/pull/687))
22+
23+
- Fixed empty query results containing `[undefined]` ([#687](https://github.com/arangodb/arangojs/issues/683))
2224

2325
## [7.0.1] - 2020-08-21
2426

src/cursor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ export class BatchedArrayCursor<T = any> {
9898
host?: number,
9999
allowDirtyRead?: boolean
100100
) {
101-
const initialBatch = new LinkedList(body.result);
102-
const batches = new LinkedList([initialBatch]);
101+
const batches = new LinkedList(
102+
body.result.length ? [new LinkedList(body.result)] : []
103+
);
103104
this._db = db;
104105
this._batches = batches;
105106
this._id = body.id;

src/test/08-cursors.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const ARANGO_VERSION = Number(
99
process.env.ARANGO_VERSION || process.env.ARANGOJS_DEVEL_VERSION || 30400
1010
);
1111

12-
const aqlQuery = aql`FOR i In 0..10 RETURN i`;
12+
const aqlQuery = aql`FOR i IN 0..10 RETURN i`;
1313
const aqlResult = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
1414

1515
async function sleep(ms: number) {
@@ -73,7 +73,7 @@ describe("Item-wise Cursor API", () => {
7373
expect(cursor.hasNext).to.equal(true);
7474
});
7575
it("returns false after last batch is consumed", async () => {
76-
const cursor = await db.query(aql`FOR i In 0..1 RETURN i`, {
76+
const cursor = await db.query(aql`FOR i IN 0..1 RETURN i`, {
7777
batchSize: 2,
7878
});
7979
expect(cursor.hasNext).to.equal(true);
@@ -88,7 +88,7 @@ describe("Item-wise Cursor API", () => {
8888
expect((cursor.batches as any)._batches.length).to.equal(0);
8989
});
9090
it("returns false after last result is consumed", async () => {
91-
const cursor = await db.query(aql`FOR i In 0..1 RETURN i`, {
91+
const cursor = await db.query(aql`FOR i IN 0..1 RETURN i`, {
9292
batchSize: 2,
9393
});
9494
expect(cursor.hasNext).to.equal(true);
@@ -103,7 +103,7 @@ describe("Item-wise Cursor API", () => {
103103
expect((cursor.batches as any)._batches.length).to.equal(0);
104104
});
105105
it.skip("returns 404 after timeout", async () => {
106-
const cursor = await db.query(aql`FOR i In 0..1 RETURN i`, {
106+
const cursor = await db.query(aql`FOR i IN 0..1 RETURN i`, {
107107
batchSize: 1,
108108
ttl: 1,
109109
});
@@ -132,9 +132,14 @@ describe("Item-wise Cursor API", () => {
132132
await loadMore(cursor, totalLength);
133133
}
134134
}
135-
const cursor = await db.query(`FOR i In 1..${EXPECTED_LENGTH} RETURN i`);
135+
const cursor = await db.query(`FOR i IN 1..${EXPECTED_LENGTH} RETURN i`);
136136
await loadMore(cursor, 0);
137137
});
138+
it("returns false if there are no results", async () => {
139+
const cursor = await db.query(aql`FOR i IN [] RETURN i`);
140+
expect(cursor.hasNext).to.equal(false);
141+
expect((cursor.batches as any)._batches.length).to.equal(0);
142+
});
138143
});
139144
describe("cursor.forEach", () => {
140145
it("invokes the callback for each value", async () => {
@@ -144,6 +149,14 @@ describe("Item-wise Cursor API", () => {
144149
});
145150
expect(results).to.eql(aqlResult);
146151
});
152+
it("correctly handles empty results", async () => {
153+
const cursor = await db.query(aql`FOR i IN [] RETURN i`);
154+
const results: any[] = [];
155+
await cursor.forEach((value) => {
156+
results.push(value);
157+
});
158+
expect(results).to.eql([]);
159+
});
147160
it("aborts if the callback returns false", async () => {
148161
const results: any[] = [];
149162
await cursor.forEach((value) => {
@@ -264,7 +277,7 @@ describe("Batch-wise Cursor API", () => {
264277
});
265278
it("returns false after last batch is consumed", async () => {
266279
const cursor = (
267-
await db.query(aql`FOR i In 0..1 RETURN i`, {
280+
await db.query(aql`FOR i IN 0..1 RETURN i`, {
268281
batchSize: 1,
269282
})
270283
).batches;
@@ -280,7 +293,7 @@ describe("Batch-wise Cursor API", () => {
280293
expect((cursor as any)._batches.length).to.equal(0);
281294
});
282295
it.skip("returns 404 after timeout", async () => {
283-
const cursor = await db.query(aql`FOR i In 0..1 RETURN i`, {
296+
const cursor = await db.query(aql`FOR i IN 0..1 RETURN i`, {
284297
batchSize: 1,
285298
ttl: 1,
286299
});
@@ -309,7 +322,7 @@ describe("Batch-wise Cursor API", () => {
309322
await loadMore(cursor, totalLength);
310323
}
311324
}
312-
const cursor = await db.query(`FOR i In 1..${EXPECTED_LENGTH} RETURN i`);
325+
const cursor = await db.query(`FOR i IN 1..${EXPECTED_LENGTH} RETURN i`);
313326
await loadMore(cursor, 0);
314327
});
315328
});

0 commit comments

Comments
 (0)