I have a list of records that are to be sorted by the inserted_at column, and I would like to test the sorting functionality. In Ecto, I would create a list of the records and use Repo.update to update the inserted_at column with a calculated value so that the records have different timestamps.
I was able to work this out when I made the inserted_at attributed writable and passed it as an attribute to the update action
I would like to have the inserted_at field as a private attribute so that users cannot update it via the form, but this approach does not return updated values.
In my resource I have:
attribute :inserted_at, :utc_datetime_usec do
writable? true
default &DateTime.utc_now/0
match_other_defaults? true
allow_nil? false
end
...
update :update do
argument :inserted_at, :utc_datetime_usec
accept [:email, :language, :creator_id, :tenant_id, :inserted_at]
primary? true
end
And In the test I have:
for invitation <- invitations, i <- 1..length(invitations) do
invitation
|> Ash.Changeset.for_update(
:update,
%{},
private_arguments: %{inserted_at: time_before_or_after(-i * 12000)},
authorize?: false
)
|> Ash.update!()
end