-
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.
Remove Game#[started|ended]_at in favor of transactions
Now that we have GameStartTransaction and GameEndTransaction we can transition over to relying on these records instead of the old Game#started_at and Game#ended_at attributes. To support this: - Remove the old columns from the database - Update named scopes to join/merge on the Game[Start|End]Transaction scopes instead - Move GameTransaction.create_between into the subclasses, so they can do the right thing for building object associations (in memory). Otherwise, we end up with a Game/User that knows about the new GameTransaction record, but not vice-versa... until reload.
- Loading branch information
Paul DobbinSchmaltz
committed
Nov 14, 2024
1 parent
b1d9ffd
commit 33ccde8
Showing
10 changed files
with
161 additions
and
66 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
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
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class GameCreateTransactionTest < ActiveSupport::TestCase | ||
describe "GameCreateTransaction" do | ||
let(:unit_class) { GameCreateTransaction } | ||
|
||
let(:user1) { users(:user1) } | ||
let(:standing_by1) { games(:standing_by1) } | ||
|
||
describe ".create_between" do | ||
subject { unit_class } | ||
|
||
context "GIVEN a new, unique pair" do | ||
before do | ||
standing_by1.game_create_transaction.delete | ||
standing_by1.game_create_transaction = nil | ||
end | ||
|
||
it "creates the expected record, and returns it" do | ||
result = | ||
_(-> { | ||
subject.create_between(user: user1, game: standing_by1) | ||
}).must_change("GameCreateTransaction.count") | ||
_(result).must_be_instance_of(subject) | ||
_(result.user).must_be_same_as(user1) | ||
_(result.game).must_be_same_as(standing_by1) | ||
end | ||
end | ||
|
||
context "GIVEN an existing, non-unique pair" do | ||
it "raises ActiveRecord::RecordInvalid" do | ||
exception = | ||
_(-> { | ||
subject.create_between(user: user1, game: standing_by1) | ||
}).must_raise(ActiveRecord::RecordInvalid) | ||
|
||
_(exception.message).must_equal( | ||
"Validation failed: Game has already been created") | ||
end | ||
end | ||
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class GameEndTransactionTest < ActiveSupport::TestCase | ||
describe "GameEndTransaction" do | ||
let(:unit_class) { GameEndTransaction } | ||
|
||
let(:user1) { users(:user1) } | ||
|
||
let(:win1) { games(:win1) } | ||
let(:standing_by1) { games(:standing_by1) } | ||
|
||
describe ".create_between" do | ||
subject { unit_class } | ||
|
||
context "GIVEN a new, unique pair" do | ||
it "creates the expected record, and returns it" do | ||
result = | ||
_(-> { | ||
subject.create_between(user: user1, game: standing_by1) | ||
}).must_change("GameEndTransaction.count") | ||
_(result).must_be_instance_of(subject) | ||
_(result.user).must_be_same_as(user1) | ||
_(result.game).must_be_same_as(standing_by1) | ||
end | ||
end | ||
|
||
context "GIVEN an existing, non-unique pair" do | ||
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 ended") | ||
end | ||
end | ||
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class GameStartTransactionTest < ActiveSupport::TestCase | ||
describe "GameStartTransaction" do | ||
let(:unit_class) { GameStartTransaction } | ||
|
||
let(:user1) { users(:user1) } | ||
|
||
let(:win1) { games(:win1) } | ||
let(:standing_by1) { games(:standing_by1) } | ||
|
||
describe ".create_between" do | ||
subject { unit_class } | ||
|
||
context "GIVEN a new, unique pair" do | ||
it "creates the expected record, and returns it" do | ||
result = | ||
_(-> { | ||
subject.create_between(user: user1, game: standing_by1) | ||
}).must_change("GameStartTransaction.count") | ||
_(result).must_be_instance_of(subject) | ||
_(result.user).must_be_same_as(user1) | ||
_(result.game).must_be_same_as(standing_by1) | ||
end | ||
end | ||
|
||
context "GIVEN an existing, non-unique pair" do | ||
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 started") | ||
end | ||
end | ||
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
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