From 874903c64b3c93743f943cb836b4219175082823 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Wed, 16 Aug 2017 12:03:32 +0800 Subject: [PATCH] Migrate mongo DAO --- src/dao/mongo.js | 4 +-- test/integration/mongo-test.js | 62 +++++++++++++++++----------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/dao/mongo.js b/src/dao/mongo.js index f1ccecc..d61d572 100644 --- a/src/dao/mongo.js +++ b/src/dao/mongo.js @@ -30,9 +30,9 @@ class Mongo { * @param {String} pass The database user's password * @yield {undefined} */ - *init({uri, user, pass}) { + async init({uri, user, pass}) { const url = `mongodb://${user}:${pass}@${uri}`; - this._db = yield MongoClient.connect(url); + this._db = await MongoClient.connect(url); } /** diff --git a/test/integration/mongo-test.js b/test/integration/mongo-test.js index fdc2abd..4f74d07 100644 --- a/test/integration/mongo-test.js +++ b/test/integration/mongo-test.js @@ -9,31 +9,31 @@ describe('Mongo Integration Tests', function() { const DB_TYPE = 'apple'; let mongo; - before(function *() { + before(async() => { mongo = new Mongo(); - yield mongo.init(config.mongo); + await mongo.init(config.mongo); }); - beforeEach(function *() { - yield mongo.clear(DB_TYPE); + beforeEach(async() => { + await mongo.clear(DB_TYPE); }); - after(function *() { - yield mongo.clear(DB_TYPE); - yield mongo.disconnect(); + after(async() => { + await mongo.clear(DB_TYPE); + await mongo.disconnect(); }); describe("create", () => { - it('should insert a document', function *() { - const r = yield mongo.create({_id: '0'}, DB_TYPE); + it('should insert a document', async() => { + const r = await mongo.create({_id: '0'}, DB_TYPE); expect(r.insertedCount).to.equal(1); }); - it('should fail if two with the same ID are inserted', function *() { - let r = yield mongo.create({_id: '0'}, DB_TYPE); + it('should fail if two with the same ID are inserted', async() => { + let r = await mongo.create({_id: '0'}, DB_TYPE); expect(r.insertedCount).to.equal(1); try { - r = yield mongo.create({_id: '0'}, DB_TYPE); + r = await mongo.create({_id: '0'}, DB_TYPE); } catch (e) { expect(e.message).to.match(/duplicate/); } @@ -41,16 +41,16 @@ describe('Mongo Integration Tests', function() { }); describe("batch", () => { - it('should insert a document', function *() { - const r = yield mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE); + it('should insert a document', async() => { + const r = await mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE); expect(r.insertedCount).to.equal(2); }); - it('should fail if docs with the same ID are inserted', function *() { - let r = yield mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE); + it('should fail if docs with the same ID are inserted', async() => { + let r = await mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE); expect(r.insertedCount).to.equal(2); try { - r = yield mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE); + r = await mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE); } catch (e) { expect(e.message).to.match(/duplicate/); } @@ -58,36 +58,36 @@ describe('Mongo Integration Tests', function() { }); describe("update", () => { - it('should update a document', function *() { - let r = yield mongo.create({_id: '0'}, DB_TYPE); - r = yield mongo.update({_id: '0'}, {foo: 'bar'}, DB_TYPE); + it('should update a document', async() => { + let r = await mongo.create({_id: '0'}, DB_TYPE); + r = await mongo.update({_id: '0'}, {foo: 'bar'}, DB_TYPE); 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'); }); }); describe("get", () => { - it('should get a document', function *() { - let r = yield mongo.create({_id: '0'}, DB_TYPE); - r = yield mongo.get({_id: '0'}, DB_TYPE); + it('should get a document', async() => { + let r = await mongo.create({_id: '0'}, DB_TYPE); + r = await mongo.get({_id: '0'}, DB_TYPE); expect(r).to.exist; }); }); describe("list", () => { - it('should list documents', function *() { - let r = yield mongo.batch([{_id: '0', foo: 'bar'}, {_id: '1', foo: 'bar'}], DB_TYPE); - r = yield mongo.list({foo: 'bar'}, DB_TYPE); + it('should list documents', async() => { + let r = await mongo.batch([{_id: '0', foo: 'bar'}, {_id: '1', 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); }); }); describe("remove", () => { - it('should remove a document', function *() { - let r = yield mongo.create({_id: '0'}, DB_TYPE); - r = yield mongo.remove({_id: '0'}, DB_TYPE); - r = yield mongo.get({_id: '0'}, DB_TYPE); + it('should remove a document', async() => { + let r = await mongo.create({_id: '0'}, DB_TYPE); + r = await mongo.remove({_id: '0'}, DB_TYPE); + r = await mongo.get({_id: '0'}, DB_TYPE); expect(r).to.not.exist; }); });