Skip to content
This repository has been archived by the owner on Jun 16, 2018. It is now read-only.

Commit

Permalink
Merge 3c26de4 into 05ef270
Browse files Browse the repository at this point in the history
  • Loading branch information
poelstra committed Oct 30, 2017
2 parents 05ef270 + 3c26de4 commit 2166012
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 186 deletions.
1 change: 1 addition & 0 deletions karma.conf.js
Expand Up @@ -21,6 +21,7 @@ module.exports = function(config) {
'src/components/idbwrapper/idbstore.js',
'spec/helpers/*.js',
'src/js/factories/*.js',
'src/js/common/*.js',
'src/js/directives/*.js',
'src/js/services/*.js',
'src/js/views/*.js',
Expand Down
131 changes: 131 additions & 0 deletions spec/common/rankingSpec.js
@@ -0,0 +1,131 @@
describe('common/ranking', function () {
var ranking;
beforeEach(function () {
ranking = factory('common/ranking');
});

describe('calculateScoreboard', function () {
it('should output used stages', function () {
var board = ranking.calculateScoreboard([], { test: 1 });
expect(Object.keys(board)).toEqual(['test']);
});

it('should fill in all rounds for a team', function () {
// If a team has played at all (i.e., they have a score for that stage)
// then all other rounds for that team need to have an entry (which can
// be null).
var board = ranking.calculateScoreboard([
{ teamNumber: 41, stageId: 'test', round: 2, score: 10, published: true }
], { test: 3 });
expect(board['test'][0].scores).toEqual([null, 10, null]);
});

it('should rank number > dnc > dsq > null', function () {
var board = ranking.calculateScoreboard([
{ teamNumber: 41, stageId: 'test', round: 1, score: 'dsq', published: true },
{ teamNumber: 42, stageId: 'test', round: 1, score: 'dnc', published: true },
{ teamNumber: 43, stageId: 'test', round: 1, score: -1, published: true },
{ teamNumber: 44, stageId: 'test', round: 1, score: 1, published: true },
], { test: 3 });
var result = board['test'].map(function (entry) {
return {
rank: entry.rank,
teamNumber: entry.teamNumber,
highest: entry.highest
};
});
expect(result).toEqual([
{ rank: 1, teamNumber: 44, highest: 1 },
{ rank: 2, teamNumber: 43, highest: -1 },
{ rank: 3, teamNumber: 42, highest: 'dnc' },
{ rank: 4, teamNumber: 41, highest: 'dsq' },
]);

});

it("should assign equal rank to equal scores", function () {
var board = ranking.calculateScoreboard([
{ teamNumber: 41, stageId: 'test', round: 1, score: 10, published: true },
{ teamNumber: 41, stageId: 'test', round: 2, score: 20, published: true },
{ teamNumber: 41, stageId: 'test', round: 3, score: 30, published: true },
{ teamNumber: 42, stageId: 'test', round: 1, score: 30, published: true },
{ teamNumber: 42, stageId: 'test', round: 2, score: 10, published: true },
{ teamNumber: 42, stageId: 'test', round: 3, score: 20, published: true },
{ teamNumber: 43, stageId: 'test', round: 1, score: 30, published: true },
{ teamNumber: 43, stageId: 'test', round: 2, score: 0, published: true },
{ teamNumber: 43, stageId: 'test', round: 3, score: 20, published: true },
], { test: 3 });
var result = board['test'].map(function (entry) {
return {
rank: entry.rank,
teamNumber: entry.teamNumber,
highest: entry.highest
};
});
// Note: for equal ranks, teams are sorted according
// to (ascending) team id
expect(result).toEqual([
{ rank: 1, teamNumber: 41, highest: 30 },
{ rank: 1, teamNumber: 42, highest: 30 },
{ rank: 2, teamNumber: 43, highest: 30 },
]);
});

it("should allow filtering rounds", function () {
var board = ranking.calculateScoreboard([
{ teamNumber: 41, stageId: 'test', round: 1, score: 10, published: true },
{ teamNumber: 41, stageId: 'test', round: 2, score: 20, published: true },
{ teamNumber: 41, stageId: 'test', round: 3, score: 30, published: true },
{ teamNumber: 42, stageId: 'test', round: 1, score: 30, published: true },
{ teamNumber: 42, stageId: 'test', round: 2, score: 10, published: true },
{ teamNumber: 42, stageId: 'test', round: 3, score: 20, published: true },
{ teamNumber: 43, stageId: 'test', round: 1, score: 30, published: true },
{ teamNumber: 43, stageId: 'test', round: 2, score: 0, published: true },
{ teamNumber: 43, stageId: 'test', round: 3, score: 20, published: true },
], { test: 2 });
var result = board['test'].map(function (entry) {
return {
rank: entry.rank,
teamNumber: entry.teamNumber,
scores: entry.scores
};
});
// Note: for equal ranks, teams are sorted according
// to (ascending) team id
expect(result).toEqual([
{ rank: 1, teamNumber: 42, scores: [30, 10] },
{ rank: 2, teamNumber: 43, scores: [30, 0] },
{ rank: 3, teamNumber: 41, scores: [10, 20] },
]);
});

it("should exclude scores for unknown rounds / stages", function () {
var board = ranking.calculateScoreboard([
{ teamNumber: 41, stageId: 'foo', round: 1, score: 0, published: true },
{ teamNumber: 41, stageId: 'test', round: 0, score: 0, published: true },
{ teamNumber: 41, stageId: 'test', round: 4, score: 0, published: true },
], { test: 3 });
expect(board['test'].length).toEqual(0);
});

it("should ignore invalid scores", function () {
var board = ranking.calculateScoreboard([
{ teamNumber: 41, stageId: 'test', round: 1, score: "foo", published: true },
{ teamNumber: 41, stageId: 'test', round: 2, score: NaN, published: true },
{ teamNumber: 41, stageId: 'test', round: 3, score: Infinity, published: true },
{ teamNumber: 42, stageId: 'test', round: 1, score: {}, published: true },
{ teamNumber: 42, stageId: 'test', round: 2, score: true, published: true },
], { test: 3 });
expect(board['test'].length).toEqual(0);
});

it("should include/overwrite duplicate score", function () {
var board = ranking.calculateScoreboard([
{ teamNumber: 41, stageId: 'test', round: 1, score: 10, published: true },
{ teamNumber: 41, stageId: 'test', round: 1, score: 20, published: true },
], { test: 3 });
// Last score will overwrite any previous entry
expect(board['test'][0].highest).toEqual(20);
});
});
});
30 changes: 30 additions & 0 deletions spec/common/scoringSpec.js
@@ -0,0 +1,30 @@
describe('common/scoring', function () {
var scoring;
beforeEach(function () {
scoring = factory('common/scoring');
});

describe('isValidScore', function () {
it('should accept valid scores', function () {
expect(scoring.isValidScore(0)).toBe(true);
expect(scoring.isValidScore(-1)).toBe(true);
expect(scoring.isValidScore(1000)).toBe(true);
expect(scoring.isValidScore("dnc")).toBe(true); // Did Not Compete
expect(scoring.isValidScore("dsq")).toBe(true); // DiSQualified
});

it('should reject invalid scores', function () {
expect(scoring.isValidScore(undefined)).toBe(false);
expect(scoring.isValidScore(null)).toBe(false);
expect(scoring.isValidScore(NaN)).toBe(false);
expect(scoring.isValidScore(Infinity)).toBe(false);
expect(scoring.isValidScore(-Infinity)).toBe(false);
expect(scoring.isValidScore("dnq")).toBe(false);
expect(scoring.isValidScore("foo")).toBe(false);
expect(scoring.isValidScore(true)).toBe(false);
expect(scoring.isValidScore(false)).toBe(false);
expect(scoring.isValidScore({})).toBe(false);
expect(scoring.isValidScore([])).toBe(false);
});
});
});
26 changes: 1 addition & 25 deletions spec/services/ng-scoresSpec.js
Expand Up @@ -235,30 +235,6 @@ describe('ng-scores',function() {
});
});

describe('isValidScore', function () {
it('should accept valid scores', function () {
expect($scores.isValidScore(0)).toBe(true);
expect($scores.isValidScore(-1)).toBe(true);
expect($scores.isValidScore(1000)).toBe(true);
expect($scores.isValidScore("dnc")).toBe(true); // Did Not Compete
expect($scores.isValidScore("dsq")).toBe(true); // DiSQualified
});

it('should reject invalid scores', function () {
expect($scores.isValidScore(undefined)).toBe(false);
expect($scores.isValidScore(null)).toBe(false);
expect($scores.isValidScore(NaN)).toBe(false);
expect($scores.isValidScore(Infinity)).toBe(false);
expect($scores.isValidScore(-Infinity)).toBe(false);
expect($scores.isValidScore("dnq")).toBe(false);
expect($scores.isValidScore("foo")).toBe(false);
expect($scores.isValidScore(true)).toBe(false);
expect($scores.isValidScore(false)).toBe(false);
expect($scores.isValidScore({})).toBe(false);
expect($scores.isValidScore([])).toBe(false);
});
});

describe('scoreboard', function() {
var board;
beforeEach(function() {
Expand Down Expand Up @@ -397,7 +373,7 @@ describe('ng-scores',function() {
expect($scores.scores[0].error).toEqual(jasmine.any($scores.UnknownStageError));
expect($scores.scores[1].error).toEqual(jasmine.any($scores.UnknownRoundError));
expect($scores.scores[2].error).toEqual(jasmine.any($scores.UnknownRoundError));
expect(board["test"].length).toEqual(1);
expect(board["test"].length).toEqual(0);
expect($scores.validationErrors.length).toEqual(3);
});

Expand Down

0 comments on commit 2166012

Please sign in to comment.