starting with unit tests

This commit is contained in:
dave 2025-03-31 01:18:40 +02:00
parent 10ceebd1e6
commit 765ca6011e
5 changed files with 88 additions and 25 deletions

View File

@ -1,2 +1,2 @@
# connection string for mongodb in the form of "mongodb://user:password@host:port/db" # 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

View File

@ -1,5 +1,6 @@
import { readFileSync } from "fs"; import { readFileSync } from "fs";
const envFile = readFileSync('./.env', { encoding: 'utf8' }); try {
const envFile = readFileSync('./.env', { encoding: 'utf8' });
process.env.MONGO_CONN_STR = envFile.split('MONGO_CONN_STR=')[1].split('\n')[0]; process.env.MONGO_TEST_CONN_STR = envFile.split('MONGO_TEST_CONN_STR=')[1].split('\n')[0];
} catch (e) {}

View File

@ -1,30 +1,50 @@
import { MongoClient } from 'mongodb'; import { MongoClient } from 'mongodb';
/*
console.log('connecting to ' + process.env.MONGO_CONN_STR);
const
*/
let mongo; let mongo;
let db; let db;
let functionCollection = {}; let functionCollection = {};
export function init(connectionString) { export function init(connectionString) {
console.log(connectionString); try {
mongo = new MongoClient(connectionString); mongo = new MongoClient(connectionString);
mongo.connect().catch(e => {throw e});
db = mongo.db(); db = mongo.db();
return mongo.connect().then(() => Promise.resolve(functionCollection));
return functionCollection; } catch (e) {
return Promise.reject(e);
}
} }
functionCollection.subsribe = function subsribe (b) { functionCollection.subscribe = function subscribe (collectionName, filter) {
let collection = db.collection(b); let collection = db.collection(collectionName);
collection.find({}, {}).toArray().then(r => console.log); return collection
.find(filter, {})
.toArray();
} }
functionCollection.persist = function persist (b) { functionCollection.persist = function persist (collectionName, filter, fieldName, value) {
console.log('persist'); 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) { functionCollection.publish = function publish (b) {
console.log('publish') 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);
}

View File

@ -5,6 +5,7 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "mocha", "test": "mocha",
"testWatch": "mocha -w",
"start": "node --env-file=.env index.js", "start": "node --env-file=.env index.js",
"watch": "node --watch --env-file=.env index.js" "watch": "node --watch --env-file=.env index.js"
}, },

View File

@ -1,12 +1,53 @@
import mocha from 'mocha'; import { before } from 'mocha';
import { subscribe } from '../lib/db.js'; 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('lib/db', function () {
describe('#indexOf()', function () { let myDb;
it('should return -1 when the value is not present', function () { let dbFuncs;
assert.equal([1, 2, 3].indexOf(4), -1);
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);
});
});
}); });
}); });
}); });