Migrate mongo DAO

This commit is contained in:
Tankred Hase 2017-08-16 12:03:32 +08:00
parent 26807e03b1
commit 874903c64b
2 changed files with 33 additions and 33 deletions

View File

@ -30,9 +30,9 @@ class Mongo {
* @param {String} pass The database user's password * @param {String} pass The database user's password
* @yield {undefined} * @yield {undefined}
*/ */
*init({uri, user, pass}) { async init({uri, user, pass}) {
const url = `mongodb://${user}:${pass}@${uri}`; const url = `mongodb://${user}:${pass}@${uri}`;
this._db = yield MongoClient.connect(url); this._db = await MongoClient.connect(url);
} }
/** /**

View File

@ -9,31 +9,31 @@ describe('Mongo Integration Tests', function() {
const DB_TYPE = 'apple'; const DB_TYPE = 'apple';
let mongo; let mongo;
before(function *() { before(async() => {
mongo = new Mongo(); mongo = new Mongo();
yield mongo.init(config.mongo); await mongo.init(config.mongo);
}); });
beforeEach(function *() { beforeEach(async() => {
yield mongo.clear(DB_TYPE); await mongo.clear(DB_TYPE);
}); });
after(function *() { after(async() => {
yield mongo.clear(DB_TYPE); await mongo.clear(DB_TYPE);
yield mongo.disconnect(); await mongo.disconnect();
}); });
describe("create", () => { describe("create", () => {
it('should insert a document', function *() { it('should insert a document', async() => {
const r = yield mongo.create({_id: '0'}, DB_TYPE); const r = await mongo.create({_id: '0'}, DB_TYPE);
expect(r.insertedCount).to.equal(1); expect(r.insertedCount).to.equal(1);
}); });
it('should fail if two with the same ID are inserted', function *() { it('should fail if two with the same ID are inserted', async() => {
let r = yield mongo.create({_id: '0'}, DB_TYPE); let r = await mongo.create({_id: '0'}, DB_TYPE);
expect(r.insertedCount).to.equal(1); expect(r.insertedCount).to.equal(1);
try { try {
r = yield mongo.create({_id: '0'}, DB_TYPE); r = await mongo.create({_id: '0'}, DB_TYPE);
} catch (e) { } catch (e) {
expect(e.message).to.match(/duplicate/); expect(e.message).to.match(/duplicate/);
} }
@ -41,16 +41,16 @@ describe('Mongo Integration Tests', function() {
}); });
describe("batch", () => { describe("batch", () => {
it('should insert a document', function *() { it('should insert a document', async() => {
const r = yield mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE); const r = await mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE);
expect(r.insertedCount).to.equal(2); expect(r.insertedCount).to.equal(2);
}); });
it('should fail if docs with the same ID are inserted', function *() { it('should fail if docs with the same ID are inserted', async() => {
let r = yield mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE); let r = await mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE);
expect(r.insertedCount).to.equal(2); expect(r.insertedCount).to.equal(2);
try { try {
r = yield mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE); r = await mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE);
} catch (e) { } catch (e) {
expect(e.message).to.match(/duplicate/); expect(e.message).to.match(/duplicate/);
} }
@ -58,36 +58,36 @@ describe('Mongo Integration Tests', function() {
}); });
describe("update", () => { describe("update", () => {
it('should update a document', function *() { it('should update a document', async() => {
let r = yield mongo.create({_id: '0'}, DB_TYPE); let r = await mongo.create({_id: '0'}, DB_TYPE);
r = yield mongo.update({_id: '0'}, {foo: 'bar'}, DB_TYPE); r = await mongo.update({_id: '0'}, {foo: 'bar'}, DB_TYPE);
expect(r.modifiedCount).to.equal(1); expect(r.modifiedCount).to.equal(1);
r = yield mongo.get({_id: '0'}, DB_TYPE); r = await mongo.get({_id: '0'}, DB_TYPE);
expect(r.foo).to.equal('bar'); expect(r.foo).to.equal('bar');
}); });
}); });
describe("get", () => { describe("get", () => {
it('should get a document', function *() { it('should get a document', async() => {
let r = yield mongo.create({_id: '0'}, DB_TYPE); let r = await mongo.create({_id: '0'}, DB_TYPE);
r = yield mongo.get({_id: '0'}, DB_TYPE); r = await mongo.get({_id: '0'}, DB_TYPE);
expect(r).to.exist; expect(r).to.exist;
}); });
}); });
describe("list", () => { describe("list", () => {
it('should list documents', function *() { it('should list documents', async() => {
let r = yield mongo.batch([{_id: '0', foo: 'bar'}, {_id: '1', foo: 'bar'}], DB_TYPE); let r = await mongo.batch([{_id: '0', foo: 'bar'}, {_id: '1', foo: 'bar'}], DB_TYPE);
r = yield mongo.list({foo: 'bar'}, DB_TYPE); r = await mongo.list({foo: 'bar'}, DB_TYPE);
expect(r).to.deep.equal([{_id: '0', foo: 'bar'}, {_id: '1', foo: 'bar'}], DB_TYPE); expect(r).to.deep.equal([{_id: '0', foo: 'bar'}, {_id: '1', foo: 'bar'}], DB_TYPE);
}); });
}); });
describe("remove", () => { describe("remove", () => {
it('should remove a document', function *() { it('should remove a document', async() => {
let r = yield mongo.create({_id: '0'}, DB_TYPE); let r = await mongo.create({_id: '0'}, DB_TYPE);
r = yield mongo.remove({_id: '0'}, DB_TYPE); r = await mongo.remove({_id: '0'}, DB_TYPE);
r = yield mongo.get({_id: '0'}, DB_TYPE); r = await mongo.get({_id: '0'}, DB_TYPE);
expect(r).to.not.exist; expect(r).to.not.exist;
}); });
}); });