starting with unit tests
This commit is contained in:
parent
10ceebd1e6
commit
765ca6011e
@ -1,2 +1,2 @@
|
||||
# connection string for mongodb in the form of "mongodb://user:password@host:port/db"
|
||||
MONGO_CONN_STR=mongodb://timo:bert@127.0.0.1:27017/timobert
|
||||
MONGO_CONN_STR=mongodb://timo:bert@127.0.0.1:27017/testSuite
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { readFileSync } from "fs";
|
||||
|
||||
const envFile = readFileSync('./.env', { encoding: 'utf8' });
|
||||
|
||||
process.env.MONGO_CONN_STR = envFile.split('MONGO_CONN_STR=')[1].split('\n')[0];
|
||||
try {
|
||||
const envFile = readFileSync('./.env', { encoding: 'utf8' });
|
||||
process.env.MONGO_TEST_CONN_STR = envFile.split('MONGO_TEST_CONN_STR=')[1].split('\n')[0];
|
||||
} catch (e) {}
|
||||
|
||||
46
lib/db.js
46
lib/db.js
@ -1,30 +1,50 @@
|
||||
import { MongoClient } from 'mongodb';
|
||||
/*
|
||||
console.log('connecting to ' + process.env.MONGO_CONN_STR);
|
||||
const
|
||||
*/
|
||||
|
||||
let mongo;
|
||||
let db;
|
||||
let functionCollection = {};
|
||||
|
||||
export function init(connectionString) {
|
||||
console.log(connectionString);
|
||||
try {
|
||||
mongo = new MongoClient(connectionString);
|
||||
mongo.connect().catch(e => {throw e});
|
||||
db = mongo.db();
|
||||
|
||||
return functionCollection;
|
||||
return mongo.connect().then(() => Promise.resolve(functionCollection));
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
functionCollection.subsribe = function subsribe (b) {
|
||||
let collection = db.collection(b);
|
||||
collection.find({}, {}).toArray().then(r => console.log);
|
||||
functionCollection.subscribe = function subscribe (collectionName, filter) {
|
||||
let collection = db.collection(collectionName);
|
||||
return collection
|
||||
.find(filter, {})
|
||||
.toArray();
|
||||
}
|
||||
|
||||
functionCollection.persist = function persist (b) {
|
||||
console.log('persist');
|
||||
functionCollection.persist = function persist (collectionName, filter, fieldName, value) {
|
||||
let collection = db.collection(collectionName);
|
||||
let newValue = { $set: {} };
|
||||
newValue.$set[fieldName + ".modifiedAt"] = Date.now();
|
||||
newValue.$set[fieldName + ".modifiedBy"] = "anonymous";
|
||||
newValue.$set[fieldName + ".transientValue"] = value;
|
||||
|
||||
return collection.updateMany(filter, newValue, { upsert: true });
|
||||
}
|
||||
|
||||
functionCollection.publish = function publish (b) {
|
||||
console.log('publish')
|
||||
}
|
||||
|
||||
functionCollection.close = function close () {
|
||||
return mongo.close();
|
||||
}
|
||||
|
||||
functionCollection.purge = function purge (collectionNames) {
|
||||
let promises = [];
|
||||
|
||||
for (let i = 0; i < collectionNames.length; i++) {
|
||||
const collection = db.collection(collectionNames[i]);
|
||||
promises.push(collection.drop());
|
||||
}
|
||||
return Promise.all(promises);
|
||||
}
|
||||
@ -5,6 +5,7 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "mocha",
|
||||
"testWatch": "mocha -w",
|
||||
"start": "node --env-file=.env index.js",
|
||||
"watch": "node --watch --env-file=.env index.js"
|
||||
},
|
||||
|
||||
@ -1,12 +1,53 @@
|
||||
import mocha from 'mocha';
|
||||
import { subscribe } from '../lib/db.js';
|
||||
import { before } from 'mocha';
|
||||
import * as db from '../lib/db.js';
|
||||
import * as assert from 'assert/strict';
|
||||
|
||||
const assert = mocha.assert;
|
||||
const rng = Math.random();
|
||||
|
||||
describe('lib/db', function () {
|
||||
describe('#indexOf()', function () {
|
||||
it('should return -1 when the value is not present', function () {
|
||||
assert.equal([1, 2, 3].indexOf(4), -1);
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#subscribe()', function () {
|
||||
it('should be able to read from mongodb', function () {
|
||||
return dbFuncs
|
||||
.subscribe('readWriteTest', {})
|
||||
.then(v => {
|
||||
assert.equal(v.length, 1);
|
||||
assert.equal(v[0].rng.transientValue, rng);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user