Make IDs more time-sortable in tests

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.
This commit is contained in:
Kevin McConnell
2025-11-13 16:18:44 +00:00
committed by Mike Dalessio
parent 4f4bf5bee1
commit 59dd8ca549
15 changed files with 34 additions and 29 deletions
+3 -3
View File
@@ -23,7 +23,7 @@ class CardTest < ActiveSupport::TestCase
cards(:logo).comments.create!(body: "Agreed.")
end
assert_equal "Agreed.", cards(:logo).comments.order(created_at: :desc).first.body.to_plain_text.chomp
assert_equal "Agreed.", cards(:logo).comments.last.body.to_plain_text.chomp
end
test "assignment states" do
@@ -38,7 +38,7 @@ class CardTest < ActiveSupport::TestCase
cards(:logo).toggle_assignment users(:kevin)
end
assert_not cards(:logo).assigned_to?(users(:kevin))
unassign_event = Event.order(created_at: :desc).first
unassign_event = Event.last
assert_equal "card_unassigned", unassign_event.action
assert_equal [ users(:kevin) ], unassign_event.assignees
@@ -46,7 +46,7 @@ class CardTest < ActiveSupport::TestCase
cards(:logo).toggle_assignment users(:kevin)
end
assert cards(:logo).assigned_to?(users(:kevin))
assign_event = Event.order(created_at: :desc).first
assign_event = Event.last
assert_equal "card_assigned", assign_event.action
assert_equal [ users(:kevin) ], assign_event.assignees
end