mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-20 13:33:50 +12:00
37cc01957d
* WIP * chore: fix mix.lock merge issues * improvement: upgrade to 3.0 * chore: remove `repo.to_tenant` * chore: continue removal of unnecessary helper * chore: use `Ash.ToTenant`
60 lines
1.4 KiB
Elixir
60 lines
1.4 KiB
Elixir
defmodule AshPostgres.Test.UpsertTest do
|
|
use AshPostgres.RepoCase, async: false
|
|
alias AshPostgres.Test.Post
|
|
|
|
require Ash.Query
|
|
|
|
test "upserting results in the same created_at timestamp, but a new updated_at timestamp" do
|
|
id = Ash.UUID.generate()
|
|
|
|
new_post =
|
|
Post
|
|
|> Ash.Changeset.for_create(:create, %{
|
|
id: id,
|
|
title: "title2"
|
|
})
|
|
|> Ash.create!(upsert?: true)
|
|
|
|
assert new_post.id == id
|
|
assert new_post.created_at == new_post.updated_at
|
|
|
|
updated_post =
|
|
Post
|
|
|> Ash.Changeset.for_create(:create, %{
|
|
id: id,
|
|
title: "title2"
|
|
})
|
|
|> Ash.create!(upsert?: true)
|
|
|
|
assert updated_post.id == id
|
|
assert updated_post.created_at == new_post.created_at
|
|
assert updated_post.created_at != updated_post.updated_at
|
|
end
|
|
|
|
test "upserting a field with a default sets to the new value" do
|
|
id = Ash.UUID.generate()
|
|
|
|
new_post =
|
|
Post
|
|
|> Ash.Changeset.for_create(:create, %{
|
|
id: id,
|
|
title: "title2"
|
|
})
|
|
|> Ash.create!(upsert?: true)
|
|
|
|
assert new_post.id == id
|
|
assert new_post.created_at == new_post.updated_at
|
|
|
|
updated_post =
|
|
Post
|
|
|> Ash.Changeset.for_create(:create, %{
|
|
id: id,
|
|
title: "title2",
|
|
decimal: Decimal.new(5)
|
|
})
|
|
|> Ash.create!(upsert?: true)
|
|
|
|
assert updated_post.id == id
|
|
assert Decimal.equal?(updated_post.decimal, Decimal.new(5))
|
|
end
|
|
end
|