From 9f2066d66a27ed31138e2916100a419d1d2d2471 Mon Sep 17 00:00:00 2001 From: Aiko Mastbbom Date: Thu, 26 Mar 2020 23:33:14 +0100 Subject: [PATCH] wip rethink test --- test/test.rethinkData.js | 419 ++++++++++++++++++++++----------------- 1 file changed, 235 insertions(+), 184 deletions(-) diff --git a/test/test.rethinkData.js b/test/test.rethinkData.js index 36110f5..4b6bd38 100644 --- a/test/test.rethinkData.js +++ b/test/test.rethinkData.js @@ -1,40 +1,40 @@ 'use strict'; //noinspection JSUnresolvedVariable -var libPath = process.env.PROTOTYPER_COV ? '../lib-cov' : '../lib'; -var chai = require('chai'); +var libPath = process.env.PROTOTYPER_COV ? '../lib-cov' : '../lib'; +var chai = require('chai'); chai.config.includeStack = true; // defaults to false chai.config.showDiff = false; // defaults to false var expect = chai.expect; -var rethink = require('rethinkdb'); +var rethink = require('rethinkdb'); var reConnection = null var rethinkData = require(libPath + '/rethinkData.js'); -var config = { - debug: function () { - //console.log(arguments); - }, - info: function () { - //console.log(arguments); - }, - warn: function () { - //console.error(arguments); - }, - error: function () { - console.error(arguments); - } +var config = { + debug: function () { + //console.log(arguments); + }, + info: function () { + //console.log(arguments); + }, + warn: function () { + //console.error(arguments); + }, + error: function () { + console.error(arguments); + } }; describe('rethinkData', function () { - beforeEach(function(done){ + beforeEach(function (done) { var config = { host: 'localhost', port: 28015, - db: 'PrototyperTest' + db: 'PrototyperTest' }; rethink.connect(config, function connection_result(err, connection) { reConnection = connection - if(err){ + if (err) { return done(err) } rethink.dbList() @@ -52,10 +52,11 @@ describe('rethinkData', function () { }); }); - afterEach(function(done){ + afterEach(function (done) { reConnection.close(done) }) - describe('getContent', function () { + + describe('getContent', function () { var r = { contains_collection: '', @@ -63,7 +64,7 @@ describe('rethinkData', function () { tableList: function () { return this; }, - contains: function(collection){ + contains: function (collection) { this.contains_collection = collection this.query = null return this; @@ -72,7 +73,7 @@ describe('rethinkData', function () { return this; }, run: function (c, callback) { - if(this.query === null) { + if (this.query === null) { // first time run switch (this.contains_collection) { @@ -90,179 +91,229 @@ describe('rethinkData', function () { return callback(new Error('bad_di')) } }, - filter: function (query){ + filter: function (query) { this.query = query } }; - var option_list = [ - {}, // no collection - {query: 'q'}, // no collection - {collection: 'test_error'}, // bad collection - {collection: 'col'}, // no query - { - collection: 'no_hex', - query: {id: 'id'} // not a hexString - }, - { - collection: 'no_col', // trigger findOne error - query: {id: '123456789012345678901234'} - }, - { - collection: 'col', // trigger null result - query: {id: '234567890123456789012345'} - }, - { - collection: 'col', // trigger '' result - query: {id: '345678901234567890123456'} - }, - { - collection: 'ok', // trigger 'ok' result - query: {id: '456789012345678901234567'} - } - ]; - var i; + var option_list = [ + {}, // no collection + {query: 'q'}, // no collection + {collection: 'no_query'}, // no query + { + collection: 'no_col', // collection does not exist + query: {id: 'id'} // not a hexString + }, + { + collection: 'no_hex', + query: {id: 'id'} // not a hexString + }, + { + collection: 'no_col', // trigger findOne error + query: {id: '123456789012345678901234'} + }, + { + collection: 'col', // trigger null result + query: {id: '234567890123456789012345'} + }, + { + collection: 'col', // trigger '' result + query: {id: '345678901234567890123456'} + }, + { + collection: 'ok', // trigger 'ok' result + query: {id: '456789012345678901234567'} + } + ]; + var i; - function testArguments(options) { - return function (done) { + function testArguments(options, testNumber) { + return function (done) { var dataInstance = rethinkData(config, rethink, reConnection); - dataInstance.getContent(options, function (err, result) { - if (result === 'ok') { - expect(result).to.equal('ok'); - expect(err).to.not.be.ok; - done(); - } else { - expect(err).to.be.instanceOf(Error); - expect(result).to.not.be.ok; - done(); - } - }); - }; - } - for (i = 0; i < option_list.length; i += 1) { - it('should handle arguments correctly ' + JSON.stringify(option_list[i]), - testArguments(option_list[i]) - ); - } - }); + function handleResult(err, result) { + // if (result === 'ok') { + // expect(result).to.equal('ok'); + // expect(err).to.not.be.ok; + // done(); + // } else { - describe('getContentAttribute', function () { - var ok = { - id: '345678901234567890123456', - parent: '456789012345678901234567', - name: 'test.content_attribute' - }; - function findOne(q, cb) { - if (q._id && q._id.toString() === '123456789012345678901234') { - return cb(null, { - id: '123456789012345678901234', - name: 'test', - error_attribute: { - guid: '234567890123456789012345' - }, - content_attribute: { - guid: '345678901234567890123456' - } - }); - } - if (q.id && q.id === '234567890123456789012345') { - return cb(new Error(q)); - } - if (q.id && q.id === '345678901234567890123456') { - return cb(null, ok); - } - if (q.id && q.id === '456789012345678901234567') { - return cb(null, { - id: '456789012345678901234567', - name: 'test' - }); - } - if (q.parent && q.parent.toString() === '456789012345678901234567') { - if (q.name === 'test.content_attribute'){ - return cb(null, ok); - } else { - return cb(new Error(q)); - } - } - throw new Error('fail:' + JSON.stringify(arguments)); - } + switch (testNumber) { + case 0: + case 1: + expect(err).to.be.instanceOf(Error); + expect(err.message).to.be.equal('Data not found / missing collection'); + expect(result).to.not.be.ok; + return done(); + case 2: + expect(err).to.be.instanceOf(Error); + expect(err.message).to.be.equal('Data not found no_query / missing query'); + expect(result).to.not.be.ok; + return done(); + case 3: + expect(err).to.be.instanceOf(Error); + expect(err.message).to.be.equal('Data not found no_col / {"id":"id"}'); + expect(result).to.not.be.ok; + return done(); + case 4: + expect(err).to.be.instanceOf(Error); + expect(err.message).to.be.equal('Data not found no_hex / {"id":"id"}'); + expect(result).to.not.be.ok; + return done(); + default: + console.log('------------------------------') + console.log('testNumber', testNumber); + console.log('options', options); + console.log('err', err); + console.log('result', result); + return done(new Error('unknown testnumber' + testNumber)); + } + } - var col = {findOne: findOne}; - var r = { + switch (testNumber) { + case 0: + case 1: + case 2: + case 3: + default: + dataInstance.getContent(options, handleResult); + break; + case 4: + dataInstance.setContent({}, options, function (err, result) { + if (err) { + return done(err); + } + dataInstance.getContent(options, handleResult); + }) + break; + } + }; + } + + for (i = 0; i < option_list.length; i += 1) { + it('should handle arguments correctly ' + JSON.stringify(option_list[i]), + testArguments(option_list[i], i) + ); + } + }); + + describe('getContentAttribute', function () { + var ok = { + id: '345678901234567890123456', + parent: '456789012345678901234567', + name: 'test.content_attribute' + }; + + function findOne(q, cb) { + if (q._id && q._id.toString() === '123456789012345678901234') { + return cb(null, { + id: '123456789012345678901234', + name: 'test', + error_attribute: { + guid: '234567890123456789012345' + }, + content_attribute: { + guid: '345678901234567890123456' + } + }); + } + if (q.id && q.id === '234567890123456789012345') { + return cb(new Error(q)); + } + if (q.id && q.id === '345678901234567890123456') { + return cb(null, ok); + } + if (q.id && q.id === '456789012345678901234567') { + return cb(null, { + id: '456789012345678901234567', + name: 'test' + }); + } + if (q.parent && q.parent.toString() === '456789012345678901234567') { + if (q.name === 'test.content_attribute') { + return cb(null, ok); + } else { + return cb(new Error(q)); + } + } + throw new Error('fail:' + JSON.stringify(arguments)); + } + + var col = {findOne: findOne}; + var r = { contains_collection: '', - tableList: function(){ - return this; - }, - contains: function(collection){ - this.contains_collection = collection - return this; - }, - do: function(){ - return this; - }, - run: function(c,callback){ - return callback(new Error('db fails')); - } - }; - var option_list = [ - {}, // no collection - {query: 'q'}, // no collection - {collection: 'col'}, // no query - { - collection: 'no_attr', - query: {id: '123456789012345678901234'} // no attribute - }, - { - collection: 'col', // trigger null result - query: {id: '123456789012345678901234'}, - attribute: 'error_attribute' - }, - { - collection: 'ok', // trigger 'ok' result - query: {id: '123456789012345678901234'}, - attribute: 'content_attribute' + tableList: function () { + return this; + }, + contains: function (collection) { + this.contains_collection = collection + return this; + }, + do: function () { + return this; + }, + run: function (c, callback) { + return callback(new Error('db fails')); + } + }; + var option_list = [ + {}, // no collection + {query: 'q'}, // no collection + {collection: 'col'}, // no query + { + collection: 'no_attr', + query: {id: '123456789012345678901234'} // no attribute + }, + { + collection: 'col', // trigger null result + query: {id: '123456789012345678901234'}, + attribute: 'error_attribute' + }, + { + collection: 'ok', // trigger 'ok' result + query: {id: '123456789012345678901234'}, + attribute: 'content_attribute' - }, - { - collection: 'col', // trigger error result - query: {id: '456789012345678901234567'}, - attribute: 'error_attribute' - }, - { - collection: 'ok', // trigger 'ok' result - query: {id: '456789012345678901234567'}, - attribute: 'content_attribute' - } - ]; - var dataInstance = rethinkData(config, r, {}); - var i; + }, + { + collection: 'col', // trigger error result + query: {id: '456789012345678901234567'}, + attribute: 'error_attribute' + }, + { + collection: 'ok', // trigger 'ok' result + query: {id: '456789012345678901234567'}, + attribute: 'content_attribute' + } + ]; + var dataInstance = rethinkData(config, r, {}); + var i; - function testArguments(options) { - return function (done) { - dataInstance.getAttribute(options, function (err, result) { - if (options.collection === 'ok') { - console.log('result',result); - console.log('err',err); - console.log('options',options); - expect(result).to.equal(ok); - expect(err).to.not.be.ok; - done(); - } else { - //console.log('err',err); - expect(err).to.be.instanceOf(Error); - expect(result).to.not.be.ok; - done(); - } - }); - }; - } + function testArguments(options) { + return function (done) { + dataInstance.getAttribute(options, function (err, result) { + if (options.collection === 'ok') { + console.log('result', result); + console.log('err', err); + console.log('options', options); + expect(result).to.equal(ok); + expect(err).to.not.be.ok; + done(); + } else { + //console.log('err',err); + expect(err).to.be.instanceOf(Error); + expect(result).to.not.be.ok; + done(); + } + }); + }; + } - for (i = 0; i < option_list.length; i += 1) { - it('should handle arguments correctly ' + JSON.stringify(option_list[i]), - testArguments(option_list[i]) - ); - } - }); + for (i = 0; i < option_list.length; i += 1) { + it('should handle arguments correctly ' + JSON.stringify(option_list[i]), + testArguments(option_list[i]) + ); + } + }); });