Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests for prediction history and fixes bugs related to that #699

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cypress/e2e/invite1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function run() {
const connection = await connect();
await connection.query("DELETE FROM users WHERE username like 'Invite%'");
await connection.query("DELETE FROM invite WHERE inviteID='invite1'");
await connection.query("DELETE FROM data WHERE value1='locked'");
await connection.query("DELETE FROM data WHERE value1 like 'locked%'");
await updateData("", "./sample/data1.json");
await connection.end();
}
31 changes: 31 additions & 0 deletions cypress/e2e/predictions.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,36 @@ describe("Create Predictions league and do some simple predictions.", () => {
.parent()
.children(":nth-child(5)")
.contains("5 - 1");
cy.exec(
"export APP_ENV=test; ts-node --project=./tsconfig2.json cypress/e2e/predictions3.ts",
);
cy.contains("Standings").click();
cy.get(".MuiPagination-ul > :nth-child(2) > .MuiButtonBase-root").click();
cy.get("#predictions0").click();
// Confirms that the historical predictions and game scores are stored directly
cy.contains("FCB - WOB")
.parent()
.children(":nth-child(3)")
.contains("3 - 0");
cy.contains("FCB - WOB")
.parent()
.children(":nth-child(5)")
.contains("5 - 1");
cy.contains("BVB - BSC")
.parent()
.children(":nth-child(3)")
.contains("2 - 2");
cy.contains("BVB - BSC")
.parent()
.children(":nth-child(5)")
.contains("2 - 2");
cy.contains("SGE - M05")
.parent()
.children(":nth-child(3)")
.contains("4 - 0");
cy.contains("SGE - M05")
.parent()
.children(":nth-child(5)")
.contains("5 - 1");
});
});
4 changes: 3 additions & 1 deletion cypress/e2e/predictions1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ async function run() {
await connection.query(
"DELETE FROM users WHERE username like 'Predictions%'",
);
await connection.query("DELETE FROM data WHERE value1='locked'");
await connection.query("DELETE FROM clubs");
await connection.query("DELETE FROM historicalClubs");
await connection.query("DELETE FROM data WHERE value1 like 'locked%'");
await updateData("", "./sample/data1.json");
await connection.end();
}
1 change: 0 additions & 1 deletion cypress/e2e/predictions2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ async function run() {
]);
connection.end();
// Simulates all the games
console.log("PART 2");
await updateData("", "./sample/data3.json");
}
9 changes: 9 additions & 0 deletions cypress/e2e/predictions3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { updateData } from "#scripts/update";

run();

async function run() {
await updateData("", "./sample/data4.json");
// Ends the matchday for matchday
await updateData("", "./sample/data5.json");
}
1 change: 0 additions & 1 deletion pages/[league]/[user]/predictions/[matchday].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
},
};
}
console.log(predictions);
connection.end();
// Checks if the user exists
if (username === "") {
Expand Down
12 changes: 6 additions & 6 deletions sample/data4.json.games.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"team_code": "WOB"
},
"result": {
"home": 0,
"away": 0
"home": 5,
"away": 1
}
}
}
Expand Down Expand Up @@ -66,8 +66,8 @@
"team_code": "BSC"
},
"result": {
"home": 0,
"away": 0
"home": 2,
"away": 2
}
}
}
Expand All @@ -91,8 +91,8 @@
"team_code": "M05"
},
"result": {
"home": 0,
"away": 0
"home": 5,
"away": 1
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const default_theme_light = JSON.stringify({
},
});
// Used to tell the program what version the database should get to
const currentVersion = "1.17.0";
const currentVersion = "1.18.0";
// Creates the default config
async function createConfig() {
const connection = await connect();
Expand Down
104 changes: 62 additions & 42 deletions scripts/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,51 +476,71 @@ async function endMatchday(league: string) {
);
console.log(`Archiving matchday data for ${league}`);
await connection.query(
"INSERT INTO historicalClubs (club, opponent, league, time) SELECT club, opponent, league, ? as time FROM clubs WHERE league=?",
"INSERT INTO historicalClubs (club, opponent, teamScore, opponentScore, league, home, time, `exists`) SELECT club, opponent, teamScore, opponentScore, league, home, ? as time, `exists` FROM clubs WHERE league=?",
[time, league],
);
// Copies all squads into the historical squads and copies predictions
let currentleagueID = 0;
console.log(`Archiving user squads for ${league}`);
const squads = await connection.query(
"SELECT * FROM squad WHERE EXISTS (SELECT * FROM leagueSettings WHERE leagueSettings.leagueID=squad.leagueID AND league=? AND archived=0) ORDER BY leagueID DESC",
[league],
);
let counter = 0;
let matchday = 1;
while (squads.length > counter) {
const squad = squads[counter];
counter++;
// Checks if the leagues matchday has already been calculated
if (squad.leagueID !== currentleagueID) {
currentleagueID = squad.leagueID;
// Calculates the latest matchday for that league
matchday = await connection
.query(
"SELECT matchday FROM points WHERE leagueID=? ORDER BY matchday DESC LIMIT 1",
[currentleagueID],
)
.then((result) => (result.length > 0 ? result[0].matchday : 1));
// Copies all predictions for that league into historical predictions
await connection.query(
"INSERT INTO historicalPredictions (matchday, leagueID, user, club, league, home, away) SELECT ? as matchday, leagueID, user, club, league, home, away FROM predictions WHERE leagueID=?",
[matchday, currentleagueID],
);
}
// Copies all the predictions that are still
await Promise.all([
// Archives all the predictions
connection
.query(
`INSERT INTO historicalPredictions (
matchday, leagueID, user, club, league,
home, away
)
SELECT
(
SELECT
matchday
FROM
points
WHERE
leagueID = predictions.leagueID
ORDER BY
matchday DESC
LIMIT
1
) as matchday,
leagueID,
user,
club,
league,
home,
away
FROM
predictions`,
)
// Clears the predictions
.then(() => connection.query("DELETE FROM predictions")),
// Archives all the squads
connection.query(
"INSERT INTO historicalSquad (matchday, leagueID, user, playeruid, position, starred) VALUES (?, ?, ?, ?, ?, ?)",
[
matchday,
squad.leagueID,
squad.user,
squad.playeruid,
squad.position,
squad.starred,
],
);
}
// Deletes predictions after they were archived
await connection.query("DELETE FROM predictions WHERE league=?", [league]);
`INSERT INTO historicalSquad (
matchday, leagueID, user, playeruid,
position, starred
)
SELECT
(
SELECT
matchday
FROM
points
WHERE
leagueID = squad.leagueID
ORDER BY
matchday DESC
LIMIT
1
) as matchday,
leagueID,
user,
playeruid,
position,
starred
FROM
squad;
`,
),
]);
// Will revalidate the downloads page so it is up to date
fetch(process.env.NEXTAUTH_URL_INTERNAL + "/api/revalidate", {
method: "POST",
Expand Down
12 changes: 12 additions & 0 deletions scripts/upgradeDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,18 @@ export default async function main(oldVersion: string): Promise<string> {
oldVersion = "1.17.0";
console.log("Upgraded database to version 1.17.0");
}
if (oldVersion === "1.17.0") {
console.log("Upgrading database to version 1.18.0");
// Fixes because historicalClub data was archived incorrectly
await connection.query(
"UPDATE historicalClubs AS test SET home=EXISTS (SELECT * FROM historicalClubs WHERE home=0 AND opponent=test.club AND league=test.league AND time=test.time)",
);
await connection.query(
"UPDATE historicalClubs SET `exists`=EXISTS (SELECT * FROM historicalPlayers WHERE `exists`=1 AND club=historicalClubs.club AND league=historicalClubs.league AND time=historicalClubs.time)",
);
oldVersion = "1.18.0";
console.log("Upgraded database to version 1.18.0");
}
connection.end();
return oldVersion;
}