1
0
mirror of https://github.com/stolksdorf/homebrewery.git synced 2025-12-23 04:01:29 +00:00

MOved the brews search to its own file, writing out more tests

This commit is contained in:
Scott Tolksdorf
2017-01-27 10:36:07 -05:00
parent 2f82d3875e
commit 8e58e5aca9
6 changed files with 180 additions and 109 deletions

View File

@@ -47,5 +47,6 @@ return Promise.resolve()
}) })
.then(() => { .then(() => {
console.log(`\n Added ${randomBrews.length + specificBrews.length} brews.`); console.log(`\n Added ${randomBrews.length + specificBrews.length} brews.`);
return DB.close();
}) })
.catch(console.error); .catch(console.error);

View File

@@ -93,63 +93,8 @@ const BrewData = {
getByEdit : (editId) => { getByEdit : (editId) => {
return BrewData.get({ editId }); return BrewData.get({ editId });
}, },
//TODO: Add a 'core search' which just takes a search object
//TODO: extend the core search with a user search and a term search
//TODO: break these functions off into a 'brew.search.js' file
//TODO: pagniation, sorting and full access should be an 'opts' param
search : (searchTerms, pagination, sorting, fullAccess = true) => {
let query = {};
if(searchTerms){
query = {$text: {
//Wrap terms in quots to perform an AND operator
$search: _.map(searchTerms.split(' '), (term)=>{
return `\"${term}\"`;
}).join(' '),
$caseSensitive : false
}};
}
pagination = _.defaults(pagination, {
limit : 25,
page : 0
});
sorting = _.defaults(sorting, {
'views' : 1
});
let filter = {
//editId : 0,
text : 0
};
if(!fullAccess){
filter.editId = 0;
query.published = false;
}
const searchQuery = Brew
.find(query)
.sort(sorting)
.select(filter)
.limit(pagination.limit)
.skip(pagination.page * pagination.limit)
.exec();
const countQuery = Brew.count(query).exec();
return Promise.all([searchQuery, countQuery])
.then((result) => {
return {
brews : result[0],
total : result[1]
}
});
},
}; };
module.exports = BrewData; const BrewSearch = require('./brew.search.js')(Brew);
module.exports = _.merge(BrewData, BrewSearch);

59
server/brew.search.js Normal file
View File

@@ -0,0 +1,59 @@
const _ = require('lodash');
module.exports = (Brew) => {
const cmds = {
termSearch : (terms='', opts, fullAccess) => {
const query = {$text: {
//Wrap terms in quotes to perform an AND operation
$search: _.map(terms.split(' '), (term)=>{
return `\"${term}\"`;
}).join(' '),
$caseSensitive : false
}};
return cmds.search(query, opts, fullAccess);
},
userSearch : (username, opts, fullAccess) => {
const query = {
authors : username
};
return cmds.search(query, opts, fullAccess);
},
search : (queryObj={}, opts={}, fullAccess = true) => {
const pagination = _.defaults(opts.pagination, {
limit : 25,
page : 0
});
const sorting = _.defaults(opts.sorting, {
'views' : 1
});
let filter = {
text : 0
};
if(!fullAccess){
filter.editId = 0;
queryObj.published = false;
}
const searchQuery = Brew
.find(queryObj)
.sort(sorting)
.select(filter)
.limit(pagination.limit)
.skip(pagination.page * pagination.limit)
.exec();
const countQuery = Brew.count(queryObj).exec();
return Promise.all([searchQuery, countQuery])
.then((result) => {
return {
brews : result[0],
total : result[1]
}
});
}
};
return cmds;
};

View File

@@ -24,5 +24,13 @@ module.exports = {
); );
}); });
}, },
close : ()=>{
return new Promise((resolve, reject) => {
mongoose.connection.close(()=>{
log.info('DB connection closed.');
return resolve();
});
});
},
instance : mongoose instance : mongoose
} }

View File

@@ -111,4 +111,28 @@ describe('Brew API', () => {
}); });
}); });
describe('Search', () => {
it.skip('should be able to search for brews with given terms', ()=>{
});
it.skip('should exclude unpublished brews and have no editIdsh', ()=>{
});
it.skip('should sort the search', ()=>{
});
it.skip('should use pagniation on the search', ()=>{
});
});
describe('User', () => {
it.skip('should be able to query brews for a specific user', ()=>{
});
it.skip('should return full access to brews if loggedin user is queried user', ()=>{
});
});
}); });

View File

@@ -11,11 +11,13 @@ const ids = (brewIds) => {
}); });
} }
//TODO: Move this brew generator to test.init
const brews = { const brews = {
BrewA : { BrewA : {
title : 'BrewA', title : 'BrewA',
description : 'fancy', description : 'fancy',
authors : [], authors : ['userA'],
systems : [], systems : [],
views : 12, views : 12,
published : false published : false
@@ -23,7 +25,7 @@ const brews = {
BrewB : { BrewB : {
title : 'BrewB', title : 'BrewB',
description : 'very fancy', description : 'very fancy',
authors : [], authors : ['userA'],
systems : [], systems : [],
views : 7, views : 7,
published : true published : true
@@ -31,7 +33,7 @@ const brews = {
BrewC : { BrewC : {
title : 'BrewC', title : 'BrewC',
description : 'test', description : 'test',
authors : [], authors : ['userA', 'userB'],
systems : [], systems : [],
views : 0, views : 0,
published : false published : false
@@ -39,7 +41,7 @@ const brews = {
BrewD : { BrewD : {
title : 'BrewD', title : 'BrewD',
description : 'test super amazing brew for 5e. Geared for Rangers.', description : 'test super amazing brew for 5e. Geared for Rangers.',
authors : [], authors : ['userC'],
systems : [], systems : [],
views : 1, views : 1,
published : true published : true
@@ -58,6 +60,10 @@ describe('Brew Search', () => {
describe('Searching', ()=>{ describe('Searching', ()=>{
it.skip('should return a total and a brew array', ()=>{
});
it('should be able to search for all brews', ()=>{ it('should be able to search for all brews', ()=>{
return BrewData.search() return BrewData.search()
.then((result) => { .then((result) => {
@@ -65,52 +71,34 @@ describe('Brew Search', () => {
result.brews.length.should.be.equal(_.size(brews)); result.brews.length.should.be.equal(_.size(brews));
}) })
}); });
it('should search brews based on title', () => {
return BrewData.search('BrewC')
.then((result) => {
result.total.should.be.equal(1);
result.brews.should.containSubset(ids(['BrewC']));
})
}); });
it('should search brews based on description', () => { describe('Pagniation', () => {
return BrewData.search('fancy') it.skip('should return the exact number of brews based on limit', () => {
.then((result) => {
result.total.should.be.equal(2);
result.brews.should.containSubset(ids(['BrewA', 'BrewB']));
})
});
it('should search brews based on multiple terms', () => {
return BrewData.search('ranger 5e')
.then((result) => {
result.total.should.be.equal(1);
result.brews.should.containSubset(ids(['BrewD']));
})
});
it('should perform an AND operation on the provided terms', () => {
return BrewData.search('BrewD GARBAGE')
.then((result) => {
result.total.should.be.equal(0);
});
});
it('should search brews based on a combination of both', () => {
return BrewData.search('BrewB fancy')
.then((result) => {
result.total.should.be.equal(1);
result.brews.should.containSubset(ids(['BrewB']));
});
});
it.skip('should be able to search for a specific system', ()=>{
}); });
it.skip('should be able to search for a specifc user', ()=>{
it.skip('should return the correct pages when specified', () => {
}); });
})
it.skip('should return a partial list if on the last page', () => {
});
});
describe('Sorting', ()=>{
it.skip('should sort ASC', () => {
});
it.skip('should sort DESC', () => {
});
it.skip('should sort based on multiple fields', () => {
});
});
describe('Permissions', () => { describe('Permissions', () => {
it.skip('should only fetch published brews', () => { it.skip('should only fetch published brews', () => {
@@ -127,16 +115,62 @@ describe('Brew Search', () => {
}); });
}); });
describe('Pagniation', () => {
it.skip('should return the exact number of brews based on limit', () => {
describe('Term Search', ()=>{
it('should search brews based on title', () => {
return BrewData.termSearch('BrewC')
.then((result) => {
result.total.should.be.equal(1);
result.brews.should.containSubset(ids(['BrewC']));
})
}); });
it('should search brews based on description', () => {
return BrewData.termSearch('fancy')
.then((result) => {
result.total.should.be.equal(2);
result.brews.should.containSubset(ids(['BrewA', 'BrewB']));
})
}); });
describe('Sorting', ()=>{ it('should search brews based on multiple terms', () => {
return BrewData.termSearch('ranger 5e')
.then((result) => {
result.total.should.be.equal(1);
result.brews.should.containSubset(ids(['BrewD']));
})
}); });
it('should perform an AND operation on the provided terms', () => {
return BrewData.termSearch('BrewD GARBAGE')
.then((result) => {
result.total.should.be.equal(0);
});
});
it('should search brews based on a combination of both', () => {
return BrewData.termSearch('BrewB fancy')
.then((result) => {
result.total.should.be.equal(1);
result.brews.should.containSubset(ids(['BrewB']));
});
});
});
describe('User Search', ()=>{
it('should return brews just for a single user', () => {
return BrewData.userSearch('userA')
.then((result) => {
result.total.should.be.equal(3);
result.brews.should.containSubset(ids(['BrewA', 'BrewB', 'BrewC']));
});
});
it('should return nothing if provided a non-exsistent user', () => {
return BrewData.userSearch('userXYZ')
.then((result) => {
result.total.should.be.equal(0);
});
});
});
}); });