-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows us to track which User started a Game and which users made the final winning/losing move to end a Game. Also, fix an inconsistency in the test fixtures where the Game with Status "Standing By" had a `started_at` value.
- Loading branch information
Paul DobbinSchmaltz
committed
Nov 12, 2024
1 parent
ca3e6cc
commit 7f24481
Showing
9 changed files
with
278 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
# GameEndTransaction is a {GameTransaction} marking which {User} made the final | ||
# move during the associated {Game}. | ||
class GameEndTransaction < GameTransaction | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
# GameStartTransaction is a {GameTransaction} marking which {User} started the | ||
# associated {Game}. | ||
class GameStartTransaction < GameTransaction | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
# GameTransaction records events transacted by {User}s on a {Game} and when they | ||
# occurred. | ||
# | ||
# @attr type [String] The Subclass name. | ||
# @attr user_id [Integer] References the {User} involved in this Transaction. | ||
# @attr game_id [Integer] References the {Game} involved in this Transaction. | ||
# @attr created_at [DateTime] When this Transaction occurred. | ||
class GameTransaction < ApplicationRecord | ||
self.implicit_order_column = "created_at" | ||
|
||
include AbstractBaseClassBehaviors | ||
include ConsoleBehaviors | ||
|
||
as_abstract_class | ||
|
||
belongs_to :user | ||
belongs_to :game | ||
|
||
scope :for_user, ->(user) { where(user:) } | ||
scope :for_game, ->(game) { where(game:) } | ||
|
||
validates :game, uniqueness: { scope: :type } | ||
|
||
def self.create_between(user:, game:) | ||
new(user:, game:).tap(&:save!) | ||
end | ||
|
||
def self.exists_between?(user:, game:) | ||
for_user(user).for_game(game).exists? | ||
end | ||
|
||
# GameTransaction::Console acts like a {GameTransaction} but otherwise handles | ||
# IRB Console-specific methods/logic. | ||
class Console | ||
include ConsoleObjectBehaviors | ||
|
||
private | ||
|
||
def inspect_info | ||
[ | ||
[user.inspect, game.inspect].join(" -> "), | ||
I18n.l(created_at, format: :debug), | ||
].join(" @ ") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# Version: 20241112041937 | ||
class CreateGameTransactions < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table(:game_transactions) do |t| | ||
t.string(:type, null: false, index: true) | ||
t.references( | ||
:user, type: :uuid, foreign_key: { on_delete: :nullify }, index: true) | ||
t.references( | ||
:game, null: false, foreign_key: { on_delete: :cascade }) | ||
|
||
t.datetime(:created_at, null: false, index: true) | ||
end | ||
|
||
add_index(:game_transactions, %i[game_id type], unique: true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
win1_game_start_transaction: | ||
type: GameStartTransaction | ||
user: user1 | ||
game: win1 | ||
win1_game_end_transaction: | ||
type: GameEndTransaction | ||
user: user1 | ||
game: win1 | ||
|
||
loss1_game_start_transaction: | ||
type: GameStartTransaction | ||
user: user1 | ||
game: loss1 | ||
loss1_game_end_transaction: | ||
type: GameEndTransaction | ||
user: user2 | ||
game: loss1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class GameTransactionTest < ActiveSupport::TestCase | ||
describe "GameTransaction" do | ||
let(:unit_class) { GameTransaction } | ||
|
||
let(:user1) { users(:user1) } | ||
let(:user2) { users(:user2) } | ||
|
||
let(:win1) { games(:win1) } | ||
let(:standing_by1) { games(:standing_by1) } | ||
|
||
describe "DB insertion (GIVEN no Rails validation)" do | ||
subject { GameStartTransaction.new(user: user1, game: win1) } | ||
|
||
it "raises ActiveRecord::RecordNotUnique" do | ||
exception = | ||
_(-> { | ||
subject.save(validate: false) | ||
}).must_raise(ActiveRecord::RecordNotUnique) | ||
|
||
_(exception.message).must_include( | ||
"PG::UniqueViolation: ERROR: "\ | ||
"duplicate key value violates unique constraint "\ | ||
'"index_game_transactions_on_game_id_and_type"') | ||
end | ||
end | ||
|
||
describe ".create_between" do | ||
context "GIVEN a new, unique pair" do | ||
subject { GameStartTransaction } | ||
|
||
it "creates the expected GameTransaction record, and returns it" do | ||
result = | ||
_(-> { | ||
subject.create_between(user: user2, game: standing_by1) | ||
}).must_change("GameTransaction.count") | ||
_(result).must_be_instance_of(subject) | ||
_(result.user).must_be_same_as(user2) | ||
_(result.game).must_be_same_as(standing_by1) | ||
end | ||
end | ||
|
||
context "GIVEN an existing, non-unique pair" do | ||
subject { [GameStartTransaction, GameEndTransaction].sample } | ||
|
||
it "raises ActiveRecord::RecordInvalid" do | ||
exception = | ||
_(-> { | ||
subject.create_between(user: user1, game: win1) | ||
}).must_raise(ActiveRecord::RecordInvalid) | ||
|
||
_(exception.message).must_equal( | ||
"Validation failed: Game has already been taken") | ||
end | ||
end | ||
end | ||
|
||
describe ".exists_between?" do | ||
subject { unit_class } | ||
|
||
context "GIVEN an existing pair" do | ||
it "returns true" do | ||
result = subject.exists_between?(user: user1, game: win1) | ||
_(result).must_equal(true) | ||
end | ||
end | ||
|
||
context "GIVEN a non-existent pair" do | ||
it "returns false" do | ||
result = subject.exists_between?(user: user2, game: win1) | ||
_(result).must_equal(false) | ||
end | ||
end | ||
end | ||
end | ||
end |