Skip to content

Instantly share code, notes, and snippets.

@simpulton
Created November 26, 2013 04:01
Show Gist options
  • Save simpulton/7653278 to your computer and use it in GitHub Desktop.
Save simpulton/7653278 to your computer and use it in GitHub Desktop.
Index test with helpers
it.only('buildIndex should properly build an index', inject(function (RealtimeQuestion) {
var post = generateMockPost();
var index = generateMockIndex(post);
RealtimeQuestion.buildIndex(post).should.deep.equal(index);
}));
//-------------------------------------------------------------------------
// HELPER FUNCTIONS
//-------------------------------------------------------------------------
var getRandomInt = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
var generateMockPost = function() {
var numAnswers = getRandomInt(1,10);
var numComments = getRandomInt(1,10);
var post = { _id: 'p1', comments:[], answers:[]};
for(var c = 0; c < numComments; ++c ) {
post.comments.push({_id: 'c' + c});
}
for(var a = 0; a < numAnswers; ++a ) {
var answer = {_id: 'a' + a, comments:[]};
var numAnswerComments = getRandomInt(1,10);
for(var ac = 0; ac < numAnswerComments; ++ac ) {
answer.comments.push({_id: 'ac' + ac});
}
post.answers.push(answer);
}
return post;
}
var generateMockIndex = function(post) {
var index = {};
index[post._id] = { obj: post, collection: []};
post.comments.each(function (comment) {
index[comment._id] = { obj: comment, collection: post.comments};
});
post.answers.each(function (answer) {
index[answer._id] = { obj: answer, collection: post.answers};
answer.comments.each(function (comment) {
index[comment._id] = { obj: comment, collection: answer.comments};
});
});
return index;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment