function hasOpenWebHook(port, reverse) { assert.ok(port); const error = new Error('open-webhook-check failed'); let connected = false; return request.get(`http://127.0.0.1:${port}`) .then(() => { connected = true; }).catch(e => { if (e.statusCode < 500) connected = true; }).finally(() => { if (reverse) { if (connected) throw error; return; } if (!connected) throw error; }); }
describe(`with invalid query as parameter`, () => { let result; beforeEach(done => { promise.any([ db.query(), db.query(``), db.query(` `), db.query({}), db.query(1), db.query(null)]) .catch(err => { result = err; }) .finally(done); }); it(`must throw an error`, () => { expect(result.length).toBe(6); expect(result[0].message).toBe($text.invalidQuery); // reject to an undefined query; expect(result[1].message).toBe($text.invalidQuery); // reject to an empty-string query; expect(result[2].message).toBe($text.invalidQuery); // reject to a white-space query string; expect(result[3].message).toBe($text.invalidQuery); // reject for an empty object; expect(result[4].message).toBe($text.invalidQuery); // reject to an invalid-type query; expect(result[5].message).toBe($text.invalidQuery); // reject to a null query; }); });
describe(`with invalid stream object`, () => { let result; beforeEach(done => { promise.any([ db.stream(), db.stream(null), db.stream(`test`), db.stream({}) ]) .catch(reason => { result = reason; }) .finally(done); }); it(`must throw an error`, () => { expect(result.length).toBe(4); for (let i = 0; i < result.length; i++) { expect(result[i] instanceof TypeError).toBe(true); expect(result[i].message).toBe($text.invalidStream); } }); });
describe(`with invalid initialization callback`, () => { let res; beforeEach(done => { const stream = new QueryStream(`select 1`); promise.any([ db.stream(stream), db.stream(stream, null), db.stream(stream, 123), db.stream(stream, {}) ]) .catch(reason => { res = reason; }) .finally(done); }); it(`must throw an error`, () => { expect(res.length).toBe(4); for (let i = 0; i < res.length; i++) { expect(res[i] instanceof TypeError).toBe(true); expect(res[i].message).toBe($text.invalidStreamCB); } }); });
describe(`with invalid qrm as parameter`, () => { let result; beforeEach(done => { promise.any([ db.query(`something`, undefined, ``), db.query(`something`, undefined, `2`), db.query(`something`, undefined, -1), db.query(`something`, undefined, 0), db.query(`something`, undefined, 100), db.query(`something`, undefined, NaN), db.query(`something`, undefined, 1 / 0), db.query(`something`, undefined, -1 / 0), db.query(`something`, undefined, 2.45)]) .catch(err => { result = err; }) .finally(done); }); it(`must throw an error`, () => { const error = `Invalid Query Result Mask specified.`; expect(result.length).toBe(9); for (let i = 0; i < 9; i++) { expect(result[i] instanceof TypeError).toBe(true); expect(result[i].message).toBe(error); } }); });
it("should receive stats", () => { const x = new MemcacheClient({ server }); const key = `num_${Date.now()}`; return Promise.try(() => x.cmd(`stats`)) .then(r => { const stat = r.STAT; expect(stat).to.be.ok; expect(stat).to.be.an("array"); expect(stat).to.not.be.empty; }) .finally(() => x.shutdown()); });
it("should fire and forget if noreply is set", () => { const x = new MemcacheClient({ server }); const key = `poem1_${Date.now()}`; return Promise.try(() => x.set(key, poem1, { noreply: true })) .then(v => expect(v).to.be.undefined) .then(() => x.get(key)) .then(v => expect(v.value).to.deep.equal(poem1)) .finally(() => x.shutdown()); });
const testReplace = (setCompress, replaceCompress) => { const x = new MemcacheClient({ server }); const key = `poem_${Date.now()}`; return Promise.try(() => x.set(key, poem2, { compress: setCompress })) .then(() => x.get(key)) .then(r => expect(r.value).to.equal(poem2)) .then(() => x.replace(key, poem3, { compress: replaceCompress })) .then(() => x.get(key)) .then(r => expect(r.value).to.equal(poem3)) .finally(() => x.shutdown()); }
it("should set an entry and then prepend to it", () => { const x = new MemcacheClient({ server }); const key = `poem_${Date.now()}`; return Promise.try(() => x.set(key, poem4)) .then(() => x.get(key)) .then(r => expect(r.value).to.equal(poem4)) .then(() => x.prepend(key, poem3)) .then(() => x.get(key)) .then(r => expect(r.value).to.equal(`${poem3}${poem4}`)) .finally(() => x.shutdown()); });
it("should update exptime with touch", () => { const x = new MemcacheClient({ server }); const key = `poem1_${Date.now()}`; return Promise.try(() => x.set(key, poem1, { noreply: true })) .then(v => expect(v).to.be.undefined) .then(() => x.touch(key, "500")) .then(r => expect(r).to.deep.equal(["TOUCHED"])) .finally(() => x.shutdown()); });