1
0
mirror of https://github.com/stolksdorf/homebrewery.git synced 2025-12-13 09:05:56 +00:00

created a brew generator and chai plugin for easier testing

This commit is contained in:
Scott Tolksdorf
2017-01-27 18:38:09 -05:00
parent 8018442f25
commit a826aaffd9
6 changed files with 124 additions and 111 deletions

View File

@@ -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",

View File

@@ -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);

22
scripts/populate.js Normal file
View File

@@ -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);

88
test/brew.gen.js Normal file
View File

@@ -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(', ')}`
)
});
}
};

View File

@@ -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', () => {

View File

@@ -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()
};