Skip to content

Commit effc7ee

Browse files
committed
Adding stream cursor tests
1 parent 61d18c3 commit effc7ee

File tree

4 files changed

+135
-4
lines changed

4 files changed

+135
-4
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ env:
88
- ARANGODB_VERSION=2.8 ARANGO_VERSION=20800
99
- ARANGODB_VERSION=3.0 ARANGO_VERSION=30000
1010
- ARANGODB_VERSION=3.1 ARANGO_VERSION=30100
11-
- ARANGODB_VERSION=devel ARANGO_VERSION=30200
11+
- ARANGODB_VERSION=3.2 ARANGO_VERSION=30200
12+
- ARANGODB_VERSION=3.3 ARANGO_VERSION=30300
13+
- ARANGODB_VERSION=devel ARANGO_VERSION=30400
1214
before_install:
1315
- curl https://www.arangodb.com/repositories/travisCI/setup_arangodb_${ARANGODB_VERSION}.sh | bash
1416
script:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@
9292
"webpack": "^3.0.0",
9393
"xunit-file": "1.0.0"
9494
}
95-
}
95+
}

src/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class ArangoError extends ExtendableError {
7272
const err = new Error(this.message);
7373
err.name = this.name;
7474
for (const key of nativeErrorKeys) {
75-
if (err[key]) this[key as keyof this] = err[key];
75+
if (err[key]) this[key] = err[key];
7676
}
7777
}
7878
}
@@ -91,7 +91,7 @@ export class HttpError extends ExtendableError {
9191
const err = new Error(this.message);
9292
err.name = this.name;
9393
for (const key of nativeErrorKeys) {
94-
if (err[key]) this[key as keyof this] = err[key];
94+
if (err[key]) this[key] = err[key];
9595
}
9696
}
9797
}

src/test/23-aql-queries-stream.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { Database, aql } from "../arangojs";
2+
3+
import { ArrayCursor } from "../cursor";
4+
import { expect } from "chai";
5+
6+
const ARANGO_VERSION = Number(process.env.ARANGO_VERSION || 30000);
7+
const describe34 = ARANGO_VERSION >= 30400 ? describe : describe.skip;
8+
9+
describe34("AQL queries", () => {
10+
let name = `testdb_${Date.now()}`;
11+
let db: Database;
12+
before(done => {
13+
db = new Database({
14+
url: process.env.TEST_ARANGODB_URL || "http://localhost:8529",
15+
arangoVersion: Number(process.env.ARANGO_VERSION || 30400)
16+
});
17+
db
18+
.createDatabase(name)
19+
.then(() => {
20+
db.useDatabase(name);
21+
done();
22+
})
23+
.catch(done);
24+
});
25+
after(done => {
26+
db.useDatabase("_system");
27+
db
28+
.dropDatabase(name)
29+
.then(() => void done())
30+
.catch(done);
31+
});
32+
describe("database.query", () => {
33+
it("returns a cursor for the query result", done => {
34+
db
35+
.query("RETURN 23", {}, { options: { stream: true } })
36+
.then(cursor => {
37+
expect(cursor).to.be.an.instanceof(ArrayCursor);
38+
done();
39+
})
40+
.catch(done);
41+
});
42+
it("supports bindVars", done => {
43+
db
44+
.query("RETURN @x", { x: 5 }, { options: { stream: true } })
45+
.then(cursor => cursor.next())
46+
.then(value => {
47+
expect(value).to.equal(5);
48+
done();
49+
})
50+
.catch(done);
51+
});
52+
it("supports options", done => {
53+
db
54+
.query("FOR x IN 1..10 RETURN x", undefined, {
55+
batchSize: 2,
56+
count: true, // should be ignored
57+
options: { stream: true }
58+
})
59+
.then(cursor => {
60+
expect(cursor.count).to.equal(undefined);
61+
expect((cursor as any)._hasMore).to.equal(true);
62+
done();
63+
})
64+
.catch(done);
65+
});
66+
it("supports compact queries with options", done => {
67+
let query: any = {
68+
query: "FOR x IN RANGE(1, @max) RETURN x",
69+
bindVars: { max: 10 }
70+
};
71+
db
72+
.query(query, { batchSize: 2, count: true, options: { stream: true } })
73+
.then(cursor => {
74+
expect(cursor.count).to.equal(undefined); // count will be ignored
75+
expect((cursor as any)._hasMore).to.equal(true);
76+
done();
77+
})
78+
.catch(done);
79+
});
80+
});
81+
describe("with some data", () => {
82+
let cname = "MyTestCollection"
83+
before(done => {
84+
let collection = db.collection(cname);
85+
collection.create()
86+
.then(() => {
87+
return Promise.all(Array.apply(null, { length: 1000 })
88+
.map(Number.call, Number)
89+
.map((i: Number) => collection.save({ hallo: i })));
90+
}).then(() => void done()).catch(done);
91+
});
92+
/*after(done => {
93+
db.collection(cname).drop().then(() => done()).catch(done);
94+
});*/
95+
it("can access large collection in parallel", (done) => {
96+
let collection = db.collection(cname);
97+
let query = aql`FOR doc in ${collection} RETURN doc`;
98+
const opts = { batchSize: 250, options: { stream: true } };
99+
100+
let count = 0;
101+
Promise.all(Array.apply(null, { length: 25 }).map(() => db.query(query, opts)))
102+
.then(cursors => {
103+
return Promise.all(cursors.map(c => (c as ArrayCursor).each(() => { count++ })))
104+
}).then(() => {
105+
expect(count).to.equal(25 * 1000);
106+
done();
107+
}).catch(done);
108+
});
109+
it("can do writes and reads", (done) => {
110+
let collection = db.collection(cname);
111+
let readQ = aql`FOR doc in ${collection} RETURN doc`;
112+
let writeQ = aql`FOR i in 1..1000 LET y = SLEEP(1) INSERT {forbidden: i} INTO ${collection}`;
113+
const opts = { batchSize: 500, ttl: 5, options: { stream: true } };
114+
115+
// 900s lock timeout + 5s ttl
116+
let readCursor = db.query(readQ, opts);
117+
let writeCursor = db.query(writeQ, opts);
118+
119+
// the read cursor should always win
120+
Promise.race([readCursor, writeCursor]).then(c => {
121+
// therefore no document should have been written here
122+
return c.every((d: any) => !(d.hasOwnProperty("forbidden")))
123+
}).then(isOk => {
124+
expect(isOk).to.equal(true);
125+
done();
126+
}).catch(done);
127+
});
128+
});
129+
});

0 commit comments

Comments
 (0)