2016-05-25 14:13:49 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('co-mocha')(require('mocha')); // monkey patch mocha for generators
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
const log = require('npmlog');
|
|
|
|
const Mongo = require('../../src/dao/mongo');
|
|
|
|
const expect = require('chai').expect;
|
2016-05-25 14:13:49 +00:00
|
|
|
|
|
|
|
describe('Mongo Integration Tests', function() {
|
|
|
|
this.timeout(20000);
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
const DB_TYPE = 'apple';
|
2016-05-25 14:13:49 +00:00
|
|
|
let mongo;
|
|
|
|
|
|
|
|
before(function *() {
|
|
|
|
let credentials;
|
|
|
|
try {
|
2016-05-27 17:57:48 +00:00
|
|
|
credentials = require('../../credentials.json');
|
|
|
|
} catch(e) {
|
|
|
|
log.info('mongo-test', 'No credentials.json found ... using environment vars.');
|
|
|
|
}
|
2016-05-25 14:13:49 +00:00
|
|
|
mongo = new Mongo({
|
|
|
|
uri: process.env.MONGO_URI || credentials.mongoUri,
|
|
|
|
user: process.env.MONGO_USER || credentials.mongoUser,
|
2016-05-27 17:57:48 +00:00
|
|
|
password: process.env.MONGO_PASS || credentials.mongoPass
|
2016-05-25 14:13:49 +00:00
|
|
|
});
|
|
|
|
yield mongo.connect();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function *() {
|
2016-05-27 17:57:48 +00:00
|
|
|
yield mongo.clear(DB_TYPE);
|
2016-05-25 14:13:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {});
|
|
|
|
|
|
|
|
after(function *() {
|
2016-05-27 17:57:48 +00:00
|
|
|
yield mongo.clear(DB_TYPE);
|
2016-05-25 14:13:49 +00:00
|
|
|
yield mongo.disconnect();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("create", function() {
|
|
|
|
it('should insert a document', function *() {
|
2016-05-27 17:57:48 +00:00
|
|
|
let r = yield mongo.create({ _id:'0' }, DB_TYPE);
|
2016-05-25 14:13:49 +00:00
|
|
|
expect(r.insertedCount).to.equal(1);
|
|
|
|
});
|
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
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);
|
2016-05-27 17:57:48 +00:00
|
|
|
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-27 17:57:48 +00:00
|
|
|
});
|
2016-05-25 14:13:49 +00:00
|
|
|
|
2016-05-27 17:57:48 +00:00
|
|
|
describe("batch", function() {
|
|
|
|
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 {
|
2016-05-27 17:57:48 +00:00
|
|
|
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/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("update", function() {
|
|
|
|
it('should update a document', function *() {
|
2016-05-27 17:57:48 +00:00
|
|
|
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);
|
2016-05-27 17:57:48 +00:00
|
|
|
r = yield mongo.get({ _id:'0' }, DB_TYPE);
|
2016-05-25 14:13:49 +00:00
|
|
|
expect(r.foo).to.equal('bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("get", function() {
|
|
|
|
it('should get a document', function *() {
|
2016-05-27 17:57:48 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("list", function() {
|
|
|
|
it('should list documents', function *() {
|
2016-05-27 17:57:48 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("remove", function() {
|
|
|
|
it('should remove a document', function *() {
|
2016-05-27 17:57:48 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|