Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Refactoring ChallengeService#getKnowledgeData
Browse files Browse the repository at this point in the history
  • Loading branch information
Akhilian committed Apr 4, 2017
1 parent 783879f commit a8bb958
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 52 deletions.
63 changes: 46 additions & 17 deletions api/lib/domain/services/challenge-service.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,54 @@
const _ = require('../../infrastructure/utils/lodash-utils');

function _countResult(about, desiredResult) {
return _.reduce(about, function(sum, o) {
return _.reduce(about, function (sum, o) {
return sum + (o.result === desiredResult ? 1 : 0);
}, 0);
}

const PIX_LEVELS_FROM_1_TO_8 = [ 1, 2, 3, 4, 5, 6, 7, 8 ];

function _initializeNbKnowledgeTagsByLevel() {
const nbKnowledgeTagsByLevel = {};
PIX_LEVELS_FROM_1_TO_8.forEach(level => {
nbKnowledgeTagsByLevel[ level ] = 0
});
return nbKnowledgeTagsByLevel;
}

function _mapChallengesById(challengesWithKnowledgeTags) {
return challengesWithKnowledgeTags.reduce((challenges, challenge) => {
challenges[ challenge.id ] = challenge;
return challenges;
}, {});
}

function _mapKnowledgeTags(challengesWithKnowledgeTags) {
return challengesWithKnowledgeTags.reduce((knowledgeTags, challenge) => {
challenge.knowledgeTags.forEach((knowledge) => {
knowledgeTags[ knowledge ] = true;
});
return knowledgeTags;
}, {});
}
function _mapNbKnowledgeTagsByLevel(knowledgeTagSet) {
const nbKnowledgeTagsByLevel = _initializeNbKnowledgeTagsByLevel();
for (const knowledgeTag in knowledgeTagSet) {
const knowledgeLevel = parseInt(knowledgeTag.slice(-1));
nbKnowledgeTagsByLevel[ knowledgeLevel ]++;
}
return nbKnowledgeTagsByLevel;
}
module.exports = {

// TODO move to assessment-service
getKnowledgeData(challengeList) {
const challengesById = {};
const knowledgeTagSet = {};
const nbKnowledgeTagsByLevel = {};
challengeList.forEach(challenge => {
if(challenge.knowledgeTags && challenge.knowledgeTags.length > 0) {
challengesById[challenge.id] = challenge;
challenge.knowledgeTags.forEach(knowledge => knowledgeTagSet[knowledge] = true);
}
});
[1, 2, 3, 4, 5, 6, 7, 8].forEach(level => nbKnowledgeTagsByLevel[level] = 0);
for(const knowledgeTag in knowledgeTagSet) {
const knowledgeLevel = parseInt(knowledgeTag.slice(-1));
nbKnowledgeTagsByLevel[knowledgeLevel]++;
}
const challengesWithKnowledgeTags = challengeList.filter(challenge => !_.isEmpty(challenge.knowledgeTags));

const challengesById = _mapChallengesById(challengesWithKnowledgeTags);
const knowledgeTagSet = _mapKnowledgeTags(challengesWithKnowledgeTags);
const nbKnowledgeTagsByLevel = _mapNbKnowledgeTagsByLevel(knowledgeTagSet);

return {
challengesById,
knowledgeTagSet,
Expand All @@ -33,8 +58,12 @@ module.exports = {

getRevalidationStatistics(oldAnswers, newAnswers) {

const oldAnswersResult = _.map(oldAnswers, (o) => { return {id : o.id, result: o.attributes.result};});
const newAnswersResult = _.map(newAnswers, (o) => { return {id : o.id, result: o.attributes.result};});
const oldAnswersResult = _.map(oldAnswers, (o) => {
return { id: o.id, result: o.attributes.result };
});
const newAnswersResult = _.map(newAnswers, (o) => {
return { id: o.id, result: o.attributes.result };
});

const okNewCount = _countResult(newAnswersResult, 'ok');
const koNewCount = _countResult(newAnswersResult, 'ko');
Expand Down
10 changes: 10 additions & 0 deletions api/tests/unit/domain/services/assessment-service_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,14 @@ describe('Unit | Domain | Services | assessment-service', function () {

});

describe('#getScoredAssessment', () => {

it('checks sanity', () => {
expect(service.getScoredAssessment).to.exist;
});



});

});
120 changes: 85 additions & 35 deletions api/tests/unit/domain/services/challenge-service_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,97 @@ const service = require('../../../../lib/domain/services/challenge-service');
const Answer = require('../../../../lib/domain/models/data/answer');

function _buildAnswer(result) {
const answer = new Answer({id: 'answer_id'});
answer.attributes = {result: result};
const answer = new Answer({ id: 'answer_id' });
answer.attributes = { result: result };
return answer;
}

describe('Unit | Service | ChallengeService', function () {

const allCases = [
{case: 'ok', oppositeCase: 'ko'},
{case: 'ko', oppositeCase: 'ok'},
{case: 'partially', oppositeCase: 'ko'},
{case: 'timedout', oppositeCase: 'ko'},
{case: 'aband', oppositeCase: 'ko'},
{case: 'unimplemented', oppositeCase: 'ko'},
];

allCases.forEach(function (testCase) {
it('should be able to return stats about added ' + testCase.case + ' solution', function () {
const old_answer = [_buildAnswer(testCase.oppositeCase)];
const new_answer = [_buildAnswer(testCase.case)];
const under_test = service.getRevalidationStatistics(old_answer, new_answer);
expect(under_test[testCase.case]).to.equal(1);
const diffProperty = testCase.case + 'Diff';
expect(under_test[diffProperty]).to.equal(1);
});
it('should be able to return stats about removed ' + testCase.case + ' solution', function () {
const old_answer = [_buildAnswer(testCase.case)];
const new_answer = [_buildAnswer(testCase.oppositeCase)];
const under_test = service.getRevalidationStatistics(old_answer, new_answer);
expect(under_test[testCase.case]).to.equal(0);
const diffProperty = testCase.case + 'Diff';
expect(under_test[diffProperty]).to.equal(-1);
});
it('should be able to return stats that add all ' + testCase.case + ' solutions', function () {
const old_answer = [_buildAnswer(testCase.case), _buildAnswer(testCase.case)];
const new_answer = [_buildAnswer(testCase.case), _buildAnswer(testCase.case)];
const under_test = service.getRevalidationStatistics(old_answer, new_answer);
expect(under_test[testCase.case]).to.equal(2);
const diffProperty = testCase.case + 'Diff';
expect(under_test[diffProperty]).to.equal(0);
describe('#getRevalidationStatistics', () => {

[
{ case: 'ok', oppositeCase: 'ko' },
{ case: 'ko', oppositeCase: 'ok' },
{ case: 'partially', oppositeCase: 'ko' },
{ case: 'timedout', oppositeCase: 'ko' },
{ case: 'aband', oppositeCase: 'ko' },
{ case: 'unimplemented', oppositeCase: 'ko' },
]
.forEach((testCase) => {

it('should be able to return stats about added ' + testCase.case + ' solution', function () {
const old_answer = [ _buildAnswer(testCase.oppositeCase) ];
const new_answer = [ _buildAnswer(testCase.case) ];
const under_test = service.getRevalidationStatistics(old_answer, new_answer);
expect(under_test[ testCase.case ]).to.equal(1);
const diffProperty = testCase.case + 'Diff';
expect(under_test[ diffProperty ]).to.equal(1);
});

it('should be able to return stats about removed ' + testCase.case + ' solution', function () {
const old_answer = [ _buildAnswer(testCase.case) ];
const new_answer = [ _buildAnswer(testCase.oppositeCase) ];
const under_test = service.getRevalidationStatistics(old_answer, new_answer);
expect(under_test[ testCase.case ]).to.equal(0);
const diffProperty = testCase.case + 'Diff';
expect(under_test[ diffProperty ]).to.equal(-1);
});

it('should be able to return stats that add all ' + testCase.case + ' solutions', function () {
const old_answer = [ _buildAnswer(testCase.case), _buildAnswer(testCase.case) ];
const new_answer = [ _buildAnswer(testCase.case), _buildAnswer(testCase.case) ];
const under_test = service.getRevalidationStatistics(old_answer, new_answer);
expect(under_test[ testCase.case ]).to.equal(2);
const diffProperty = testCase.case + 'Diff';
expect(under_test[ diffProperty ]).to.equal(0);
});
});

});

describe('#getKnowledgeData', () => {

it('should extract knowledge data from a challenge list', () => {
// given
const challenges = [
{ id: 'tata', knowledgeTags: [ '@acquisA_1', '@acquisB_5', '@acquisC_3' ] },
{ id: 'titi', knowledgeTags: [ '@acquisA_3' ] },
{ id: 'toto' }
];

// when
let result = service.getKnowledgeData(challenges);


// then
const expected = {
challengesById: {
'tata': { id: 'tata', knowledgeTags: [ '@acquisA_1', '@acquisB_5', '@acquisC_3' ] },
'titi': { id: 'titi', knowledgeTags: [ '@acquisA_3' ] }
},
knowledgeTagSet: {
'@acquisA_1': true,
'@acquisB_5': true,
'@acquisC_3': true,
'@acquisA_3': true
},
nbKnowledgeTagsByLevel: {
1: 1,
2: 0,
3: 2,
4: 0,
5: 1,
6: 0,
7: 0,
8: 0
}
};

expect(result).to.deep.equal(expected);
});

});


});

0 comments on commit a8bb958

Please sign in to comment.