59dd8ca549
In order for model ordering to work as expected in tests, we need to
keep two properties:
- Fixtures are all created in the past
- Models sort in the order that they were created
This allows us to do things like this:
post cards_path, params: { ... }
created_card = Card.last
When using UUIDv7 PKs rather than sequential integers, we have to make
sure a couple of things happen in order for this still to be true:
- Fixtures should generate deterministic IDs that translate to UUIDs
that would have been created in the past (i.e. before today)
- Newly created objects must have enough precision in their timestamps
so that they sort in the order they were created, and their random
component doesn't come into play.
To solve this, we use the deterministic numeric ID as a number of
milliseconds after an early year. And we ensure that the new timestamps
we create have sub-millisecond precision.
55 lines
1.8 KiB
Ruby
55 lines
1.8 KiB
Ruby
require "test_helper"
|
|
|
|
class Card::StatusesTest < ActiveSupport::TestCase
|
|
test "cards start out in a `drafted` state" do
|
|
card = boards(:writebook).cards.create! creator: users(:kevin), title: "Newly created card"
|
|
|
|
assert card.drafted?
|
|
end
|
|
|
|
test "cards are only visible to the creator when drafted" do
|
|
card = boards(:writebook).cards.create! creator: users(:kevin), title: "Drafted Card"
|
|
card.drafted!
|
|
|
|
assert_includes Card.published_or_drafted_by(users(:kevin)), card
|
|
assert_not_includes Card.published_or_drafted_by(users(:jz)), card
|
|
end
|
|
|
|
test "cards are visible to everyone when published" do
|
|
card = boards(:writebook).cards.create! creator: users(:kevin), title: "Published Card"
|
|
card.published!
|
|
|
|
assert_includes Card.published_or_drafted_by(users(:kevin)), card
|
|
assert_includes Card.published_or_drafted_by(users(:jz)), card
|
|
end
|
|
|
|
test "an event is created when a card is created in the published state" do
|
|
Current.session = sessions(:david)
|
|
|
|
assert_no_difference(-> { Event.count }) do
|
|
boards(:writebook).cards.create! creator: users(:kevin), title: "Draft Card"
|
|
end
|
|
|
|
assert_difference(-> { Event.count } => +1) do
|
|
@card = boards(:writebook).cards.create! creator: users(:kevin), title: "Published Card", status: :published
|
|
end
|
|
|
|
event = Event.last
|
|
assert_equal @card, event.eventable
|
|
assert_equal "card_published", event.action
|
|
end
|
|
|
|
test "an event is created when a card is published" do
|
|
Current.session = sessions(:david)
|
|
|
|
card = boards(:writebook).cards.create! creator: users(:kevin), title: "Published Card"
|
|
assert_difference(-> { Event.count } => +1) do
|
|
card.publish
|
|
end
|
|
|
|
event = Event.last
|
|
assert_equal card, event.eventable
|
|
assert_equal "card_published", event.action
|
|
end
|
|
end
|