Skip to content

Instantly share code, notes, and snippets.

@calvinfroedge
Created July 17, 2015 17:26
Show Gist options
  • Save calvinfroedge/5a07cc7a6df41d874aea to your computer and use it in GitHub Desktop.
Save calvinfroedge/5a07cc7a6df41d874aea to your computer and use it in GitHub Desktop.
var quizStore = require('../dist/quiz').quizStore;
var quizInstance = require('../dist/quiz').quizInstance;
var md5 = require('md5');
var sampleQuiz = function(){
return {
name: 'test',
questions: [
[
'the question',
'the answer',
['a distraction']
]
]
}
}
//Create the storage instance and create the sample quiz
function setupCreate(callback){
var storage = require('../dist/storage/memory').memoryStore;
var qs = new quizStore(storage);
qs.create(sampleQuiz(), function(err, id){
callback(err, id, qs);
});
}
//Should be able to instantiate quiz with memory storage
//Test adding a quiz. Quiz should send id to callback.
exports.testCreate = function(test){
test.expect(1);
setupCreate(function(err, id){
test.ok(id);
test.done();
});
}
//quizStore.retrieve should give us all quizzes if only callback is given
exports.testRetrieveAll = function(test){
test.expect(1);
var qs = setupCreate(function(err, id, qs){
qs.retrieve(function(quizzes){
var l = 0;
for(key in quizzes) l++;
test.equals(1, l);
test.done();
});
});
};
//quizStore.retrieveSingle should find a single item by name or id
exports.testRetrieveSingle = function(test){
test.expect(2);
var qs = setupCreate(function(err, id, qs){
qs.retrieve('test', function(quiz){
test.ok(quiz);
});
qs.retrieve(md5('test'), function(quiz){
test.ok(quiz);
test.done();
});
});
};
//Get a question from a quiz
exports.testGetQuestionFromQuiz = function(test){
test.expect(1);
var qs = setupCreate(function(err, id, qs){
quizInstance(qs.storageInstance, id).getQuestion('the question', function(err, question){
test.ok(question);
test.done();
});
});
}
//should be able to add a new question to a quiz
exports.testAddQuestionToQuiz = function(test){
test.expect(1);
var qs = setupCreate(function(err, id, qs){
quizInstance(qs.storageInstance, id).addQuestion('new question', 'answer', ['distraction'], function(err, questionId){
test.ok(questionId);
test.done();
});
});
}
//should be able to add / remove questions
exports.testAddRemoveQuestionFromQuiz = function(test){
test.expect(1);
var qs = setupCreate(function(err, id, qs){
var instance = quizInstance(qs.storageInstance, id);
instance.addQuestion('new question', 'answer', ['distraction'], function(err, questionId){
instance.removeQuestion('new question', function(err){
test.ok(!err);
test.done();
});
});
});
}
//should throw an error rmoeving a question that doesn't exist
exports.testRemoveNonExistentQuestionFromQuiz = function(test){
test.expect(1);
var qs = setupCreate(function(err, id, qs){
var instance = quizInstance(qs.storageInstance, id);
instance.removeQuestion('new question', function(err){
test.ok(err);
test.done();
});
});
}
//should be able to edit a question
exports.testEditQuestionFromQuiz = function(test){
test.expect(2);
var qs = setupCreate(function(err, id, qs){
var instance = quizInstance(qs.storageInstance, id);
instance.editQuestion('the question', {answer: 'bar'}, function(err, qid){
test.equals(md5('the question'), qid);
test.ok(qid);
test.done();
});
});
}
//should recognize correct and incorrect answers
exports.testAnswerQuizQuestion = function(test){
var n = 3;
test.expect(n);
var i = 0;
var done = function(){
++i;
console.log('i is', i, 'n is', n);
if(i == n) test.done();
}
var qs = setupCreate(function(err, id, qs){
var instance = new quizInstance(qs.storageInstance, id);
instance.checkAnswer('the question', 'the answer', function(err, correct){
test.ok(correct);
done();
});
instance.checkAnswer(md5('the question'), 'the answer', function(err, correct){
test.ok(correct);
done();
});
instance.checkAnswer('the question', 'the answerz', function(err, correct){
test.equals(false, correct);
done();
});
});
}
//should return a sample of ids
exports.testRandomSampleFromQuiz = function(test){
test.expect(1);
var qs = setupCreate(function(err, id, qs){
var instance = quizInstance(qs.storageInstance, id);
var n = 3;
var added = 0;
function done(){
++added;
if(added == n){
instance.randomSample(3, function(err, sample){
var l = 0;
for(var key in sample) l++;
test.equals(n, l);
test.done();
});
}
}
instance.addQuestion('new question', 'answer', ['distraction'], done);
instance.addQuestion('another question', 'answer', ['distraction', 'dist'], done);
instance.addQuestion('last', 'I\'m the answer', ['distraction', 'dist'], done);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment