diff --git a/package.json b/package.json index df28a42..02d5ab8 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "quick": "node scripts/quick.js", "build": "node scripts/build.js", "phb": "node scripts/phb.js", - "genbrews": "node scripts/genbrews.js", + "populate": "node scripts/populate.js", "prod": "set NODE_ENV=production&& npm run build", "postinstall": "npm run build", "start": "node server.js", diff --git a/scripts/genbrews.js b/scripts/genbrews.js deleted file mode 100644 index e6dac10..0000000 --- a/scripts/genbrews.js +++ /dev/null @@ -1,52 +0,0 @@ -//Populates the DB with a bunch of brews for UI testing - -const _ = require('lodash'); - -const DB = require('../server/db.js'); -const BrewData = require('../server/brew.data.js'); - - -//TODO: pull in snippets and randomly add them - -const genBrew = () => { - return { - title : 'BrewA', - description : '', - text : '', - authors : _.sampleSize(['userA','userB','userC','userD'], _.random(0, 3)), - systems : _.sampleSize(['5e', '4e', '3.5e', 'Pathfinder'], _.random(0,2)), - views : _.random(0,1000), - published : !!_.random(0,1) - } -} - -const randomBrews = _.times(20, genBrew); - -const specificBrews = [ - { - text : 'Cool test', - authors : ['test'] - } -]; - - -return Promise.resolve() - .then(DB.connect) - .then(BrewData.removeAll) - .then(() => { - console.log('Adding random brews...'); - return Promise.all(_.map(randomBrews, (brew) => { - return BrewData.create(brew); - })); - }) - .then(() => { - console.log('Adding specific brews...'); - return Promise.all(_.map(specificBrews, (brew) => { - return BrewData.create(brew); - })); - }) - .then(() => { - console.log(`\n Added ${randomBrews.length + specificBrews.length} brews.`); - return DB.close(); - }) - .catch(console.error); diff --git a/scripts/populate.js b/scripts/populate.js new file mode 100644 index 0000000..7a392af --- /dev/null +++ b/scripts/populate.js @@ -0,0 +1,22 @@ +//Populates the DB with a bunch of brews for UI testing +const _ = require('lodash'); + +const DB = require('../server/db.js'); +const BrewData = require('../server/brew.data.js'); +const BrewGen = require('../test/brew.gen.js'); + +return Promise.resolve() + .then(DB.connect) + .then(BrewData.removeAll) + .then(() => { + console.log('Adding random brews...'); + return return BrewGen.populateDB(BrewGen.random(5)); + }) + .then(() => { + console.log('Adding specific brews...'); + return return BrewGen.populateDB(BrewGen.static()); + }) + .then(() => { + return DB.close(); + }) + .catch(console.error); diff --git a/test/brew.gen.js b/test/brew.gen.js new file mode 100644 index 0000000..fa67643 --- /dev/null +++ b/test/brew.gen.js @@ -0,0 +1,88 @@ +const _ = require('lodash'); +const BrewData = require('../server/brew.data.js'); + +let PopulatedBrews = {}; + +module.exports = { + random : (num = 20)=>{ + return _.times(num, ()=>{ + //TODO: Build better generator + return { + title : 'BrewA', + description : '', + text : '', + authors : _.sampleSize(['userA','userB','userC','userD'], _.random(0, 3)), + systems : _.sampleSize(['5e', '4e', '3.5e', 'Pathfinder'], _.random(0,2)), + views : _.random(0,1000), + published : !!_.random(0,1) + }; + }); + }, + static : () => { + return { + BrewA : { + title : 'Brew-Alpha', + description : 'fancy', + authors : ['userA'], + systems : [], + views : 12, + published : false + }, + BrewB : { + title : 'Brew-Beta', + description : 'very fancy', + authors : ['userA'], + systems : [], + views : 7, + published : true + }, + BrewC : { + title : 'Brew-Charlie', + description : 'test', + authors : ['userA', 'userB'], + systems : [], + views : 0, + published : false + }, + BrewD : { + title : 'Brew-Delta', + description : 'test super amazing brew for 5e. Geared for Rangers.', + authors : ['userC'], + systems : [], + views : 1, + published : true + } + }; + }, + + populateDB : (brewCollection)=>{ + return Promise.all(_.map(brewCollection, (brewData, id) => { + return BrewData.create(brewData) + .then((brew)=>{ + PopulatedBrews[id] = brew; + }); + }) + ); + }, + + chaiPlugin : (chai, utils) => { + chai.Assertion.addMethod('brews', function(...brewIds){ + new chai.Assertion(this._obj).to.be.instanceof(Array); + const valid = _.every(brewIds, (brewId) => { + const storedBrew = PopulatedBrews[brewId]; + if(!storedBrew) return false; + return _.some(this._obj, (brew)=>{ + return brew.editId == storedBrew.editId && + brew.shareId == storedBrew.shareId && + brew.title == storedBrew.title && + brew.views == storedBrew.views; + }); + }); + this.assert( + valid, + `expect #{this} to have brews ${brewIds.join(', ')}`, + `expect #{this} to not have brews ${brewIds.join(', ')}` + ) + }); + } +}; \ No newline at end of file diff --git a/test/search.test.js b/test/search.test.js index 4d88145..058199e 100644 --- a/test/search.test.js +++ b/test/search.test.js @@ -3,59 +3,16 @@ const _ = require('lodash'); const DB = require('db.js'); const BrewData = require('brew.data.js'); -const Error = require('error.js'); - -const ids = (brewIds) => { - return _.map(brewIds, (brewId) => { - return { editId : brews[brewId].editId }; - }); -} +const BrewGen = require('./brew.gen.js'); +//const Error = require('error.js'); -//TODO: Move this brew generator to test.init -const brews = { - BrewA : { - title : 'Brew-Alpha', - description : 'fancy', - authors : ['userA'], - systems : [], - views : 12, - published : false - }, - BrewB : { - title : 'Brew-Beta', - description : 'very fancy', - authors : ['userA'], - systems : [], - views : 7, - published : true - }, - BrewC : { - title : 'Brew-Charlie', - description : 'test', - authors : ['userA', 'userB'], - systems : [], - views : 0, - published : false - }, - BrewD : { - title : 'Brew-Delta', - description : 'test super amazing brew for 5e. Geared for Rangers.', - authors : ['userC'], - systems : [], - views : 1, - published : true - } -}; describe('Brew Search', () => { before('Connect DB', DB.connect); before('Clear DB', BrewData.removeAll); before('Populate brews', ()=>{ - return Promise.all(_.map(brews, (brewData, id) => { - return BrewData.create(brewData) - .then((brew)=>{ brews[id] = brew; }); - })); + return BrewGen.populateDB(BrewGen.static()); }); @@ -67,8 +24,9 @@ describe('Brew Search', () => { it('should be able to search for all brews', ()=>{ return BrewData.search() .then((result) => { - result.total.should.be.equal(_.size(brews)); - result.brews.length.should.be.equal(_.size(brews)); + const brewCount = _.size(BrewGen.static()); + result.total.should.be.equal(brewCount); + result.brews.length.should.be.equal(brewCount); }) }); }); @@ -122,7 +80,7 @@ describe('Brew Search', () => { return BrewData.termSearch('Charlie') .then((result) => { result.total.should.be.equal(1); - result.brews.should.containSubset(ids(['BrewC'])); + result.brews.should.have.brews('BrewC'); }) }); @@ -130,7 +88,7 @@ describe('Brew Search', () => { return BrewData.termSearch('fancy') .then((result) => { result.total.should.be.equal(2); - result.brews.should.containSubset(ids(['BrewA', 'BrewB'])); + result.brews.should.have.brews('BrewA', 'BrewB'); }) }); @@ -138,7 +96,7 @@ describe('Brew Search', () => { return BrewData.termSearch('ranger 5e') .then((result) => { result.total.should.be.equal(1); - result.brews.should.containSubset(ids(['BrewD'])); + result.brews.should.have.brews('BrewD'); }) }); @@ -153,7 +111,7 @@ describe('Brew Search', () => { return BrewData.termSearch('Brew Beta fancy') .then((result) => { result.total.should.be.equal(1); - result.brews.should.containSubset(ids(['BrewB'])); + result.brews.should.have.brews('BrewB'); }); }); it.skip('should not worry about the case of the terms', () => { @@ -166,7 +124,7 @@ describe('Brew Search', () => { return BrewData.userSearch('userA') .then((result) => { result.total.should.be.equal(3); - result.brews.should.containSubset(ids(['BrewA', 'BrewB', 'BrewC'])); + result.brews.should.have.brews('BrewA', 'BrewB', 'BrewC'); }); }); it('should return nothing if provided a non-exsistent user', () => { diff --git a/test/test.init.js b/test/test.init.js index 317df50..ca80e7f 100644 --- a/test/test.init.js +++ b/test/test.init.js @@ -9,15 +9,12 @@ const config = require('nconf') const Chai = require('chai') .use(require('chai-as-promised')) - .use(require('chai-subset')); + .use(require('chai-subset')) + .use(require('./brew.gen.js').chaiPlugin); const log = require('loglevel'); log.setLevel(config.get('log_level')); -//TODO: extend should to have a brewCheck -// eg. result.brews.should.haveBrews('BrewA', 'BrewB') -// Then can remove chai-subset - module.exports = { should: Chai.should() };