<NME> 23-aql-queries-stream.ts
<BEF> import { Database, aql } from "../arangojs";

import { ArrayCursor } from "../cursor";
import { expect } from "chai";

const ARANGO_VERSION = Number(process.env.ARANGO_VERSION || 30000);
const describe34 = ARANGO_VERSION >= 30400 ? describe : describe.skip;

describe34("AQL Stream queries", function() {
  let system: Database, db: Database;
  let allCursors: ArrayCursor[];

  let name = `testdb_${Date.now()}`;
  let db: Database;
  before(done => {
    db = new Database({
      url: process.env.TEST_ARANGODB_URL || "http://localhost:8529",
      arangoVersion: Number(process.env.ARANGO_VERSION || 30400)
    });
    db
      .createDatabase(name)
      .then(() => {
        db.useDatabase(name);
        done();
      })
      .catch(done);
  });
  after(done => {
    db.useDatabase("_system");
    db
      .dropDatabase(name)
      .then(() => void done())
      .catch(done);
  });
  describe("database.query", () => {
    it("returns a cursor for the query result", done => {
      db
        .query("RETURN 23", {}, { options: { stream: true } })
        .then(cursor => {
          expect(cursor).to.be.an.instanceof(ArrayCursor);
          done();
      allCursors.push(cursor);
        .catch(done);
    });
    it("supports bindVars", done => {
      db
        .query("RETURN @x", { x: 5 }, { options: { stream: true } })
        .then(cursor => cursor.next())
        .then(value => {
          expect(value).to.equal(5);
        stream: true,
      });
        .catch(done);
    });
    it("supports options", done => {
      db
        .query("FOR x IN 1..10 RETURN x", undefined, {
          batchSize: 2,
          count: true, // should be ignored
          options: { stream: true }
        })
        .then(cursor => {
          expect(cursor.count).to.equal(undefined);
          expect((cursor as any)._hasMore).to.equal(true);
        count: true,
        stream: true,
      });
      allCursors.push(cursor);
      expect(cursor.count).to.equal(undefined); // count will be ignored
      expect((cursor as any).batches.hasMore).to.equal(true);
        query: "FOR x IN RANGE(1, @max) RETURN x",
        bindVars: { max: 10 }
      };
      db
        .query(query, { batchSize: 2, count: true, options: { stream: true } })
        .then(cursor => {
          expect(cursor.count).to.equal(undefined); // count will be ignored
          expect((cursor as any)._hasMore).to.equal(true);
        { path: `/_api/collection/${collection.name}` },
        10000
      );
      await Promise.all(
        Array.from(Array(1000).keys()).map((i: number) =>
          collection.save({ hallo: i })
        )
      );
    });
    /*after(async () => {
      await db.collection(cname).drop()
    });*/
    it("can access large collection in parallel", async () => {
      const collection = db.collection(cname);
      const query = aql`FOR doc in ${collection} RETURN doc`;
      const options = { batchSize: 250, stream: true };

      let count = 0;
      const cursors = await Promise.all(
        Array.from(Array(25)).map(() => db.query(query, options))
      );
      allCursors.push(...cursors);
      await Promise.all(
        cursors.map((c) =>
          (c as ArrayCursor).forEach(() => {
            count++;
          })
        )
      );
      expect(count).to.equal(25 * 1000);
    });
    it("can do writes and reads", async () => {
      const collection = db.collection(cname);
      const readQ = aql`FOR doc in ${collection} RETURN doc`;
      const writeQ = aql`FOR i in 1..10000 LET y = SLEEP(1) INSERT {forbidden: i} INTO ${collection}`;
      const options: QueryOptions = {
        batchSize: 500,
        ttl: 5,
        maxRuntime: 5,
        stream: true,
      };

      const readCursor = db.query(readQ, options);
      const writeCursor = db.query(writeQ, options);

      // the read cursor should always win
      const c = await Promise.race([readCursor, writeCursor]);
      allCursors.push(c);
      // therefore no document should have been written here
      for await (const d of c) {
        expect(d).not.to.haveOwnProperty("forbidden");
      }
    });
  });
});

<MSG> Cleanup after tests, bump default test version

<DFF> @@ -1,9 +1,8 @@
-import { Database, aql } from "../arangojs";
-
-import { ArrayCursor } from "../cursor";
 import { expect } from "chai";
+import { aql, Database } from "../arangojs";
+import { ArrayCursor } from "../cursor";
 
-const ARANGO_VERSION = Number(process.env.ARANGO_VERSION || 30000);
+const ARANGO_VERSION = Number(process.env.ARANGO_VERSION || 30400);
 const describe34 = ARANGO_VERSION >= 30400 ? describe : describe.skip;
 
 describe34("AQL Stream queries", function() {
@@ -12,30 +11,25 @@ describe34("AQL Stream queries", function() {
 
   let name = `testdb_${Date.now()}`;
   let db: Database;
-  before(done => {
+  before(async () => {
     db = new Database({
       url: process.env.TEST_ARANGODB_URL || "http://localhost:8529",
       arangoVersion: Number(process.env.ARANGO_VERSION || 30400)
     });
-    db
-      .createDatabase(name)
-      .then(() => {
-        db.useDatabase(name);
-        done();
-      })
-      .catch(done);
+    await db.createDatabase(name);
+    db.useDatabase(name);
   });
-  after(done => {
-    db.useDatabase("_system");
-    db
-      .dropDatabase(name)
-      .then(() => void done())
-      .catch(done);
+  after(async () => {
+    try {
+      db.useDatabase("_system");
+      await db.dropDatabase(name);
+    } finally {
+      db.close();
+    }
   });
   describe("database.query", () => {
     it("returns a cursor for the query result", done => {
-      db
-        .query("RETURN 23", {}, { options: { stream: true } })
+      db.query("RETURN 23", {}, { options: { stream: true } })
         .then(cursor => {
           expect(cursor).to.be.an.instanceof(ArrayCursor);
           done();
@@ -43,8 +37,7 @@ describe34("AQL Stream queries", function() {
         .catch(done);
     });
     it("supports bindVars", done => {
-      db
-        .query("RETURN @x", { x: 5 }, { options: { stream: true } })
+      db.query("RETURN @x", { x: 5 }, { options: { stream: true } })
         .then(cursor => cursor.next())
         .then(value => {
           expect(value).to.equal(5);
@@ -53,12 +46,11 @@ describe34("AQL Stream queries", function() {
         .catch(done);
     });
     it("supports options", done => {
-      db
-        .query("FOR x IN 1..10 RETURN x", undefined, {
-          batchSize: 2,
-          count: true, // should be ignored
-          options: { stream: true }
-        })
+      db.query("FOR x IN 1..10 RETURN x", undefined, {
+        batchSize: 2,
+        count: true, // should be ignored
+        options: { stream: true }
+      })
         .then(cursor => {
           expect(cursor.count).to.equal(undefined);
           expect((cursor as any)._hasMore).to.equal(true);
@@ -71,8 +63,7 @@ describe34("AQL Stream queries", function() {
         query: "FOR x IN RANGE(1, @max) RETURN x",
         bindVars: { max: 10 }
       };
-      db
-        .query(query, { batchSize: 2, count: true, options: { stream: true } })
+      db.query(query, { batchSize: 2, count: true, options: { stream: true } })
         .then(cursor => {
           expect(cursor.count).to.equal(undefined); // count will be ignored
           expect((cursor as any)._hasMore).to.equal(true);

{'addition_count': 21, 'commit_subject': 'Cleanup after tests, bump default test version', 'deletion_count': 30, 'file_extension': '.ts', 'lang': 'ts', 'license': 'apache-2.0', 'repo_name': 'arangodb/arangojs'}