X-Git-Url: http://git.squeep.com/?p=squeep-test-helper;a=blobdiff_plain;f=lib%2Fstub-database.js;fp=lib%2Fstub-database.js;h=41abaf7819279e19eb42fbfc8cd1641f914b86d1;hp=0000000000000000000000000000000000000000;hb=f4818ada492c17c8941616e935579ed7555ec636;hpb=3c28b4072422f3922ede43b91013e7da6d1e067e diff --git a/lib/stub-database.js b/lib/stub-database.js new file mode 100644 index 0000000..41abaf7 --- /dev/null +++ b/lib/stub-database.js @@ -0,0 +1,55 @@ +/* eslint-disable class-methods-use-this */ +'use strict'; + +const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require + +class StubDatabase { + constructor() { + this._implementation.forEach((fn) => { + if (!(fn in this)) { + this[fn] = async () => { /* */ }; // eslint-disable-line security/detect-object-injection + } + }); + } + + get _implementation() { + return [ + ...this._spyFns, + ...this._stubFns, + ]; + } + + get _spyFns() { + return [ + 'context', + 'transaction', + ]; + } + + get _stubFns() { + return [ + 'initialize', + 'healthCheck', + ]; + } + + _reset() { + this._spyFns.forEach((fn) => { + sinon.spy(this, fn); + }); + this._stubFns.forEach((fn) => { + sinon.stub(this, fn); + }); + } + + async context(fn) { + await fn({}); + } + + async transaction(dbCtx, fn) { + await fn(dbCtx); + } + +} + +module.exports = StubDatabase;