Skip to content

Commit

Permalink
initial docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmehta committed Jul 31, 2020
1 parent 6ad4ca2 commit e3d9c6d
Showing 1 changed file with 88 additions and 92 deletions.
180 changes: 88 additions & 92 deletions __tests__/rethinkdb-iterable.test.ts
@@ -1,84 +1,80 @@
/// <reference types="jest" />

import rethinkdb, { Connection } from "rethinkdb";
import rethinkdb, { Connection } from 'rethinkdb'

import RethinkdbIterable from "..";
import _assert from "assert";
import RethinkdbIterable from '..'
import _assert from 'assert'

function assert(val: any, msg: string): asserts val {
_assert(val, msg);
_assert(val, msg)
}

describe("RethinkdbIterable", () => {
let index = 0;
describe('RethinkdbIterable', () => {
let index = 0
const ctx: {
conn: Connection | null;
db: string;
intervalId: NodeJS.Timeout | null;
table: string;
conn: Connection | null
db: string
intervalId: NodeJS.Timeout | null
table: string
} = {
conn: null,
db: "rethinkdb_iterator_test",
db: 'rethinkdb_iterable_test',
intervalId: null,
table: "rethinkdb_iterator_test",
};
table: 'rethinkdb_iterable_test',
}

beforeEach(async () => {
ctx.conn = await rethinkdb.connect({
host: "localhost",
db: "",
});
host: 'localhost',
db: '',
})
await rethinkdb
.dbCreate(ctx.db)
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return;
});
if (/exists/.test(err.message)) return
})
await rethinkdb
.db(ctx.db)
.tableCreate(ctx.table)
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return;
});
if (/exists/.test(err.message)) return
})
await rethinkdb
.db(ctx.db)
.table(ctx.table)
.indexCreate("value", rethinkdb.row("value"))
.indexCreate('value', rethinkdb.row('value'))
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return;
});
await rethinkdb
.db(ctx.db)
.table(ctx.table)
.indexWait("value")
.run(ctx.conn);
});
if (/exists/.test(err.message)) return
})
await rethinkdb.db(ctx.db).table(ctx.table).indexWait('value').run(ctx.conn)
})
afterEach(async () => {
if (ctx.conn) {
await rethinkdb
.dbDrop(ctx.db)
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return;
});
if (/exists/.test(err.message)) return
})
await rethinkdb
.db(ctx.db)
.tableDrop(ctx.table)
.run(ctx.conn)
.catch((err) => {
if (/exists/.test(err.message)) return;
});
await ctx.conn.close();
if (/exists/.test(err.message)) return
})
await ctx.conn.close()
}
});
})

describe("rows being created in table on interval", () => {
const activePromises = new Set();
describe('rows being created in table on interval', () => {
const activePromises = new Set()
beforeEach(() => {
assert(ctx.conn != null, "connection is missing");
const conn = ctx.conn;
assert(ctx.conn != null, 'connection is missing')
const conn = ctx.conn
// insert new rows on interval
ctx.intervalId = setInterval(() => {
const p = rethinkdb
Expand All @@ -88,34 +84,34 @@ describe("RethinkdbIterable", () => {
id: index++,
})
.run(conn)
.finally(() => activePromises.delete(p));
activePromises.add(p);
}, 100);
});
.finally(() => activePromises.delete(p))
activePromises.add(p)
}, 100)
})
afterEach(async () => {
if (ctx.intervalId) clearInterval(ctx.intervalId);
await Promise.all(Array.from(activePromises));
});
if (ctx.intervalId) clearInterval(ctx.intervalId)
await Promise.all(Array.from(activePromises))
})

it("should create an async iterable, iterate through it, and eventually close the cursor", async () => {
assert(ctx.conn != null, "connection is missing");
const conn = ctx.conn;
it('should create an async iterable, iterate through it, and eventually close the cursor', async () => {
assert(ctx.conn != null, 'connection is missing')
const conn = ctx.conn
const cursor = await rethinkdb
.db(ctx.db)
.table(ctx.table)
.changes()
.run(conn);
const iterator = new RethinkdbIterable(cursor);
.run(conn)
const iterable = new RethinkdbIterable(cursor)

let done = false;
let done = false
setTimeout(() => {
done = true;
}, 300);
done = true
}, 300)

const results = [];
for await (const val of iterator) {
results.push(val);
if (done) break;
const results = []
for await (const val of iterable) {
results.push(val)
if (done) break
}
expect(results).toMatchInlineSnapshot(`
Array [
Expand All @@ -138,15 +134,15 @@ describe("RethinkdbIterable", () => {
"old_val": null,
},
]
`);
});
});
`)
})
})

describe("one row exists", () => {
const indexValue = "indexValue";
describe('one row exists', () => {
const indexValue = 'indexValue'
beforeEach(async () => {
assert(ctx.conn != null, "connection is missing");
const conn = ctx.conn;
assert(ctx.conn != null, 'connection is missing')
const conn = ctx.conn
// insert new rows on interval
await rethinkdb
.db(ctx.db)
Expand All @@ -155,41 +151,41 @@ describe("RethinkdbIterable", () => {
id: index++,
value: indexValue,
})
.run(conn);
.run(conn)
await rethinkdb
.db(ctx.db)
.table(ctx.table)
.insert({
id: index++,
value: indexValue,
})
.run(conn);
.run(conn)
await rethinkdb
.db(ctx.db)
.table(ctx.table)
.insert({
id: index++,
value: indexValue,
})
.run(conn);
});
.run(conn)
})
afterEach(() => {
if (ctx.intervalId) clearInterval(ctx.intervalId);
});
if (ctx.intervalId) clearInterval(ctx.intervalId)
})

it("should create an async iterable that gets row and completes successfully", async () => {
assert(ctx.conn != null, "connection is missing");
const conn = ctx.conn;
it('should create an async iterable that gets row and completes successfully', async () => {
assert(ctx.conn != null, 'connection is missing')
const conn = ctx.conn
// @ts-ignore
const cursor = await rethinkdb
.db(ctx.db)
.table(ctx.table)
.getAll(indexValue, { index: "value" })
.run(ctx.conn);
const iterator = new RethinkdbIterable(cursor);
const results = [];
for await (const val of iterator) {
results.push(val);
.getAll(indexValue, { index: 'value' })
.run(ctx.conn)
const iterable = new RethinkdbIterable(cursor)
const results = []
for await (const val of iterable) {
results.push(val)
}
expect(results).toMatchInlineSnapshot(`
Array [
Expand All @@ -206,22 +202,22 @@ describe("RethinkdbIterable", () => {
"value": "indexValue",
},
]
`);
});
});
});
`)
})
})
})

const toPromise = <R>(
task: (cb: (err?: Error, val?: R) => void) => void
): Promise<R> => {
return new Promise((resolve, reject) => {
task((...args) => {
const [err, val] = args;
if (err != null) return void reject(err);
if (args.length === 2) return void resolve(val);
resolve();
});
});
};
const [err, val] = args
if (err != null) return void reject(err)
if (args.length === 2) return void resolve(val)
resolve()
})
})
}

export default toPromise;
export default toPromise

0 comments on commit e3d9c6d

Please sign in to comment.