|
| 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