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
* @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);
}
/**

View File

@ -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;
});
});