Skip to content

Commit

Permalink
add test coverage for Math Models (integration)
Browse files Browse the repository at this point in the history
  • Loading branch information
syarhei committed Apr 24, 2018
1 parent ee092c4 commit c55120d
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/test/integration/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ after("Delete user", async () => {

const resForLogoutForAdmin: Response = await admin.delete("/api/sessions");
assert.deepEqual(resForLogoutForAdmin.statusCode, 200);

process.exit();
});
152 changes: 152 additions & 0 deletions app/test/integration/match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import {createFootballSet, deleteFootballSet, getMatchesOfLastRound} from "../../scripts/TeamStatisticCreation";
import {Team} from "../../src/contracts/Team";
import {admin} from "../TestEnvironment";
import {Response} from "request";
import {Match} from "../../src/contracts/Match";
import {assert} from "chai";

describe("Match's operations (using Math Models)", async () => {
let categoryId: string = null;
let teams: Team[] = null;
let matches: Match[] = null;

describe("BradleyTerry Model", async () => {
before("Add Team Statistic about BPL (2012-13)", async () => {
({ categoryId, teams, matches } = await createFootballSet());
});

let countOfPredictedResults = 0;
let countOfUnpredictedResults = 0;
const matchesOfLastRound: Match[] = getMatchesOfLastRound();
const matchBodies: Match[] = matchesOfLastRound.map(match => {
return {
teamHomeId: match.teamHomeId,
teamGuestId: match.teamGuestId,
matchCategoryId: match.matchCategoryId,
date: match.date,
place: match.place
} as Match;
});

matchBodies.forEach((body, index) => {
it(`Create match [${index}] using BradleyTerry Model`, async () => {
const res: Response = await admin.post("/api/matches/engine/bradley-terry", { body });
assert.deepEqual(res.statusCode, 201);
const matchResponse: Match = res.body;
const match: Match = matchesOfLastRound[index];
assert.deepEqual(matchResponse.teamHomeId, match.teamHomeId);
assert.deepEqual(matchResponse.teamGuestId, match.teamGuestId);
if (matchResponse) {
matches.push(matchResponse);
}
if (match.homeGoals > match.guestGoals) {
if (
matchResponse.coefficientWin1 < matchResponse.coefficientDraw &&
matchResponse.coefficientWin1 < matchResponse.coefficientWin2
) { countOfPredictedResults++; } else { countOfUnpredictedResults++; }
}
if (match.homeGoals === match.guestGoals) {
if (
matchResponse.coefficientDraw < matchResponse.coefficientWin1 &&
matchResponse.coefficientDraw < matchResponse.coefficientWin2
) { countOfPredictedResults++; } else { countOfUnpredictedResults++; }
}
if (match.homeGoals === match.guestGoals) {
if (
matchResponse.coefficientWin2 < matchResponse.coefficientDraw &&
matchResponse.coefficientWin2 < matchResponse.coefficientWin1
) { countOfPredictedResults++; } else { countOfUnpredictedResults++; }
}

const teamName1: string = teams.find(t => t.id === match.teamHomeId).name;
const teamName2: string = teams.find(t => t.id === match.teamGuestId).name;
console.log(`win1: ${matchResponse.coefficientWin1}`);
console.log(`draw: ${matchResponse.coefficientDraw}`);
console.log(`win2: ${matchResponse.coefficientWin2}`);
console.log(`${teamName1} ${match.homeGoals} : ${match.guestGoals} ${teamName2}`);
});
});

it("Check success of predicted result", async () => {
console.log(`Percentage of Match's predicting is ${countOfPredictedResults / matchesOfLastRound.length}`);
});

after("Delete Team Statistic", async () => {
await deleteFootballSet(
matches.map(match => match.id),
categoryId,
teams.map(team => team.id)
);
});
});

describe("MaherPoisson Model", async () => {
before("Add Team Statistic about BPL (2012-13)", async () => {
({ categoryId, teams, matches } = await createFootballSet());
});

let countOfPredictedResults = 0;
let countOfUnpredictedResults = 0;
const matchesOfLastRound: Match[] = getMatchesOfLastRound();
const matchBodies: Match[] = matchesOfLastRound.map(match => {
return {
teamHomeId: match.teamHomeId,
teamGuestId: match.teamGuestId,
matchCategoryId: match.matchCategoryId,
date: match.date,
place: match.place
} as Match;
});

matchBodies.forEach((body, index) => {
it(`Generate coefficient [${index}]`, async function () {
const res: Response = await admin.post("/api/matches/engine/maher-poisson", { body });
assert.deepEqual(res.statusCode, 201);
const matchResponse: Match = res.body;
const match: Match = matchesOfLastRound[index];
assert.deepEqual(matchResponse.teamHomeId, match.teamHomeId);
assert.deepEqual(matchResponse.teamGuestId, match.teamGuestId);
if (matchResponse) {
matches.push(matchResponse);
}
if (match.homeGoals > match.guestGoals) {
if (
matchResponse.coefficientWin1 < matchResponse.coefficientDraw &&
matchResponse.coefficientWin1 < matchResponse.coefficientWin2
) { countOfPredictedResults++; } else { countOfUnpredictedResults++; }
}
if (match.homeGoals === match.guestGoals) {
if (
matchResponse.coefficientDraw < matchResponse.coefficientWin1 &&
matchResponse.coefficientDraw < matchResponse.coefficientWin2
) { countOfPredictedResults++; } else { countOfUnpredictedResults++; }
}
if (match.homeGoals === match.guestGoals) {
if (
matchResponse.coefficientWin2 < matchResponse.coefficientDraw &&
matchResponse.coefficientWin2 < matchResponse.coefficientWin1
) { countOfPredictedResults++; } else { countOfUnpredictedResults++; }
}

const teamName1: string = teams.find(t => t.id === match.teamHomeId).name;
const teamName2: string = teams.find(t => t.id === match.teamGuestId).name;
console.log(`win1: ${matchResponse.coefficientWin1}`);
console.log(`draw: ${matchResponse.coefficientDraw}`);
console.log(`win2: ${matchResponse.coefficientWin2}`);
console.log(`${teamName1} ${match.homeGoals} : ${match.guestGoals} ${teamName2}`);
});
});

it("Check success of predicted result", async () => {
console.log(`Percentage of Match's predicting is ${countOfPredictedResults / matchesOfLastRound.length}`);
});

after("Delete Team Statistic", async () => {
await deleteFootballSet(
matches.map(match => match.id),
categoryId,
teams.map(team => team.id)
);
});
});
});
3 changes: 2 additions & 1 deletion app/test/integration/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "./main";
import "./auth";
import "./team";
import "./category";
import "./category";
import "./match";

0 comments on commit c55120d

Please sign in to comment.