Skip to content

Commit

Permalink
Fixed bad db enforcements
Browse files Browse the repository at this point in the history
  • Loading branch information
Molyna committed Nov 1, 2018
1 parent 9c9ca5c commit 04385d5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ public void addGame(Game game, UUID league) {
Player updatedBlack = glicko.glicko2(black, white, game.result() == 1 ? 0.0 : game.result() == 0 ? 0.5 : 1.0);

query.transaction().inNoResult(() -> {
query.update("INSERT INTO Game (id, white_id, black_id, result, written_result, played_at) VALUES (?, ?, ?, ?, ?, ?)")
query.update("INSERT INTO Game (id, white_id, black_id, result, written_result, played_at, league_id) VALUES (?, ?, ?, ?, ?, ?, ?)")
.params(
game.id().toString(),
white.id().toString(),
black.id().toString(),
game.result(),
game.writtenResult(),
game.timestamp())
game.timestamp(),
league.toString())
.run();
playerService.updatePlayer(updatedWhite, updatedBlack);
});
Expand Down
42 changes: 21 additions & 21 deletions src/main/resources/db/migration/V1__initial.sql
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
create table league (
id varchar(255) PRIMARY KEY,
name varchar(255) UNIQUE,
draw_allowed boolean,
id varchar(255) PRIMARY KEY NOT NULL,
name varchar(255) UNIQUE NOT NULL,
draw_allowed boolean NOT NULL,
period_length int,
scored_results boolean
scored_results boolean NOT NULL
);

create table player (
id varchar(255) PRIMARY KEY,
name varchar(255),
rating varchar(255),
rd varchar(255),
volatility varchar(255),
league_id varchar(255) references League(id),
id varchar(255) PRIMARY KEY NOT NULL,
name varchar(255) NOT NULL,
rating varchar(255) NOT NULL,
rd varchar(255) NOT NULL,
volatility varchar(255) NOT NULL,
league_id varchar(255) NOT NULL references League(id),
UNIQUE (name, league_id)
);

create table game (
id varchar(255) PRIMARY KEY,
white_id varchar(255) references Player(id),
black_id varchar(255) references Player(id),
result int,
id varchar(255) PRIMARY KEY NOT NULL,
white_id varchar(255) NOT NULL references Player(id),
black_id varchar(255) NOT NULL references Player(id),
result int NOT NULL,
written_result varchar(255),
played_at TIMESTAMP,
league_id varchar(255) references League(id)
played_at TIMESTAMP NOT NULL,
league_id varchar(255) NOT NULL references League(id)
);

create table api_user (
username varchar(255) PRIMARY KEY,
password varchar(255)
username varchar(255) PRIMARY KEY NOT NULL,
password varchar(255) NOT NULL
);

create table token (
token varchar(255) PRIMARY KEY,
username varchar(255) references api_user(username),
expiry TIMESTAMP
token varchar(255) PRIMARY KEY NOT NULL,
username varchar(255) NOT NULL references api_user(username),
expiry TIMESTAMP NOT NULL
);
12 changes: 12 additions & 0 deletions src/test/groovy/solutions/desperate/glicko/GameApiTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class GameApiTest extends GlickoTestApp {
and:
new JsonSlurper().parse(client.httpGet("/${league.id}/player/${player1.id}").body().byteStream()).rating == 1578.801716729907
new JsonSlurper().parse(client.httpGet("/${league.id}/player/${player2.id}").body().byteStream()).rating == 1421.198283270093

when:
def allGames = new JsonSlurper().parse(client.httpGet("/${league.id}/game").body().byteStream())

then:
allGames.size() == 1
}

def "Can add a game between three players"() {
Expand Down Expand Up @@ -74,5 +80,11 @@ class GameApiTest extends GlickoTestApp {
and:
new JsonSlurper().parse(client.httpGet("/${league.id}/player/${player1.id}").body().byteStream()).rating == 1499.4739527513502
new JsonSlurper().parse(client.httpGet("/${league.id}/player/${player3.id}").body().byteStream()).rating == 1596.4657600150126

when:
def allGames = new JsonSlurper().parse(client.httpGet("/${league.id}/game").body().byteStream())

then:
allGames.size() == 2
}
}

0 comments on commit 04385d5

Please sign in to comment.