add additional database table connecting game matches to participating players
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe MatchPlayer, type: :model do
|
||||
let(:player1) { User.create!(nickname: "player_one", password: "secret123") }
|
||||
let(:player2) { User.create!(nickname: "player_two", password: "secret123") }
|
||||
let(:other_player) { User.create!(nickname: "player_three", password: "secret123") }
|
||||
|
||||
let(:match) do
|
||||
Match.create!(
|
||||
player1: player1,
|
||||
player2: player2,
|
||||
status: :ongoing,
|
||||
game_server: "ws://game-server:8080/ws",
|
||||
started_at: Time.current,
|
||||
player1_rating_before: 100,
|
||||
player2_rating_before: 100,
|
||||
player1_rating_change: 0,
|
||||
player2_rating_change: 0
|
||||
)
|
||||
end
|
||||
|
||||
it "belongs to a match by uuid and a player by id" do
|
||||
match_player = match.match_players.find_by(player: player1)
|
||||
expect(match_player.match).to eq(match)
|
||||
expect(match_player.player).to eq(player1)
|
||||
expect(match_player.match_uuid).to eq(match.uuid)
|
||||
expect(match_player.player_id).to eq(player1.id)
|
||||
end
|
||||
|
||||
it "validates uniqueness of player_id scoped to match_uuid" do
|
||||
duplicate = MatchPlayer.new(match_uuid: match.uuid, player_id: player1.id)
|
||||
expect(duplicate).not_to be_valid
|
||||
expect(duplicate.errors[:player_id]).to be_present
|
||||
end
|
||||
|
||||
it "requires match_uuid and player_id" do
|
||||
expect(MatchPlayer.new(match_uuid: nil, player_id: player1.id)).not_to be_valid
|
||||
expect(MatchPlayer.new(match_uuid: match.uuid, player_id: nil)).not_to be_valid
|
||||
end
|
||||
end
|
||||
@@ -33,6 +33,12 @@ RSpec.describe Match, type: :model do
|
||||
expect(match.player2).to eq(player2)
|
||||
end
|
||||
|
||||
it "automatically creates match_player records on creation" do
|
||||
match = Match.create!(valid_attributes)
|
||||
expect(match.match_players.pluck(:player_id)).to contain_exactly(player1.id, player2.id)
|
||||
expect(match.players).to contain_exactly(player1, player2)
|
||||
end
|
||||
|
||||
it "does not allow the same player on both sides" do
|
||||
match = Match.new(valid_attributes.merge(player2: player1))
|
||||
expect(match).not_to be_valid
|
||||
|
||||
@@ -92,6 +92,7 @@ RSpec.describe MatchmakingService do
|
||||
expect(match.player1_rating_change).to eq(0)
|
||||
expect(match.player2_rating_change).to eq(0)
|
||||
expect(match.finished_at).to be_nil
|
||||
expect(match.match_players.pluck(:player_id)).to contain_exactly(a, b)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user