74 lines
2.4 KiB
JavaScript
74 lines
2.4 KiB
JavaScript
import { before } from 'mocha';
|
|
import * as db from '../lib/db.js';
|
|
import * as assert from 'assert/strict';
|
|
|
|
const rng = Math.random();
|
|
|
|
describe('lib/db', function () {
|
|
let myDb;
|
|
let dbFuncs;
|
|
|
|
describe('#init()', function () {
|
|
it('should fail to connect given invalid connection string', function () {
|
|
return db.init('not a connection string').then(() => Promise.reject()).catch(() => Promise.resolve());
|
|
});
|
|
it('should successfully connect to mongodb', function () {
|
|
return myDb = db.init(process.env.MONGO_TEST_CONN_STR);
|
|
});
|
|
});
|
|
describe('#close()', function () {
|
|
it('should close the connection', function () {
|
|
return myDb.then(db => db.close());
|
|
});
|
|
});
|
|
|
|
describe('with a established connection ->', function () {
|
|
before(function () {
|
|
myDb = db.init(process.env.MONGO_TEST_CONN_STR)
|
|
.then(funcs => dbFuncs = funcs);
|
|
});
|
|
|
|
after(function () {
|
|
dbFuncs.purge(['readWriteTest'])
|
|
.then(() => myDb.then(db => db.close()));
|
|
});
|
|
|
|
describe('#persist()', function () {
|
|
it('should be able to write to mongodb', function () {
|
|
return dbFuncs.persist('readWriteTest', {}, 'rng', rng);
|
|
});
|
|
it('should be able to write a second entry into the same collection based on a filter', function () {
|
|
return dbFuncs.persist('readWriteTest', { 'rng.transientValue': 2*rng }, 'rng', 2*rng);
|
|
});
|
|
});
|
|
|
|
describe('#subscribe()', function () {
|
|
it('should be able to read from mongodb', function () {
|
|
return dbFuncs
|
|
.subscribe('readWriteTest', {})
|
|
.then(v => {
|
|
assert.equal(v.length, 2);
|
|
});
|
|
});
|
|
it('should be able to query for specific values from mongodb', function () {
|
|
return dbFuncs
|
|
.subscribe('readWriteTest', { 'rng.transientValue': rng })
|
|
.then(v => {
|
|
assert.equal(v.length, 1);
|
|
assert.equal(v[0].rng.transientValue, rng);
|
|
});
|
|
});
|
|
it('should be able to query for specific values from mongodb based on _id', function () {
|
|
return dbFuncs
|
|
.subscribe('readWriteTest', { 'rng.transientValue': rng })
|
|
.then(v => {
|
|
return dbFuncs.subscribe('readWriteTest', { '_id': v[0]._id });
|
|
})
|
|
.then(v => {
|
|
assert.equal(v.length, 1);
|
|
assert.equal(v[0].rng.transientValue, rng);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}); |