keyserver/test/integration/mongo-test.js

95 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-05-25 14:13:49 +00:00
'use strict';
const config = require('config');
const Mongo = require('../../src/dao/mongo');
2016-05-25 14:13:49 +00:00
describe('Mongo Integration Tests', function() {
this.timeout(20000);
const DB_TYPE = 'apple';
2016-05-25 14:13:49 +00:00
let mongo;
before(function *() {
2016-06-08 12:01:30 +00:00
mongo = new Mongo();
yield mongo.init(config.mongo);
2016-05-25 14:13:49 +00:00
});
beforeEach(function *() {
yield mongo.clear(DB_TYPE);
2016-05-25 14:13:49 +00:00
});
after(function *() {
yield mongo.clear(DB_TYPE);
2016-05-25 14:13:49 +00:00
yield mongo.disconnect();
});
2016-05-31 11:52:18 +00:00
describe("create", () => {
2016-05-25 14:13:49 +00:00
it('should insert a document', function *() {
let r = yield mongo.create({ _id:'0' }, DB_TYPE);
2016-05-25 14:13:49 +00:00
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);
2016-05-25 14:13:49 +00:00
expect(r.insertedCount).to.equal(1);
try {
r = yield mongo.create({ _id:'0' }, DB_TYPE);
} catch(e) {
expect(e.message).to.match(/duplicate/);
}
2016-05-25 14:13:49 +00:00
});
});
2016-05-25 14:13:49 +00:00
2016-05-31 11:52:18 +00:00
describe("batch", () => {
it('should insert a document', function *() {
let r = yield 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);
expect(r.insertedCount).to.equal(2);
2016-05-25 14:13:49 +00:00
try {
r = yield mongo.batch([{ _id:'0' }, { _id:'1' }], DB_TYPE);
2016-05-25 14:13:49 +00:00
} catch(e) {
expect(e.message).to.match(/duplicate/);
}
});
});
2016-05-31 11:52:18 +00:00
describe("update", () => {
2016-05-25 14:13:49 +00:00
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);
2016-05-25 14:13:49 +00:00
expect(r.modifiedCount).to.equal(1);
r = yield mongo.get({ _id:'0' }, DB_TYPE);
2016-05-25 14:13:49 +00:00
expect(r.foo).to.equal('bar');
});
});
2016-05-31 11:52:18 +00:00
describe("get", () => {
2016-05-25 14:13:49 +00:00
it('should get a document', function *() {
let r = yield mongo.create({ _id:'0' }, DB_TYPE);
r = yield mongo.get({ _id:'0' }, DB_TYPE);
2016-05-25 14:13:49 +00:00
expect(r).to.exist;
});
});
2016-05-31 11:52:18 +00:00
describe("list", () => {
2016-05-25 14:13:49 +00:00
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);
expect(r).to.deep.equal([{ _id:'0', foo:'bar' }, { _id:'1', foo:'bar' }], DB_TYPE);
2016-05-25 14:13:49 +00:00
});
});
2016-05-31 11:52:18 +00:00
describe("remove", () => {
2016-05-25 14:13:49 +00:00
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);
2016-05-25 14:13:49 +00:00
expect(r).to.not.exist;
});
});
});